Group = class(Turbine.UI.Window);
function Group:Constructor(parent, groupID, settings)
Turbine.UI.Window.Constructor(self);
self.constructing = true;
self.parent = parent;
self.manager = parent.manager;
self.groupID = groupID;
self.objectID = groupID;
self.globals = self.manager.settings;
-- Default settings
local defaults = {};
defaults.type = "group";
defaults.hidden = false;
defaults.barsMoveTogether = false;
defaults.caption = {};
defaults.caption.text = L:GetText("/Default/GroupName");
defaults.caption.width = 80;
defaults.caption.height = 20;
defaults.caption.font = Turbine.UI.Lotro.Font.TrajanPro15;
defaults.color = "C0C0C0";
defaults.barIDs = { "new" };
defaults.groupIDs = {};
defaults.deletedBarIDs = {};
defaults.deletedGroupIDs = {};
defaults.eventHandlers = {};
defaults.eventsEnabled = false;
-- Previously-saved settings override the defaults
DeepTableCopy(settings, defaults);
DeepTableCopy(defaults, settings);
self.settings = settings;
-- Make color object from hex string
self.color = Thurallor.Utils.Color(1, 0, 0, 0);
self.color:SetHex(self.settings.color);
self:LoadBars();
self:LoadGroups();
self.constructing = nil;
self:SetWantsEvents(self.settings.eventsEnabled);
end
function Group:LoadBars()
for b = 1, #self.settings.barIDs, 1 do
local barID = self.settings.barIDs[b];
if (barID == "new") then
barID = self:FindNewObjectID();
self.settings.barIDs[b] = barID;
end
if (self.globals.bars[barID] == nil) then
self.globals.bars[barID] = {};
end
self.manager.objects[barID] = Bar(self, barID, self.globals.bars[barID]);
end
end
function Group:LoadGroups()
for g = 1, #self.settings.groupIDs, 1 do
local groupID = self.settings.groupIDs[g];
if (groupID == "new") then
groupID = self:FindNewObjectID();
self.settings.groupIDs[g] = groupID;
end
if (self.globals.groups[groupID] == nil) then
self.globals.groups[groupID] = {};
end
self.manager.objects[groupID] = Group(self, groupID, self.globals.groups[groupID]);
end
end
function Group:SaveSettings(updateOptionsPanel)
if (not self.constructing) then
self.manager:SaveSettings(updateOptionsPanel);
end
end
function Group:IsHidden()
return (self.settings.hidden or self:IsParentHidden());
end
function Group:IsParentHidden()
return self.parent and self.parent:IsHidden();
end
function Group:SetHidden(hide)
--Puts("Group " .. Serialize(self.settings.caption.text) .. ": SetHidden(" .. Serialize(hide) .. ")");
self.settings.hidden = hide;
self:ApplyHiddenness();
end
function Group:ApplyHiddenness()
for b = 1, #self.settings.barIDs, 1 do
local barID = self.settings.barIDs[b];
local bar = self.manager.objects[barID];
if (bar) then
bar:ApplyHiddenness();
end
end
for g = 1, #self.settings.groupIDs, 1 do
local groupID = self.settings.groupIDs[g];
local group = self.manager.objects[groupID];
if (group) then
group:ApplyHiddenness();
end
end
self:SaveSettings(true);
end
function Group:Redraw()
for b = 1, #self.settings.barIDs, 1 do
local barID = self.settings.barIDs[b];
local bar = self.manager.objects[barID];
if (bar) then
bar:Redraw();
end
end
for g = 1, #self.settings.groupIDs, 1 do
local groupID = self.settings.groupIDs[g];
local group = self.manager.objects[groupID];
if (group) then
group:Redraw();
end
end
end
function Group:CanHaveChildren()
return true;
end
function Group:Contains(object)
while (object.parent) do
if (object.parent == self) then
return true;
end
object = object.parent;
end
return false;
end
function Group:GetID()
return self.groupID;
end
function Group:GetChildIDs()
local objectIDs = {};
for b = 1, #self.settings.barIDs, 1 do
local barID = self.settings.barIDs[b];
table.insert(objectIDs, barID);
end
for g = 1, #self.settings.groupIDs, 1 do
local groupID = self.settings.groupIDs[g];
table.insert(objectIDs, groupID);
end
return objectIDs;
end
function Group:GetChild(childID)
return self.manager.objects[childID];
end
function Group:GetName()
return self.settings.caption.text;
end
function Group:GetColor()
return self.color;
end
function Group:GetBarsMoveTogether()
if (self.settings.barsMoveTogether) then
return true;
elseif (self.parent and self.parent.GetBarsMoveTogether) then
return self.parent:GetBarsMoveTogether();
else
return false;
end
end
function Group:SetName(newName)
self.settings.caption.text = newName;
self:SaveSettings(true);
end
function Group:IsEmpty()
return ((#self.settings.barIDs == 0) and (#self.settings.groupIDs == 0));
end
function Group:CloneBar(barID)
local oldSettings = self.globals.bars[barID];
local newBarID = self:FindNewObjectID();
local newSettings = { };
self.globals.bars[newBarID] = newSettings;
DeepTableCopy(oldSettings, newSettings);
local newBar = Bar(self, newBarID, newSettings);
self.manager.objects[newBarID] = newBar;
local gridSize = self.globals.gridSize;
newBar:OffsetPosition(gridSize, gridSize);
newBar:MoveOntoScreen();
table.insert(self.settings.barIDs, newBarID);
self:SaveSettings(true);
return newBarID;
end
function Group:CloneGroup(groupID)
--Alert("Settings before cloning", PrettyPrint(self.globals, ""));
local oldSettings = self.globals.groups[groupID];
local newGroupID = self:FindNewObjectID();
local newSettings = {};
-- Create a clone of the existing group, but with nothing in it
local oldGroup = self.manager.objects[groupID];
self.globals.groups[newGroupID] = newSettings;
DeepTableCopy(oldSettings, newSettings);
newSettings.barIDs = {};
newSettings.groupIDs = {};
table.insert(self.settings.groupIDs, newGroupID);
local newGroup = Group(self, newGroupID, newSettings);
self.manager.objects[newGroupID] = newGroup;
-- Clone the bars and subgroups, and transfer the clones from old group to new group
for b = 1, #oldSettings.barIDs, 1 do
local barID = oldSettings.barIDs[b];
local newBarID = oldGroup:CloneBar(barID);
self.manager:TransferBar(newBarID, oldGroup, newGroup);
end
for g = 1, #oldSettings.groupIDs, 1 do
local oldGroupID = oldSettings.groupIDs[g];
local _newGroupID = oldGroup:CloneGroup(oldGroupID);
self.manager:TransferGroup(_newGroupID, oldGroup, newGroup);
end
self:SaveSettings(true);
--Alert("Settings after cloning", PrettyPrint(self.globals, ""));
return newGroupID;
end
function Group:DeleteBar(barID)
local foundPos;
local bar = self.manager.objects[barID];
if (bar) then
bar:Destroy();
end
for pos = 1, #self.settings.barIDs, 1 do
if (self.settings.barIDs[pos] == barID) then
foundPos = pos;
break;
end
end
table.remove(self.settings.barIDs, foundPos);
table.insert(self.settings.deletedBarIDs, barID);
self.manager.objects[barID] = nil;
self:SaveSettings(true);
end
function Group:DeleteGroup(groupID)
local foundPos;
local group = self.manager.objects[groupID];
if (group) then
group:Destroy();
end
for pos = 1, #self.settings.groupIDs, 1 do
if (self.settings.groupIDs[pos] == groupID) then
foundPos = pos;
end
end
if (foundPos) then
table.remove(self.settings.groupIDs, foundPos);
end
table.insert(self.settings.deletedGroupIDs, groupID);
self.manager.objects[groupID] = nil;
self:SaveSettings(true);
end
function Group:UndeleteBar(barID)
local foundPos;
for pos = 1, #self.settings.deletedBarIDs, 1 do
if (self.settings.deletedBarIDs[pos] == barID) then
foundPos = pos;
end
end
if (foundPos) then
table.remove(self.settings.deletedBarIDs, foundPos);
self.manager.objects[barID] = Bar(self, barID, self.globals.bars[barID]);
table.insert(self.settings.barIDs, barID);
self:SaveSettings(true);
end
end
function Group:UndeleteGroup(groupID)
local foundPos;
for pos = 1, #self.settings.deletedGroupIDs, 1 do
if (self.settings.deletedGroupIDs[pos] == groupID) then
foundPos = pos;
end
end
if (foundPos) then
table.remove(self.settings.deletedGroupIDs, foundPos);
self.manager.objects[groupID] = Group(self, groupID, self.globals.groups[groupID]);
table.insert(self.settings.groupIDs, groupID);
self:SaveSettings(true);
end
end
function Group:CreateBar()
local barID = self:FindNewObjectID();
self.globals.bars[barID] = {};
table.insert(self.settings.barIDs, barID);
self.manager.objects[barID] = Bar(self, barID, self.globals.bars[barID]);
self:SaveSettings(true);
end
function Group:CreateGroup()
local groupID = self:FindNewObjectID();
self.globals.groups[groupID] = { };
table.insert(self.settings.groupIDs, groupID);
self.manager.objects[groupID] = Group(self, groupID, self.globals.groups[groupID]);
self:SaveSettings(true);
end
function Group:FindNewObjectID()
return self.manager:FindNewObjectID();
end
function Group:Destroy()
self:SetWantsEvents(false);
for b = 1, #self.settings.barIDs, 1 do
local barID = self.settings.barIDs[b];
local bar = self.manager.objects[barID];
if (bar) then
bar:Destroy();
end
end
for g = 1, #self.settings.groupIDs, 1 do
local groupID = self.settings.groupIDs[g];
local group = self.manager.objects[groupID];
if (group) then
group:Destroy();
end
end
if (self.settingsMenu) then
self.settingsMenu:Close();
end
if (self.caption) then
self.caption:Close();
end
if (self.captionEditor) then
self.captionEditor:Close();
end
if (self.exportWindow) then
self.exportWindow:Close();
end
if (self.importWindow) then
self.importWindow:Close();
end
if (self.colorPicker) then
self.colorPicker:Close();
end
self.manager.objects[self.groupID] = nil;
self:Close();
end
function Group:HaveTrash()
if (#self.settings.deletedBarIDs + #self.settings.deletedGroupIDs > 0) then
return true;
end
for g = 1, #self.settings.groupIDs, 1 do
local groupID = self.settings.groupIDs[g];
local group = self.manager.objects[groupID];
if (group and group:HaveTrash()) then
return true;
end
end
return false;
end
function Group:Export()
if (self.exportWindow) then
self.exportWindow:Close();
self.exportWindow = nil;
return self:Export();
end
-- Compile the data to be exported
Puts(L:GetText("/ExportWindow/Exporting"));
local exportObjects = { [self.groupID] = {} };
DeepTableCopy(self.settings, exportObjects[self.groupID]);
self:StripSettingsForExport(exportObjects[self.groupID]);
local descendentIDs = self:FindAllDescendentIDs(nil, true, true);
for d = 1, #descendentIDs do
local objectID = descendentIDs[d];
local object = self.manager.objects[objectID];
exportObjects[objectID] = {};
DeepTableCopy(object.settings, exportObjects[objectID]);
self:StripSettingsForExport(exportObjects[objectID]);
end
-- Create the export window
local title = L:GetText("/ExportWindow/Title");
title = string.gsub(title, "<name>", self.settings.caption.text);
self.exportWindow = ExportWindow(title, exportObjects);
self.exportWindow.Closing = function()
self.exportWindow = nil;
end
end
function Group:StripSettingsForExport(settings)
settings.deletedBarIDs = nil;
settings.deletedGroupIDs = nil;
if (settings.eventHandlers) then
for h = 1, #settings.eventHandlers do
settings.eventHandlers[h].func = nil;
end
end
end
function Group:OpenImportWindow()
-- Create the import window
local title = L:GetText("/ImportWindow/Title");
title = string.gsub(title, "<group>", self.settings.caption.text);
self.importWindow = ImportWindow(title, self);
self.importWindow.Closing = function()
self.importWindow = nil;
end
end
function Group:Import(importData)
self.importData = importData;
-- Uncompress the data in the next Update cycle.
self:SetWantsUpdates(true);
self.updateCount = 0;
self.oldUpdateFunction = self.Update;
self.Update = function(g)
g.updateCount = g.updateCount + 1;
if (g.updateCount == 1) then
if (not string.find(g.importData, "{")) then
Puts(L:GetText("/ImportWindow/Decoding"));
g.encodedData = g.importData;
else
g.decodedData = g.importData;
end
g.importData = nil;
elseif (g.updateCount == 3) then
if (g.encodedData) then
-- Remove whitespace and newlines
g.encodedData = string.gsub(g.encodedData, "[%c%s]", "");
local success, decodedData = pcall(Text2Bin, g.encodedData);
if (not success) then
Puts(L:GetText("/ImportWindow/InvalidEncoding"));
g.encodedData = nil;
g.updateCount = 10;
return;
end
g.encodedData = nil;
g.decodedData = decodedData;
end
elseif (g.updateCount == 4) then
Puts(L:GetText("/ImportWindow/Decompressing"));
g.compressor = Thurallor.Utils.LibCompress();
elseif (g.updateCount == 6) then
if (g.decodedData) then
local success, uncompressedData = pcall(g.compressor.Decompress, g.compressor, g.decodedData);
if ((not success) or (not uncompressedData)) then
-- Data was either not compressed or invalid. Let's assume the former for now.
uncompressedData = g.decodedData;
end
g.decodedData = nil;
g.uncompressedData = uncompressedData;
end
elseif (g.updateCount == 7) then
--Alert("g.uncompressedData", g.uncompressedData);
local f, e = loadstring("return " .. g.uncompressedData);
if (not f) then
Puts(L:GetText("/ImportWindow/ParseError") .. tostring(e));
else
g.newObjects = f();
if (type(g.newObjects) == "table") then
Puts(L:GetText("/ImportWindow/Importing"));
g:AddImportedObjects();
else
Puts(L:GetText("/ImportWindow/NothingToDo"));
end
end
g.compressor = nil;
g.uncompressedData = nil;
g.newObjects = nil;
elseif (g.updateCount == 10) then
g:SetWantsUpdates(false);
g.Update = g.oldUpdateFunction; -- if this is a Manager object, it needs to have its Update function restored
g:SaveSettings(true);
end
end
end
function Group:AddImportedObjects()
-- Assign new object IDs to the new bars and groups.
-- Find out which one(s) are at the top level.
local newObjectIDs, hasParent = {}, {};
for oldObjectID, settings in pairs(self.newObjects) do
if (not newObjectIDs[oldObjectID]) then
newObjectIDs[oldObjectID] = self:FindNewObjectID();
--Puts(" " .. oldObjectID .. " -> " .. newObjectIDs[oldObjectID]);
end
if (settings.type == "group") then
local title = L:GetText("/GroupMenu/UndeleteMenu/GroupName");
title = string.gsub(title, "<name>", settings.caption.text);
Puts(" + " .. title);
for b = 1, #settings.barIDs do
local oldBarID = settings.barIDs[b];
if (not newObjectIDs[oldBarID]) then
newObjectIDs[oldBarID] = self:FindNewObjectID();
--Puts(" " .. oldObjectID .. " -> " .. newObjectIDs[oldObjectID]);
end
local barID = newObjectIDs[oldBarID];
settings.barIDs[b] = barID;
hasParent[barID] = true;
end
for g = 1, #settings.groupIDs do
local oldGroupID = settings.groupIDs[g];
if (not newObjectIDs[oldGroupID]) then
newObjectIDs[oldGroupID] = self:FindNewObjectID();
--Puts(" " .. oldObjectID .. " -> " .. newObjectIDs[oldObjectID]);
end
local groupID = newObjectIDs[oldGroupID];
settings.groupIDs[g] = groupID;
hasParent[groupID] = true;
end
else
local title = L:GetText("/GroupMenu/UndeleteMenu/BarName");
title = string.gsub(title, "<name>", settings.caption.text);
Puts(" + " .. title);
end
end
-- Add new group/bar settings to the database.
for oldObjectID, settings in pairs(self.newObjects) do
if (settings.type == "group") then
local groupID = newObjectIDs[oldObjectID];
self.globals.groups[groupID] = settings;
else
local barID = newObjectIDs[oldObjectID];
self.globals.bars[barID] = settings;
end
end
-- For the top-level bars/groups, add them to the current group.
for oldObjectID, settings in pairs(self.newObjects) do
if (settings.type == "group") then
local groupID = newObjectIDs[oldObjectID];
if (not hasParent[groupID]) then
table.insert(self.settings.groupIDs, groupID);
self.manager.objects[groupID] = Group(self, groupID, settings);
end
else
local barID = newObjectIDs[oldObjectID];
if (not hasParent[barID]) then
table.insert(self.settings.barIDs, barID);
self.manager.objects[barID] = Bar(self, barID, settings);
end
end
end
end
function Group:ShowSettingsMenu(fromOptionsPanel)
-- Build the settings menu.
if (not self.settingsMenu) then
self.settingsMenu = Turbine.UI.ContextMenu();
end
self.amSubMenu = false;
self:AddSettingsMenuItem(self.settingsMenu, "Root", fromOptionsPanel);
self.settingsMenu:ShowMenu();
end
function Group:AddSettingsMenuItem(parent, itemName, amSubMenu)
if (amSubMenu ~= nil) then
self.amSubMenu = amSubMenu;
end
local item = Turbine.UI.MenuItem(L:GetText(itemName), true, false);
parent:GetItems():Add(item);
if (itemName == "Root") then
local prevContext = L:SetContext("/GroupMenu");
parent:GetItems():Clear();
self:AddSettingsMenuItem(parent, "Hide");
self:AddSettingsMenuItem(parent, "CloneGroup");
self:AddSettingsMenuItem(parent, "DeleteGroup");
self:AddSettingsMenuItem(parent, "ExportGroup");
if (not self.importWindow) then
self:AddSettingsMenuItem(parent, "Import");
end
self:AddSettingsMenuItem(parent, "CreateNewBar");
self:AddSettingsMenuItem(parent, "CreateNewSubgroup");
if (#self.settings.deletedBarIDs + #self.settings.deletedGroupIDs > 0) then
self:AddSettingsMenuItem(parent, "Undelete");
end
self:AddSettingsMenuItem(parent, "ArrangeBars");
self:AddSettingsMenuItem(parent, "EventBehaviors");
self:AddSettingsMenuItem(parent, "GroupSettings");
if (self.amSubMenu) then
-- self:AddSettingsMenuItem(parent, "Other Bars");
else
-- self:AddSettingsMenuItem(parent, "Bars");
self:AddSettingsMenuItem(parent, "Global");
end
L:SetContext(prevContext);
elseif (itemName == "ExportGroup") then
item.Click = function(sender, args)
self:Export();
end
elseif (itemName == "Import") then
item.Click = function(sender, args)
self:OpenImportWindow();
end
elseif (itemName == "Hide") then
item:SetChecked(self.settings.hidden);
item.Click = function(sender, args)
self:SetHidden(not self.settings.hidden);
end
elseif (itemName == "GroupSettings") then
local prevContext = L:SetContext("/GroupMenu/GroupSettingsMenu");
self:AddSettingsMenuItem(item, "Rename");
self:AddSettingsMenuItem(item, "BarsMoveTogether");
L:SetContext(prevContext);
elseif (itemName == "Undelete") then
local prevContext = L:SetContext("/GroupMenu/UndeleteMenu");
for b = 1, #self.settings.deletedBarIDs, 1 do
local barID = self.settings.deletedBarIDs[b];
local barItemName = L:GetText("BarName");
if (self.globals.bars[barID]) then
local barCaption = self.globals.bars[barID].caption.text;
barItemName = string.gsub(barItemName, "<name>", barCaption);
local barItem = Turbine.UI.MenuItem(barItemName, true, false);
item:GetItems():Add(barItem);
barItem.Click = function(sender, args)
self:UndeleteBar(barID);
end
end
end
for g = 1, #self.settings.deletedGroupIDs, 1 do
local groupID = self.settings.deletedGroupIDs[g];
local groupItemName = L:GetText("GroupName");
if (self.globals.groups[groupID]) then
local groupName = self.globals.groups[groupID].caption.text;
groupItemName = string.gsub(groupItemName, "<name>", groupName);
local groupItem = Turbine.UI.MenuItem(groupItemName, true, false);
item:GetItems():Add(groupItem);
groupItem.Click = function(sender, args)
self:UndeleteGroup(groupID);
end
end
end
L:SetContext(prevContext);
elseif (itemName == "Rename") then
item:SetEnabled(not self.captionEditor);
item.Click = function(sender, args)
self:EditCaption();
end
elseif (itemName == "BarsMoveTogether") then
item:SetChecked(self.settings.barsMoveTogether);
item.Click = function(sender, args)
self.settings.barsMoveTogether = not self.settings.barsMoveTogether;
end
elseif (itemName == "ArrangeBars") then
local prevContext = L:SetContext("/GroupMenu/ArrangeBarsMenu");
local descendentBarIDs = self:FindAllDescendentIDs(nil, true, nil);
if (#descendentBarIDs < 2) then
item:SetEnabled(false);
else
self:AddSettingsMenuItem(item, "AlignVertically");
self:AddSettingsMenuItem(item, "AlignHorizontally");
end
L:SetContext(prevContext);
elseif (itemName == "EventBehaviors") then
local prevContext = L:SetContext("/GroupMenu/EventBehaviorsMenu");
if (self.settings.eventsEnabled) then
if (#self.settings.eventHandlers > 0) then
self:AddSettingsMenuItem(item, "CurrentBehaviors");
end
self:AddSettingsMenuItem(item, "AddBehavior");
self:AddSettingsMenuItem(item, "DisableEvents");
else
self:AddSettingsMenuItem(item, "EnableEvents");
end
L:SetContext(prevContext);
elseif (itemName == "EnableEvents") then
item.Click = function()
self.settings.eventsEnabled = true;
self:SaveSettings(false);
self:SetWantsEvents(true);
end
elseif (itemName == "DisableEvents") then
item.Click = function()
self.settings.eventsEnabled = false;
self:SaveSettings(false);
self:SetWantsEvents(true);
end
elseif (itemName == "CurrentBehaviors") then
if (self.settings.eventHandlers) then
for h = 1, #self.settings.eventHandlers do
local handler = self.settings.eventHandlers[h];
local handlerItem = Turbine.UI.MenuItem(handler.description, true, false);
item:GetItems():Add(handlerItem);
local removeItem = Turbine.UI.MenuItem(L:GetText("Remove"), true, false);
handlerItem:GetItems():Add(removeItem);
removeItem.Click = function()
self:RemoveEventHandler(h);
end
end
end
elseif (itemName == "AddBehavior") then
self:AddSettingsMenuItem(item, "ShowGroup");
self:AddSettingsMenuItem(item, "HideGroup");
self:AddSettingsMenuItem(item, "ResetGroup");
elseif (string.find("ShowGroup|HideGroup|ResetGroup", itemName)) then
item.itemName = itemName;
self:AddSettingsMenuItem(item, "AtStartup");
self:AddSettingsMenuItem(item, "WhenCombatBegins");
self:AddSettingsMenuItem(item, "WhenCombatEnds");
self.availableEvents = self.manager:GetEventNames();
if (#self.availableEvents > 0) then
self:AddSettingsMenuItem(item, "WhenEventOccurs");
end
elseif (itemName == "WhenEventOccurs") then
for e = 1, #self.availableEvents do
local eventName = self.availableEvents[e];
local eventNameItem = Turbine.UI.MenuItem(eventName, true, false);
item:GetItems():Add(eventNameItem);
eventNameItem.Click = function()
self:AddEventHandler("WhenEventOccurs:" .. eventName, parent.itemName);
end
end
elseif (string.find("AtStartup|WhenCombatBegins|WhenCombatEnds", itemName)) then
item.Click = function()
self:AddEventHandler(itemName, parent.itemName);
end
elseif (itemName == "AlignVertically") then
local prevContext = L:SetContext("/GroupMenu/ArrangeBarsMenu/AlignVerticallyMenu");
self:AddSettingsMenuItem(item, "Tops");
self:AddSettingsMenuItem(item, "Middles");
self:AddSettingsMenuItem(item, "Bottoms");
L:SetContext(prevContext);
elseif (itemName == "AlignHorizontally") then
local prevContext = L:SetContext("/GroupMenu/ArrangeBarsMenu/AlignHorizontallyMenu");
self:AddSettingsMenuItem(item, "LeftSides");
self:AddSettingsMenuItem(item, "Centers");
self:AddSettingsMenuItem(item, "RightSides");
L:SetContext(prevContext);
elseif (string.find("Tops|Middles|Bottoms|LeftSides|Centers|RightSides", itemName)) then
local checked = self:BarsAreAligned(itemName);
item:SetChecked(checked);
item:SetEnabled(not checked);
item.Click = function(sender, args)
self:AlignBars(itemName);
end
elseif (itemName == "CloneGroup") then
item.Click = function(sender, args)
self.parent:CloneGroup(self.groupID);
end
elseif (itemName == "DeleteGroup") then
item.Click = function(sender, args)
self.parent:DeleteGroup(self.groupID);
end
elseif (itemName == "Global") then
self.manager:AddSettingsMenuItem(item, "Root", true);
elseif (itemName == "CreateNewBar") then
item.Click = function(sender, args)
self:CreateBar();
end
elseif (itemName == "CreateNewSubgroup") then
item.Click = function(sender, args)
self:CreateGroup();
end
end
end
function Group:SetWantsEvents(enable)
for h = 1, #self.settings.eventHandlers, 1 do
local handler = self.settings.eventHandlers[h];
local object = loadstring("self = ...; return " .. handler.object)(self);
local trigger = handler.trigger;
if (enable) then
-- Should save this function reference as handler.func, so we can pass the same reference to RemoveCallback().
local funcBody = loadstring(handler.action);
handler.func = function(sender, args)
funcBody(self, object, trigger, args);
end;
--Puts("Group " .. Serialize(self.settings.caption.text) .. ": Compiled handler.action = " .. Serialize(handler.action) .. " into " .. Serialize(handler.func));
--Puts("Group " .. Serialize(self.settings.caption.text) .. ": Registering event handler " .. Serialize(handler.description) .. " (" .. Serialize(handler.func) .. ")");
AddCallback(object, trigger, handler.func);
else
--Puts("Group " .. Serialize(self.settings.caption.text) .. ": Unregistering event handler: " .. Serialize(handler.description) .. " (" .. Serialize(handler.func) .. ")");
RemoveCallback(object, trigger, handler.func);
handler.func = nil;
end
end
end
function Group:AddEventHandler(trigger, action)
self:SetWantsEvents(false);
-- Initialize action
local handler = {};
handler.action = "";
if (action == "ShowGroup") then
handler.action = "self:SetHidden(false); ";
elseif (action == "HideGroup") then
handler.action = "self:SetHidden(true); ";
elseif (action == "ResetGroup") then
handler.action = "self:Reset(); ";
else
error("Unknown event handler action: " .. tostring(action), 2);
end
-- Add object, trigger, action, description
local prevContext = L:SetContext("/EventHandlers");
if (trigger == "AtStartup") then
handler.object = "self.manager:GetEventCallbacks()";
handler.trigger = "Startup\n"; -- includes "\n" so it can't be generated by user
handler.action =
"self, object = ...; " ..
handler.action;
handler.description = L:GetText(action .. trigger);
elseif (trigger == "WhenCombatBegins") then
handler.object = "Turbine.Gameplay.LocalPlayer.GetInstance()";
handler.trigger = "InCombatChanged";
handler.action =
"self, object = ...; " ..
"if (object:IsInCombat()) then " ..
handler.action ..
"end";
handler.description = L:GetText(action .. trigger);
elseif (trigger == "WhenCombatEnds") then
handler.object = "Turbine.Gameplay.LocalPlayer.GetInstance()";
handler.trigger = "InCombatChanged";
handler.action =
"self, object = ...; " ..
"if (not object:IsInCombat()) then " ..
handler.action ..
"end";
handler.description = L:GetText(action .. trigger);
elseif (string.find(trigger, "WhenEventOccurs:")) then
local eventName = string.sub(trigger, 17);
handler.object = "self.manager:GetEventCallbacks()";
handler.trigger = eventName;
handler.action =
"self, object = ...; " ..
handler.action;
handler.description = L:GetText(action .. "WhenEventOccurs");
handler.description = string.gsub(handler.description, "<event>", eventName);
else
error("Unknown event handler trigger: " .. tostring(trigger), 2);
end
L:SetContext(prevContext);
-- Save new event handler and activate it
table.insert(self.settings.eventHandlers, handler);
self:SaveSettings(false);
self:SetWantsEvents(true);
end
function Group:RemoveEventHandler(h)
self:SetWantsEvents(false);
table.remove(self.settings.eventHandlers, h);
self:SaveSettings(false);
self:SetWantsEvents(true);
end
function Group:Reset()
for b = 1, #self.settings.barIDs, 1 do
local barID = self.settings.barIDs[b];
local bar = self.manager.objects[barID];
if (bar) then
bar:Reset();
end
end
end
function Group:FindAllDescendentIDs(descendents, includeBars, includeGroups)
if (descendents == nil) then
descendents = {};
end
for g = 1, #self.settings.groupIDs do
local groupID = self.settings.groupIDs[g];
local group = self.manager.objects[groupID];
if (includeGroups) then
table.insert(descendents, groupID);
end
group:FindAllDescendentIDs(descendents, includeBars, includeGroups);
end
for b = 1, #self.settings.barIDs do
local barID = self.settings.barIDs[b];
if (includeBars) then
table.insert(descendents, barID);
end
end
return descendents;
end
function Group:GetBarLocationData(barIDs)
local lefts, tops, rights, bottoms, centers, middles = {}, {}, {}, {}, {}, {};
if (barIDs == nil) then
barIDs = self:FindAllDescendentIDs(nil, true, nil);
end
local numBars = #barIDs;
for b = 1, numBars, 1 do
local barID = barIDs[b];
local bar = self.manager.objects[barID];
local left, top, width, height = bar:GetVisibleLeft(), bar:GetVisibleTop(), bar:GetVisibleWidth(), bar:GetVisibleHeight();
--Puts("For bar " .. bar.settings.caption.text .. ", left = " .. left ..", top = " .. top .. ", width = " .. width .. ", height = " .. height);
table.insert(lefts, left);
table.insert(tops, top);
local right, bottom = left + width, top + height;
table.insert(rights, right);
table.insert(bottoms, bottom);
local center = (left + right) / 2;
local middle = (top + bottom) / 2;
table.insert(centers, center);
table.insert(middles, middle);
end
return numBars, lefts, tops, rights, bottoms, centers, middles;
end
function Group:GetBarLocationStats(numBars, lefts, tops, rights, bottoms, centers, middles)
local minLeft, maxRight = math.min(unpack(lefts)), math.max(unpack(rights));
local minTop, maxBottom = math.min(unpack(tops)), math.max(unpack(bottoms));
local avgCenter, avgMiddle = 0, 0;
for b = 1, numBars, 1 do
avgCenter = avgCenter + centers[b];
avgMiddle = avgMiddle + middles[b];
end
avgCenter = avgCenter / numBars;
avgMiddle = avgMiddle / numBars;
local minCenter, maxCenter = math.min(unpack(centers)), math.max(unpack(centers));
local minMiddle, maxMiddle = math.min(unpack(middles)), math.max(unpack(middles));
return numBars, minLeft, maxRight, minTop, maxBottom, minCenter, avgCenter, maxCenter, minMiddle, avgMiddle, maxMiddle;
end
function Group:BarsAreAligned(alignment)
local barIDs = self:FindAllDescendentIDs(nil, true, nil);
local numBars, lefts, tops, rights, bottoms, centers, middles = self:GetBarLocationData(barIDs);
local _, minLeft, maxRight, minTop, maxBottom, _, avgCenter, _, _, avgMiddle, _ = self:GetBarLocationStats(numBars, lefts, tops, rights, bottoms, centers, middles);
local aligned = true;
for b = 1, numBars, 1 do
local barID = barIDs[b];
local bar = self.manager.objects[barID];
if (alignment == "LeftSides") then
aligned = aligned and lefts[b] == minLeft;
elseif (alignment == "RightSides") then
aligned = aligned and rights[b] == maxRight;
elseif (alignment == "Tops") then
aligned = aligned and tops[b] == minTop;
elseif (alignment == "Bottoms") then
aligned = aligned and bottoms[b] == maxBottom;
elseif (alignment == "Centers") then
aligned = aligned and math.floor(centers[b]) == math.floor(avgCenter);
elseif (alignment == "Middles") then
aligned = aligned and math.floor(middles[b]) == math.floor(avgMiddle);
end
end
return aligned;
end
function Group:AlignBars(alignment)
local barIDs = self:FindAllDescendentIDs(nil, true, nil);
local numBars, lefts, tops, rights, bottoms, centers, middles = self:GetBarLocationData(barIDs);
local _, minLeft, maxRight, minTop, maxBottom, _, avgCenter, _, _, avgMiddle, _ = self:GetBarLocationStats(numBars, lefts, tops, rights, bottoms, centers, middles);
for b = 1, numBars, 1 do
local barID = barIDs[b];
local bar = self.manager.objects[barID];
if (alignment == "LeftSides") then
bar:SetVisibleLeft(minLeft);
elseif (alignment == "RightSides") then
--Puts("For bar " .. bar.settings.caption.text .. ", setting visible left to maxRight=" .. maxRight .. " - visibleWidth=" .. bar:GetVisibleWidth());
bar:SetVisibleLeft(maxRight - bar:GetVisibleWidth());
elseif (alignment == "Tops") then
bar:SetVisibleTop(minTop);
elseif (alignment == "Bottoms") then
bar:SetVisibleTop(maxBottom - bar:GetVisibleHeight());
elseif (alignment == "Centers") then
bar:SetVisibleLeft(avgCenter - math.floor((bar:GetVisibleWidth() + 0.5) / 2));
elseif (alignment == "Middles") then
bar:SetVisibleTop(avgMiddle - math.floor((bar:GetVisibleHeight() + 0.5) / 2));
end
bar:Redraw();
end
self:SaveSettings(false);
end
function Group:SnapToGrid()
for b = 1, #self.settings.barIDs, 1 do
local barID = self.settings.barIDs[b];
local bar = self.manager.objects[barID];
if (bar) then
bar:SnapToGrid();
end
end
for g = 1, #self.settings.groupIDs, 1 do
local groupID = self.settings.groupIDs[g];
local group = self.manager.objects[groupID];
if (group) then
group:SnapToGrid();
end
end
end
function Group:OffsetPosition(deltaX, deltaY, direction)
-- Check the parent group for a higher-level operation
if ((direction == "up") and self.parent and self.parent.GetBarsMoveTogether and self.parent:GetBarsMoveTogether()) then
self.parent:OffsetPosition(deltaX, deltaY, "up");
return;
end
-- Move all bars contained in this group
for b = 1, #self.settings.barIDs, 1 do
local barID = self.settings.barIDs[b];
local bar = self.manager.objects[barID];
if (bar and (not bar:IsLocked())) then
--Puts("Offsetting position for " .. tostring(barID) .. " by " .. tostring(deltaX) .. ", " .. tostring(deltaY));
bar:OffsetPosition(deltaX, deltaY);
end
end
-- Move all subgroups contained in this group
for g = 1, #self.settings.groupIDs, 1 do
local groupID = self.settings.groupIDs[g];
local group = self.manager.objects[groupID];
if (group) then
group:OffsetPosition(deltaX, deltaY, "down");
end
end
end
function Group:EditCaption(optionsNode)
if (self.captionEditor == nil) then
if (optionsNode) then
self.settings.caption.font = optionsNode:GetFont();
self.settings.caption.width = optionsNode:GetWidth();
self.settings.caption.height = optionsNode:GetHeight();
end
self.captionEditor = CaptionEditor("GroupName", self, self.settings.caption.width, self.settings.caption.height, self.settings.caption.text);
self:FormatCaptionEditorTextBox();
self.captionEditor.Closed = function(sender, args)
self.captionEditor = nil;
end
end
end
function Group:EditColor()
if (self.colorPicker == nil) then
self.previousColor = self.color;
self.colorPicker = Thurallor.UI.ColorPicker(self.color, "H");
self.colorPicker.ColorChanged = function(picker)
self:SetColor(picker:GetColor());
end
self.colorPicker.Accepted = function()
self.colorPicker = nil;
self:SaveSettings(true);
end
self.colorPicker.Canceled = function()
self:SetColor(self.previousColor);
self.colorPicker = nil;
end
end
end
function Group:FormatCaptionEditorTextBox()
if (self.captionEditor ~= nil) then
local control = self.captionEditor:GetTextBox();
control:SetFont(self.settings.caption.font);
control:SetForeColor(self.color);
control:SetFontStyle(Turbine.UI.FontStyle.Outline);
control:SetOutlineColor(Turbine.UI.Color(0.75, 0, 0, 0));
control:SetTextAlignment(Turbine.UI.ContentAlignment.MiddleLeft);
control:SetWidth(self.settings.caption.width);
control:SetHeight(self.settings.caption.height);
self.captionEditor:Redraw();
end
end
function Group:SetCaptionSize(width, height)
self.settings.caption.width = width;
self.settings.caption.height = height;
self:FormatCaptionEditorTextBox();
end
function Group:SetColor(color)
self.color = color;
self.settings.color = color:GetHex();
self:SaveSettings(true);
self:FormatCaptionEditorTextBox();
end