lotrointerface.com
Search Downloads

LoTROInterface SVN SequenceBars

[/] [trunk/] [Thurallor/] [SequenceBars/] [Group.lua] - Blame information for rev 49

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 8 Thurallor-7095
Group = class(Node);
2 2 Thurallor-7095
 
3 Thurallor-7095
function Group:Constructor(parent, groupID, settings)
4 Thurallor-7095
    Turbine.UI.Window.Constructor(self);
5 Thurallor-7095
    self.constructing = true;
6 Thurallor-7095
 
7 Thurallor-7095
    self.parent = parent;
8 Thurallor-7095
    self.manager = parent.manager;
9 Thurallor-7095
    self.groupID = groupID;
10 Thurallor-7095
    self.objectID = groupID;
11 Thurallor-7095
    self.globals = self.manager.settings;
12 Thurallor-7095
 
13 Thurallor-7095
    -- Default settings
14 Thurallor-7095
    local defaults = {};
15 Thurallor-7095
    defaults.type = "group";
16 Thurallor-7095
    defaults.hidden = false;
17 Thurallor-7095
    defaults.barsMoveTogether = false;
18 Thurallor-7095
    defaults.caption = {};
19 Thurallor-7095
    defaults.caption.text = L:GetText("/Default/GroupName");
20 Thurallor-7095
    defaults.caption.width = 80;
21 Thurallor-7095
    defaults.caption.height = 20;
22 Thurallor-7095
    defaults.caption.font = Turbine.UI.Lotro.Font.TrajanPro15;
23 Thurallor-7095
    defaults.color = "C0C0C0";
24 Thurallor-7095
    defaults.barIDs = { "new" };
25 Thurallor-7095
    defaults.groupIDs = {};
26 Thurallor-7095
    defaults.deletedBarIDs = {};
27 Thurallor-7095
    defaults.deletedGroupIDs = {};
28 Thurallor-7095
    defaults.eventHandlers = {};
29 Thurallor-7095
    defaults.eventsEnabled = false;
30 Thurallor-7095
 
31 Thurallor-7095
    -- Previously-saved settings override the defaults
32 Thurallor-7095
    DeepTableCopy(settings, defaults);
33 Thurallor-7095
    DeepTableCopy(defaults, settings);
34 Thurallor-7095
    self.settings = settings;
35 Thurallor-7095
 
36 29 Thurallor-7095
    self:ReapplySettings();
37 2 Thurallor-7095
    self:LoadBars();
38 Thurallor-7095
    self:LoadGroups();
39 Thurallor-7095
    self.constructing = nil;
40 Thurallor-7095
    self:SetWantsEvents(self.settings.eventsEnabled);
41 Thurallor-7095
end
42 Thurallor-7095
 
43 29 Thurallor-7095
function Group:ReapplySettings()
44 Thurallor-7095
 
45 Thurallor-7095
    -- Make color object from hex string
46 Thurallor-7095
    self.color = Thurallor.Utils.Color(1, 0, 0, 0);
47 Thurallor-7095
    self.color:SetHex(self.settings.color);
48 Thurallor-7095
 
49 Thurallor-7095
    -- For backward compatibility
50 Thurallor-7095
    for _, handler in pairs(self.settings.eventHandlers) do
51 Thurallor-7095
        if (handler.action) then
52 30 Thurallor-7095
            handler.action = string.gsub(handler.action, "self:SetHidden%(false%); self:Activate%(%); ", "self:SetHidden(false); ");
53 31 Thurallor-7095
            handler.action = string.gsub(handler.action, "self:SetHidden%(false%); self:BringToFront%(%); ", "self:SetHidden(false); ");
54 30 Thurallor-7095
            handler.action = string.gsub(handler.action, "self:Expand%(%); self:Activate%(%); ", "self:Expand(); ");
55 31 Thurallor-7095
            handler.action = string.gsub(handler.action, "self:Expand%(%); self:BringToFront%(%); ", "self:Expand(); ");
56 29 Thurallor-7095
        end
57 Thurallor-7095
    end
58 Thurallor-7095
end
59 Thurallor-7095
 
60 2 Thurallor-7095
function Group:LoadBars()
61 Thurallor-7095
    for b = 1, #self.settings.barIDs, 1 do
62 Thurallor-7095
        local barID = self.settings.barIDs[b];
63 Thurallor-7095
        if (barID == "new") then
64 Thurallor-7095
            barID = self:FindNewObjectID();
65 Thurallor-7095
            self.settings.barIDs[b] = barID;
66 Thurallor-7095
        end
67 Thurallor-7095
        if (self.globals.bars[barID] == nil) then
68 Thurallor-7095
            self.globals.bars[barID] = {};
69 Thurallor-7095
        end
70 Thurallor-7095
        self.manager.objects[barID] = Bar(self, barID, self.globals.bars[barID]);
71 Thurallor-7095
    end
72 Thurallor-7095
end
73 Thurallor-7095
 
74 Thurallor-7095
function Group:LoadGroups()
75 Thurallor-7095
    for g = 1, #self.settings.groupIDs, 1 do
76 Thurallor-7095
        local groupID = self.settings.groupIDs[g];
77 Thurallor-7095
        if (groupID == "new") then
78 Thurallor-7095
            groupID = self:FindNewObjectID();
79 Thurallor-7095
            self.settings.groupIDs[g] = groupID;
80 Thurallor-7095
        end
81 Thurallor-7095
        if (self.globals.groups[groupID] == nil) then
82 Thurallor-7095
            self.globals.groups[groupID] = {};
83 Thurallor-7095
        end
84 Thurallor-7095
        self.manager.objects[groupID] = Group(self, groupID, self.globals.groups[groupID]);
85 Thurallor-7095
    end
86 Thurallor-7095
end
87 Thurallor-7095
 
88 20 Thurallor-7095
-- Iterator for bar objects
89 Thurallor-7095
function Group:bar_objects()
90 Thurallor-7095
    local state = { ["index"] = 1 };
91 Thurallor-7095
    local function iterator(state)
92 Thurallor-7095
        local barID = self.settings.barIDs[state.index];
93 Thurallor-7095
        state.index = state.index + 1;
94 Thurallor-7095
        if (barID ~= nil) then
95 Thurallor-7095
            local bar = self.manager.objects[barID];
96 Thurallor-7095
            if (bar == nil) then -- proceed to the next bar ID
97 Thurallor-7095
                return iterator(state);
98 Thurallor-7095
            end
99 Thurallor-7095
            return bar;
100 2 Thurallor-7095
        end
101 Thurallor-7095
    end
102 20 Thurallor-7095
    return iterator, state, nil;
103 Thurallor-7095
end
104 Thurallor-7095
 
105 Thurallor-7095
-- Iterator for subgroup objects
106 Thurallor-7095
function Group:group_objects()
107 Thurallor-7095
    local state = { ["index"] = 1 };
108 Thurallor-7095
    local function iterator(state)
109 Thurallor-7095
        local groupID = self.settings.groupIDs[state.index];
110 Thurallor-7095
        state.index = state.index + 1;
111 Thurallor-7095
        if (groupID ~= nil) then
112 Thurallor-7095
            local group = self.manager.objects[groupID];
113 Thurallor-7095
            if (group == nil) then -- proceed to the next group ID
114 Thurallor-7095
                return iterator(state);
115 Thurallor-7095
            end
116 Thurallor-7095
            return group;
117 2 Thurallor-7095
        end
118 Thurallor-7095
    end
119 20 Thurallor-7095
    return iterator, state, nil;
120 Thurallor-7095
end
121 Thurallor-7095
 
122 Thurallor-7095
function Group:ApplyHiddenness()
123 Thurallor-7095
    for bar in self:bar_objects() do
124 Thurallor-7095
        bar:ApplyHiddenness();
125 Thurallor-7095
    end
126 Thurallor-7095
    for group in self:group_objects() do
127 Thurallor-7095
        group:ApplyHiddenness();
128 Thurallor-7095
    end
129 2 Thurallor-7095
    self:SaveSettings(true);
130 Thurallor-7095
end
131 Thurallor-7095
 
132 20 Thurallor-7095
function Group:Activate()
133 Thurallor-7095
    for bar in self:bar_objects() do
134 Thurallor-7095
        bar:Activate();
135 Thurallor-7095
    end
136 Thurallor-7095
    for group in self:group_objects() do
137 Thurallor-7095
        group:Activate();
138 Thurallor-7095
    end
139 Thurallor-7095
end
140 Thurallor-7095
 
141 31 Thurallor-7095
function Group:BringToFront()
142 Thurallor-7095
    for bar in self:bar_objects() do
