lotrointerface.com
Search Downloads

LoTROInterface SVN SequenceBars

[/] [trunk/] [Thurallor/] [SequenceBars/] [Manager.lua] - Blame information for rev 2

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

Line No. Rev Author Line
1 2 Thurallor-7095
Manager = class(Group);
2 Thurallor-7095
 
3 Thurallor-7095
function Manager:Constructor()
4 Thurallor-7095
    Turbine.UI.Window.Constructor(self);
5 Thurallor-7095
    self.constructing = true;
6 Thurallor-7095
 
7 Thurallor-7095
    self.manager = self;
8 Thurallor-7095
    self.groupID = "0";
9 Thurallor-7095
 
10 Thurallor-7095
    self.objects = {};
11 Thurallor-7095
    self.eventGenerators = {};
12 Thurallor-7095
    self.eventCallbacks = {};
13 Thurallor-7095
    self.lastSaveTime = 0;
14 Thurallor-7095
    self.username = Turbine.Gameplay.LocalPlayer:GetInstance():GetName();
15 Thurallor-7095
 
16 Thurallor-7095
    -- Default settings
17 Thurallor-7095
    self.settings = {};
18 Thurallor-7095
    self.settings.type = "group";
19 Thurallor-7095
    self.settings.caption = {};
20 Thurallor-7095
    self.settings.snapToGrid = false;
21 Thurallor-7095
    self.settings.gridSize = 35;
22 Thurallor-7095
    self.settings.language = Turbine.Engine:GetLanguage();
23 Thurallor-7095
    local name = L:GetText("/Default/GlobalName");
24 Thurallor-7095
    self.settings.caption.text = string.gsub(name, "<name>", self.username);
25 Thurallor-7095
    self.settings.caption.width = 80;
26 Thurallor-7095
    self.settings.caption.height = 20;
27 Thurallor-7095
    self.settings.caption.font = Turbine.UI.Lotro.Font.TrajanPro15;
28 Thurallor-7095
    self.settings.color = "FFFFFF";
29 Thurallor-7095
    self.settings.barIDs = {};
30 Thurallor-7095
    self.settings.groupIDs = { "new" };
31 Thurallor-7095
    self.settings.deletedBarIDs = {};
32 Thurallor-7095
    self.settings.deletedGroupIDs = {};
33 Thurallor-7095
    self.settings.eventHandlers = {};
34 Thurallor-7095
    self.settings.eventsEnabled = false;
35 Thurallor-7095
    self.settings.groups = {};
36 Thurallor-7095
    self.settings.bars = {};
37 Thurallor-7095
    self.settings.nextObjectID = 1;
38 Thurallor-7095
 
39 Thurallor-7095
    self.globals = self.settings;
40 Thurallor-7095
    self:LoadSettings();
41 Thurallor-7095
end
42 Thurallor-7095
 
43 Thurallor-7095
function Manager:LoadSettings()
44 Thurallor-7095
    Turbine.PluginData.Load(Turbine.DataScope.Character, "SequenceBars", function(loadStr)
45 Thurallor-7095
        if (loadStr) then
46 Thurallor-7095
            -- Workaround for Turbine localization bug -- Thanks, Lynx3d!
47 Thurallor-7095
            local settings = ImportTable(loadStr);
48 Thurallor-7095
            if (not settings) then
49 Thurallor-7095
                Turbine.Shell.WriteLine("Failed to parse SequenceBars.plugindata file!");
50 Thurallor-7095
                return;
51 Thurallor-7095
            end
52 Thurallor-7095
 
53 Thurallor-7095
            -- Previously-saved settings override the defaults
54 Thurallor-7095
            DeepTableCopy(settings, self.settings);
55 Thurallor-7095
        end
56 Thurallor-7095
 
57 Thurallor-7095
        -- Make color object from hex string
58 Thurallor-7095
        self.color = Thurallor.Utils.Color(1, 0, 0, 0);
59 Thurallor-7095
        self.color:SetHex(self.settings.color);
60 Thurallor-7095
 
61 Thurallor-7095
        -- Select language
62 Thurallor-7095
        L:SetLanguage(self.settings.language);
63 Thurallor-7095
 
64 Thurallor-7095
        self:LoadBars();
65 Thurallor-7095
        self:LoadGroups();
66 Thurallor-7095
        self:UpdateOptionsPanel();
67 Thurallor-7095
        self.constructing = nil;
68 Thurallor-7095
    end);
