lotrointerface.com
Search Downloads

LoTROInterface SVN SequenceBars

[/] [trunk/] [Thurallor/] [SequenceBars/] [Bar.lua] - Rev 13

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

Bar = class(Node);

function Bar:Constructor(group, barID, settings)
    Turbine.UI.Window.Constructor(self);
    self.constructing = true;
    self:SetZOrder(0);
    self:SetMouseVisible(false);
    self.slotContainer = Turbine.UI.Control();
    self.slotContainer:SetParent(self);
    self.slotContainer:SetMouseVisible(false);
    
    self.parent = group;
    self.manager = group.manager;
    self.objectID = barID;
    self.onLoad = onLoad;
    self.sequenceItems = { };
    self.cursorSlot = 3;
    self.cursorSequenceItem = 1;
    self.globals = self.manager.settings;
    self.eventGeneratorIDs = {};
    
    -- Default settings
    local defaults = {};
    defaults.type = "bar";
    defaults.locked = false;
    defaults.hidden = false;
    defaults.color = Thurallor.Utils.Color();
    defaults.color:SetHSV(math.random(), 1, 1);
    defaults.color = defaults.color:GetHex();
    defaults.cursorStyle = "SmallSquareGlow";
    defaults.caption = { };
    defaults.caption.hidden = false;
    defaults.caption.text = L:GetText("/Default/BarName");
    defaults.caption.position = "beginning";
    defaults.caption.width = 80;
    defaults.caption.height = 35;
    defaults.caption.font = Turbine.UI.Lotro.Font.TrajanPro15;
    defaults.slotSize = 35; -- must be an even number
    defaults.slotSpacing = 0;
    defaults.orientation = "Right";
    defaults.visibleSlots = 5;
    defaults.cursorHomePosition = 2;
    defaults.animation = { duration = 0.1 };
    defaults.sequenceItemInfo = { {}, {}, {}, {}, {} };
    defaults.position = { left = math.floor(Turbine.UI.Display:GetWidth() / 2); top = math.floor(Turbine.UI.Display:GetHeight() / 2 )};
    defaults.eventHandlers = {};
    defaults.eventsEnabled = false;
    
    -- Previously-saved settings override the defaults
    DeepTableCopy(settings, defaults);
    DeepTableCopy(defaults, settings);
    self.settings = settings;

    self:ReapplySettings();
    self.constructing = nil;
    self:SetWantsEvents(self.settings.eventsEnabled);
end

function Bar:ReapplySettings()

    -- Make color object from hex string
    self.color = Thurallor.Utils.Color(1, 0, 0, 0);
    self.color:SetHex(self.settings.color);

    -- For backward compatibility
    self.settings.cursorStyle = string.gsub(self.settings.cursorStyle, "SilverFrame(%d)", "MetalFrame%1");
    for i = 1, #self.settings.sequenceItemInfo, 1 do
        local e = self.settings.sequenceItemInfo[i];
        if (e.action) then
            e.action = string.gsub(e.action, "item.eventName", "item.info.eventName");
            e.action = string.gsub(e.action, "item.bagSlot", "item.info.bagSlot");
        end
    end
    
    -- Derived from settings
    self:SetCursorStyle(self.settings.cursorStyle);
    
    self:GetQuickSlots();
    self:Redraw();
end

function Bar:CanHaveChildren()
    return false;
end

function Bar:Contains(object)
    return false;
end

function Bar:SetColor(color)
    Node.SetColor(self, color);
    self:Redraw();
    self:SetCursorStyle();
    if (self.sequenceEditor) then
        self.sequenceEditor:Redraw();
    end
end

function Bar:SetName(caption)
    Node.SetName(self, caption);
    self:Redraw();
    if (self.sequenceEditor) then
        self.sequenceEditor:SetText(caption);
    end
    if (self.exportWindow) then
        self.exportWindow:SetText(caption);
    end
end

function Bar:SetCaptionSize(width, height)
    Node.SetCaptionSize(self, width, height);
    self:Redraw();
    self:OffsetPosition(0, 0);
end

function Bar:SetCaptionFont(font)
    self.settings.caption.font = font;
    self:SaveSettings(false);
    self:Redraw();
end

function Bar:SetCaptionHidden(hide)
    self.settings.caption.hidden = hide;
    self:SaveSettings(false);
    self:Redraw();
end

function Bar:ApplyHiddenness()
    local hidden = self:IsHidden();
    self:SetVisible(not hidden);
    self:SaveSettings(true);
end

-- Updates the sequence item when a user drags a shortcut onto the sequence editor
function Bar:ShortcutChanged(i)

    -- Hide and destroy all objects in preparation for recreation (self:GetQuickSlots())
    for s = 1, #self.sequenceItems, 1 do
        self.sequenceItems[s]:SetVisible(false);
        self.sequenceItems[s] = nil;
    end
    
    self:SaveSettings(false);
    self:GetQuickSlots();
    self:Redraw();
end

