lotrointerface.com
Search Downloads

LoTROInterface SVN SequenceBars

[/] [trunk/] [Thurallor/] [SequenceBars/] [OptionsPanel.lua] - Rev 5

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

OptionsPanel = class(Turbine.UI.Control);

function OptionsPanel:Constructor(manager)
    Turbine.UI.Control.Constructor(self);

    self.manager = manager;
    self.ignoredFirstTwoSizeChanges = 0;
    
    self.instructions = Turbine.UI.Label();
    self.instructions:SetParent(self);
    self.instructions:SetMultiline(true);
    self.instructions:SetSize(self:GetWidth(), 32);
    self.instructions:SetFont(Turbine.UI.Lotro.Font.TrajanPro14);
    self.instructions:SetForeColor(Turbine.UI.Color.PaleGoldenrod);
    
    plugin.GetOptionsPanel = function()
        return self;
    end
end

function OptionsPanel:ShowRefreshButton()
    if (self.tree) then
        self.instructions:SetParent(nil);
        self.tree:SetParent(nil);
    end
    if (not self.refreshButton) then
        local prevContext = L:SetContext("/PluginManager/OptionsTab/");
        self.refreshButton = Turbine.UI.Lotro.Button();
        self.refreshButton:SetSize(L:GetNumber("ShowHideButtonWidth"), 20);
        self.refreshButton:SetText(L:GetText("Show"));
        self.refreshButton.Click = function()
            self:Update();
        end
        L:SetContext(prevContext);
    end
    self.refreshButton:SetParent(self);
    self.refreshButton:SetPosition(math.floor(self:GetWidth() / 2 - self.refreshButton:GetWidth() / 2), 10);
end

function OptionsPanel:Show()
    Turbine.PluginManager.ShowOptions(Plugins.SequenceBars);
    self:Update();
end

function OptionsPanel:Update()
    if (self:IsDisplayed()) then
        self.refreshButton:SetParent(nil);
    else
        return self:ShowRefreshButton();
    end
    
    self.treeHeight = 0;
    self.instructions:SetText(L:GetText("/PluginManager/OptionsTab/Instructions"));
    self.instructions:SetWidth(self:GetWidth());
    self.instructions:SetParent(self);

    -- Remove the old tree only after creating its replacement.
    local oldTree = self.tree;
    
    self.tree = Turbine.UI.TreeView();
    self.tree:SetParent(self);
    self.tree:SetTop(self.instructions:GetHeight());
    self.tree:SetVisible(true);
    self.tree:SetMouseVisible(false);
    self.tree:SetIndentationWidth(16);
    self.tree:SetTop(self.instructions:GetHeight());
    self.tree:SetWidth(self:GetWidth());
    self.tree.textBoxes = {};
    self:AddTreeNode(self.tree:GetNodes(), 1, self.tree:GetWidth(), self.manager);
    self.tree:SetHeight(self.treeHeight);
    local newHeight = self.treeHeight + self.instructions:GetHeight();
    if (self:GetHeight() ~= newHeight) then
        self:SetHeight(newHeight);
    end
    
    if (oldTree) then
        oldTree:SetParent(nil);
        oldTree = nil;
        --collectgarbage();
    end
end

function OptionsPanel:SizeChanged()
    local width = self:GetWidth();
    if (self.previousWidth ~= width) then
        self:Update();
        self.previousWidth = width;
    end
end