69 Thurallor-7095
end
70 Thurallor-7095
 
71 Thurallor-7095
function Manager:Unload()
72 Thurallor-7095
    self.lastSaveTime = 0;
73 Thurallor-7095
    self.constructing = false;
74 Thurallor-7095
    self.editingCaption = true;
75 Thurallor-7095
    self.lastSaveTime = 0; -- force save
76 Thurallor-7095
    self:SaveSettings(false);
77 Thurallor-7095
    for objectID, object in pairs(self.objects) do
78 Thurallor-7095
--Puts("Destroying object " .. tostring(objectID));
79 Thurallor-7095
        if (object) then
80 Thurallor-7095
            object:Destroy();
81 Thurallor-7095
        end
82 Thurallor-7095
    end
83 Thurallor-7095
end
84 Thurallor-7095
 
85 Thurallor-7095
function Manager:SaveSettings(updateOptionsPanel)
86 Thurallor-7095
    if (updateOptionsPanel) then
87 Thurallor-7095
        self.updateOptionsPanel = true;
88 Thurallor-7095
    end
89 Thurallor-7095
    if (not self.constructing) then
90 Thurallor-7095
        -- Start the idle timer.
91 Thurallor-7095
        self.lastIdleTime = Turbine.Engine.GetGameTime();
92 Thurallor-7095
        self:SetWantsUpdates(true);
93 Thurallor-7095
    end
94 Thurallor-7095
end
95 Thurallor-7095
 
96 Thurallor-7095
function Manager:Update()
97 Thurallor-7095
    -- Save will only occur when idle time reaches 0.25 s.
98 Thurallor-7095
    local currentTime = Turbine.Engine.GetGameTime();
99 Thurallor-7095
    if (currentTime - self.lastIdleTime < 0.25) then
100 Thurallor-7095
        return;
101 Thurallor-7095
    end
102 Thurallor-7095
 
103 Thurallor-7095
--Puts("Saving...");
104 Thurallor-7095
    -- Workaround for Turbine localization bug -- Thanks, Lynx3d!
105 Thurallor-7095
    local saveData = ExportTable(self.settings);
106 Thurallor-7095
    Turbine.PluginData.Save(Turbine.DataScope.Character, "SequenceBars", saveData, function()
107 Thurallor-7095
--Puts("Save complete.");
108 Thurallor-7095
    end);
109 Thurallor-7095
 
110 Thurallor-7095
    if (self.updateOptionsPanel and (not self.editingCaption)) then
111 Thurallor-7095
        self:UpdateOptionsPanel();
112 Thurallor-7095
        self.updateOptionsPanel = false;
113 Thurallor-7095
    end
114 Thurallor-7095
    self:SetWantsUpdates(false);
115 Thurallor-7095
end
116 Thurallor-7095
 
117 Thurallor-7095
function Manager:UpdateOptionsPanel()
118 Thurallor-7095
    if (not self.optionsPanel) then
119 Thurallor-7095
        self.optionsPanel = OptionsPanel(self);
120 Thurallor-7095
    end
121 Thurallor-7095
    self.optionsPanel:Update();
122 Thurallor-7095
end
123 Thurallor-7095
 
124 Thurallor-7095
function Manager:FindNewObjectID()
125 Thurallor-7095
    local id = self.settings.nextObjectID;
126 Thurallor-7095
    self.settings.nextObjectID = id + 1;
127 Thurallor-7095
    id = self.username .. "." .. tostring(id);
128 Thurallor-7095
    self.objects[id] = { "reserved" };
129 Thurallor-7095
    return id;
130 Thurallor-7095
end
131 Thurallor-7095
 