-- Creates (or recreates) the array of objects (e.g. Quickslot) according to self.settings.sequenceItemInfo
function Bar:GetQuickSlots()
    -- Unregister event generator.  We will re-register for each generated event as we loop through the sequence below.
    for eventName, generatorID in pairs(self.eventGeneratorIDs) do
        self.manager:RemoveEventGenerator(generatorID, eventName);
    end
    self.eventGeneratorIDs = {};

    -- Iterate through the sequence creating clickable objects.
    self.sequenceItems = {};
    local s = 0;
    for i = 1, #self.settings.sequenceItemInfo, 1 do
        local info = self.settings.sequenceItemInfo[i];
        if ((type(info) == "table") and info.class) then
            s = s + 1;
            local item = self.sequenceItems[s];
            if (item) then
                -- Reuse the existing item
                item:SetInfo(info);
            else
                -- Create a new item
                item = Slot(info);
                self.sequenceItems[s] = item;
                item:SetRoundedCorners(true);
                item:SetParent(self.slotContainer);
                item:SetZOrder(2);
                item:SetActionEnabled(true);
                item:SetAllowDrop(false);
                item:SetDraggable(false);
                item.bar = self;
                item.s = s;
                item.MouseClick = function(sender, args)
                    if (args.Button == Turbine.UI.MouseButton.Left) then
                        if (sender.actionFunc) then
                            sender:actionFunc(args);
                        end
                        self:SequenceItemClick(sender.s);
       
                    -- Right-click displays the settings menu.
                    elseif (args.Button == Turbine.UI.MouseButton.Right) then
                        self:ShowSettingsMenu();
                    end
                end
            end
            if (info.class == "Turbine.UI.Control") then
                if ((info.type == "GenerateEvent") and (not self.eventGeneratorIDs[info.eventName])) then
                    self.eventGeneratorIDs[info.eventName] = self.manager:AddEventGenerator(self, info.eventName);
                end
                item.actionFunc = loadstring(info.action);
            else
                item.actionFunc = nil;
            end
        end
    end
end

function Bar:Redraw()
    local displaySlots = self.settings.visibleSlots;
    local slotsLongDimension = (2 * self.cursorMargin) + (displaySlots + 1) * self.settings.slotSize + displaySlots * self.settings.slotSpacing;
    local slotsShortDimension = (2 * self.cursorMargin) + self.settings.slotSize;
    
    local captionMargin = 4;
    local captionWidth, captionHeight = self.settings.caption.width, self.settings.caption.height;
    local barWidth, barHeight, slotsWidth, slotsHeight, slotsLeft, slotsTop, captionLeft, captionTop;
    if ((self.settings.orientation == "Up") or (self.settings.orientation == "Down")) then
        slotsWidth, slotsHeight = slotsShortDimension, slotsLongDimension;

        -- Position slots and caption horizontally; set bar width
        if (slotsWidth < captionWidth) then
            barWidth = captionWidth;
            slotsLeft = math.floor((barWidth - slotsWidth) / 2);
            captionLeft = 0;
        elseif (slotsWidth > captionWidth) then
            barWidth = slotsWidth;
            slotsLeft = 0;
            captionLeft = math.floor((barWidth - captionWidth) / 2);
        else -- (slotsWidth == captionWidth)
            barWidth, slotsLeft, captionLeft = slotsWidth, 0, 0;
        end

        -- Position slots and caption vertically; set bar height
        if (self.settings.orientation == "Up") then
            if (self.settings.caption.position == "beginning") then
                slotsTop = 0;
                captionTop = slotsHeight - self.cursorMargin + captionMargin;
                barHeight = math.max(slotsHeight, captionTop + captionHeight);
            else -- (self.settings.caption.position == "end")
                captionTop = math.max(0, self.cursorMargin + self.settings.slotSize - (captionHeight + captionMargin));
                slotsTop = math.max((captionHeight + captionMargin) - (self.cursorMargin + self.settings.slotSize), 0);
                barHeight = slotsTop + slotsHeight;
            end
        else -- (self.settings.orientation == "Down")
            if (self.settings.caption.position == "beginning") then
                captionTop = math.max(0, self.cursorMargin - (captionHeight + captionMargin));
                slotsTop = math.max((captionHeight + captionMargin) - self.cursorMargin, 0);
                barHeight = slotsTop + slotsHeight;
            else -- (self.settings.caption.position == "end")
                slotsTop = 0;
                captionTop = slotsHeight - self.cursorMargin - self.settings.slotSize + captionMargin;
                barHeight = math.max(slotsHeight, captionTop + captionHeight);
            end
        end
    else -- ((self.settings.orientation == "Left") or (self.settings.orientation == "Right")) then
        slotsWidth, slotsHeight = slotsLongDimension, slotsShortDimension;

        -- Position slots and caption vertically; set bar height
        if (slotsHeight < captionHeight) then
            barHeight = captionHeight;
            slotsTop = math.floor((barHeight - slotsHeight) / 2);
            captionTop = 0;
        elseif (slotsHeight > captionHeight) then
            barHeight = slotsHeight;
            slotsTop = 0;
            captionTop = math.floor((barHeight - captionHeight) / 2);
        else -- (slotsHeight == captionHeight)
            barHeight, slotsTop, captionTop = slotsHeight, 0, 0;
        end
    
        -- Position slots and caption horizontally; set bar width
        if (self.settings.orientation == "Left") then
            if (self.settings.caption.position == "beginning") then
                slotsLeft = 0;
                captionLeft = slotsWidth - self.cursorMargin + captionMargin;
                barWidth = math.max(slotsWidth, captionLeft + captionWidth);
            else -- (self.settings.caption.position == "end")
                captionLeft = math.max(0, self.cursorMargin + self.settings.slotSize - (captionWidth + captionMargin));
                slotsLeft = math.max((captionLeft + captionWidth + captionMargin) - (self.cursorMargin + self.settings.slotSize), 0);
                barWidth = slotsLeft + slotsWidth;
            end
        else -- (self.settings.orientation == "Right")
            if (self.settings.caption.position == "beginning") then
                captionLeft = math.max(0, self.cursorMargin - (captionWidth + captionMargin));
                slotsLeft = math.max((captionWidth + captionMargin) - self.cursorMargin, 0);
                barWidth = slotsLeft + slotsWidth;
            else -- (self.settings.caption.position == "end")
                slotsLeft = 0;
                captionLeft = slotsWidth - self.cursorMargin - self.settings.slotSize + captionMargin;
                barWidth = math.max(slotsWidth, captionLeft + captionWidth);
            end
        end
    end
    
    self.slotContainer:SetSize(slotsWidth, slotsHeight);
    self.slotContainer:SetPosition(slotsLeft, slotsTop);