function OptionsPanel:AddTreeNode(parentNodeList, nodeIndex, textBoxWidth, object)
    local prevContext = L:SetContext("/PluginManager/OptionsTab");
    local textBoxLeft = 0;
    local newNode = Turbine.UI.TreeNode();
    newNode.object = object;
    parentNodeList:Add(newNode);
    newNode:SetVisible(true);
    newNode:SetMouseVisible(false);
    if (object:CanHaveChildren()) then
        newNode:SetExpanded(true);
    end
    newNode:SetWidth(textBoxWidth);

    -- Add the "Show" or "Hide" button.
    if (not (object.parent and object.parent:IsHidden()))  then
        local showHideButtonWidth = L:GetNumber("ShowHideButtonWidth");
        newNode.showHideButton = Turbine.UI.Lotro.Button();
        newNode.showHideButton:SetParent(newNode);
        newNode.showHideButton.node = newNode;
        newNode.showHideButton:SetFont(Turbine.UI.Lotro.Font.TrajanPro13);
        newNode.showHideButton:SetWidth(showHideButtonWidth);
        newNode.showHideButton.Click = function(sender, args)
            local hidden = sender.node.object.settings.hidden;
            sender.node.object:SetHidden(not hidden, true);
            sender:UpdateText();
            sender.node:SetExpanded(true);
            if (sender.node.object:CanHaveChildren()) then
                sender.node.icon:MouseClick();
            end
        end
        newNode.showHideButton.UpdateText = function(sender)
            local hidden = sender.node.object.settings.hidden;
            local prevContext = L:SetContext("/PluginManager/OptionsTab");
            if (hidden) then
                sender:SetText(L:GetText("Show"));
            else
                sender:SetText(L:GetText("Hide"));
            end
            L:SetContext(prevContext);
        end
        newNode.showHideButton:UpdateText();
        newNode.showHideButton:SetLeft(textBoxWidth - showHideButtonWidth);
        textBoxWidth = textBoxWidth - showHideButtonWidth;
    end

    -- Show expand/collapse icon
    if (object:CanHaveChildren()) then
        newNode.icon = Turbine.UI.Control();
        newNode.icon:SetParent(newNode);
        newNode.icon.node = newNode;
        newNode.icon:SetSize(16, 16);
        newNode.icon:SetBlendMode(Turbine.UI.BlendMode.AlphaBlend);
        newNode.icon.MouseClick = function(sender)
            local background;
            if (sender.node:IsExpanded()) then
                background = resources.Icon.Collapse;
            else
                background = resources.Icon.Expand;
            end
            sender:SetBackground(background);
        end
        newNode:SetVisible(true);
        newNode.icon:MouseClick();
    end
    local indent = self.tree:GetIndentationWidth();
    textBoxWidth = textBoxWidth - indent;
    textBoxLeft = textBoxLeft + indent;

    -- Adjust height of element and tree based on contents
    --newNode:SetHeight(newNode.showHideButton:GetHeight());
    newNode:SetHeight(20);
    self.treeHeight = self.treeHeight + newNode:GetHeight();

    -- Add the item name
    newNode.textBox = Turbine.UI.Label();
    table.insert(self.tree.textBoxes, newNode.textBox);
    newNode.textBox:SetParent(newNode);
    newNode.textBox.node = newNode;
    newNode.textBox:SetFont(Turbine.UI.Lotro.Font.TrajanPro15);
    newNode.textBox:SetFontStyle(Turbine.UI.FontStyle.Outline);
    newNode.textBox:SetOutlineColor(Turbine.UI.Color.Black);
    newNode.textBox:SetMultiline(false);
    newNode.textBox:SetVisible(true);
    newNode.textBox:SetSize(textBoxWidth, 20);
    newNode.textBox:SetLeft(textBoxLeft);
    newNode.textBox:SetForeColor(object:GetColor());
    newNode.textBox:SetText(object:GetName());
    newNode.textBox.MouseClick = function(sender, args)
        -- Left-click expands/contracts.
        if (sender.node.object:CanHaveChildren()) then
            sender.node.icon:MouseClick();
        end
        -- Right-click displays the settings menu.
        if (args.Button == Turbine.UI.MouseButton.Right) then
            sender.node.object:ShowSettingsMenu(true);
        end
    end
    newNode.textBox.MouseDoubleClick = function(sender, args)
        -- Left-double-click opens the caption editor.
        if (args.Button == Turbine.UI.MouseButton.Left) then
            sender.node.object:EditCaption(newNode.textBox);
        end
    end
    newNode.textBox.MouseDown = function(sender, args)
        if (args.Button == Turbine.UI.MouseButton.Left) then
            sender.mouseDown = true;
            sender.originalLeft, sender.originalTop = sender.node:PointToScreen(sender:GetPosition());
            sender.originalMouseX, sender.originalMouseY = Turbine.UI.Display.GetMouseX(), Turbine.UI.Display.GetMouseY();

            -- Detach from tree
            sender:SetParent(self);
            sender:SetPosition(self:PointToClient(sender.originalLeft, sender.originalTop));
        end
    end
    newNode.textBox.MouseInside = function(sender)
        local x, y = Turbine.UI.Display.GetMouseX(), Turbine.UI.Display.GetMouseY();
        local left, top = sender:PointToScreen(0, 0);
        local width, height = sender:GetSize();
        return ((x >= left) and (x < left + width) and (y >= top) and (y < top + height));
    end
    newNode.textBox.MouseMove = function(sender)
        if (sender.mouseDown) then
            local newMouseX, newMouseY = Turbine.UI.Display.GetMouseX(), Turbine.UI.Display.GetMouseY();
            local deltaX, deltaY = newMouseX - sender.originalMouseX, newMouseY - sender.originalMouseY;
            local left = sender.originalLeft + deltaX;
            local top = sender.originalTop + deltaY;
            sender:SetPosition(self:PointToClient(left, top));
            if (sender.previousTarget) then
                sender.previousTarget:SetBackColor(nil);
                sender.previousTarget = nil;
            end
            
            -- Inefficiently figure out if we're over another textBox
            local sourceObject = sender.node.object;
            sender.targetObject = nil;
            for t = 1, #self.tree.textBoxes do
                local textBox = self.tree.textBoxes[t];
                if (textBox:MouseInside()) then
                    -- found the textBox we're hovering over!
                    local object = textBox.node.object;
                    if (object:CanHaveChildren() and (textBox ~= sender) and (not sourceObject:Contains(object)) and (sourceObject.parent ~= object)) then
                        sender.targetObject = object;