143 Thurallor-7095
        bar:BringToFront();
144 Thurallor-7095
    end
145 Thurallor-7095
    for group in self:group_objects() do
146 Thurallor-7095
        group:BringToFront();
147 Thurallor-7095
    end
148 Thurallor-7095
end
149 Thurallor-7095
 
150 26 Thurallor-7095
function Group:Expand()
151 Thurallor-7095
    for bar in self:bar_objects() do
152 Thurallor-7095
        bar:Expand();
153 Thurallor-7095
    end
154 Thurallor-7095
    for group in self:group_objects() do
155 Thurallor-7095
        group:Expand();
156 Thurallor-7095
    end
157 Thurallor-7095
end
158 Thurallor-7095
 
159 Thurallor-7095
function Group:IsExpanded()
160 Thurallor-7095
    for bar in self:bar_objects() do
161 Thurallor-7095
        if (not bar:IsExpanded()) then
162 Thurallor-7095
            return false;
163 Thurallor-7095
        end
164 Thurallor-7095
    end
165 Thurallor-7095
    for group in self:group_objects() do
166 Thurallor-7095
        if (not group:IsExpanded()) then
167 Thurallor-7095
            return false;
168 Thurallor-7095
        end
169 Thurallor-7095
    end
170 Thurallor-7095
    return true;
171 Thurallor-7095
end
172 Thurallor-7095
 
173 Thurallor-7095
function Group:Collapse()
174 Thurallor-7095
    for bar in self:bar_objects() do
175 Thurallor-7095
        bar:Collapse();
176 Thurallor-7095
    end
177 Thurallor-7095
    for group in self:group_objects() do
178 Thurallor-7095
        group:Collapse();
179 Thurallor-7095
    end
180 Thurallor-7095
end
181 Thurallor-7095
 
182 Thurallor-7095
function Group:IsCollapsed()
183 Thurallor-7095
    for bar in self:bar_objects() do
184 Thurallor-7095
        if (not bar:IsCollapsed()) then
185 Thurallor-7095
            return false;
186 Thurallor-7095
        end
187 Thurallor-7095
    end
188 Thurallor-7095
    for group in self:group_objects() do
189 Thurallor-7095
        if (not group:IsCollapsed()) then
190 Thurallor-7095
            return false;
191 Thurallor-7095
        end
192 Thurallor-7095
    end
193 Thurallor-7095
    return true;
194 Thurallor-7095
end
195 Thurallor-7095
 
196 43 Thurallor-7095
function Group:StartExecuting()
197 Thurallor-7095
    for bar in self:bar_objects() do
198 Thurallor-7095
        bar:StartExecuting();
199 Thurallor-7095
    end
200 Thurallor-7095
    for group in self:group_objects() do
201 Thurallor-7095
        group:StartExecuting();
202 Thurallor-7095
    end
203 Thurallor-7095
end
204 Thurallor-7095
 
205 Thurallor-7095
function Group:StopExecuting()
206 Thurallor-7095
    for bar in self:bar_objects() do
207 Thurallor-7095
        bar:StopExecuting();
208 Thurallor-7095
    end
209 Thurallor-7095
    for group in self:group_objects() do
210 Thurallor-7095
        group:StopExecuting();
211 Thurallor-7095
    end
212 Thurallor-7095
end
213 Thurallor-7095
 
214 16 Thurallor-7095
function Group:Redraw(noReset)
215 20 Thurallor-7095
    for bar in self:bar_objects() do
216 Thurallor-7095
        bar:Redraw(noReset);
217 2 Thurallor-7095
    end
218 20 Thurallor-7095
    for group in self:group_objects() do
219 Thurallor-7095
        group:Redraw(noReset);
220 2 Thurallor-7095
    end
221 Thurallor-7095
end
222 Thurallor-7095
 
223 Thurallor-7095
function Group:CanHaveChildren()
224 Thurallor-7095
    return true;
225 Thurallor-7095
end
226 Thurallor-7095
 
227 Thurallor-7095
function Group:Contains(object)
228 Thurallor-7095
    while (object.parent) do
229 Thurallor-7095
        if (object.parent == self) then
230 Thurallor-7095
            return true;
231 Thurallor-7095
        end
232 Thurallor-7095
        object = object.parent;
233 Thurallor-7095
    end
234 Thurallor-7095
    return false;
235 Thurallor-7095
end
236 Thurallor-7095
 
237 Thurallor-7095
function Group:GetChildIDs()
238 Thurallor-7095
    local objectIDs = {};
239 Thurallor-7095
    for b = 1, #self.settings.barIDs, 1 do
240 Thurallor-7095
        local barID = self.settings.barIDs[b];
241 Thurallor-7095
        table.insert(objectIDs, barID);
242 Thurallor-7095
    end
243 Thurallor-7095
    for g = 1, #self.settings.groupIDs, 1 do
244 Thurallor-7095
        local groupID = self.settings.groupIDs[g];
245 Thurallor-7095
        table.insert(objectIDs, groupID);
246 Thurallor-7095
    end
247 Thurallor-7095
    return objectIDs;
248 Thurallor-7095
end
249 Thurallor-7095
 
250 Thurallor-7095
function Group:GetChild(childID)
251 Thurallor-7095
    return self.manager.objects[childID];
252 Thurallor-7095
end
253 Thurallor-7095
 
254 Thurallor-7095
function Group:GetBarsMoveTogether()
255 Thurallor-7095
    if (self.settings.barsMoveTogether) then
256 Thurallor-7095
        return true;
257 Thurallor-7095
    elseif (self.parent and self.parent.GetBarsMoveTogether) then
258 Thurallor-7095
        return self.parent:GetBarsMoveTogether();
259 Thurallor-7095
    else
260 Thurallor-7095
        return false;
261 Thurallor-7095
    end
262 Thurallor-7095
end
263 Thurallor-7095
 
