lotrointerface.com
Search Downloads

LoTROInterface SVN SequenceBars

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

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

All times are GMT -5. The time now is 05:15 PM.


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