--[[ Author: Jarth Filename: MementoBar.lua ]] -- ------------------------------------------------------------------------------------------------- -- Libraries -- ------------------------------------------------------------------------------------------------- local LAM2 = LibStub:GetLibrary("LibAddonMenu-2.0") ------------------------------------------------------------------------------------------------- -- VARIABLE -- ------------------------------------------------------------------------------------------------- local ADDON_NAME = "MementoBar" local ADDON_VERSION = 1.0 local ADDON_MINOR_VERSION = 0 local ADDON_AUTHOR = "Jarth" local ADDON_WEBSITE = "" local MementoBar = {} MementoBar.wm = GetWindowManager() MementoBar.Name = ADDON_NAME MementoBar.Buttons = {} MementoBar.Fragment = nil MementoBar.Mementos = {} MementoBar.MementosOrdered = {} MementoBar.Default = { Left = CENTER, Top = CENTER, CenterColor = {r = 0.88, g = 0.88, b = 0.88, a = 0.4}, -- {red, green, blue, alpha} EdgeColor = {r = 0.57, g = 0.57, b = 0.57, a = 0.6}, -- {red, green, blue, alpha} SelectedMementos = {}, UseAccountSettings = true, Horizontal = true, BarDepth = 1, HideBarInMenu = true, ShowMoveAssitance = false } MementoBar.CountDown = { Event = "Cooldown", Tick = 100, StartTime = 0, CollectibleId = nil } MementoBar.Global = { LblOffset = 10, LblHeight = 60, LblWidth = 60 } ------------------------------------------------------------------------------------------------- -- FUNCTIONS -- ------------------------------------------------------------------------------------------------- function MementoBar.OnAddOnLoaded(_, addonName) if addonName == MementoBar.Name then MementoBar:Initialize() end end function MementoBar:Initialize() -- Load saved values.. MementoBar.Saved = ZO_SavedVars:New("MementoBar_Character", ADDON_VERSION, nil, MementoBar.Default) if MementoBar.Saved.UseAccountSettings then MementoBar.Saved = ZO_SavedVars:NewAccountWide("MementoBar_Account", ADDON_VERSION, nil, MementoBar.Default) end MementoBar_Frame:SetHandler("OnMoveStop", MementoBar.OnMoveStop) -- Create menu variables. MementoBar:CreateSettingsWindow() MementoBar:FrameBackdropColor() -- Must be run after :CreateSettingsWindow, as it populates the data MementoBar:RestorePanel() EVENT_MANAGER:UnregisterForEvent(MementoBar.Name, EVENT_ADD_ON_LOADED) end function MementoBar.OnMoveStop() MementoBar.Saved.Top = MementoBar_Frame:GetTop() MementoBar.Saved.Left = MementoBar_Frame:GetLeft() end ------------------------------------------------------------------------------------------------- -- PRIVATE FUNCTIONS -- ------------------------------------------------------------------------------------------------- function MementoBar:OnClicked(collectibleId) if collectibleId > 0 and IsCollectibleUnlocked(collectibleId) then local cooldownRemaining, cooldownDuration = GetCollectibleCooldownAndDuration(collectibleId) if cooldownRemaining == 0 then MementoBar:Activate(collectibleId) end end end function MementoBar:InitializeButtons() local index = 1 for _, _value in ipairs(MementoBar.MementosOrdered) do local collectibleId = _value.CollectibleId local key = _value.Name local left, top = MementoBar:GetButtonPosition(index) if MementoBar.Saved.SelectedMementos[collectibleId] and IsCollectibleUnlocked(collectibleId) then if MementoBar.Buttons[collectibleId] == nil then MementoBar.Buttons[collectibleId] = WINDOW_MANAGER:CreateControlFromVirtual("MementoBar_Button", MementoBar_FrameBackdrop, "MementoBar_Button", collectibleId) MementoBar.Buttons[collectibleId]:SetId(collectibleId) end MementoBar:SetupButton(collectibleId, MementoBar.Buttons[collectibleId]) MementoBar.Buttons[collectibleId]:ClearAnchors() MementoBar.Buttons[collectibleId]:SetAnchor(TOPLEFT, MementoBar_FrameBackdrop, TOPLEFT, left, top) MementoBar.Buttons[collectibleId]:SetHidden(false) index = index + 1 elseif MementoBar.Buttons[collectibleId] ~= nil then MementoBar.Buttons[collectibleId]:SetHidden(true) end end end function MementoBar:GetButtonPosition(index) local left = MementoBar.Global.LblOffset + MementoBar.Global.LblWidth * ((index - 1) % MementoBar.Saved.BarDepth) local top = MementoBar.Global.LblOffset + MementoBar.Global.LblHeight * (math.floor((index - 1) / MementoBar.Saved.BarDepth)) if MementoBar.Saved.Horizontal then left = MementoBar.Global.LblOffset + MementoBar.Global.LblHeight * (math.floor((index - 1) / MementoBar.Saved.BarDepth)) top = MementoBar.Global.LblOffset + MementoBar.Global.LblWidth * ((index - 1) % MementoBar.Saved.BarDepth) end return left, top end function MementoBar:SetFragmentBehaviour(menuOpen) if MementoBar.Fragment == nil then MementoBar.Fragment = ZO_HUDFadeSceneFragment:New(MementoBar_Frame) end if MementoBar.Saved.HideBarInMenu then MementoBar:AddControlToFrame(menuOpen) else MementoBar:RemoveControlToFrame(menuOpen) end end function MementoBar:AddControlToFrame(menuOpen) if menuOpen then MementoBar_Frame:SetHidden(true) end SCENE_MANAGER:GetScene("hud"):AddFragment(MementoBar.Fragment) SCENE_MANAGER:GetScene("hudui"):AddFragment(MementoBar.Fragment) end function MementoBar:RemoveControlToFrame(menuOpen) if menuOpen then MementoBar_Frame:SetHidden(false) end SCENE_MANAGER:GetScene("hud"):RemoveFragment(MementoBar.Fragment) SCENE_MANAGER:GetScene("hudui"):RemoveFragment(MementoBar.Fragment) end function MementoBar:SetButtonFrameWidth() local mbCount = 0 for _ in pairs(MementoBar.Saved.SelectedMementos) do mbCount = mbCount + 1 end local mbDepth = math.ceil(mbCount / MementoBar.Saved.BarDepth) local mbHeight = MementoBar.Global.LblHeight * (MementoBar.Saved.Horizontal and MementoBar.Saved.BarDepth or mbDepth) local mbWidth = MementoBar.Global.LblWidth * (MementoBar.Saved.Horizontal and mbDepth or MementoBar.Saved.BarDepth) MementoBar_Frame:SetHidden(mbCount == 0) MementoBar_FrameBackdrop:SetHidden(mbCount == 0) MementoBar_Frame:SetHeight(mbHeight) MementoBar_FrameBackdrop:SetHeight(mbHeight) MementoBar_Frame:SetWidth(mbWidth) MementoBar_FrameBackdrop:SetWidth(mbWidth) end function MementoBar:FrameBackdropColor(centerColor, edgeColor) if not centerColor then centerColor = MementoBar.Saved.CenterColor end if not edgeColor then edgeColor = MementoBar.Saved.EdgeColor end MementoBar_FrameBackdrop:SetCenterColor(centerColor.r, centerColor.g, centerColor.b, centerColor.a) MementoBar_FrameBackdrop:SetEdgeColor(edgeColor.r, edgeColor.g, edgeColor.b, edgeColor.a) end function MementoBar:SetupButton(collectibleId, Buttons) local MementoBar_ButtonTexture = MementoBar.wm:GetControlByName("MementoBar_Button" .. collectibleId .. "Texture") if MementoBar_ButtonTexture and MementoBar.Mementos[collectibleId].CollectibleIcon then MementoBar_ButtonTexture:SetTexture(MementoBar.Mementos[collectibleId].CollectibleIcon) Buttons:SetHandler( "OnClicked", function(self) MementoBar:OnClicked(Buttons:GetId()) end ) end end function MementoBar:RestorePosition() local Top = MementoBar.Saved.Top local Left = MementoBar.Saved.Left MementoBar_Frame:ClearAnchors() MementoBar_Frame:SetAnchor(TOPLEFT, GuiRoot, TOPLEFT, Left, Top) end function MementoBar:RestorePanel() MementoBar:SetFragmentBehaviour() MementoBar:SetButtonFrameWidth() MementoBar:FrameBackdropColor() MementoBar:InitializeButtons() MementoBar:RestorePosition() MementoBar:ActivateMoveHandlers() end function MementoBar:GetVersion(showMinor) if showMinor == false or ADDON_MINOR_VERSION == nil then return tostring(ADDON_VERSION) end return tostring(ADDON_VERSION) .. "." .. tostring(ADDON_MINOR_VERSION) end function MementoBar:Activate(collectibleId) if collectibleId and IsCollectibleUsable(collectibleId) then MementoBar.CountDown.StartTime = GetTimeStamp() MementoBar.CountDown.CollectibleId = collectibleId EVENT_MANAGER:UnregisterForUpdate(MementoBar.Name .. MementoBar.CountDown.Event) UseCollectible(collectibleId) EVENT_MANAGER:RegisterForUpdate(MementoBar.Name .. MementoBar.CountDown.Event, MementoBar.CountDown.Tick, MementoBar.Update) end end function MementoBar.Update() local cooldownRemaining, cooldownDuration = GetCollectibleCooldownAndDuration(MementoBar.CountDown.CollectibleId) if cooldownRemaining > 0 then MementoBar:UpdateLabel(cooldownRemaining) elseif (cooldownDuration > 0 and (GetTimeStamp() < (MementoBar.CountDown.StartTime + math.floor(cooldownDuration / 1000)))) then --Should not be relevant, it should always have a cooldown remaining and duration. MementoBar:UpdateLabel(cooldownDuration) else MementoBar:UpdateLabel("") EVENT_MANAGER:UnregisterForUpdate(MementoBar.Name .. MementoBar.CountDown.Event) end end function MementoBar:UpdateLabel(remaining) if type(remaining) == "number" and remaining > 0 then remaining = string.format("%.1fs", (math.floor(remaining / 100) / 10)) end for collectibleId in pairs(MementoBar.Saved.SelectedMementos) do local MementoBar_ButtonTimer = MementoBar.wm:GetControlByName("MementoBar_Button" .. collectibleId .. "Timer") if MementoBar_ButtonTimer and MementoBar.Mementos[collectibleId] then MementoBar_ButtonTimer:SetText(remaining) end end end function MementoBar:ActivateMoveHandlers() if not MementoBar_Frame.LabelTopLeft and MementoBar.Saved.ShowMoveAssitance then MementoBar_Frame.LabelTopLeft = MementoBar.wm:CreateControl(nil, MementoBar_Frame, CT_LABEL) MementoBar_Frame.LabelTopLeft:SetDrawLevel(3) MementoBar_Frame.LabelTopLeft:SetAnchorFill(MementoBar_Frame) MementoBar_Frame.LabelTopLeft:SetFont("ZoFontWinH5") MementoBar_Frame.LabelTopLeft:SetHorizontalAlignment(0) MementoBar_Frame.LabelTopLeft:SetVerticalAlignment(0) MementoBar_Frame["UpdateText"] = MementoBar.UpdateText MementoBar_Frame:SetHandler("OnMouseEnter", MementoBar.OnMouseEnter) MementoBar_Frame:SetHandler("OnMouseDown", MementoBar.OnMouseDown) MementoBar_Frame:SetHandler("OnMouseUp", MementoBar.OnMouseUp) MementoBar_Frame:SetHandler("OnMouseExit", MementoBar.OnMouseExit) elseif MementoBar_Frame.LabelTopLeft and not MementoBar.Saved.ShowMoveAssitance then MementoBar_Frame.LabelTopLeft = nil MementoBar_Frame["UpdateText"] = nil MementoBar_Frame:SetHandler("OnMouseEnter", nil) MementoBar_Frame:SetHandler("OnMouseDown", nil) MementoBar_Frame:SetHandler("OnMouseUp", nil) MementoBar_Frame:SetHandler("OnMouseExit", nil) end end ------------------------------------------------------------------------------------------------- -- FUNCTIONS -- -- Move handlers -- ------------------------------------------------------------------------------------------------- function MementoBar.OnMouseEnter(frame) frame.UpdateText(frame, true) end function MementoBar.OnMouseDown(frame) frame:SetHandler("OnUpdate", MementoBar.OnMouseEnter) end function MementoBar.OnMouseExit(frame) frame.UpdateText(frame, false) end function MementoBar.OnMouseUp(frame) frame:SetHandler("OnUpdate", nil) end function MementoBar.UpdateText(frame, position) if position then frame.labelTextTopLeft = frame:GetLeft() .. "," .. frame:GetTop() else frame.labelTextTopLeft = "" end frame.LabelTopLeft:SetText(frame.labelTextTopLeft) end ------------------------------------------------------------------------------------------------- -- Menu Functions -- ------------------------------------------------------------------------------------------------- function MementoBar:CreateSettingsWindow() local mementTopLevelIndex = 7 local catName, catNumSubCat, catNumCollectibles, catNumUnlocked, catTotal, catHidesLocked = GetCollectibleCategoryInfo(mementTopLevelIndex) local panelData = { type = "panel", name = "Memento Bar", displayName = "Memento Bar", author = ADDON_AUTHOR, version = MementoBar:GetVersion(true), slashCommand = "/mb", registerForRefresh = true, registerForDefaults = true } local cntrlOptionsPanel = LAM2:RegisterAddonPanel(ADDON_NAME, panelData) local optionsData = { [1] = { type = "header", name = "Memento Bar Settings" }, [2] = { type = "description", text = "Here you can adjust the memento bar settings. Including what icons to show, and color of the bar." }, [3] = { type = "checkbox", name = "Use account settings", tooltip = "When ON the account settings will be used. When OFF character settings will be used.", default = MementoBar.Default.UseAccountSettings, getFunc = function() return MementoBar.Saved.UseAccountSettings end, setFunc = function(newValue) --Apply value to existing variable MementoBar.Saved.UseAccountSettings = newValue if newValue then MementoBar.Saved = ZO_SavedVars:NewAccountWide("MementoBar_Account", ADDON_VERSION, nil, MementoBar.Default) else MementoBar.Saved = ZO_SavedVars:New("MementoBar_Character", ADDON_VERSION, nil, MementoBar.Default) end --Apply value to new variable MementoBar.Saved.UseAccountSettings = newValue MementoBar:RestorePanel() end }, [4] = { type = "checkbox", name = "Bar orientation horizontal", tooltip = "When ON the bar will orientate horizontally.", default = MementoBar.Default.Horizontal, getFunc = function() return MementoBar.Saved.Horizontal end, setFunc = function(newValue) MementoBar.Saved.Horizontal = newValue MementoBar:RestorePanel() end }, [5] = { type = "checkbox", name = "Hide bar in menu", tooltip = "When ON the bar will hide when a menu is opened.", default = MementoBar.Default.HideBarInMenu, getFunc = function() return MementoBar.Saved.HideBarInMenu end, setFunc = function(newValue) MementoBar.Saved.HideBarInMenu = newValue MementoBar:SetFragmentBehaviour(true) end }, [6] = { type = "checkbox", name = "Show bar position", tooltip = "When ON the bar will show the X,Y position of the top left corner, when the mouse enters and drags the panel around.", default = function() return MementoBar.Default.ShowMoveAssitance end, getFunc = function() return MementoBar.Saved.ShowMoveAssitance end, setFunc = function(newValue) MementoBar.Saved.ShowMoveAssitance = newValue MementoBar:RestorePanel() end }, [7] = { type = "submenu", name = "Colors", tooltip = "Allows you to change colors.", controls = { [1] = { type = "description", text = "Here you can adjust the colors." }, [2] = { type = "colorpicker", name = "Backdrop Color", default = function() return MementoBar.Default.CenterColor end, tooltip = "Changes the color of the background.", getFunc = function() return MementoBar.Saved.CenterColor.r, MementoBar.Saved.CenterColor.g, MementoBar.Saved.CenterColor.b, MementoBar.Saved.CenterColor.a end, setFunc = function(r, g, b, a) MementoBar.Saved.CenterColor = {r = r, g = g, b = b, a = a} MementoBar:RestorePanel() end, width = "half" }, [3] = { type = "colorpicker", name = "Frame Color", default = function() return MementoBar.Default.EdgeColor end, tooltip = "Changes the color of the frame.", getFunc = function() return MementoBar.Saved.EdgeColor.r, MementoBar.Saved.EdgeColor.g, MementoBar.Saved.EdgeColor.b, MementoBar.Saved.EdgeColor.a end, setFunc = function(r, g, b, a) MementoBar.Saved.EdgeColor = {r = r, g = g, b = b, a = a} MementoBar:RestorePanel() end, width = "half" } } }, [8] = { type = "submenu", name = catName .. " (Total: " .. catTotal .. " Unlocked: " .. catNumUnlocked .. ")", tooltip = "Choose what memento's you want on the bar.", controls = { [1] = { type = "slider", name = "Choose bar depth (number of rows/columns)", -- or string id or function returning a string default = MementoBar.Default.BarDepth, getFunc = function() return MementoBar.Saved.BarDepth end, setFunc = function(value) MementoBar.Saved.BarDepth = value MementoBar:RestorePanel() end, min = 1, max = catNumUnlocked }, [2] = { type = "description", text = "Choose what memento's you want enabled." } } } } for i = 1, catTotal do local index = i + 1 local collectibleId = GetCollectibleId(mementTopLevelIndex, nil, i) local collectibleName, collectibleDescription, collectibleIcon, collectibleLockedIcon, collectibleUnlocked, _, _, _, collectibleHint, _ = GetCollectibleInfo(collectibleId) MementoBar.Mementos[collectibleId] = { CollectibleId = collectibleId, CollectibleIcon = collectibleIcon } MementoBar.MementosOrdered[i] = MementoBar.Mementos[collectibleId] local line = { type = "checkbox", name = "Show " .. collectibleName, tooltip = string.format("Description: %s \nHint: %s", collectibleDescription, collectibleHint), --tooltip = "When ON the vendor button will be visible. When OFF the vendor button will not be visible (disabled if locked for the account).", default = false, disabled = not collectibleUnlocked, getFunc = function() if not MementoBar.Saved.SelectedMementos[collectibleId] then return false else return true end end, setFunc = function(newValue) if newValue == true then MementoBar.Saved.SelectedMementos[collectibleId] = collectibleId else MementoBar.Saved.SelectedMementos[collectibleId] = nil end MementoBar:RestorePanel() end } table.insert(optionsData[8].controls, index, line) end LAM2:RegisterOptionControls(ADDON_NAME, optionsData) end ------------------------------------------------------------------------------------------------- -- Initialize Functions -- ------------------------------------------------------------------------------------------------- EVENT_MANAGER:RegisterForEvent(MementoBar.Name, EVENT_ADD_ON_LOADED, MementoBar.OnAddOnLoaded)