264 Thurallor-7095
function Group:IsEmpty()
265 Thurallor-7095
    return ((#self.settings.barIDs == 0) and (#self.settings.groupIDs == 0));
266 Thurallor-7095
end
267 Thurallor-7095
 
268 Thurallor-7095
function Group:CloneBar(barID)
269 Thurallor-7095
    local oldSettings = self.globals.bars[barID];
270 Thurallor-7095
    local newBarID = self:FindNewObjectID();
271 Thurallor-7095
    local newSettings = { };
272 Thurallor-7095
    self.globals.bars[newBarID] = newSettings;
273 Thurallor-7095
    DeepTableCopy(oldSettings, newSettings);
274 6 Thurallor-7095
    local newBar = Bar(self, newBarID, newSettings);
275 Thurallor-7095
    self.manager.objects[newBarID] = newBar;
276 2 Thurallor-7095
    local gridSize = self.globals.gridSize;
277 6 Thurallor-7095
    newBar:OffsetPosition(gridSize, gridSize);
278 Thurallor-7095
    newBar:MoveOntoScreen();
279 2 Thurallor-7095
    table.insert(self.settings.barIDs, newBarID);
280 Thurallor-7095
    self:SaveSettings(true);
281 Thurallor-7095
    return newBarID;
282 Thurallor-7095
end
283 Thurallor-7095
 
284 Thurallor-7095
function Group:CloneGroup(groupID)
285 Thurallor-7095
--Alert("Settings before cloning", PrettyPrint(self.globals, ""));
286 Thurallor-7095
    local oldSettings = self.globals.groups[groupID];
287 Thurallor-7095
    local newGroupID = self:FindNewObjectID();
288 Thurallor-7095
    local newSettings = {};
289 Thurallor-7095
 
290 Thurallor-7095
    -- Create a clone of the existing group, but with nothing in it
291 Thurallor-7095
    local oldGroup = self.manager.objects[groupID];
292 Thurallor-7095
    self.globals.groups[newGroupID] = newSettings;
293 Thurallor-7095
    DeepTableCopy(oldSettings, newSettings);
294 Thurallor-7095
    newSettings.barIDs = {};
295 Thurallor-7095
    newSettings.groupIDs = {};
296 Thurallor-7095
    table.insert(self.settings.groupIDs, newGroupID);
297 Thurallor-7095
    local newGroup = Group(self, newGroupID, newSettings);
298 Thurallor-7095
    self.manager.objects[newGroupID] = newGroup;
299 Thurallor-7095
 
300 Thurallor-7095
    -- Clone the bars and subgroups, and transfer the clones from old group to new group
301 Thurallor-7095
    for b = 1, #oldSettings.barIDs, 1 do
302 Thurallor-7095
        local barID = oldSettings.barIDs[b];
303 Thurallor-7095
        local newBarID = oldGroup:CloneBar(barID);
304 Thurallor-7095
        self.manager:TransferBar(newBarID, oldGroup, newGroup);
305 Thurallor-7095
    end
306 Thurallor-7095
    for g = 1, #oldSettings.groupIDs, 1 do
307 Thurallor-7095
        local oldGroupID = oldSettings.groupIDs[g];
308 Thurallor-7095
        local _newGroupID = oldGroup:CloneGroup(oldGroupID);
309 Thurallor-7095
        self.manager:TransferGroup(_newGroupID, oldGroup, newGroup);
310 Thurallor-7095
    end
311 Thurallor-7095
 
312 Thurallor-7095
    self:SaveSettings(true);
313 Thurallor-7095
--Alert("Settings after cloning", PrettyPrint(self.globals, ""));
314 Thurallor-7095
    return newGroupID;
315 Thurallor-7095
end
316 Thurallor-7095
 
317 Thurallor-7095
function Group:DeleteBar(barID)
318 Thurallor-7095
    local foundPos;
319 Thurallor-7095
    local bar = self.manager.objects[barID];
320 Thurallor-7095
    if (bar) then
321 16 Thurallor-7095
        self.globals.bars[barID].deleted = true;
322 2 Thurallor-7095
        bar:Destroy();
323 Thurallor-7095
    end
324 Thurallor-7095
    for pos = 1, #self.settings.barIDs, 1 do
325 Thurallor-7095
        if (self.settings.barIDs[pos] == barID) then
326 Thurallor-7095
            foundPos = pos;
327 Thurallor-7095
            break;
328 Thurallor-7095
        end
329 Thurallor-7095
    end
330 Thurallor-7095
    table.remove(self.settings.barIDs, foundPos);
331 Thurallor-7095
    table.insert(self.settings.deletedBarIDs, barID);
332 5 Thurallor-7095
    self.manager.objects[barID] = nil;
333 2 Thurallor-7095
    self:SaveSettings(true);
334 Thurallor-7095
end
335 Thurallor-7095
 
336 Thurallor-7095
function Group:DeleteGroup(groupID)
337 Thurallor-7095
    local group = self.manager.objects[groupID];
338 Thurallor-7095
    if (group) then
339 16 Thurallor-7095
        self.globals.groups[groupID].deleted = true;
340 2 Thurallor-7095
        group:Destroy();
341 Thurallor-7095
    end
342 20 Thurallor-7095
    local foundPos = Search(self.settings.groupIDs, groupID);
343 2 Thurallor-7095
    if (foundPos) then
344 Thurallor-7095
        table.remove(self.settings.groupIDs, foundPos);
345 Thurallor-7095
    end
346 Thurallor-7095
    table.insert(self.settings.deletedGroupIDs, groupID);
347 5 Thurallor-7095
    self.manager.objects[groupID] = nil;
348 2 Thurallor-7095
    self:SaveSettings(true);
349 Thurallor-7095
end
350 Thurallor-7095
 
351 Thurallor-7095
function Group:UndeleteBar(barID)
352 20 Thurallor-7095
    local foundPos = Search(self.settings.deletedBarIDs, barID);
353 2 Thurallor-7095
    if (foundPos) then
354 Thurallor-7095
        table.remove(self.settings.deletedBarIDs, foundPos);
355 16 Thurallor-7095
        self.globals.bars[barID].deleted = nil;
356 2 Thurallor-7095
        self.manager.objects[barID] = Bar(self, barID, self.globals.bars[barID]);
357 Thurallor-7095
        table.insert(self.settings.barIDs, barID);
358 Thurallor-7095
        self:SaveSettings(true);
359 Thurallor-7095
    end
360 Thurallor-7095
end
361 Thurallor-7095
 
362 Thurallor-7095
function Group:UndeleteGroup(groupID)
363 20 Thurallor-7095
    local foundPos = Search(self.settings.deletedGroupIDs, groupID);
364 2 Thurallor-7095
    if (foundPos) then
365 Thurallor-7095
        table.remove(self.settings.deletedGroupIDs, foundPos);
366 16 Thurallor-7095
        self.globals.groups[groupID].deleted = nil;
367 2 Thurallor-7095
        self.manager.objects[groupID] = Group(self, groupID, self.globals.groups[groupID]);
368 Thurallor-7095
        table.insert(self.settings.groupIDs, groupID);
369 Thurallor-7095
        self:SaveSettings(true);
370 Thurallor-7095
    end
371 Thurallor-7095
end
372 Thurallor-7095
 
373 Thurallor-7095
function Group:CreateBar()
374 Thurallor-7095
    local barID = self:FindNewObjectID();
375 Thurallor-7095
    self.globals.bars[barID] = {};
376 Thurallor-7095
    table.insert(self.settings.barIDs, barID);
377 13 Thurallor-7095
    local bar = Bar(self, barID, self.globals.bars[barID]);
378 Thurallor-7095
    self.manager.objects[barID] = bar;
379 Thurallor-7095
    bar:SnapToGrid();
380 2 Thurallor-7095
    self:SaveSettings(true);
381 17 Thurallor-7095
    return bar;
382 2 Thurallor-7095
end
383 Thurallor-7095
 
384 Thurallor-7095
function Group:CreateGroup()
385 Thurallor-7095
    local groupID = self:FindNewObjectID();
386 Thurallor-7095
    self.globals.groups[groupID] = { };
387 Thurallor-7095
    table.insert(self.settings.groupIDs, groupID);
388 Thurallor-7095
    self.manager.objects[groupID] = Group(self, groupID, self.globals.groups[groupID]);
389 Thurallor-7095
    self:SaveSettings(true);
390 17 Thurallor-7095
    return group;
391 2 Thurallor-7095
end
392 Thurallor-7095
 
393 Thurallor-7095
function Group:FindNewObjectID()
394 Thurallor-7095
    return self.manager:FindNewObjectID();
395 Thurallor-7095
end
396 Thurallor-7095
 
397 Thurallor-7095
function Group:Destroy()
398 20 Thurallor-7095
    for bar in self:bar_objects() do
399 Thurallor-7095
        bar:Destroy();
400 2 Thurallor-7095
    end
401 20 Thurallor-7095
    for group in self:group_objects() do
402 Thurallor-7095
        group:Destroy();
403 2 Thurallor-7095
    end
404 Thurallor-7095
    if (self.importWindow) then
405 Thurallor-7095
        self.importWindow:Close();
406 Thurallor-7095
    end
407 8 Thurallor-7095
    Node.Destroy(self);
408 2 Thurallor-7095
end
409 Thurallor-7095
 
410 Thurallor-7095
function Group:HaveTrash()
411 Thurallor-7095
    if (#self.settings.deletedBarIDs + #self.settings.deletedGroupIDs > 0) then
412 Thurallor-7095
        return true;
413 Thurallor-7095
    end
414 20 Thurallor-7095
    for group in self:group_objects() do
415 Thurallor-7095
        if (group:HaveTrash()) then
416 2 Thurallor-7095
            return true;
417 Thurallor-7095
        end
418 20 Thurallor-7095
    end
419 2 Thurallor-7095
    return false;
420 Thurallor-7095
end
421 Thurallor-7095
 
422 Thurallor-7095
function Group:OpenImportWindow()
423 Thurallor-7095
    -- Create the import window
424 Thurallor-7095
    local title = L:GetText("/ImportWindow/Title");
425 Thurallor-7095
    title = string.gsub(title, "<group>", self.settings.caption.text);
426 Thurallor-7095
    self.importWindow = ImportWindow(title, self);
427 Thurallor-7095
    self.importWindow.Closing = function()
428 Thurallor-7095
        self.importWindow = nil;
429 Thurallor-7095
    end
430 Thurallor-7095
end
431 Thurallor-7095
 
432 Thurallor-7095
function Group:Import(importData)
433 Thurallor-7095
    self.importData = importData;
434 Thurallor-7095
 
435 Thurallor-7095
    -- Uncompress the data in the next Update cycle.
436 Thurallor-7095
    self:SetWantsUpdates(true);
437 Thurallor-7095
    self.updateCount = 0;
438 Thurallor-7095
    self.oldUpdateFunction = self.Update;
439 Thurallor-7095
    self.Update = function(g)
440 Thurallor-7095
        g.updateCount = g.updateCount + 1;
441 Thurallor-7095
        if (g.updateCount == 1) then
442 Thurallor-7095
            if (not string.find(g.importData, "{")) then
443 Thurallor-7095
                Puts(L:GetText("/ImportWindow/Decoding"));
444 Thurallor-7095
                g.encodedData = g.importData;
445 Thurallor-7095
            else
446 Thurallor-7095
                g.decodedData = g.importData;
447 Thurallor-7095
            end
448 Thurallor-7095
            g.importData = nil;
449 Thurallor-7095
        elseif (g.updateCount == 3) then
450 Thurallor-7095
            if (g.encodedData) then
451 Thurallor-7095
                -- Remove whitespace and newlines
452 Thurallor-7095
                g.encodedData = string.gsub(g.encodedData, "[%c%s]", "");
453 Thurallor-7095
                local success, decodedData = pcall(Text2Bin, g.encodedData);
454 Thurallor-7095
                if (not success) then
455 6 Thurallor-7095
                    Puts(L:GetText("/ImportWindow/InvalidEncoding"));
456 2 Thurallor-7095
                    g.encodedData = nil;
457 Thurallor-7095
                    g.updateCount = 10;
458 Thurallor-7095
                    return;
459 Thurallor-7095
                end
460 Thurallor-7095
                g.encodedData = nil;
461 Thurallor-7095
                g.decodedData = decodedData;
462 Thurallor-7095
            end
463 Thurallor-7095
        elseif (g.updateCount == 4) then
464 Thurallor-7095
            Puts(L:GetText("/ImportWindow/Decompressing"));
465 Thurallor-7095
            g.compressor = Thurallor.Utils.LibCompress();
466 Thurallor-7095
        elseif (g.updateCount == 6) then
467 Thurallor-7095
            if (g.decodedData) then
468 Thurallor-7095
                local success, uncompressedData = pcall(g.compressor.Decompress, g.compressor, g.decodedData);
469 Thurallor-7095
                if ((not success) or (not uncompressedData)) then
470 Thurallor-7095
                    -- Data was either not compressed or invalid.  Let's assume the former for now.
471 Thurallor-7095
                    uncompressedData = g.decodedData;
472 Thurallor-7095
                end
473 Thurallor-7095
                g.decodedData = nil;
474 Thurallor-7095
                g.uncompressedData = uncompressedData;
475 Thurallor-7095
            end
476 Thurallor-7095
        elseif (g.updateCount == 7) then
477 6 Thurallor-7095
--Alert("g.uncompressedData", g.uncompressedData);
478 2 Thurallor-7095
            local f, e = loadstring("return " .. g.uncompressedData);
479 Thurallor-7095
            if (not f) then
480 6 Thurallor-7095
                Puts(L:GetText("/ImportWindow/ParseError") .. tostring(e));
481 2 Thurallor-7095
            else
482 Thurallor-7095
                g.newObjects = f();
483 6 Thurallor-7095
                if (type(g.newObjects) == "table") then
484 Thurallor-7095
                    Puts(L:GetText("/ImportWindow/Importing"));
485 Thurallor-7095
                    g:AddImportedObjects();
486 Thurallor-7095
                else
487 Thurallor-7095
                    Puts(L:GetText("/ImportWindow/NothingToDo"));
488 Thurallor-7095
                end
489 2 Thurallor-7095
            end
490 Thurallor-7095
            g.compressor = nil;
491 Thurallor-7095
            g.uncompressedData = nil;
492 Thurallor-7095
            g.newObjects = nil;
493 Thurallor-7095
        elseif (g.updateCount == 10) then
494 Thurallor-7095
            g:SetWantsUpdates(false);
495 Thurallor-7095
            g.Update = g.oldUpdateFunction; -- if this is a Manager object, it needs to have its Update function restored
496 Thurallor-7095
            g:SaveSettings(true);
497 Thurallor-7095
        end
498 Thurallor-7095
    end
499 Thurallor-7095
end
500 Thurallor-7095
 
501 Thurallor-7095
function Group:AddImportedObjects()
502 Thurallor-7095
 
503 Thurallor-7095
    -- Assign new object IDs to the new bars and groups.
504 Thurallor-7095
    -- Find out which one(s) are at the top level.
505 Thurallor-7095
    local newObjectIDs, hasParent = {}, {};
506 Thurallor-7095
    for oldObjectID, settings in pairs(self.newObjects) do
507 Thurallor-7095
        if (not newObjectIDs[oldObjectID]) then
508 Thurallor-7095
            newObjectIDs[oldObjectID] = self:FindNewObjectID();
509 Thurallor-7095
--Puts("     " .. oldObjectID .. " -> " .. newObjectIDs[oldObjectID]);
510 Thurallor-7095
        end
511 Thurallor-7095
        if (settings.type == "group") then
512 Thurallor-7095
            local title = L:GetText("/GroupMenu/UndeleteMenu/GroupName");
513 Thurallor-7095
            title = string.gsub(title, "<name>", settings.caption.text);
514 6 Thurallor-7095
            Puts(" + " .. title);
515 2 Thurallor-7095
            for b = 1, #settings.barIDs do
516 Thurallor-7095
                local oldBarID = settings.barIDs[b];
517 Thurallor-7095
                if (not newObjectIDs[oldBarID]) then
518 Thurallor-7095
                    newObjectIDs[oldBarID] = self:FindNewObjectID();
519 Thurallor-7095
--Puts("     " .. oldObjectID .. " -> " .. newObjectIDs[oldObjectID]);
520 Thurallor-7095
                end
521 Thurallor-7095
                local barID = newObjectIDs[oldBarID];
522 Thurallor-7095
                settings.barIDs[b] = barID;
523 Thurallor-7095
                hasParent[barID] = true;
524 Thurallor-7095
            end
525 Thurallor-7095
            for g = 1, #settings.groupIDs do
526 Thurallor-7095
                local oldGroupID = settings.groupIDs[g];
527 Thurallor-7095
                if (not newObjectIDs[oldGroupID]) then
528 Thurallor-7095
                    newObjectIDs[oldGroupID] = self:FindNewObjectID();
529 Thurallor-7095
--Puts("     " .. oldObjectID .. " -> " .. newObjectIDs[oldObjectID]);
530 Thurallor-7095
                end
531 Thurallor-7095
                local groupID = newObjectIDs[oldGroupID];
532 Thurallor-7095
                settings.groupIDs[g] = groupID;
533 Thurallor-7095
                hasParent[groupID] = true;
534 Thurallor-7095
            end
535 Thurallor-7095
        else
536 Thurallor-7095
            local title = L:GetText("/GroupMenu/UndeleteMenu/BarName");
537 Thurallor-7095
            title = string.gsub(title, "<name>", settings.caption.text);
538 6 Thurallor-7095
            Puts(" + " .. title);
539 2 Thurallor-7095
        end
540 Thurallor-7095
    end
541 Thurallor-7095
 
542 16 Thurallor-7095
    -- Update all "include" slots with new object IDs
543 Thurallor-7095
    for oldObjectID, settings in pairs(self.newObjects) do
544 Thurallor-7095
        if (settings.sequenceItemInfo) then
545 Thurallor-7095
            for i = 1, #settings.sequenceItemInfo, 1 do
546 Thurallor-7095
                local item = settings.sequenceItemInfo[i];
547 Thurallor-7095
                if (item.include and newObjectIDs[item.include]) then
548 Thurallor-7095
                    item.include = newObjectIDs[item.include];
549 Thurallor-7095
                end
550 Thurallor-7095
            end
551 Thurallor-7095
        end
552 Thurallor-7095
    end
553 Thurallor-7095
 
554 2 Thurallor-7095
    -- Add new group/bar settings to the database.
555 Thurallor-7095
    for oldObjectID, settings in pairs(self.newObjects) do
556 Thurallor-7095
        if (settings.type == "group") then
557 Thurallor-7095
            local groupID = newObjectIDs[oldObjectID];
558 Thurallor-7095
            self.globals.groups[groupID] = settings;
559 Thurallor-7095
        else
560 Thurallor-7095
            local barID = newObjectIDs[oldObjectID];
561 Thurallor-7095
            self.globals.bars[barID] = settings;
562 Thurallor-7095
        end
563 Thurallor-7095
    end
564 Thurallor-7095
 
565 Thurallor-7095
    -- For the top-level bars/groups, add them to the current group.
566 Thurallor-7095
    for oldObjectID, settings in pairs(self.newObjects) do
567 Thurallor-7095
        if (settings.type == "group") then
568 Thurallor-7095
            local groupID = newObjectIDs[oldObjectID];
569 Thurallor-7095
            if (not hasParent[groupID]) then
570 Thurallor-7095
                table.insert(self.settings.groupIDs, groupID);
571 Thurallor-7095
                self.manager.objects[groupID] = Group(self, groupID, settings);
572 Thurallor-7095
            end
573 Thurallor-7095
        else
574 Thurallor-7095
            local barID = newObjectIDs[oldObjectID];
575 Thurallor-7095
            if (not hasParent[barID]) then
576 Thurallor-7095
                table.insert(self.settings.barIDs, barID);
577 Thurallor-7095
                self.manager.objects[barID] = Bar(self, barID, settings);
578 Thurallor-7095
            end
579 Thurallor-7095
        end
580 Thurallor-7095
    end
581 Thurallor-7095
end
582 Thurallor-7095
 
583 Thurallor-7095
function Group:AddSettingsMenuItem(parent, itemName, amSubMenu)
584 Thurallor-7095
    if (amSubMenu ~= nil) then
585 Thurallor-7095
        self.amSubMenu = amSubMenu;
586 Thurallor-7095
    end
587 Thurallor-7095
    local item = Turbine.UI.MenuItem(L:GetText(itemName), true, false);
588 Thurallor-7095
    parent:GetItems():Add(item);
589 Thurallor-7095
 
590 Thurallor-7095
    if (itemName == "Root") then
591 Thurallor-7095
        local prevContext = L:SetContext("/GroupMenu");
592 Thurallor-7095
        parent:GetItems():Clear();
593 Thurallor-7095
        self:AddSettingsMenuItem(parent, "Hide");
594 26 Thurallor-7095
        if (not self:IsExpanded()) then
595 Thurallor-7095
            self:AddSettingsMenuItem(parent, "Expand");
596 Thurallor-7095
        end
597 Thurallor-7095
        if (not self:IsCollapsed()) then
598 Thurallor-7095
            self:AddSettingsMenuItem(parent, "Collapse");
599 Thurallor-7095
        end
600 2 Thurallor-7095
        self:AddSettingsMenuItem(parent, "CloneGroup");
601 Thurallor-7095
        self:AddSettingsMenuItem(parent, "DeleteGroup");
602 Thurallor-7095
        self:AddSettingsMenuItem(parent, "ExportGroup");
603 Thurallor-7095
        if (not self.importWindow) then
604 Thurallor-7095
            self:AddSettingsMenuItem(parent, "Import");
605 Thurallor-7095
        end
606 Thurallor-7095
        self:AddSettingsMenuItem(parent, "CreateNewBar");
607 Thurallor-7095
        self:AddSettingsMenuItem(parent, "CreateNewSubgroup");
608 Thurallor-7095
        if (#self.settings.deletedBarIDs + #self.settings.deletedGroupIDs > 0) then
609 Thurallor-7095
            self:AddSettingsMenuItem(parent, "Undelete");
610 Thurallor-7095
        end
611 Thurallor-7095
        self:AddSettingsMenuItem(parent, "ArrangeBars");
612 12 Thurallor-7095
        self:AddSettingsMenuItem(parent, "GroupEventBehaviors");
613 2 Thurallor-7095
        self:AddSettingsMenuItem(parent, "GroupSettings");
614 Thurallor-7095
        if (self.amSubMenu) then
615 Thurallor-7095
            -- self:AddSettingsMenuItem(parent, "Other Bars");
616 Thurallor-7095
        else
617 Thurallor-7095
            -- self:AddSettingsMenuItem(parent, "Bars");
618 Thurallor-7095
            self:AddSettingsMenuItem(parent, "Global");
619 Thurallor-7095
        end
620 Thurallor-7095
        L:SetContext(prevContext);
621 Thurallor-7095
    elseif (itemName == "ExportGroup") then
622 Thurallor-7095
        item.Click = function(sender, args)
623 Thurallor-7095
            self:Export();
624 Thurallor-7095
        end
625 Thurallor-7095
    elseif (itemName == "Import") then
626 Thurallor-7095
        item.Click = function(sender, args)
627 Thurallor-7095
            self:OpenImportWindow();
628 Thurallor-7095
        end
629 Thurallor-7095
    elseif (itemName == "Hide") then
630 Thurallor-7095
        item:SetChecked(self.settings.hidden);
631 Thurallor-7095
        item.Click = function(sender, args)
632 Thurallor-7095
            self:SetHidden(not self.settings.hidden);
633 Thurallor-7095
        end
634 Thurallor-7095
    elseif (itemName == "GroupSettings") then
635 Thurallor-7095
        local prevContext = L:SetContext("/GroupMenu/GroupSettingsMenu");
636 Thurallor-7095
        self:AddSettingsMenuItem(item, "Rename");
637 Thurallor-7095
        self:AddSettingsMenuItem(item, "BarsMoveTogether");
638 Thurallor-7095
        L:SetContext(prevContext);
639 Thurallor-7095
    elseif (itemName == "Undelete") then
640 Thurallor-7095
        local prevContext = L:SetContext("/GroupMenu/UndeleteMenu");
641 Thurallor-7095
        for b = 1, #self.settings.deletedBarIDs, 1 do
642 Thurallor-7095
            local barID = self.settings.deletedBarIDs[b];
643 Thurallor-7095
            local barItemName = L:GetText("BarName");
644 5 Thurallor-7095
            if (self.globals.bars[barID]) then
645 Thurallor-7095
                local barCaption = self.globals.bars[barID].caption.text;
646 Thurallor-7095
                barItemName = string.gsub(barItemName, "<name>", barCaption);
647 Thurallor-7095
                local barItem = Turbine.UI.MenuItem(barItemName, true, false);
648 Thurallor-7095
                item:GetItems():Add(barItem);
649 Thurallor-7095
                barItem.Click = function(sender, args)
650 Thurallor-7095
                    self:UndeleteBar(barID);
651 Thurallor-7095
                end
652 2 Thurallor-7095
            end
653 Thurallor-7095
        end
654 Thurallor-7095
        for g = 1, #self.settings.deletedGroupIDs, 1 do
655 Thurallor-7095
            local groupID = self.settings.deletedGroupIDs[g];
656 Thurallor-7095
            local groupItemName = L:GetText("GroupName");
657 5 Thurallor-7095
            if (self.globals.groups[groupID]) then
658 Thurallor-7095
                local groupName = self.globals.groups[groupID].caption.text;
659 Thurallor-7095
                groupItemName = string.gsub(groupItemName, "<name>", groupName);
660 Thurallor-7095
                local groupItem = Turbine.UI.MenuItem(groupItemName, true, false);
661 Thurallor-7095
                item:GetItems():Add(groupItem);
662 Thurallor-7095
                groupItem.Click = function(sender, args)
663 Thurallor-7095
                    self:UndeleteGroup(groupID);
664 Thurallor-7095
                end
665 2 Thurallor-7095
            end
666 Thurallor-7095
        end
667 Thurallor-7095
        L:SetContext(prevContext);
668 Thurallor-7095
    elseif (itemName == "Rename") then
669 Thurallor-7095
        item:SetEnabled(not self.captionEditor);
670 Thurallor-7095
        item.Click = function(sender, args)
671 Thurallor-7095
            self:EditCaption();
672 Thurallor-7095
        end
673 Thurallor-7095
    elseif (itemName == "BarsMoveTogether") then
674 Thurallor-7095
        item:SetChecked(self.settings.barsMoveTogether);
675 Thurallor-7095
        item.Click = function(sender, args)
676 Thurallor-7095
            self.settings.barsMoveTogether = not self.settings.barsMoveTogether;
677 Thurallor-7095
        end
678 Thurallor-7095
    elseif (itemName == "ArrangeBars") then
679 Thurallor-7095
        local prevContext = L:SetContext("/GroupMenu/ArrangeBarsMenu");
680 Thurallor-7095
        local descendentBarIDs = self:FindAllDescendentIDs(nil, true, nil);
681 Thurallor-7095
        if (#descendentBarIDs < 2) then
682 Thurallor-7095
            item:SetEnabled(false);
683 Thurallor-7095
        else
684 Thurallor-7095
            self:AddSettingsMenuItem(item, "AlignVertically");
685 Thurallor-7095
            self:AddSettingsMenuItem(item, "AlignHorizontally");
686 Thurallor-7095
        end
687 Thurallor-7095
        L:SetContext(prevContext);
688 12 Thurallor-7095
    elseif ((itemName == "GroupEventBehaviors") or (itemName == "GlobalEventBehaviors")) then
689 8 Thurallor-7095
        local prevContext = L:SetContext("/EventBehaviorsMenu");
690 2 Thurallor-7095
        if (self.settings.eventsEnabled) then
691 Thurallor-7095
            if (#self.settings.eventHandlers > 0) then
692 Thurallor-7095
                self:AddSettingsMenuItem(item, "CurrentBehaviors");
693 Thurallor-7095
            end
694 Thurallor-7095
            self:AddSettingsMenuItem(item, "AddBehavior");
695 Thurallor-7095
            self:AddSettingsMenuItem(item, "DisableEvents");
696 Thurallor-7095
        else
697 Thurallor-7095
            self:AddSettingsMenuItem(item, "EnableEvents");
698 Thurallor-7095
        end
699 Thurallor-7095
        L:SetContext(prevContext);
700 Thurallor-7095
    elseif (itemName == "EnableEvents") then
701 Thurallor-7095
        item.Click = function()
702 Thurallor-7095
            self.settings.eventsEnabled = true;
703 Thurallor-7095
            self:SaveSettings(false);
704 Thurallor-7095
            self:SetWantsEvents(true);
705 Thurallor-7095
        end
706 Thurallor-7095
    elseif (itemName == "DisableEvents") then
707 Thurallor-7095
        item.Click = function()
708 Thurallor-7095
            self.settings.eventsEnabled = false;
709 Thurallor-7095
            self:SaveSettings(false);
710 8 Thurallor-7095
            self:SetWantsEvents(false);
711 2 Thurallor-7095
        end
712 Thurallor-7095
    elseif (itemName == "CurrentBehaviors") then
713 Thurallor-7095
        if (self.settings.eventHandlers) then
714 Thurallor-7095
            for h = 1, #self.settings.eventHandlers do
715 Thurallor-7095
                local handler = self.settings.eventHandlers[h];
716 Thurallor-7095
                local handlerItem = Turbine.UI.MenuItem(handler.description, true, false);
717 Thurallor-7095
                item:GetItems():Add(handlerItem);
718 Thurallor-7095
                local removeItem = Turbine.UI.MenuItem(L:GetText("Remove"), true, false);
719 Thurallor-7095
                handlerItem:GetItems():Add(removeItem);
720 Thurallor-7095
                removeItem.Click = function()
721 Thurallor-7095
                    self:RemoveEventHandler(h);
722 Thurallor-7095
                end
723 Thurallor-7095
            end
724 Thurallor-7095
        end
725 Thurallor-7095
    elseif (itemName == "AddBehavior") then
726 20 Thurallor-7095
        local prevContext = L:SetContext("/EventBehaviorsMenu/Actions");
727 2 Thurallor-7095
        self:AddSettingsMenuItem(item, "ShowGroup");
728 Thurallor-7095
        self:AddSettingsMenuItem(item, "HideGroup");
729 Thurallor-7095
        self:AddSettingsMenuItem(item, "ResetGroup");
730 26 Thurallor-7095
        self:AddSettingsMenuItem(item, "ExpandGroup");
731 Thurallor-7095
        self:AddSettingsMenuItem(item, "CollapseGroup");
732 43 Thurallor-7095
        self:AddSettingsMenuItem(item, "StartExecuting");
733 Thurallor-7095
        self:AddSettingsMenuItem(item, "StopExecuting");
734 20 Thurallor-7095
        L:SetContext(prevContext);
735 43 Thurallor-7095
    elseif (string.find("|ShowGroup|HideGroup|ResetGroup|ExpandGroup|CollapseGroup|StartExecuting|StopExecuting|", "|" .. itemName .. "|")) then
736 20 Thurallor-7095
        local prevContext = L:SetContext("/EventBehaviorsMenu/Triggers");
737 2 Thurallor-7095
        item.itemName = itemName;
738 43 Thurallor-7095
        if ((itemName ~= "ResetGroup") and (itemName ~= "StopExecuting")) then
739 34 Thurallor-7095
            self:AddSettingsMenuItem(item, "AtStartup");
740 Thurallor-7095
        end
741 2 Thurallor-7095
        self:AddSettingsMenuItem(item, "WhenCombatBegins");
742 Thurallor-7095
        self:AddSettingsMenuItem(item, "WhenCombatEnds");
743 46 Thurallor-7095
        self:AddSettingsMenuItem(item, "WhenTargetChanges");
744 43 Thurallor-7095
        if (string.find("|ResetGroup|ExpandGroup|StartExecuting|", "|" .. itemName .. "|")) then
745 42 Thurallor-7095
            self:AddSettingsMenuItem(item, "WhenMouseArrives");
746 Thurallor-7095
        end
747 43 Thurallor-7095
        if (string.find("|HideGroup|ResetGroup|CollapseGroup|StopExecuting|", "|" .. itemName .. "|")) then
748 42 Thurallor-7095
            self:AddSettingsMenuItem(item, "WhenMouseDeparts");
749 Thurallor-7095
        end
750 43 Thurallor-7095
        if (string.find("|ShowGroup|ResetGroup|StartExecuting|StopExecuting|", "|" .. itemName .. "|")) then
751 35 Thurallor-7095
            self:AddSettingsMenuItem(item, "WhenTraitTreeChanges");
752 Thurallor-7095
        end
753 2 Thurallor-7095
        self.availableEvents = self.manager:GetEventNames();
754 33 Thurallor-7095
        self:AddSettingsMenuItem(item, "WhenEventOccurs");
755 20 Thurallor-7095
        L:SetContext(prevContext);
756 2 Thurallor-7095
    elseif (itemName == "WhenEventOccurs") then
757 33 Thurallor-7095
        if (#self.availableEvents > 0) then
758 Thurallor-7095
            for e = 1, #self.availableEvents do
759 Thurallor-7095
                local eventName = self.availableEvents[e];
760 Thurallor-7095
                local eventNameItem = Turbine.UI.MenuItem(eventName, true, false);
761 Thurallor-7095
                item:GetItems():Add(eventNameItem);
762 Thurallor-7095
                eventNameItem.Click = function()
763 Thurallor-7095
                    self:AddEventHandler("WhenEventOccurs:" .. eventName, parent.itemName);
764 Thurallor-7095
                end
765 2 Thurallor-7095
            end
766 33 Thurallor-7095
        else
767 Thurallor-7095
            local noEventsItem = Turbine.UI.MenuItem(L:GetText("/EventBehaviorsMenu/NoEventsDefined"), false, false);
768 Thurallor-7095
            item:GetItems():Add(noEventsItem);
769 2 Thurallor-7095
        end
770 46 Thurallor-7095
    elseif (string.find("|AtStartup|WhenTargetChanges|WhenCombatBegins|WhenCombatEnds|WhenMouseArrives|WhenMouseDeparts|WhenTraitTreeChanges|", "|" .. itemName .. "|")) then
771 2 Thurallor-7095
        item.Click = function()
772 Thurallor-7095
            self:AddEventHandler(itemName, parent.itemName);
773 Thurallor-7095
        end
774 Thurallor-7095
    elseif (itemName == "AlignVertically") then
775 Thurallor-7095
        local prevContext = L:SetContext("/GroupMenu/ArrangeBarsMenu/AlignVerticallyMenu");
776 Thurallor-7095
        self:AddSettingsMenuItem(item, "Tops");
777 Thurallor-7095
        self:AddSettingsMenuItem(item, "Middles");
778 Thurallor-7095
        self:AddSettingsMenuItem(item, "Bottoms");
779 Thurallor-7095
        L:SetContext(prevContext);
780 Thurallor-7095
    elseif (itemName == "AlignHorizontally") then
781 Thurallor-7095
        local prevContext = L:SetContext("/GroupMenu/ArrangeBarsMenu/AlignHorizontallyMenu");
782 Thurallor-7095
        self:AddSettingsMenuItem(item, "LeftSides");
783 Thurallor-7095
        self:AddSettingsMenuItem(item, "Centers");
784 Thurallor-7095
        self:AddSettingsMenuItem(item, "RightSides");
785 Thurallor-7095
        L:SetContext(prevContext);
786 11 Thurallor-7095
    elseif (string.find("|Tops|Middles|Bottoms|LeftSides|Centers|RightSides|", "|" .. itemName .. "|")) then
787 2 Thurallor-7095
        local checked = self:BarsAreAligned(itemName);
788 Thurallor-7095
        item:SetChecked(checked);
789 Thurallor-7095
        item:SetEnabled(not checked);
790 Thurallor-7095
        item.Click = function(sender, args)
791 Thurallor-7095
            self:AlignBars(itemName);
792 Thurallor-7095
        end
793 26 Thurallor-7095
    elseif (itemName == "Expand") then
794 Thurallor-7095
        item.Click = function(sender, args)
795 Thurallor-7095
            self:Expand();
796 Thurallor-7095
        end
797 Thurallor-7095
    elseif (itemName == "Collapse") then
798 Thurallor-7095
        item.Click = function(sender, args)
799 Thurallor-7095
            self:Collapse();
800 Thurallor-7095
        end
801 2 Thurallor-7095
    elseif (itemName == "CloneGroup") then
802 Thurallor-7095
        item.Click = function(sender, args)
803 Thurallor-7095
            self.parent:CloneGroup(self.groupID);
804 Thurallor-7095
        end
805 Thurallor-7095
    elseif (itemName == "DeleteGroup") then
806 Thurallor-7095
        item.Click = function(sender, args)
807 Thurallor-7095
            self.parent:DeleteGroup(self.groupID);
808 Thurallor-7095
        end
809 Thurallor-7095
    elseif (itemName == "Global") then
810 Thurallor-7095
        self.manager:AddSettingsMenuItem(item, "Root", true);
811 Thurallor-7095
    elseif (itemName == "CreateNewBar") then
812 Thurallor-7095
        item.Click = function(sender, args)
813 Thurallor-7095
            self:CreateBar();
814 Thurallor-7095
        end
815 Thurallor-7095
    elseif (itemName == "CreateNewSubgroup") then
816 Thurallor-7095
        item.Click = function(sender, args)
817 Thurallor-7095
            self:CreateGroup();
818 Thurallor-7095
        end
819 Thurallor-7095
    end
820 Thurallor-7095
end
821 Thurallor-7095
 
822 Thurallor-7095
function Group:Reset()
823 20 Thurallor-7095
    for bar in self:bar_objects() do
824 Thurallor-7095
        bar:Reset();
825 2 Thurallor-7095
    end
826 20 Thurallor-7095
    for group in self:group_objects() do
827 Thurallor-7095
        group:Reset();
828 Thurallor-7095
    end
829 2 Thurallor-7095
end
830 Thurallor-7095
 
831 Thurallor-7095
function Group:FindAllDescendentIDs(descendents, includeBars, includeGroups)
832 Thurallor-7095
    if (descendents == nil) then
833 Thurallor-7095
        descendents = {};
834 Thurallor-7095
    end
835 20 Thurallor-7095
    for group in self:group_objects() do
836 2 Thurallor-7095
        if (includeGroups) then
837 20 Thurallor-7095
            table.insert(descendents, group.objectID);
838 2 Thurallor-7095
        end
839 Thurallor-7095
        group:FindAllDescendentIDs(descendents, includeBars, includeGroups);
840 Thurallor-7095
    end
841 20 Thurallor-7095
    if (includeBars) then
842 Thurallor-7095
        for bar in self:bar_objects() do
843 Thurallor-7095
            table.insert(descendents, bar.objectID);
844 2 Thurallor-7095
        end
845 Thurallor-7095
    end
846 Thurallor-7095
    return descendents;
847 Thurallor-7095
end
848 Thurallor-7095
 
849 Thurallor-7095
function Group:GetBarLocationData(barIDs)
850 Thurallor-7095
    local lefts, tops, rights, bottoms, centers, middles = {}, {}, {}, {}, {}, {};
851 Thurallor-7095
    if (barIDs == nil) then
852 Thurallor-7095
        barIDs = self:FindAllDescendentIDs(nil, true, nil);
853 Thurallor-7095
    end
854 20 Thurallor-7095
    local numBars = 0;
855 Thurallor-7095
    for b = 1, #barIDs, 1 do
856 2 Thurallor-7095
        local barID = barIDs[b];
857 Thurallor-7095
        local bar = self.manager.objects[barID];
858 20 Thurallor-7095
        if (bar) then
859 Thurallor-7095
            numBars = numBars + 1;
860 Thurallor-7095
            local left, top, width, height = bar:GetVisibleLeft(), bar:GetVisibleTop(), bar:GetVisibleWidth(), bar:GetVisibleHeight();
861 Thurallor-7095
            table.insert(lefts, left);
862 Thurallor-7095
            table.insert(tops, top);
863 Thurallor-7095
            local right, bottom = left + width, top + height;
864 Thurallor-7095
            table.insert(rights, right);
865 Thurallor-7095
            table.insert(bottoms, bottom);
866 Thurallor-7095
            local center = (left + right) / 2;
867 Thurallor-7095
            local middle = (top + bottom) / 2;
868 Thurallor-7095
            table.insert(centers, center);
869 Thurallor-7095
            table.insert(middles, middle);
870 Thurallor-7095
        end
871 2 Thurallor-7095
    end
872 Thurallor-7095
    return numBars, lefts, tops, rights, bottoms, centers, middles;
873 Thurallor-7095
end
874 Thurallor-7095
 
875 Thurallor-7095
function Group:GetBarLocationStats(numBars, lefts, tops, rights, bottoms, centers, middles)
876 Thurallor-7095
    local minLeft, maxRight = math.min(unpack(lefts)), math.max(unpack(rights));
877 Thurallor-7095
    local minTop, maxBottom = math.min(unpack(tops)), math.max(unpack(bottoms));
878 Thurallor-7095
    local avgCenter, avgMiddle = 0, 0;
879 Thurallor-7095
    for b = 1, numBars, 1 do
880 Thurallor-7095
        avgCenter = avgCenter + centers[b];
881 Thurallor-7095
        avgMiddle = avgMiddle + middles[b];
882 Thurallor-7095
    end
883 Thurallor-7095
    avgCenter = avgCenter / numBars;
884 Thurallor-7095
    avgMiddle = avgMiddle / numBars;
885 Thurallor-7095
    local minCenter, maxCenter = math.min(unpack(centers)), math.max(unpack(centers));
886 Thurallor-7095
    local minMiddle, maxMiddle = math.min(unpack(middles)), math.max(unpack(middles));
887 Thurallor-7095
    return numBars, minLeft, maxRight, minTop, maxBottom, minCenter, avgCenter, maxCenter, minMiddle, avgMiddle, maxMiddle;
888 Thurallor-7095
end
889 Thurallor-7095
 
890 Thurallor-7095
function Group:BarsAreAligned(alignment)
891 Thurallor-7095
    local barIDs = self:FindAllDescendentIDs(nil, true, nil);
892 Thurallor-7095
    local numBars, lefts, tops, rights, bottoms, centers, middles = self:GetBarLocationData(barIDs);
893 Thurallor-7095
    local _, minLeft, maxRight, minTop, maxBottom, _, avgCenter, _, _, avgMiddle, _ = self:GetBarLocationStats(numBars, lefts, tops, rights, bottoms, centers, middles);
894 Thurallor-7095
    local aligned = true;
895 Thurallor-7095
    for b = 1, numBars, 1 do
896 Thurallor-7095
        if (alignment == "LeftSides") then
897 Thurallor-7095
            aligned = aligned and lefts[b] == minLeft;
898 Thurallor-7095
        elseif (alignment == "RightSides") then
899 Thurallor-7095
            aligned = aligned and rights[b] == maxRight;
900 Thurallor-7095
        elseif (alignment == "Tops") then
901 Thurallor-7095
            aligned = aligned and tops[b] == minTop;
902 Thurallor-7095
        elseif (alignment == "Bottoms") then
903 Thurallor-7095
            aligned = aligned and bottoms[b] == maxBottom;
904 Thurallor-7095
        elseif (alignment == "Centers") then
905 Thurallor-7095
            aligned = aligned and math.floor(centers[b]) == math.floor(avgCenter);
906 Thurallor-7095
        elseif (alignment == "Middles") then
907 Thurallor-7095
            aligned = aligned and math.floor(middles[b]) == math.floor(avgMiddle);
908 Thurallor-7095
        end
909 Thurallor-7095
    end
910 Thurallor-7095
    return aligned;
911 Thurallor-7095
end
912 Thurallor-7095
 
913 Thurallor-7095
function Group:AlignBars(alignment)
914 Thurallor-7095
    local barIDs = self:FindAllDescendentIDs(nil, true, nil);
915 Thurallor-7095
    local numBars, lefts, tops, rights, bottoms, centers, middles = self:GetBarLocationData(barIDs);
916 Thurallor-7095
    local _, minLeft, maxRight, minTop, maxBottom, _, avgCenter, _, _, avgMiddle, _ = self:GetBarLocationStats(numBars, lefts, tops, rights, bottoms, centers, middles);
917 20 Thurallor-7095
    for bar in self:bar_objects() do
918 2 Thurallor-7095
        if (alignment == "LeftSides") then
919 Thurallor-7095
            bar:SetVisibleLeft(minLeft);
920 Thurallor-7095
        elseif (alignment == "RightSides") then
921 Thurallor-7095
--Puts("For bar " .. bar.settings.caption.text .. ", setting visible left to maxRight=" .. maxRight .. " - visibleWidth=" .. bar:GetVisibleWidth());
922 Thurallor-7095
            bar:SetVisibleLeft(maxRight - bar:GetVisibleWidth());
923 Thurallor-7095
        elseif (alignment == "Tops") then
924 Thurallor-7095
            bar:SetVisibleTop(minTop);
925 Thurallor-7095
        elseif (alignment == "Bottoms") then
926 Thurallor-7095
            bar:SetVisibleTop(maxBottom - bar:GetVisibleHeight());
927 Thurallor-7095
        elseif (alignment == "Centers") then
928 Thurallor-7095
            bar:SetVisibleLeft(avgCenter - math.floor((bar:GetVisibleWidth() + 0.5) / 2));
929 Thurallor-7095
        elseif (alignment == "Middles") then
930 Thurallor-7095
            bar:SetVisibleTop(avgMiddle - math.floor((bar:GetVisibleHeight() + 0.5) / 2));
931 Thurallor-7095
        end
932 Thurallor-7095
        bar:Redraw();
933 Thurallor-7095
    end
934 Thurallor-7095
    self:SaveSettings(false);
935 Thurallor-7095
end
936 Thurallor-7095
 
937 17 Thurallor-7095
function Group:SnapToGrid(direction)
938 Thurallor-7095
    -- Check the parent group for a higher-level operation
939 Thurallor-7095
    if ((direction == "up") and self.parent and self.parent.GetBarsMoveTogether and self.parent:GetBarsMoveTogether()) then
940 Thurallor-7095
        self.parent:SnapToGrid("up");
941 Thurallor-7095
        return;
942 Thurallor-7095
    end
943 Thurallor-7095
 
944 20 Thurallor-7095
    -- Snap all children contained in this group to grid
945 Thurallor-7095
    for bar in self:bar_objects() do
946 Thurallor-7095
        bar:SnapToGrid();
947 2 Thurallor-7095
    end
948 20 Thurallor-7095
    for group in self:group_objects() do
949 Thurallor-7095
        group:SnapToGrid("down");
950 2 Thurallor-7095
    end
951 Thurallor-7095
end
952 Thurallor-7095
 
953 Thurallor-7095
function Group:OffsetPosition(deltaX, deltaY, direction)
954 Thurallor-7095
 
955 Thurallor-7095
    -- Check the parent group for a higher-level operation
956 Thurallor-7095
    if ((direction == "up") and self.parent and self.parent.GetBarsMoveTogether and self.parent:GetBarsMoveTogether()) then
957 Thurallor-7095
        self.parent:OffsetPosition(deltaX, deltaY, "up");
958 Thurallor-7095
        return;
959 Thurallor-7095
    end
960 Thurallor-7095
 
961 20 Thurallor-7095
    -- Move all children contained in this group
962 Thurallor-7095
    for bar in self:bar_objects() do
963 Thurallor-7095
        if (not bar:IsLocked()) then
964 2 Thurallor-7095
            bar:OffsetPosition(deltaX, deltaY);
965 Thurallor-7095
        end
966 Thurallor-7095
    end
967 20 Thurallor-7095
    for group in self:group_objects() do
968 Thurallor-7095
        group:OffsetPosition(deltaX, deltaY, "down");
969 2 Thurallor-7095
    end
970 Thurallor-7095
end
971 Thurallor-7095
 
972 42 Thurallor-7095
function Group:MouseArrive(objectID)
973 Thurallor-7095
    if (self.mouseInside) then
974 Thurallor-7095
        -- Already inside; do nothing.
975 Thurallor-7095
        return;
976 Thurallor-7095
    end
977 Thurallor-7095
    self.mouseInside = true;
978 Thurallor-7095
    if (self.parent) then
979 Thurallor-7095
        self.parent:MouseArrive(self.objectID);
980 Thurallor-7095
    end
981 Thurallor-7095
    DoCallbacks(self, "MouseArrived");
982 Thurallor-7095
end
983 Thurallor-7095
 
984 Thurallor-7095
function Group:MouseInside(mouseLeft, mouseTop)
985 Thurallor-7095
    if (self:IsHidden()) then
986 Thurallor-7095
        return false;
987 Thurallor-7095
    end
988 Thurallor-7095
    for bar in self:bar_objects() do
989 Thurallor-7095
        if (bar:MouseInside(mouseLeft, mouseTop)) then
990 Thurallor-7095
            return true;
991 Thurallor-7095
        end
992 Thurallor-7095
    end
993 Thurallor-7095
    for group in self:group_objects() do
994 Thurallor-7095
        if (group:MouseInside(mouseLeft, mouseTop)) then
995 Thurallor-7095
            return true;
996 Thurallor-7095
        end
997 Thurallor-7095
    end
998 Thurallor-7095
    return false;
999 Thurallor-7095
end
1000 Thurallor-7095
 
1001 Thurallor-7095
function Group:MouseDepart()
1002 Thurallor-7095
    if (self.parent) then
1003 Thurallor-7095
        self.parent:MouseDepart(self.objectID);
1004 Thurallor-7095
    end
1005 Thurallor-7095
 
1006 Thurallor-7095
    -- If a callback is registered, we need to make sure the mouse hasn't
1007 Thurallor-7095
    -- entered another bar in the group before generating the MouseDeparted event.
1008 Thurallor-7095
    if (self.MouseDeparted) then
1009 Thurallor-7095
        local mouseLeft, mouseTop = Turbine.UI.Display:GetMousePosition();
1010 Thurallor-7095
        if (not self:MouseInside(mouseLeft, mouseTop)) then
1011 Thurallor-7095
            self.mouseInside = false;
1012 Thurallor-7095
            DoCallbacks(self, "MouseDeparted");
1013 Thurallor-7095
        end
1014 Thurallor-7095
    else
1015 Thurallor-7095
        self.mouseInside = false;
1016 Thurallor-7095
    end
1017 Thurallor-7095
end
1018 Thurallor-7095
 
1019 2 Thurallor-7095
function Group:EditCaption(optionsNode)
1020 Thurallor-7095
    if (self.captionEditor == nil) then
1021 Thurallor-7095
        if (optionsNode) then
1022 Thurallor-7095
            self.settings.caption.font = optionsNode:GetFont();
1023 Thurallor-7095
            self.settings.caption.width = optionsNode:GetWidth();
1024 Thurallor-7095
            self.settings.caption.height = optionsNode:GetHeight();
1025 Thurallor-7095
        end
1026 Thurallor-7095
        self.captionEditor = CaptionEditor("GroupName", self, self.settings.caption.width, self.settings.caption.height, self.settings.caption.text);
1027 Thurallor-7095
        self:FormatCaptionEditorTextBox();
1028 Thurallor-7095
        self.captionEditor.Closed = function(sender, args)
1029 Thurallor-7095
            self.captionEditor = nil;
1030 Thurallor-7095
        end
1031 Thurallor-7095
    end
1032 Thurallor-7095
end
1033 Thurallor-7095
 
1034 Thurallor-7095
function Group:FormatCaptionEditorTextBox()
1035 Thurallor-7095
    if (self.captionEditor ~= nil) then
1036 Thurallor-7095
        local control = self.captionEditor:GetTextBox();
1037 Thurallor-7095
        control:SetFont(self.settings.caption.font);
1038 Thurallor-7095
        control:SetForeColor(self.color);
1039 Thurallor-7095
        control:SetFontStyle(Turbine.UI.FontStyle.Outline);
1040 Thurallor-7095
        control:SetOutlineColor(Turbine.UI.Color(0.75, 0, 0, 0));
1041 Thurallor-7095
        control:SetTextAlignment(Turbine.UI.ContentAlignment.MiddleLeft);
1042 Thurallor-7095
        control:SetWidth(self.settings.caption.width);
1043 Thurallor-7095
        control:SetHeight(self.settings.caption.height);
1044 Thurallor-7095
        self.captionEditor:Redraw();
1045 Thurallor-7095
    end
1046 Thurallor-7095
end
1047 Thurallor-7095
 
1048 Thurallor-7095
function Group:SetCaptionSize(width, height)
1049 10 Thurallor-7095
    Node.SetCaptionSize(self, width, height);
1050 2 Thurallor-7095
    self:FormatCaptionEditorTextBox();
1051 Thurallor-7095
end
1052 Thurallor-7095
 
1053 Thurallor-7095
function Group:SetColor(color)
1054 8 Thurallor-7095
    Node.SetColor(self, color);
1055 2 Thurallor-7095
    self:FormatCaptionEditorTextBox();
1056 Thurallor-7095
end

All times are GMT -5. The time now is 02:42 PM.


Our Network
EQInterface | EQ2Interface | Minion | WoWInterface | ESOUI | LoTROInterface | MMOUI | Swtorui