205 lines
6.7 KiB
Lua
205 lines
6.7 KiB
Lua
-- pfUI-RaidProfile
|
|
-- Automatically switches the pfUI "Addon Profile" (Escape -> AddOns -> Addon Profile)
|
|
-- when you join or leave a raid. Lua 5.0 / 1.12 client compatible.
|
|
|
|
pfUI:RegisterModule("raidprofile", function ()
|
|
local G = getfenv(0)
|
|
local ADDON = "pfUI-RaidProfile"
|
|
local DELAY = 5 -- seconds to wait before switching (lets the roster settle)
|
|
|
|
local function Print(msg)
|
|
DEFAULT_CHAT_FRAME:AddMessage("|cff33ffccpfUI Raid Profile:|r " .. msg)
|
|
end
|
|
|
|
-- own saved variables, independent of pfUI's config profiles
|
|
local function DB()
|
|
local db = G.pfUI_RaidProfileDB
|
|
if type(db) ~= "table" then
|
|
db = {}
|
|
G.pfUI_RaidProfileDB = db
|
|
end
|
|
if db.enabled == nil then db.enabled = "1" end
|
|
if db.raid == nil then db.raid = "" end
|
|
if db.normal == nil then db.normal = "" end
|
|
return db
|
|
end
|
|
|
|
-- pfUI stores each addon profile as a list of enabled addon names in pfUI_addon_profiles
|
|
local function Exists(name)
|
|
return name and name ~= "" and name ~= T["Current"]
|
|
and G.pfUI_addon_profiles and G.pfUI_addon_profiles[name] ~= nil
|
|
end
|
|
|
|
local function ProfileNames()
|
|
local names = {}
|
|
if G.pfUI_addon_profiles then
|
|
for name in pairs(G.pfUI_addon_profiles) do
|
|
if name ~= T["Current"] then table.insert(names, name) end
|
|
end
|
|
end
|
|
table.sort(names)
|
|
return names
|
|
end
|
|
|
|
local function InRaid()
|
|
return GetNumRaidMembers() > 0
|
|
end
|
|
|
|
-- profile that should be active right now (nil = nothing configured)
|
|
local function Target()
|
|
local db = DB()
|
|
local name = InRaid() and db.raid or db.normal
|
|
if Exists(name) then return name end
|
|
return nil
|
|
end
|
|
|
|
local reloadedAt = nil -- set when ReloadUI() was called; if we are still running later, it was blocked
|
|
|
|
-- ReloadUI() may be refused when not triggered by a click, so fall back to a
|
|
-- confirmation dialog: the click on "Yes" is a real user action.
|
|
local function AskReload(name)
|
|
local text = "Addon profile '|cff33ffcc" .. name .. "|r' is applied, but the automatic reload was blocked.\nReload the UI now to finish switching?"
|
|
if CreateQuestionDialog then
|
|
CreateQuestionDialog(text, function() ReloadUI() end)
|
|
else
|
|
Print("addon profile '" .. name .. "' applied, type /reload to finish switching.")
|
|
end
|
|
end
|
|
|
|
local function Apply(name)
|
|
local want = { pfUI = true }
|
|
want[ADDON] = true -- never disable ourselves, or we could not switch back
|
|
for _, n in ipairs(G.pfUI_addon_profiles[name]) do want[n] = true end
|
|
|
|
for i = 1, GetNumAddOns() do
|
|
local aname = GetAddOnInfo(i)
|
|
if want[aname] then EnableAddOn(i) else DisableAddOn(i) end
|
|
end
|
|
|
|
DB().last = name
|
|
Print("switching addon profile to '|cff33ffcc" .. name .. "|r', reloading UI...")
|
|
reloadedAt = { time = GetTime(), name = name }
|
|
ReloadUI()
|
|
end
|
|
|
|
------------------------------------------------------------------
|
|
-- State tracking
|
|
------------------------------------------------------------------
|
|
local pending, timer, warned = nil, 0, nil
|
|
|
|
local function Evaluate()
|
|
local db = DB()
|
|
pending = nil
|
|
if db.enabled ~= "1" then return end
|
|
|
|
local target = Target()
|
|
if not target then return end
|
|
|
|
-- first run: assume the current addon set is the normal profile
|
|
if db.last == nil and not InRaid() then
|
|
db.last = target
|
|
return
|
|
end
|
|
|
|
if db.last == target then return end
|
|
pending = target
|
|
timer = DELAY
|
|
end
|
|
|
|
local frame = CreateFrame("Frame", "pfRaidProfile", UIParent)
|
|
frame:RegisterEvent("PLAYER_ENTERING_WORLD")
|
|
frame:RegisterEvent("RAID_ROSTER_UPDATE")
|
|
frame:SetScript("OnEvent", function()
|
|
Evaluate()
|
|
end)
|
|
|
|
frame:SetScript("OnUpdate", function()
|
|
-- still running 3s after ReloadUI(): the reload did not happen
|
|
if reloadedAt and GetTime() - reloadedAt.time > 3 then
|
|
local name = reloadedAt.name
|
|
reloadedAt = nil
|
|
AskReload(name)
|
|
return
|
|
end
|
|
|
|
if not pending then return end
|
|
timer = timer - arg1
|
|
if timer > 0 then return end
|
|
|
|
-- state may have changed while waiting
|
|
if Target() ~= pending then
|
|
Evaluate()
|
|
return
|
|
end
|
|
|
|
-- never switch (and reload) while dead or running back as a ghost
|
|
if UnitIsDeadOrGhost("player") then
|
|
timer = 2
|
|
return
|
|
end
|
|
|
|
if UnitAffectingCombat("player") then
|
|
if not warned then
|
|
Print("in combat, addon profile switch postponed.")
|
|
warned = true
|
|
end
|
|
timer = 3
|
|
return
|
|
end
|
|
|
|
warned = nil
|
|
local name = pending
|
|
pending = nil
|
|
Apply(name)
|
|
end)
|
|
|
|
------------------------------------------------------------------
|
|
-- Slash commands
|
|
------------------------------------------------------------------
|
|
G.SLASH_PFRAIDPROFILE1 = "/raidprofile"
|
|
G.SLASH_PFRAIDPROFILE2 = "/prp"
|
|
G.SlashCmdList["PFRAIDPROFILE"] = function(msg)
|
|
local db = DB()
|
|
local _, _, cmd, arg = strfind(msg or "", "^(%S*)%s*(.-)$")
|
|
cmd = strlower(cmd or "")
|
|
|
|
if cmd == "raid" or cmd == "normal" then
|
|
if Exists(arg) then
|
|
db[cmd] = arg
|
|
Print(cmd .. " profile = " .. arg)
|
|
else
|
|
Print("unknown profile '" .. arg .. "'. Available: " .. table.concat(ProfileNames(), ", "))
|
|
end
|
|
elseif cmd == "toggle" then
|
|
db.enabled = (db.enabled == "1") and "0" or "1"
|
|
Print(db.enabled == "1" and "enabled" or "disabled")
|
|
elseif cmd == "list" then
|
|
Print("addon profiles: " .. table.concat(ProfileNames(), ", "))
|
|
elseif cmd == "now" then
|
|
local target = Target()
|
|
if target then Apply(target) else Print("no profile configured for the current state.") end
|
|
else
|
|
Print("in raid = '" .. db.raid .. "', outside raid = '" .. db.normal .. "', enabled = " .. db.enabled)
|
|
Print("/prp raid <profile> | /prp normal <profile> | /prp list | /prp toggle | /prp now")
|
|
end
|
|
end
|
|
|
|
------------------------------------------------------------------
|
|
-- pfUI GUI: Thirdparty -> Raid Profile
|
|
------------------------------------------------------------------
|
|
if pfUI.gui and pfUI.gui.CreateGUIEntry and pfUI.gui.CreateConfig then
|
|
local CreateConfig = pfUI.gui.CreateConfig
|
|
local CreateGUIEntry = pfUI.gui.CreateGUIEntry
|
|
local nop = function() end
|
|
|
|
CreateGUIEntry(T["Thirdparty"], GetAddOnMetadata(ADDON, "X-LocalName"), function()
|
|
local db = DB()
|
|
CreateConfig(nop, "Enable automatic switching", db, "enabled", "checkbox")
|
|
CreateConfig(nop, "Addon profile while in a raid", db, "raid", "dropdown", ProfileNames)
|
|
CreateConfig(nop, "Addon profile outside a raid", db, "normal", "dropdown", ProfileNames)
|
|
CreateConfig(nil, "Profiles are managed in Escape -> AddOns -> Addon Profile", nil, nil, "header")
|
|
CreateConfig(nil, "Slash commands: /prp (raid, normal, list, toggle, now)", nil, nil, "header")
|
|
end)
|
|
end
|
|
end)
|