1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
#include "UI.hpp"
#include <Cplt/Model/GlobalStates.hpp>
#include <Cplt/Model/Project.hpp>
#include <Cplt/Utils/I18n.hpp>
#include <IconsFontAwesome.h>
#include <imgui.h>
#include <imgui_stdlib.h>
namespace CPLT_UNITY_ID {
enum class ActionResult {
Confirmed,
Canceled,
Pending,
};
/// \param list Item list that the item is in.
/// \param item A non-null pointer to the currently being edited item. It should not change until this function returns a non-\c ActionResult::Pending value.
template <class T>
ActionResult ItemEditor(ItemList<T>& list, T* item) {
constexpr bool kHasDescription = requires(T t) {
t.GetDescription();
};
constexpr bool kHasEmail = requires(T t) {
t.GetEmail();
};
static bool duplicateName = false;
static std::string name;
static std::string description;
static std::string email;
if (name.empty()) {
name = item->GetName();
if constexpr (kHasDescription) description = item->GetDescription();
if constexpr (kHasEmail) email = item->GetEmail();
}
auto ClearStates = [&]() {
duplicateName = false;
name = {};
description = {};
};
if (ImGui::InputText(I18N_TEXT("Name", L10N_ITEM_COLUMN_NAME), &name)) {
duplicateName = name != item->GetName() && list.Find(name) != nullptr;
}
if constexpr (kHasDescription) ImGui::InputText(I18N_TEXT("Description", L10N_ITEM_COLUMN_DESCRIPTION), &description);
if constexpr (kHasEmail) ImGui::InputText(I18N_TEXT("Email", L10N_ITEM_COLUMN_EMAIL), &email);
if (name.empty()) {
ImGui::ErrorMessage(I18N_TEXT("Name cannot be empty", L10N_EMPTY_NAME_ERROR));
}
if (duplicateName) {
ImGui::ErrorMessage(I18N_TEXT("Duplicate name", L10N_DUPLICATE_NAME_ERROR));
}
// Return Value
auto rv = ActionResult::Pending;
if (ImGui::Button(I18N_TEXT("Confirm", L10N_CONFIRM), name.empty() || duplicateName)) {
if (item->GetName() != name) {
item->SetName(std::move(name));
}
if constexpr (kHasDescription)
if (item->GetDescription() != description) {
item->SetDescription(std::move(description));
}
if constexpr (kHasEmail)
if (item->GetEmail() != email) {
item->SetEmail(std::move(email));
}
ImGui::CloseCurrentPopup();
ClearStates();
rv = ActionResult::Confirmed;
}
ImGui::SameLine();
if (ImGui::Button(I18N_TEXT("Cancel", L10N_CANCEL))) {
ImGui::CloseCurrentPopup();
ClearStates();
rv = ActionResult::Canceled;
}
return rv;
}
template <class T>
void ItemListEntries(ItemList<T>& list, int& selectedIdx) {
constexpr bool kHasDescription = requires(T t) {
t.GetDescription();
};
constexpr bool kHasEmail = requires(T t) {
t.GetEmail();
};
constexpr bool kHasStock = requires(T t) {
t.GetPrice();
};
constexpr bool kHasPrice = requires(T t) {
t.GetPrice();
};
constexpr int kColumns = 1 /* Name column */ + kHasDescription + kHasEmail + kHasStock + kHasPrice;
if (ImGui::BeginTable("", kColumns, ImGuiTableFlags_Borders)) {
ImGui::TableSetupColumn(I18N_TEXT("Name", L10N_ITEM_COLUMN_NAME));
if constexpr (kHasDescription) ImGui::TableSetupColumn(I18N_TEXT("Description", L10N_ITEM_COLUMN_DESCRIPTION));
if constexpr (kHasEmail) ImGui::TableSetupColumn(I18N_TEXT("Email", L10N_ITEM_COLUMN_EMAIL));
if constexpr (kHasStock) ImGui::TableSetupColumn(I18N_TEXT("Stock", L10N_ITEM_COLUMN_STOCK));
if constexpr (kHasPrice) ImGui::TableSetupColumn(I18N_TEXT("Price", L10N_ITEM_COLUMN_PRICE));
ImGui::TableHeadersRow();
size_t idx = 0;
for (auto& entry : list) {
if (entry.IsInvalid()) {
continue;
}
ImGui::TableNextRow();
ImGui::TableNextColumn();
if (ImGui::Selectable(entry.GetName().c_str(), selectedIdx == idx, ImGuiSelectableFlags_SpanAllColumns)) {
selectedIdx = idx;
}
if constexpr (kHasDescription) {
ImGui::TableNextColumn();
ImGui::TextUnformatted(entry.GetDescription().c_str());
}
if constexpr (kHasEmail) {
ImGui::TableNextColumn();
ImGui::TextUnformatted(entry.GetEmail().c_str());
}
if constexpr (kHasStock) {
ImGui::TableNextColumn();
ImGui::Text("%d", entry.GetStock());
}
if constexpr (kHasPrice) {
ImGui::TableNextColumn();
// TODO format in dollars
ImGui::Text("%d", entry.GetPrice());
}
idx++;
}
ImGui::EndTable();
}
}
template <class T>
void ItemListEditor(ItemList<T>& list) {
bool opened = true;
static int selectedIdx = -1;
static T* editingItem = nullptr;
if (ImGui::Button(ICON_FA_PLUS " " I18N_TEXT("Add", L10N_ADD))) {
ImGui::SetNextWindowCentered();
ImGui::OpenPopup(I18N_TEXT("Add item", L10N_ITEM_ADD_DIALOG_TITLE));
editingItem = &list.Insert("");
}
if (ImGui::BeginPopupModal(I18N_TEXT("Add item", L10N_ITEM_ADD_DIALOG_TITLE), &opened, ImGuiWindowFlags_AlwaysAutoResize)) {
switch (ItemEditor(list, editingItem)) {
case ActionResult::Confirmed:
editingItem = nullptr;
break;
case ActionResult::Canceled:
list.Remove(editingItem->GetId());
editingItem = nullptr;
break;
default:
break;
}
ImGui::EndPopup();
}
ImGui::SameLine();
if (ImGui::Button(ICON_FA_EDIT " " I18N_TEXT("Edit", L10N_EDIT), selectedIdx == -1)) {
ImGui::SetNextWindowCentered();
ImGui::OpenPopup(I18N_TEXT("Edit item", L10N_ITEM_EDIT_DIALOG_TITLE));
editingItem = list.Find(selectedIdx);
}
if (ImGui::BeginPopupModal(I18N_TEXT("Edit item", L10N_ITEM_EDIT_DIALOG_TITLE), &opened, ImGuiWindowFlags_AlwaysAutoResize)) {
if (ItemEditor(list, editingItem) != ActionResult::Pending) {
editingItem = nullptr;
}
ImGui::EndPopup();
}
ImGui::SameLine();
if (ImGui::Button(ICON_FA_TRASH " " I18N_TEXT("Delete", L10N_DELETE), selectedIdx == -1)) {
ImGui::SetNextWindowCentered();
ImGui::OpenPopup(I18N_TEXT("Delete item", L10N_ITEM_DELETE_DIALOG_TITLE));
list.Remove(selectedIdx);
}
if (ImGui::BeginPopupModal(I18N_TEXT("Delete item", L10N_ITEM_DELETE_DIALOG_TITLE), &opened, ImGuiWindowFlags_AlwaysAutoResize)) {
ImGui::TextUnformatted(I18N_TEXT("Are you sure you want to delete this item?", L10N_ITEM_DELETE_DIALOG_MESSAGE));
if (ImGui::Button(I18N_TEXT("Confirm", L10N_CONFIRM))) {
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if (ImGui::Button(I18N_TEXT("Cancel", L10N_CANCEL))) {
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
ItemListEntries<T>(list, selectedIdx);
}
} // namespace CPLT_UNITY_ID
void UI::ItemsTab() {
auto& gs = GlobalStates::GetInstance();
if (ImGui::BeginTabBar("ItemViewTabs")) {
if (ImGui::BeginTabItem(I18N_TEXT("Products", L10N_ITEM_CATEGORY_PRODUCT))) {
CPLT_UNITY_ID::ItemListEditor(gs.GetCurrentProject()->Products);
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem(I18N_TEXT("Factories", L10N_ITEM_CATEGORY_FACTORY))) {
CPLT_UNITY_ID::ItemListEditor(gs.GetCurrentProject()->Factories);
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem(I18N_TEXT("Customers", L10N_ITEM_CATEGORY_CUSTOMER))) {
CPLT_UNITY_ID::ItemListEditor(gs.GetCurrentProject()->Customers);
ImGui::EndTabItem();
}
ImGui::EndTabBar();
}
}
|