--self.slotContainer:SetBackColor(Turbine.UI.Color(0.20, 0, 1, 0));
    self:DrawCaption();
    self.caption:SetSize(captionWidth, captionHeight);
    self.caption:SetPosition(captionLeft, captionTop);
--self.caption:SetBackColor(Turbine.UI.Color(0.20, 0, 0, 1));
    self:SetSize(barWidth, barHeight);
    self:SetPosition(self.settings.position.left, self.settings.position.top);
--self:SetBackColor(Turbine.UI.Color(0.10, 1, 1, 1));

    -- Save these for use in Animate().
    self.slotsTop, self.slotsLeft = slotsTop, slotsLeft;

    -- Draw cursor icon.
    self:Reset();

    self:SetWantsUpdates(false);
    self:ApplyHiddenness();
end

function Bar:Reset()
    self:SetCursorSlot(self.settings.cursorHomePosition);
    self:SetCursorSequenceItem(1);
    self.animationStopped = false;
end

-- Moves the cursor to the specified position on the bar
function Bar:SetCursorSlot(pos)
    local resource = resources.Cursor[self.settings.cursorStyle];
    local left, top = self:GetSlotCoords(pos);
    left = left - self.cursorMargin + resource.xOffset;
    top = top - self.cursorMargin + resource.yOffset;
    self.cursor.left, self.cursor.top = left, top;
    self.cursor:SetPosition(left, top);
    self.cursorSlot = pos;
end

function Bar:DrawCaption()
    if (not self.caption) then
        self.caption = Turbine.UI.Control();
        self.caption:SetParent(self);
        self.caption:SetZOrder(10);
    end
    local caption = self.caption;
    if (not caption.label) then
        caption.label = Turbine.UI.Label();
        caption.label:SetParent(caption);
        caption.label:SetMouseVisible(false);
    end
    local label = caption.label;
    
    local alignment;
    local where = self.settings.orientation;
    if (self.settings.caption.position == "end") then
        local swap = {Up = "Down"; Down = "Up"; Left = "Right"; Right = "Left"};
        where = swap[where];
    end
    if (where == "Up") then
        alignment = Turbine.UI.ContentAlignment.TopCenter;
    elseif (where == "Down") then
        alignment = Turbine.UI.ContentAlignment.BottomCenter;
    elseif (where == "Right") then
        alignment = Turbine.UI.ContentAlignment.MiddleRight;
    else -- "Left"
        alignment = Turbine.UI.ContentAlignment.MiddleLeft;
    end

    label:SetText(self.settings.caption.text);
    label:SetVisible(not self.settings.caption.hidden);
    self:FormatCaptionLabel(label, alignment);
    if (self.captionEditor and self.captionEditor:WantsCaptionUpdates()) then
        local textBox = self.captionEditor:GetTextBox();
        self:FormatCaptionLabel(textBox, alignment);
        self.captionEditor:Redraw();
    end
    
    caption.MouseClick = function(sender, args)
        -- Left-click resets the sequence back to the beginning.
        if (args.Button == Turbine.UI.MouseButton.Left) then
            self:Reset();
        end
        
        -- Right-click displays the settings menu.
        if (args.Button == Turbine.UI.MouseButton.Right) then
            self:ShowSettingsMenu();
        end
    end

    caption.MouseDoubleClick = function(sender, args)
        -- Left-double-click opens the caption editor.
        if (args.Button == Turbine.UI.MouseButton.Left) then
            self:EditCaption();
        end
        
        -- Right-double-click would open the sequence editor, but
        -- since right-click opens the settings menu, right-double
        -- -click isn't possible.
    end
        
    -- While mouse is over label, turn it yellow to indicate that it can be clicked.
    caption.MouseEnter = function(sender, args)
        label:SetForeColor(Turbine.UI.Color(1, 1, 1, 0));
        label:SetVisible(true);
    end
    caption.MouseLeave = function(sender, args)
        label:SetForeColor(self.color);
        label:SetVisible(not self.settings.caption.hidden);
    end
    
    -- MouseDown, MouseUp, and MouseMove are used to move the window (if not locked)
    caption.MouseDown = function(sender, args)
        if (args.Button == Turbine.UI.MouseButton.Left) then
            caption.mouseDown = true;
            caption.originalMouseX, caption.originalMouseY = Turbine.UI.Display.GetMouseX(), Turbine.UI.Display.GetMouseY();
        end
    end
    caption.MouseUp = function(sender, args)
        if (args.Button == Turbine.UI.MouseButton.Left) then
            caption.mouseDown = false;
            if (not self.settings.locked) then
                if (self.parent:GetBarsMoveTogether()) then
                    self.parent:SnapToGrid();
                else
                    self:SnapToGrid();
                end
                self:SaveSettings(false);
            end
        end
    end
    caption.MouseMove = function(sender, args)
        if (caption.mouseDown and not self.settings.locked) then
            local newMouseX, newMouseY = Turbine.UI.Display.GetMouseX(), Turbine.UI.Display.GetMouseY();
            local deltaX, deltaY = newMouseX - caption.originalMouseX, newMouseY - caption.originalMouseY;
            if (self.parent:GetBarsMoveTogether()) then
                self.parent:OffsetPosition(deltaX, deltaY, "up");
            else
                self:OffsetPosition(deltaX, deltaY);
            end
            caption.originalMouseX, caption.originalMouseY = newMouseX, newMouseY;
        end
    end
    caption:SetAllowDrop(true);
    caption.DragDrop = function(sender, args)
        self:EditSequence();
    end