--Puts("target object is " .. object.settings.type .. " " .. Serialize(object.settings.caption.text));
                        textBox:SetBackColor(Turbine.UI.Color(1, 0.25, 0.25, 0.75));
                        sender.previousTarget = textBox;
                        break;
                    end
                end
            end
        end
    end
    newNode.textBox.MouseUp = function(sender, args)
        if (args.Button == Turbine.UI.MouseButton.Left) then
            sender.mouseDown = false;
            local newMouseX, newMouseY = Turbine.UI.Display.GetMouseX(), Turbine.UI.Display.GetMouseY();
            local deltaX, deltaY = newMouseX - sender.originalMouseX, newMouseY - sender.originalMouseY;

            -- Reattach to tree
            sender:SetParent(sender.node);
            sender:SetPosition(sender.node:PointToClient(sender.originalLeft, sender.originalTop));
            
            -- Do the object transfer
            if (sender.targetObject) then
                local sourceObject = sender.node.object;
                if (sourceObject.settings.type == "bar") then
                    self.manager:TransferBar(sourceObject.objectID, sourceObject.parent, sender.targetObject);
                else
                    self.manager:TransferGroup(sourceObject.objectID, sourceObject.parent, sender.targetObject);
                end
            end
        end    
    end

    -- Add subnodes, if any
    local childIDs = {};
    if (object:CanHaveChildren()) then
        childIDs = object:GetChildIDs();
        for c = 1, #childIDs, 1 do
            local childID = childIDs[c];
            local child = object:GetChild(childID);
            local childWidth = newNode:GetWidth() - self.tree:GetIndentationWidth();
            self:AddTreeNode(newNode:GetChildNodes(), c, childWidth, child);
        end
    end
    
    L:SetContext(prevContext);
end

Go to most recent revision | Compare with Previous | Blame


All times are GMT -5. The time now is 12:56 AM.


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