From 7242720c7296abf5c9556d966606b381fbf3a747 Mon Sep 17 00:00:00 2001 From: shagu Date: Sat, 7 Jul 2018 04:04:25 +0200 Subject: [PATCH] addons: introduce addon profiles based on the work of @dein0s --- modules/addons.lua | 177 ++++++++++++++++++++++++++++++++++++++++----- pfUI.lua | 1 + pfUI.toc | 2 +- 3 files changed, 161 insertions(+), 19 deletions(-) diff --git a/modules/addons.lua b/modules/addons.lua index c9238cdf..dda9d23e 100644 --- a/modules/addons.lua +++ b/modules/addons.lua @@ -1,22 +1,38 @@ pfUI:RegisterModule("addons", function () + -- add main menu button + local pfUIAddonButton = CreateFrame("Button", "GameMenuButtonPFUIAddOns", GameMenuFrame, "GameMenuButtonTemplate") + pfUIAddonButton:SetPoint("TOP", 0, -32) + pfUIAddonButton:SetText(T["AddOns"]) + pfUIAddonButton:SetScript("OnClick", function() + pfUI.addons:Show() + HideUIPanel(GameMenuFrame) + end) + SkinButton(pfUIAddonButton) + + local point, relativeTo, relativePoint, xOffset, yOffset = GameMenuButtonOptions:GetPoint() + GameMenuButtonOptions:SetPoint(point, relativeTo, relativePoint, xOffset, yOffset - 22) + GameMenuFrame:SetHeight(GameMenuFrame:GetHeight()+22) + + -- addon window pfUI.addons = CreateFrame("Frame", "pfAddons", UIParent) pfUI.addons:SetFrameStrata("DIALOG") - pfUI.addons:SetHeight(450) + pfUI.addons:SetHeight(490) pfUI.addons:SetWidth(420) pfUI.addons:SetPoint("CENTER", 0,0) pfUI.addons:EnableMouseWheel(1) pfUI.addons:SetMovable(true) pfUI.addons:EnableMouse(true) - pfUI.addons:SetScript("OnMouseDown", function() pfUI.addons:StartMoving() end) - pfUI.addons:SetScript("OnMouseUp", function() pfUI.addons:StopMovingOrSizing() end) + pfUI.addons:SetScript("OnMouseDown", function() this:StartMoving() end) + pfUI.addons:SetScript("OnMouseUp", function() this:StopMovingOrSizing() end) pfUI.addons:Hide() CreateBackdrop(pfUI.addons, nil, true, .75) pfUI.addons:SetScript("OnHide", function() - if pfUI.addons.hasChanged then + UIDropDownMenu_ClearAll(this.profile.dropdown) + if this.hasChanged then pfUI.gui:Reload() - pfUI.addons.hasChanged = nil + this.hasChanged = nil end end) @@ -53,6 +69,125 @@ pfUI:RegisterModule("addons", function () pfUI.addons:Hide() end) + -- addons profile + local function GetSelectedAddonsList() + local selected_addons = {} + for i, frame in ipairs(pfUI.addons.list) do + if frame.input:GetChecked() then + table.insert(selected_addons, frame.aname) + end + end + return selected_addons + end + + local function SetAddonProfile(profile) + UIDropDownMenu_SetText(profile, pfUI.addons.profile.dropdown) + + -- create new profile if not existing + if not pfUI_addon_profiles[profile] then + pfUI_addon_profiles[profile] = GetSelectedAddonsList() + end + + -- load profile + local selected_list = pfUI_addon_profiles[profile] + for i=1, GetNumAddOns() do + local active = false + local f = pfUI.addons.list[i] + + for i_i, n in ipairs(selected_list) do + if f.aname == n then + active = true + break + end + end + + f.input:SetChecked(active) + end + end + + -- addon profiles + pfUI.addons.profile = CreateFrame("Frame", "pfAddonListProfile", pfUI.addons) + pfUI.addons.profile:SetScript("OnShow", function() + pfUI_addon_profiles["Current"] = GetSelectedAddonsList() + UIDropDownMenu_SetText("Current", pfUI.addons.profile.dropdown) + end) + + pfUI.addons.profile:SetPoint("TOP", pfUI.addons, "TOP", 0, -34) + pfUI.addons.profile:SetHeight(37) + pfUI.addons.profile:SetWidth(410) + CreateBackdrop(pfUI.addons.profile, nil, true) + + -- addon profile: title + pfUI.addons.profile.caption = pfUI.addons.profile:CreateFontString("Status", "LOW", "GameFontNormal") + pfUI.addons.profile.caption:SetFontObject(GameFontWhite) + pfUI.addons.profile.caption:SetFont(pfUI.font_default, C.global.font_size, "OUTLINE") + pfUI.addons.profile.caption:SetPoint("TOPLEFT", 10, -12) + pfUI.addons.profile.caption:SetText(T["Addon Profile"]) + + -- addon profile: dropdown + pfUI.addons.profile.dropdown = CreateFrame("Frame", "pfAddonListProfileList", pfUI.addons.profile, "UIDropDownMenuTemplate") + pfUI.addons.profile.dropdown:ClearAllPoints() + pfUI.addons.profile.dropdown:SetPoint("TOPRIGHT", -51, -5) + SkinDropDown(pfUI.addons.profile.dropdown) + UIDropDownMenu_SetWidth(220, pfUI.addons.profile.dropdown) + UIDropDownMenu_SetButtonWidth(220, pfUI.addons.profile.dropdown) + UIDropDownMenu_Initialize(pfUI.addons.profile.dropdown, function() + local info = {} + for name, list in pairs(pfUI_addon_profiles) do + info.text = name + info.checked = false + info.func = function() + UIDropDownMenu_SetSelectedID(pfUI.addons.profile.dropdown, this:GetID(), 0) + SetAddonProfile(this:GetText()) + end + UIDropDownMenu_AddButton(info) + end + end) + + -- addon profile: create + pfUI.addons.profile.create = CreateFrame("Button", "pfAddonListProfileCreate", pfUI.addons.profile, "UIPanelButtonTemplate") + CreateBackdrop(pfUI.addons.profile.create, nil, true) + SkinButton(pfUI.addons.profile.create) + pfUI.addons.profile.create:SetWidth(25) + pfUI.addons.profile.create:SetHeight(25) + pfUI.addons.profile.create:SetPoint("LEFT", pfUI.addons.profile.dropdown.backdrop, "RIGHT", 35, 0) + pfUI.addons.profile.create:GetFontString():SetPoint("CENTER", 1, 0) + pfUI.addons.profile.create:SetText("+") + pfUI.addons.profile.create:SetTextColor(.5,1,.5,1) + pfUI.addons.profile.create:SetScript("OnClick", function() + CreateQuestionDialog(T["Please enter a name for the new profile."], + function() + local profile_name = this:GetParent().input:GetText() + local bad = string.gsub(profile_name,"([%w%s]+)","") + if bad ~= "" then + message("\"" .. bad .. "\" " .. T["is not allowed in profile name"]) + else + SetAddonProfile(profile_name) + end + end, false, true) + end) + + -- addon profile: delete + pfUI.addons.profile.delete = CreateFrame("Button", "pfAddonListProfileDelete", pfUI.addons.profile, "UIPanelButtonTemplate") + CreateBackdrop(pfUI.addons.profile.delete, nil, true) + SkinButton(pfUI.addons.profile.delete) + pfUI.addons.profile.delete:SetWidth(25) + pfUI.addons.profile.delete:SetHeight(25) + pfUI.addons.profile.delete:SetPoint("LEFT", pfUI.addons.profile.dropdown.backdrop, "RIGHT", 5, 0) + pfUI.addons.profile.delete:GetFontString():SetPoint("CENTER", 0, 0) + pfUI.addons.profile.delete:SetText("-") + pfUI.addons.profile.delete:SetTextColor(1,.5,.5,1) + pfUI.addons.profile.delete:SetScript("OnClick", function() + local profile_name = UIDropDownMenu_GetText(pfUI.addons.profile.dropdown) + if profile_name then + CreateQuestionDialog(T["Delete profile"] .. " '|cff33ffcc" .. profile_name .. "|r'?", function() + pfUI_addon_profiles[profile_name] = nil + SetAddonProfile("Current") + end) + end + end) + + -- addon list: scroll frame pfUI.addons.scroll = CreateScrollFrame("pfAddonListScroll", pfUI.addons) pfUI.addons.scroll:SetHeight(400) pfUI.addons.scroll:SetWidth(400) @@ -64,10 +199,11 @@ pfUI:RegisterModule("addons", function () pfUI.addons.scroll.backdrop:SetPoint("BOTTOMRIGHT", pfUI.addons.scroll, "BOTTOMRIGHT", 5, -5) CreateBackdrop(pfUI.addons.scroll.backdrop, nil, true) + -- addon list: scroll parent pfUI.addons.list = CreateScrollChild("pfAddonList", pfUI.addons.scroll) pfUI.addons.list:RegisterEvent("ADDON_LOADED") pfUI.addons.list:RegisterEvent("PLAYER_ENTERING_WORLD") - pfUI.addons.list:SetHeight(GetNumAddOns() * 25) + pfUI.addons.list:SetHeight(GetNumAddOns() * 25 + 26) pfUI.addons.list:SetScript("OnEvent", function() for i=1, GetNumAddOns() do @@ -77,6 +213,7 @@ pfUI:RegisterModule("addons", function () if not pfUI.addons.list[i] then pfUI.addons.list[i] = CreateFrame("Button", nil, pfUI.addons.list) local frame = pfUI.addons.list[i] + frame.aname = aname frame:SetWidth(350) frame:SetHeight(25) @@ -125,6 +262,8 @@ pfUI:RegisterModule("addons", function () DisableAddOn(this:GetID()) end pfUI.addons.hasChanged = true + local profile_name = UIDropDownMenu_GetText(pfUI.addons.profile.dropdown) or "Current" + pfUI_addon_profiles[profile_name] = GetSelectedAddonsList() end) end @@ -137,18 +276,20 @@ pfUI:RegisterModule("addons", function () end end) - -- add main menu button - local pfUIAddonButton = CreateFrame("Button", "GameMenuButtonPFUIAddOns", GameMenuFrame, "GameMenuButtonTemplate") - pfUIAddonButton:SetPoint("TOP", 0, -32) - pfUIAddonButton:SetText(T["AddOns"]) - pfUIAddonButton:SetScript("OnClick", function() - pfUI.addons:Show() - HideUIPanel(GameMenuFrame) - end) - SkinButton(pfUIAddonButton) + -- show only frames in visible range + function pfUI.addons.scroll:ShowVisibleRange() + local top_index, bottom_index, range + range = floor(pfUI.addons.scroll:GetHeight() / 25) + top_index = floor(pfUI.addons.scroll:GetVerticalScroll() / 25) > 0 and floor(pfUI.addons.scroll:GetVerticalScroll() / 25) or 1 + bottom_index = top_index + range + for i=1, GetNumAddOns() do + if i >= top_index and i <= bottom_index then + pfUI.addons.list[i]:Show() + else + pfUI.addons.list[i]:Hide() + end + end + end - local point, relativeTo, relativePoint, xOffset, yOffset = GameMenuButtonOptions:GetPoint() - GameMenuButtonOptions:SetPoint(point, relativeTo, relativePoint, xOffset, yOffset - 22) - GameMenuFrame:SetHeight(GameMenuFrame:GetHeight()+22) end) diff --git a/pfUI.lua b/pfUI.lua index 46d5623e..3928979c 100644 --- a/pfUI.lua +++ b/pfUI.lua @@ -28,6 +28,7 @@ pfUI_playerDB = {} pfUI_config = {} pfUI_init = {} pfUI_profiles = {} +pfUI_addon_profiles = {} pfUI_gold = {} -- localization diff --git a/pfUI.toc b/pfUI.toc index 1cef046a..b0d28840 100644 --- a/pfUI.toc +++ b/pfUI.toc @@ -3,7 +3,7 @@ ## Author: Shagu ## Notes: A complete user interface replacement. ## Version: 3.11.0 -## SavedVariables: pfUI_profiles, pfUI_gold +## SavedVariables: pfUI_profiles, pfUI_gold, pfUI_addon_profiles ## SavedVariablesPerCharacter: pfUI_config, pfUI_init, pfUI_playerDB # main