end

function Bar:EditCaption()
    if (self.captionEditor == nil) then
        self.captionEditor = CaptionEditor("BarCaption", self, self.caption.label:GetWidth(), self.caption.label:GetHeight(), self.settings.caption.text);
        self.captionEditor.Closed = function(sender, args)
            self.captionEditor = nil;
        end
        self:Redraw();
    end
end

function Bar:EditSequence()
    if (self.sequenceEditor == nil) then
        self.sequenceEditor = SequenceEditor(self, self.settings);
        self.sequenceEditor.Closed = function(sender, args)
            self.sequenceEditor = nil;
        end
    end
end

function Bar:FormatCaptionLabel(control, alignment)
    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(alignment);
    control:SetWidth(self.settings.caption.width);
    control:SetHeight(self.settings.caption.height);
end

function Bar:MoveToMouseCursor()
    cursorX, cursorY = self.cursor:GetPosition();
    cursorWidth, cursorHeight = self.cursor:GetSize();
    cursorCenter = (cursorX + math.floor(cursorWidth / 2));
    cursorMiddle = (cursorY + math.floor(cursorHeight / 2));
    mouseX, mouseY = Turbine.UI.Display.GetMouseX(), Turbine.UI.Display.GetMouseY();
    self:OffsetPosition(mouseX - cursorCenter, mouseY - cursorMiddle);
end

-- Bar is positioned with respect to the topmost/leftmost quickslot.
function Bar:GetSlotsScreenCoords()
    local winLeft, winTop = Turbine.UI.Window.GetPosition(self);
    local firstLeft, firstTop = self:GetSlotCoords(1);
    local lastLeft, lastTop = self:GetSlotCoords(self.settings.visibleSlots);
    local slotLeft, slotTop = math.min(firstLeft, lastLeft), math.min(firstTop, lastTop);
    local slotRight = math.max(firstLeft, lastLeft) + self.settings.slotSize;
    local slotBottom = math.max(firstTop, lastTop) + self.settings.slotSize;
    local slotContainerLeft, slotContainerTop = self.slotContainer:GetPosition();
    local left = winLeft + slotContainerLeft + slotLeft;
    local top = winTop + slotContainerTop + slotTop;
    local width = slotRight - slotLeft;
    local height = slotBottom - slotTop;
    return left, top, width, height;
end

function Bar:SetLeft(left)
    self:SetPosition(left, self:GetTop());
end

function Bar:SetTop(top)
    self:SetPosition(self:GetLeft(), top);
end

function Bar:SetPosition(left, top)
    local winLeft, winTop = Turbine.UI.Window.GetPosition(self);
    local slotLeft, slotTop = self:GetSlotsScreenCoords();
    local leftOffset, topOffset = slotLeft - winLeft, slotTop - winTop;
    Turbine.UI.Window.SetPosition(self, left - leftOffset, top - topOffset);
end

function Bar:GetLeft()
    local left = self:GetSlotsScreenCoords();
    return left;
end

function Bar:GetTop()
    local _, top = self:GetSlotsScreenCoords();
    return top;
end

function Bar:GetPosition()
    local left, top = self:GetSlotsScreenCoords();
    return left, top;
end

function Bar:OffsetPosition(deltaX, deltaY)
    self:SetPosition(self:GetLeft() + deltaX, self:GetTop() + deltaY);
    self.settings.position = { left = self:GetLeft(); top = self:GetTop() };
    self:SaveSettings(false);
end

function Bar:SnapToGrid()
    if (not self.globals.snapToGrid) then
        return;
    end
    local left, top = self:GetSlotsScreenCoords();
    local newLeft, newTop = self.parent.manager:GetNearestGridPos(left, top);
    self:SetVisibleLeft(newLeft);
    self:SetVisibleTop(newTop);
    self:SaveSettings(false);
end