132 Thurallor-7095
function Manager:HaveTrash()
133 Thurallor-7095
    local haveTrash = (#self.settings.deletedGroupIDs > 0);
134 Thurallor-7095
    for g = 1, #self.settings.groupIDs, 1 do
135 Thurallor-7095
        local groupID = self.settings.groupIDs[g];
136 Thurallor-7095
        local object = self.objects[groupID];
137 Thurallor-7095
        if ((object) and (object:HaveTrash())) then
138 Thurallor-7095
            haveTrash = true;
139 Thurallor-7095
        end
140 Thurallor-7095
    end
141 Thurallor-7095
    return haveTrash;
142 Thurallor-7095
end
143 Thurallor-7095
 
144 Thurallor-7095
function Manager:TransferBar(barID, oldGroup, newGroup)
145 Thurallor-7095
    local foundPos = nil;
146 Thurallor-7095
    for pos = 1, #oldGroup.settings.barIDs, 1 do
147 Thurallor-7095
        if (oldGroup.settings.barIDs[pos] == barID) then
148 Thurallor-7095
            foundPos = pos;
149 Thurallor-7095
            break;
150 Thurallor-7095
        end
151 Thurallor-7095
    end
152 Thurallor-7095
    if (foundPos) then
153 Thurallor-7095
        table.remove(oldGroup.settings.barIDs, foundPos);
154 Thurallor-7095
        table.insert(newGroup.settings.barIDs, barID);
155 Thurallor-7095
        local bar = self.objects[barID];
156 Thurallor-7095
        bar.parent = newGroup;
157 Thurallor-7095
        self:SaveSettings(true);
158 Thurallor-7095
    end
159 Thurallor-7095
end
160 Thurallor-7095
 
161 Thurallor-7095
function Manager:TransferGroup(groupID, oldGroup, newGroup)
162 Thurallor-7095
    local foundPos = nil;
163 Thurallor-7095
    for pos = 1, #oldGroup.settings.groupIDs, 1 do
164 Thurallor-7095
        if (oldGroup.settings.groupIDs[pos] == groupID) then
165 Thurallor-7095
            foundPos = pos;
166 Thurallor-7095
            break;
167 Thurallor-7095
        end
168 Thurallor-7095
    end
169 Thurallor-7095
    if (foundPos) then
170 Thurallor-7095
        table.remove(oldGroup.settings.groupIDs, foundPos);
171 Thurallor-7095
        table.insert(newGroup.settings.groupIDs, groupID);
172 Thurallor-7095
        local group = self.objects[groupID];
173 Thurallor-7095
        group.parent = newGroup;
174 Thurallor-7095
        self:SaveSettings(true);
175 Thurallor-7095
    end
176 Thurallor-7095
end
177 Thurallor-7095
 
178 Thurallor-7095
function Manager:EmptyTrash()
179 Thurallor-7095
    self.settings.deletedGroupIDs = {};
180 Thurallor-7095
    for groupID, settings in pairs(self.settings.groups) do
181 Thurallor-7095
        settings.deletedGroupIDs = {};
182 Thurallor-7095
        settings.deletedBarIDs = {};
183 Thurallor-7095
        if (not self.objects[groupID]) then
184 Thurallor-7095
            self.settings.groups[groupID] = nil;
185 Thurallor-7095
        end
186 Thurallor-7095
    end
187 Thurallor-7095
    for barID, bar in pairs(self.settings.bars) do
188 Thurallor-7095
        if (not self.objects[barID]) then
189 Thurallor-7095
            self.settings.bars[barID] = nil;
190 Thurallor-7095
        end
191 Thurallor-7095
    end
192 Thurallor-7095
    self:SaveSettings(false);
193 Thurallor-7095
end
194 Thurallor-7095
 
195 Thurallor-7095
function Manager:ShowSettingsMenu()
196 Thurallor-7095
    -- Build the settings menu.
197 Thurallor-7095
    if (not self.settingsMenu) then
198 Thurallor-7095
        self.settingsMenu = Turbine.UI.ContextMenu();
199 Thurallor-7095
    end
200 Thurallor-7095
    Group.AddSettingsMenuItem(self, self.settingsMenu, "Root", true);
201 Thurallor-7095
    self.amSubMenu = false;
202 Thurallor-7095
    self:AddSettingsMenuItem(self.settingsMenu, "Root");
203 Thurallor-7095
    self.settingsMenu:ShowMenu();
204 Thurallor-7095
end
205 Thurallor-7095
 
206 Thurallor-7095
function Manager:AddSettingsMenuItem(parent, itemName, amSubMenu)
207 Thurallor-7095
    if (amSubMenu ~= nil) then
208 Thurallor-7095
        self.amSubMenu = amSubMenu;
209 Thurallor-7095
    end
210 Thurallor-7095
    local item = Turbine.UI.MenuItem(L:GetText(itemName), true, false);
211 Thurallor-7095
    parent:GetItems():Add(item);
212 Thurallor-7095
 
213 Thurallor-7095
    if (itemName == "Root") then
214 Thurallor-7095
        local prevContext = L:SetContext("/GlobalMenu");
215 Thurallor-7095
        parent:GetItems():Clear();
216 Thurallor-7095
        self:AddSettingsMenuItem(parent, "HideAll");
217 Thurallor-7095
        if (not self.amSubMenu) then
218 Thurallor-7095
            L:SetContext("/GroupMenu");
219 Thurallor-7095
            Group.AddSettingsMenuItem(self, parent, "CreateNewBar");
220 Thurallor-7095
            Group.AddSettingsMenuItem(self, parent, "CreateNewSubgroup");
221 Thurallor-7095
            Group.AddSettingsMenuItem(self, parent, "Import");
222 Thurallor-7095
        end
223 Thurallor-7095
        if (#self.settings.deletedBarIDs + #self.settings.deletedGroupIDs > 0) then
224 Thurallor-7095
            self:AddSettingsMenuItem(parent, "Undelete");
225 Thurallor-7095
        end
226 Thurallor-7095
        L:SetContext("/GroupMenu");
227 Thurallor-7095
        Group.AddSettingsMenuItem(self, parent, "ArrangeBars");
228 Thurallor-7095
        Group.AddSettingsMenuItem(self, parent, "EventBehaviors");
229 Thurallor-7095
        L:SetContext("/GlobalMenu");
230 Thurallor-7095
        self:AddSettingsMenuItem(parent, "GlobalSettings");
231 Thurallor-7095
        if (self:HaveTrash()) then
232 Thurallor-7095
            self:AddSettingsMenuItem(parent, "EmptyTrash");
233 Thurallor-7095
        end
234 Thurallor-7095
        L:SetContext(prevContext);
235 Thurallor-7095
    elseif (itemName == "HideAll") then
236 Thurallor-7095
        item:SetChecked(self.settings.hidden);
237 Thurallor-7095
        item.Click = function(sender, args)
238 Thurallor-7095
            self:SetHidden(not self.settings.hidden);
239 Thurallor-7095
        end
240 Thurallor-7095
    elseif (itemName == "GlobalSettings") then
241 Thurallor-7095
        local prevContext = L:SetContext("/GlobalMenu/GlobalSettingsMenu");
242 Thurallor-7095
        self:AddSettingsMenuItem(item, "SnapToGrid");
243 Thurallor-7095
        self:AddSettingsMenuItem(item, "Language");
244 Thurallor-7095
        L:SetContext(prevContext);
245 Thurallor-7095
    elseif (itemName == "SnapToGrid") then
246 Thurallor-7095
        item:SetChecked(self.settings.snapToGrid);
247 Thurallor-7095
        item.Click = function(sender, args)
248 Thurallor-7095
            self.settings.snapToGrid = not self.settings.snapToGrid;
249 Thurallor-7095
            if (self.settings.snapToGrid) then
250 Thurallor-7095
                self:SnapToGrid();
251 Thurallor-7095
            end
252 Thurallor-7095
        end
253 Thurallor-7095
    elseif (itemName == "Language") then
254 Thurallor-7095
        local prevContext = L:SetContext("LanguageMenu");
255 Thurallor-7095
        self:AddSettingsMenuItem(item, "English");
256 Thurallor-7095
        self:AddSettingsMenuItem(item, "French");
257 Thurallor-7095
        self:AddSettingsMenuItem(item, "German");
258 Thurallor-7095
        self:AddSettingsMenuItem(item, "Russian");
259 Thurallor-7095
        L:SetContext(prevContext);
260 Thurallor-7095
    elseif (string.find("English|French|German|Russian", itemName)) then
261 Thurallor-7095
        local checked = (L:GetLanguage() == Turbine.Language[itemName]);
262 Thurallor-7095
        item:SetChecked(checked);
263 Thurallor-7095
        item:SetEnabled(not checked);
264 Thurallor-7095
        item.Click = function(sender, args)
265 Thurallor-7095
            local language = Turbine.Language[itemName];
266 Thurallor-7095
            self.settings.language = language;
267 Thurallor-7095
            L:SetLanguage(language);
268 Thurallor-7095
            self:SaveSettings(true);
269 Thurallor-7095
        end
270 Thurallor-7095
    elseif (itemName == "EmptyTrash") then
271 Thurallor-7095
        item.Click = function(sender, args)
272 Thurallor-7095
            self:EmptyTrash();
273 Thurallor-7095
        end
274 Thurallor-7095
    else
275 Thurallor-7095
        -- Item is not handled here; send it to Group:AddSettingsMenuItem().
276 Thurallor-7095
        parent:GetItems():RemoveAt(parent:GetItems():GetCount());
277 Thurallor-7095
        Group.AddSettingsMenuItem(self, parent, itemName, true);
278 Thurallor-7095
    end
279 Thurallor-7095
end
280 Thurallor-7095
 
281 Thurallor-7095
function Manager:GetNearestGridPos(left, top)
282 Thurallor-7095
    local gridSize = self.settings.gridSize;
283 Thurallor-7095
    local displayBottom = Turbine.UI.Display:GetHeight() - 1;
284 Thurallor-7095
    local centerLeft = math.floor(0.5 + Turbine.UI.Display:GetWidth() / 2 + gridSize / 2) + 3;
285 Thurallor-7095
    local offsetLeft, offsetTop = left - centerLeft, displayBottom - top;
286 Thurallor-7095
    local newLeft = centerLeft + math.floor(0.5 + offsetLeft / gridSize) * gridSize;
287 Thurallor-7095
    local newTop = displayBottom - math.floor(0.5 + offsetTop / gridSize) * gridSize;
288 Thurallor-7095
    return newLeft, newTop;
289 Thurallor-7095
end
290 Thurallor-7095
 
291 Thurallor-7095
function Manager:AddEventGenerator(object, eventName)
292 Thurallor-7095
    if (not self.eventGenerators[eventName]) then
293 Thurallor-7095
        self.eventGenerators[eventName] = {};
294 Thurallor-7095
    end
295 Thurallor-7095
    table.insert(self.eventGenerators[eventName], object);
296 Thurallor-7095
    local generatorID = #self.eventGenerators[eventName];
297 Thurallor-7095
--Puts("Adding event generator " .. tostring(generatorID) .. " for event " .. Serialize(eventName) .. " from object " .. Serialize(object.settings.caption.text));
298 Thurallor-7095
    return generatorID;
299 Thurallor-7095
end
300 Thurallor-7095
 
301 Thurallor-7095
function Manager:RemoveEventGenerator(generatorID, eventName)
302 Thurallor-7095
    local object = self.eventGenerators[eventName][generatorID];
303 Thurallor-7095
--Puts("Removing event generator " .. tostring(generatorID) .. " for event " .. Serialize(eventName) .. " (was from object " .. Serialize(object.settings.caption.text));
304 Thurallor-7095
    table.remove(self.eventGenerators[eventName], generatorID);
305 Thurallor-7095
    if (#self.eventGenerators[eventName] == 0) then
306 Thurallor-7095
        self.eventGenerators[eventName] = nil;
307 Thurallor-7095
    end
308 Thurallor-7095
end
309 Thurallor-7095
 
310 Thurallor-7095
-- Returns a list of bars that generates the specified event
311 Thurallor-7095
function Manager:GetEventGenerators(eventName)
312 Thurallor-7095
    return self.eventGenerators[eventName];
313 Thurallor-7095
end
314 Thurallor-7095
 
315 Thurallor-7095
-- Returns the names of all of the events generated by all bars
316 Thurallor-7095
function Manager:GetEventNames()
317 Thurallor-7095
    local eventNames = {};
318 Thurallor-7095
    for k in keys(self.eventGenerators) do
319 Thurallor-7095
        table.insert(eventNames, k);
320 Thurallor-7095
    end
321 Thurallor-7095
    return eventNames;
322 Thurallor-7095
end
323 Thurallor-7095
 
324 Thurallor-7095
function Manager:GetEventCallbacks()
325 Thurallor-7095
    return self.eventCallbacks;
326 Thurallor-7095
end
327 Thurallor-7095
 
328 Thurallor-7095
function Manager:PropagateEvent(eventName)
329 Thurallor-7095
    local callbacks = self.eventCallbacks[eventName];
330 Thurallor-7095
    if (type(callbacks) == "table") then
331 Thurallor-7095
--Puts("Propagating event \"" .. tostring(eventName) .. "\" to " .. tostring(#callbacks) .. " callback functions: " .. Serialize(callbacks));
332 Thurallor-7095
        for f = 1, #callbacks do
333 Thurallor-7095
            local func = callbacks[f];
334 Thurallor-7095
            func(eventName);
335 Thurallor-7095
        end
336 Thurallor-7095
    elseif (type(callbacks) == "function") then
337 Thurallor-7095
--Puts("Propagating event \"" .. tostring(eventName) .. "\" to a single callback function: " .. Serialize(callbacks));
338 Thurallor-7095
        callbacks(eventName);
339 Thurallor-7095
    end
340 Thurallor-7095
end

All times are GMT -5. The time now is 04:16 PM.


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