-
Notifications
You must be signed in to change notification settings - Fork 0
/
ItemsManager.lua
431 lines (383 loc) · 12 KB
/
ItemsManager.lua
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
local SModule = require('StateModule');
local Set = require('common/Set');
local SContainer = require('common/SortableContainer')
local IQueue = require('InfoQueue');
local OEMsg = require('ErrorMessage');
local SortMenu = require('common/SortMenu');
local WManager = require('WindowsManager');
local Utils = require('common/Utils');
local NIDialog = require('NewItemDialog');
local im = SModule:subclass("ItemsManager");
function im:initialize(id, naming, ItemClass, itemWindowFunc, mandatoryParent)
SModule.initialize(self, id);
self.parents = {};
self.naming = naming;
self.ItemClass = ItemClass;
self.onWindowUpdate = itemWindowFunc;
self.isParentMandatory = mandatoryParent;
self.container = SContainer(id, ItemClass);
self.selection = Set();
self.currentItem = nil;
self.selectMode = false;
self.container.itemAdded:connect(self.onAddNewItem, self);
self.container.itemRemoved:connect(self.onDeleteItem, self);
self.container.itemsSorted:connect(self.StateChanged, self);
WManager:register(self);
end
function im:windowTitle()
return self.naming.title
end
function im:windowContent()
if self.currentItem == nil then return end
local item = self.currentItem;
self.onWindowUpdate(item, self);
Slab.Separator();
local cols = 1;
if self.child then cols = 2; end
Slab.BeginLayout("GeneralItemWindowButtons", { Columns = cols, AlignX = 'center' });
Slab.SetLayoutColumn(1);
if Slab.Button("Delete") then
self:confirmDelete(item);
end
if self.child then
Slab.SetLayoutColumn(2);
if Slab.Button("New "..self.child.naming.name) then
self.child:openNewItemDialog(item);
end
end
Slab.EndLayout();
end
function im:generateNewItemId(parent)
local i = 1;
while self.container:hasItemId(self.naming.title.." #"..tostring(i), parent) do
i = i + 1;
end
return self.naming.title.." #"..tostring(i)
end
function im:setCurrentItem(item)
-- if self.currentItem == item then return; end
self.currentItem = item;
if self.currentItem ~= nil then
WManager:setCurrentModule(self.id);
end
self:StateChanged();
end
function im:toggleCurrentItem(item)
if self.currentItem == item then
self.currentItem = nil;
else
self.currentItem = item;
if self.currentItem ~= nil then
WManager:setCurrentModule(self.id);
end
end
self:StateChanged();
end
function im:showCurrentItemWindow()
WManager:showModuleWindow(self.id);
end
function im:showItemWindow(item)
self:setCurrentItem(item);
self:showCurrentItemWindow();
end
function im:onClick(item)
if not self.container:hasItem(item) then
print(self, "Warning: clicked item is not in container:", item);
end
if self.selectMode then
self.selection:toggle(item);
end
if self.currentMode then
self:toggleCurrentItem(item);
end
self:StateChanged();
end
function im:onDoubleClick(item)
self:showItemWindow(item);
end
function im:onAddNewItem(item)
self:StateChanged();
end
function im:onDeleteItem(item)
if item == self.currentItem then
self.currentItem = nil;
end
self.selection:remove(item);
WManager:delItem(self.id, item, true);
if self.child then
self.child.container:deleteGroup(item);
end
item:destroy();
self:StateChanged();
end
function im:onItemLoad(id, parent)
return self.container:getItem(id, parent);
end
function im:checkIdExists(id, parent)
if self.container:hasItemId(id, parent) then
IQueue:pushMessage("Item already exists!", "Item "..tostring(id).." already exists!");
return true;
end
return false;
end
function im:createItem(...)
local status, value = OEMsg(
"Cannot create item!",
"An error occured while creating item of class "..tostring(self.ItemClass),
self.ItemClass.new, self.ItemClass, ...);
if not status then return; end
if self:checkIdExists(value.id, value.parent) then
return;
end
self.container:addItem(value);
-- self.container:dumpGroups();
return value;
end
function im:reCreateItem(item)
local data = item:getSerializableData();
local parent = item.parent;
self.container:deleteItem(item);
self:createItem(data, parent);
end
function im:setItemId(item, id)
if not self:checkIdExists(id, item.parent) then
item.id = id;
self:StateChanged();
end
end
function im:addParent(manager)
if not Utils.IsClassOrSubClass(manager.class, im) then
error(tostring(manager).." is not of class "..self.class.name.."!");
end
self.parents[manager.ItemClass.name] = manager;
manager.child = self;
manager.container.child = self.container;
end
function im:getActiveParents()
local list = {};
for class, manager in pairs(self.parents) do
if manager.currentItem ~= nil then
table.insert(list, manager.currentItem);
end
if not self.isParentMandatory then
table.insert(list, "None");
end
end
return list;
end
function im:loadParentItem(parentdata)
if type(parentdata) ~= 'table' then return; end
local parentclassname = Utils.TryValue(parentdata.classname, nil, 'string', 'warning');
if parentclassname == nil then return; end
local parentmanager = self.parents[parentclassname];
if parentmanager == nil then
print("Warning! No parent manager for class \""..parentclassname.."\"!");
return;
end
local parentparentdata = parentdata.parent;
local parentofparent = nil;
if parentparentdata then
parentofparent = parentmanager:loadParentItem(parentparentdata);
end
return parentmanager.container:getItem(parentdata.id, parentofparent)
end
function im:loadItem(itemdata)
local parentdata = itemdata.parent;
if parentdata ~= nil then
parent = self:loadParentItem(parentdata);
end
local status, value = pcall(self.ItemClass.new, self.ItemClass, itemdata, parent);
if status then
return value;
else
print("Error while loading item:", value)
return;
end
end
function im:loadItems(items)
if type(items) ~= 'table' then return; end
for _, itemdata in pairs(items) do
local item = self:loadItem(itemdata);
if item ~= nil then
self.container:addItem(item);
else
print("Warning: Cannot load item!", itemdata.id);
self:StateChanged(true);
end
if itemdata.selected then
self.selection:addSingle(item);
end
end
end
function im:LoadState(data)
if data == nil then return; end
self:SetLoadPhase(true);
WManager:SetLoadPhase(true);
self.container:LoadState(data.container);
self:loadItems(data.items);
if data.currentItem then
local parent = self:loadParentItem(data.currentItem.parent);
self.currentItem = self.container:getItem(data.currentItem.id, parent);
end
if data.currentMode == true then
self.currentMode = true;
end
if data.selectMode == true then
self.selectMode = true;
end
WManager:SetLoadPhase(false);
self:SetLoadPhase(false);
self:LoadSubmodulesState(data.children);
end
function im:serializeItems()
local items = {};
for gid, group in pairs(self.container.groups) do
for id, item in pairs(group.ids) do
local itemdata = item:getSerializableData();
if self.selection:has(item) then
itemdata.selected = true;
end
table.insert(items, itemdata);
end
end
return items;
end
function im:DumpState()
local data = { container = self.container:DumpState() };
data.items = self:serializeItems();
if self.currentItem then
data.currentItem = self.currentItem:getSerializableData();
end
data.currentMode = self.currentMode;
data.selectMode = self.selectMode;
data.children = self:DumpSubmodulesState();
return data;
end
function im:modesMenu()
if Slab.MenuItemChecked("Make current mode", self.currentMode) then
self.currentMode = not self.currentMode;
end
if Slab.MenuItemChecked("Selection mode", self.selectMode) then
self.selectMode = not self.selectMode;
end
end
function im:itemContextMenu(item)
local text = "Set current";
if self.currentItem == item then
text = "Clear current";
end
if Slab.MenuItem(text) then
if self.currentItem == item then
self.currentItem = nil;
else
self.currentItem = item;
WManager:setCurrentModule(self.id);
end
end
local seltext = "Select";
if self.selection:has(item) then
seltext = "Deselect";
end
if Slab.MenuItem(seltext) then
self.selection:toggle(item);
end
if Slab.MenuItem("Delete") then
self:confirmDelete(item);
end
end
function im:openNewItemDialog(parent)
local list = nil;
if parent == nil then
list = self:getActiveParents();
end
if self.isParentMandatory and parent == nil and #list == 0 then
IQueue:pushMessage("No parent item!", "Cannot create new "..self.naming.name.." without parent item!");
else
self._newItemDialog = NIDialog(self:generateNewItemId(parent), parent, list, "Create new "..self.naming.name);
end
end
function im:openModItemDialog(item)
self._modItem = item;
self._modItemDialog = NIDialog(item.id, parent, nil, "Set new id for "..self.naming.name);
end
function im:updateNewItemDialog()
if self._newItemDialog then
local dialog = self._newItemDialog;
dialog:update();
if not dialog.open then
if not dialog.canceled then
local item = self:createItem(dialog.newItemData.id, dialog.newItemData.parent);
self:showItemWindow(item);
end
self._newItemDialog = nil;
end
end
end
function im:updateModItemDialog()
if self._modItemDialog then
local dialog = self._modItemDialog;
dialog:update();
if not dialog.open then
if not dialog.canceled then
self:setItemId(self._modItem, dialog.newItemData.id);
end
self._modItemDialog = nil;
self._modItem = nil;
end
end
end
function im:confirmDelete(item)
self._confirmDelete = item;
end
function im:confirmDeleteSelected()
self._confirmDeleteSelected = true;
end
function im:updateConfirmDelete()
if self._confirmDeleteSelected then
local count = self.selection:size();
if count == 0 then
self._confirmDeleteSelected = false;
return;
end
local name = self.naming.name;
if count > 1 then name = self.naming.names; end
local result = Slab.MessageBox("Are You sure?", "Are You sure to delete "..count.." "..name.."?", { Buttons = { "Yes", "No" } });
if result ~= "" then
self._confirmDeleteSelected = false;
if result == "Yes" then
self.container:deleteSet(self.selection);
end
end
end
if self._confirmDelete then
local result = Slab.MessageBox("Are You sure?", "Are You sure to delete "..self.naming.name.."\n\""..tostring(self._confirmDelete).."\"?", { Buttons = { "Yes", "No" } });
if result ~= "" then
if result == "Yes" then
self.container:deleteItem(self._confirmDelete);
end
self._confirmDelete = nil;
end
end
end
function im:updateDialogs()
self:updateConfirmDelete();
self:updateNewItemDialog();
self:updateModItemDialog();
end
function im:updateMainMenuContent()
SortMenu(self.container);
self:modesMenu();
if Slab.MenuItem("Add") then
self:openNewItemDialog();
end
if Slab.MenuItem("Delete selected") then
self:confirmDeleteSelected();
end
end
function im:updateMainMenu()
if Slab.BeginMenu(self.naming.titles) then
self:updateMainMenuContent();
Slab.EndMenu();
end
end
return im;