function Bar:AddSettingsMenuItem(parent, itemName, fromOptionsPanel)
    local item = Turbine.UI.MenuItem(L:GetText(itemName), true, false);
    parent:GetItems():Add(item);

    if (itemName == "Root") then
        local prevContext = L:SetContext("/BarMenu");
        parent:GetItems():Clear();
        if (fromOptionsPanel and (not self:IsHidden())) then
            self:AddSettingsMenuItem(parent, "Find");
        end
        self:AddSettingsMenuItem(parent, "Hide");
        self:AddSettingsMenuItem(parent, "LockPosition");
        self:AddSettingsMenuItem(parent, "Clone");
        self:AddSettingsMenuItem(parent, "Delete");
        self:AddSettingsMenuItem(parent, "Export");
        self:AddSettingsMenuItem(parent, "EditSequence");
        self:AddSettingsMenuItem(parent, "EventBehaviors");
        self:AddSettingsMenuItem(parent, "Settings");
        if (not fromOptionsPanel) then
            self:AddSettingsMenuItem(parent, "Group");
            Group.AddSettingsMenuItem(self.parent, parent, "Global", true);
            self:AddSettingsMenuItem(parent, "Directory");
        end
        L:SetContext(prevContext);
    elseif (itemName == "Directory") then
        item.Click = function(sender, args)
            self.manager.optionsPanel:ShowDirectory();
        end
    elseif (itemName == "Export") then
        item.Click = function(sender, args)
            self:Export();
        end
    elseif (itemName == "Find") then
        item.Click = function(sender, args)
            self:Find();
        end
    elseif (itemName == "Hide") then
        item:SetChecked(self.settings.hidden);
        item.Click = function(sender, args)
            self:SetHidden(not self.settings.hidden);
        end
    elseif (itemName == "LockPosition") then
        item:SetChecked(self.settings.locked);
        item.Click = function(sender, args)
            self:SetLocked(not self.settings.locked);
        end
    elseif (itemName == "EditSequence") then
        item:SetEnabled(not self.sequenceEditor);
        item.Click = function(sender, args)
            self:EditSequence();
        end
    elseif (itemName == "EventBehaviors") then
        local prevContext = L:SetContext("/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(false);
        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, "ShowBar");
        self:AddSettingsMenuItem(item, "HideBar");
        self:AddSettingsMenuItem(item, "ResetBar");
        self:AddSettingsMenuItem(item, "MoveToCursor");
    elseif (string.find("|ShowBar|HideBar|ResetBar|MoveToCursor|", "|" .. itemName .. "|")) then
        item.itemName = itemName;
        if (itemName ~= "MoveToCursor") then
            self:AddSettingsMenuItem(item, "AtStartup");
            self:AddSettingsMenuItem(item, "WhenCombatBegins");
            self:AddSettingsMenuItem(item, "WhenCombatEnds");
        end
        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 == "Settings") then
        local prevContext = L:SetContext("SettingsMenu");
        self:AddSettingsMenuItem(item, "Orientation");
        if ((self.settings.orientation == "Up") or (self.settings.orientation == "Down")) then
            self:AddSettingsMenuItem(item, "Height");
        else
            self:AddSettingsMenuItem(item, "Width");
        end
        self:AddSettingsMenuItem(item, "Cursor");
        self:AddSettingsMenuItem(item, "Caption");
        self:AddSettingsMenuItem(item, "Color");
        self:AddSettingsMenuItem(item, "Animation");
        L:SetContext(prevContext);
    elseif (itemName == "Color") then
        item:SetEnabled(not self.colorPicker);
        item.Click = function(sender, args)
            self:EditColor();
        end
    elseif (itemName == "Animation") then
        local prevContext = L:SetContext("AnimationMenu");
        self:AddSettingsMenuItem(item, "Instantaneous");
        self:AddSettingsMenuItem(item, "Fast");
        self:AddSettingsMenuItem(item, "Slow");
        self:AddSettingsMenuItem(item, "VerySlow");
        self:AddSettingsMenuItem(item, "Disabled");
        L:SetContext(prevContext);
    elseif (string.find("|Instantaneous|Fast|Slow|VerySlow|Disabled|", "|" .. itemName .. "|")) then
        local duration = { ["Instantaneous"] = 0; ["Fast"] = 0.05; ["Slow"] = 0.1; ["VerySlow"] = 0.5; ["Disabled"] = -1; };
        local checked = (self.settings.animation.duration == duration[itemName]);
        item:SetChecked(checked);
        item:SetEnabled(not checked);
        item.Click = function(sender, args)
            self:SetAnimationSpeed(duration[itemName]);
        end
    elseif (itemName == "Orientation") then
        local prevContext = L:SetContext("OrientationMenu");
        self:AddSettingsMenuItem(item, "Up");
        self:AddSettingsMenuItem(item, "Right");
        self:AddSettingsMenuItem(item, "Left");
        self:AddSettingsMenuItem(item, "Down");
        L:SetContext(prevContext);
    elseif ((itemName == "Up") or (itemName == "Down") or (itemName == "Left") or (itemName == "Right")) then
        local checked = (self.settings.orientation == itemName);
        item:SetChecked(checked);
        item:SetEnabled(not checked);
        item.Click = function(sender, args)
            self.settings.orientation = itemName;
            self:SaveSettings(false);
            self:Redraw();
            self:MoveOntoScreen();
        end
    elseif ((itemName == "Width") or (itemName == "Height")) then
        local size = self.settings.visibleSlots;
        local displayWidth, displayHeight = Turbine.UI.Display.GetSize();
        local minSize = 1, maxSize;
        if ((self.settings.orientation == "Up") or (self.settings.orientation == "Down")) then
            maxSize = math.floor(displayHeight / self.settings.slotSize);
        else
            maxSize = math.floor(displayWidth / self.settings.slotSize);
        end
        local prevContext = L:SetContext("HeightWidthMenu");
        for i = minSize, maxSize, 1 do
            if (math.fmod (i, 20) == 0) then
                local subItemName = ". . .";
                local subItem = Turbine.UI.MenuItem(subItemName);
                item:GetItems():Add(subItem);
                item = subItem;
            else
                local subItemName = L:GetText("Slots");
                if (i == 1) then
                    subItemName = L:GetText("Slot");
                end
                subItemName = string.gsub(subItemName, "<num>", tostring(i));
                local checked = (i == self.settings.visibleSlots)
                local subItem = Turbine.UI.MenuItem(subItemName, not checked, checked);
                item:GetItems():Add(subItem);
                subItem.Click = function(sender, args)
                    self:ResizeBar(i);
                    self:MoveOntoScreen();
                end
            end
        end
        L:SetContext(prevContext);
    elseif (itemName == "Cursor") then
        local prevContext = L:SetContext("CursorMenu");
        self:AddSettingsMenuItem(item, "Style");
        self:AddSettingsMenuItem(item, "HomePosition");
        L:SetContext(prevContext);
    elseif (itemName == "HomePosition") then
        local prevContext = L:SetContext("HomePositionMenu");
        for i = 1, self.settings.visibleSlots, 1 do
            if (math.fmod (i, 20) == 0) then
                local subItemName = ". . .";
                local subItem = Turbine.UI.MenuItem(subItemName);
                item:GetItems():Add(subItem);
                item = subItem;
            else
                local subItemName = L:GetText("Slot");
                subItemName = string.gsub(subItemName, "<num>", tostring(i));
                local checked = (i == self.settings.cursorHomePosition);
                local subItem = Turbine.UI.MenuItem(subItemName, not checked, checked);
                item:GetItems():Add(subItem);
                subItem.Click = function(sender, args)
                    self.settings.cursorHomePosition = i;
                    self:SaveSettings(false);
                    self:Redraw();
                end
            end
        end
        L:SetContext(prevContext);
    elseif (itemName == "Style") then
        local prevContext = L:SetContext("StyleMenu");
        local styles = {};
        for name, info in pairs(resources.Cursor) do
            local checked = (name == self.settings.cursorStyle);
            local text = L:GetText(name);
            local style = Turbine.UI.MenuItem(text, not checked, checked);
            styles[text] = style;
            style.Click = function(sender, args)
                self:SetCursorStyle(name);
                self:SaveSettings(false);
                self:Redraw();
            end
        end
        -- Display alphabetically.
        for text in sorted_keys(styles) do
            item:GetItems():Add(styles[text]);
        end
        L:SetContext(prevContext);
    elseif (itemName == "Caption") then
        local prevContext = L:SetContext("CaptionMenu");
        self:AddSettingsMenuItem(item, "Edit");
        self:AddSettingsMenuItem(item, "HideCaption");
        self:AddSettingsMenuItem(item, "Position");
        L:SetContext(prevContext);
    elseif (itemName == "Edit") then
        item:SetEnabled(not self.captionEditor);
        item.Click = function(sender, args)
            self:EditCaption();
        end
    elseif (itemName == "HideCaption") then
        item:SetChecked(self.settings.caption.hidden);
        item.Click = function(sender, args)
            self:SetCaptionHidden(not self.settings.caption.hidden);
        end
    elseif (itemName == "Position") then
        local prevContext = L:SetContext("PositionMenu");
        if ((self.settings.orientation == "Up") or (self.settings.orientation == "Down")) then
            self:AddSettingsMenuItem(item, "Top");
            self:AddSettingsMenuItem(item, "Bottom");
        else -- "Left" or "Right"
            self:AddSettingsMenuItem(item, "LeftSide");
            self:AddSettingsMenuItem(item, "RightSide");
        end
        L:SetContext(prevContext);
    elseif (string.find("|Top|Bottom|LeftSide|RightSide|", "|" .. itemName .. "|")) then
        local beginPosMap = { Left = "RightSide"; Right = "LeftSide"; Up = "Bottom"; Down = "Top" };
        local amBeginPos = (itemName == beginPosMap[self.settings.orientation]);
        local beginPosSelected = (self.settings.caption.position == "beginning");
        local checked = (amBeginPos and beginPosSelected) or (not amBeginPos and not beginPosSelected);
        local myOrientation = (amBeginPos and "beginning") or "end";
        item:SetChecked(checked);
        item:SetEnabled(not checked);
        item.Click = function(sender, args)
            self.settings.caption.position = myOrientation;
            self:SaveSettings(false);
            self:Redraw();
            self:MoveOntoScreen();
        end
    elseif (itemName == "Clone") then
        item.Click = function(sender, args)
            self.parent:CloneBar(self.objectID);
        end
    elseif (itemName == "Group") then
        self.parent.AddSettingsMenuItem(self.parent, item, "Root", true, self);
    elseif (itemName == "Delete") then
        item.Click = function(sender, args)
            self.parent:DeleteBar(self.objectID);
        end
    end
end

function Bar:ResizeBar(size)
    self.settings.visibleSlots = size;
    if (self.settings.cursorHomePosition > size) then
        self.settings.cursorHomePosition = size;
    end
    self:SaveSettings(false);
    self:Redraw();
end

function Bar:SetCursorStyle(style)
    if (style == nil) then
        style = self.settings.cursorStyle;
    end
    self.settings.cursorStyle = style;
    local resource = resources.Cursor[style];
 
    if (self.cursor) then
        self.cursor:SetParent(nil);
        self.cursor = nil;
    end
    self.cursor = Turbine.UI.Control();
    self.cursor:SetParent(self.slotContainer);
    self.cursor:SetMouseVisible(false);
    if (resource.ApplyColor) then
        self.cursor:SetBackColor(self.color);
    end
    local background = resources.Cursor[style].Background;
    if (background) then
        self.cursor:SetBackground(resource.Background);
        self.cursorWidth, self.cursorHeight = GetAssetSize(resource.Background);
        self.cursor:SetSize(self.cursorWidth, self.cursorHeight);
        -- for now, presume square shape
        self.cursorSize = math.ceil(self.cursorWidth / 2) * 2;
    else
        self.cursorSize = 36;
    end
    if (resource.BackColorBlendMode) then
        self.cursor:SetBackColorBlendMode(resource.BackColorBlendMode);
    end
    if (resource.BlendMode) then
        self.cursor:SetBlendMode(resource.BlendMode);
    end
    if (resource.ZOrder) then
        self.cursor:SetZOrder(2 + resource.ZOrder);
    end
--    self.cursor:SetVisible(not self:IsHidden());
    if (not resource.xOffset) then
        resource.xOffset = 0;
    end
    if (not resource.yOffset) then
        resource.yOffset = 0;
    end
    
    self.cursorMargin = math.ceil((self.cursorSize - self.settings.slotSize) / 2);
    self:SetCursorSlot(self.cursorSlot);
end

function Bar:SetLocked(state)
    self.settings.locked = state;
    self:SaveSettings(false);
end

function Bar:IsLocked()
    return self.settings.locked;
end

-- Scrolls the sequence such that the specified item appears under the cursor
function Bar:SetCursorSequenceItem(item)

    if (item > #self.sequenceItems) then
        item = 1;
    end
    
    local first = item - self.cursorSlot + 1;
    local last = first + self.settings.visibleSlots - 1;
--Turbine.Shell.WriteLine("visibleSlots = " .. tostring(self.settings.visibleSlots) .. "; first = " .. tostring(first) .. "; item = " .. tostring(item) .. "; last = " .. tostring(last) .. "; #sequenceItems = " .. tostring(#self.sequenceItems));

    for i = 1, #self.sequenceItems, 1 do
        local item = self.sequenceItems[i];
        
        if ((i < first) or (i > last)) then
            item:SetVisible(false);
        else
            local left, top = self:GetSlotCoords(i - first + 1);
            item:SetPosition(left - 1, top  - 1);
--Puts("Positioning item " .. i .. " at " .. tostring(left - 1) .. ", " .. tostring(top - 1));
--            item:SetVisible(not self:IsHidden());
            item:SetVisible(true);
        end
    end
    
    self.cursorSequenceItem = item;
end

function Bar:SequenceItemClick(clickedItem)
    local clickedSlot = self.cursorSlot + clickedItem - self.cursorSequenceItem;
--Turbine.Shell.WriteLine("clickedItem = " .. tostring(clickedItem) .. "; clickedSlot = " .. tostring(clickedSlot));
--Turbine.Shell.WriteLine("before: cursorSlot = " .. tostring(self.cursorSlot) .. "; cursorSequenceItem = " .. tostring(self.cursorSequenceItem));
    if ((self.settings.animation.duration ~= -1) and (not self.animationStopped)) then
        if (clickedSlot == self.cursorSlot) then
--Puts("Clicked cursor slot (" .. tostring(self.cursorSlot) .. ". Setting item to " .. tostring(self.cursorSequenceItem + 1));
            self:SetCursorSequenceItem(self.cursorSequenceItem + 1);
            if (self.settings.animation.duration > 0) then
                self:Animate(0);
            end
        elseif (clickedSlot > self.cursorSlot) then
            local cursorOffset = clickedSlot - self.cursorSlot;
            self:SetCursorSlot(clickedSlot);
            self:SetCursorSequenceItem(self.cursorSequenceItem + cursorOffset + 1);
            if (self.settings.animation.duration > 0) then
                self:Animate(0);
            end
        end

        -- Execute automatic actions
        local nextItem = self.sequenceItems[clickedItem + 1];
        if (nextItem and nextItem.info.automatic) then
            nextItem:MouseClick({["Button"] = Turbine.UI.MouseButton.Left});
        end
    end
--Turbine.Shell.WriteLine("after: cursorSlot = " .. tostring(self.cursorSlot) .. "; cursorSequenceItem = " .. tostring(self.cursorSequenceItem));
end

function Bar:Update()
    local timeDelta = Turbine.Engine.GetGameTime() - self.animationStartTime;
    if (timeDelta > 0) then
        self:Animate(timeDelta);
    end
end

function Bar:SetAnimationSpeed(duration)
    self.settings.animation.duration = duration;
    self:SaveSettings(false);
end

-- Achieves the scrolling effect.  Gets called by Bar:Update().
function Bar:Animate(timeDelta)
    local progress = timeDelta / self.settings.animation.duration;
    if (progress == 0) then
        self.animationStartTime = Turbine.Engine.GetGameTime();
        self.animationDistance = self.settings.slotSize + self.settings.slotSpacing;
        self:SetWantsUpdates(true);
    elseif (progress > 1) then
        progress = 1;
    end
    local displacement = math.floor(self.animationDistance * (1 - progress));
    if (self.settings.orientation == "Up") then
        self.slotContainer:SetTop(self.slotsTop - displacement);
    elseif (self.settings.orientation == "Down") then
        self.slotContainer:SetTop(self.slotsTop + displacement);
    elseif (self.settings.orientation == "Left") then
        self.slotContainer:SetLeft(self.slotsLeft - displacement);
    elseif (self.settings.orientation == "Right") then
        self.slotContainer:SetLeft(self.slotsLeft + displacement);
    end

    -- Make sure the item under the cursor is always clickable.  This hack is
    -- necessary because as the slot moves, no MouseEnter event occurs and
    -- hence no MouseClick events can be received.
    self.sequenceItems[self.cursorSequenceItem]:SetVisible(false);
    self.sequenceItems[self.cursorSequenceItem]:SetVisible(true);

    if (progress == 1) then
        self:SetWantsUpdates(false);
    end
end

function Bar:Destroy()
    if (self.sequenceEditor) then
        self.sequenceEditor:Close();
    end
    -- Unregister as event generator
    for eventName, generatorID in pairs(self.eventGeneratorIDs) do
        self.manager:RemoveEventGenerator(generatorID, eventName);
    end
    Node.Destroy(self);
end

function Bar:GetVisibleLeft()
    local left = self:GetSlotsScreenCoords();
    return left;
end

function Bar:SetVisibleLeft(left)
    self:OffsetPosition(left - self:GetVisibleLeft(), 0);
end

function Bar:GetVisibleTop()
    local _, top = self:GetSlotsScreenCoords();
    return top;
end

function Bar:SetVisibleTop(top)
    self:OffsetPosition(0, top - self:GetVisibleTop());
end

function Bar:GetVisibleWidth()
    local _, _, width = self:GetSlotsScreenCoords();
    return width;
end

function Bar:GetVisibleHeight()
    local _, _, _, height = self:GetSlotsScreenCoords();
    return height;
end

function Bar:GetSlotCoords(slot)
    local slotSize = self.settings.slotSize + self.settings.slotSpacing;
    local offset = self.cursorMargin + (slot - 1) * slotSize;
    local left, top;
    if (self.settings.orientation == "Up") then
        left = self.cursorMargin;
        top = self.slotContainer:GetHeight() - slotSize - offset;
    elseif (self.settings.orientation == "Down") then
        left = self.cursorMargin;
        top = offset;
    elseif (self.settings.orientation == "Left") then
        left = self.slotContainer:GetWidth() - slotSize - offset;
        top = self.cursorMargin;
    else -- "Right"
        left = offset;
        top = self.cursorMargin;
    end
    return left, top
end

function Bar:Find()
    self:MoveOntoScreen();
    self:Flash();
end

function Bar:MoveOntoScreen()
    local screenWidth, screenHeight = Turbine.UI.Display:GetWidth(), Turbine.UI.Display:GetHeight();
    local left, top = self:PointToScreen(self.caption:GetLeft(), self.caption:GetTop());
    local right, bottom = left + self.caption:GetWidth(), top + self.caption:GetHeight();
    local deltaX, deltaY = 0, 0;
    if (left < 0) then
        deltaX = -left;
    elseif (right > screenWidth) then
        deltaX = screenWidth - right;
    end
    if (top < 0) then
        deltaY = -top;
    elseif (bottom > screenHeight) then
        deltaY = screenHeight - bottom;
    end
    self:OffsetPosition(deltaX, deltaY);
    self:SnapToGrid();
end

function Bar:Flash()
    self:Activate();
    self.caption.startTime = Turbine.Engine.GetGameTime();
    self.caption.label:SetVisible(true);
    if (not self.marquee) then
        self.marquee = Thurallor.UI.Marquee();
        self.marquee:SetParent(self);
        self.marquee:SetSize(self:GetSize());
    end
    self.caption.Update = function(caption, args)
        local timeDelta = Turbine.Engine.GetGameTime() - caption.startTime;
        if (timeDelta > 3) then
            caption.label:SetForeColor(self.color);
            caption.label:SetVisible(not self.settings.caption.hidden);
            caption:SetWantsUpdates(false);
            self.marquee:SetParent(nil);
            self.marquee = nil;
        else
            local progress = (math.sin(timeDelta * 15) + 1) / 2; --math.modf(timeDelta / 0.3);
            caption.label:SetForeColor(Turbine.UI.Color(progress, 1, 1, 0));
        end
    end
    self.caption:SetWantsUpdates(true);
end

Go to most recent revision | Compare with Previous | Blame


All times are GMT -5. The time now is 04:42 AM.


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