ruen-ui/RuenUI/CharacterProfiles.lua

234 lines
11 KiB
Lua
Raw Normal View History

local _, P = ...
local numbers = {"1","2","3","4","5","6","7","8","9","0","-","="}
local petClasses = {HUNTER=true, WARLOCK=true}
local healClasses = {PRIEST=true, PALADIN=true, SHAMAN=true, DRUID=true}
local owner, listener, queued, signature
local effective, hooked = {}, {}
-- Executed by SecureHandlerStateTemplate, including transitions IN combat.
-- Never intercept native vehicle/possession/override controls. Ordinary rogue
-- stealth and druid forms keep the same utility keys and native main-bar paging.
local bindingSnippet = [[
self:ClearBindings()
if self:GetAttribute("state-ruencontrols") == "normal" then
for i = 1, self:GetAttribute("bindingCount") or 0 do
self:SetBinding(false, self:GetAttribute("key"..i), self:GetAttribute("action"..i))
end
end
]]
local conditions = "[vehicleui][overridebar][possessbar] special; normal"
function P.EnsureCharacterProfile()
if not RuenUICharacter or not RuenUIDB then return end
if RuenUICharacter.profileVersion == 1 then return end
-- Older releases recorded these shared migrations separately. Establish
-- their ownership before any fresh-character setup can replay them.
if not RuenUIDB.combatBindingsVersion and RuenUIDB.extraBindingsVersion == 1 and RuenUIDB.controlsVersion == 1 then
RuenUIDB.combatBindingsVersion = 2
end
-- Preserve the EFFECTIVE old modes, including explicit off. Fresh installs
-- start without duplicate cooldown icons; upgrades retain their choice.
RuenUICharacter.cooldownsMode = RuenUICharacter.cooldownsMode or RuenUIDB.cooldownsMode
or (RuenUICharacter.version and "auto" or "off")
RuenUICharacter.damageMeterMode = RuenUICharacter.damageMeterMode or RuenUIDB.damageMeterMode or "auto"
if (not RuenUICharacter.mouseoverMode or RuenUICharacter.mouseoverMode == "auto") and RuenUICharacter.version then
local old = RuenUICharacter.mouseoverApplied
local _, class = UnitClass("player")
local enabled = old and old.enabled
if enabled == nil then enabled = healClasses[class] or false end
local identity = (UnitGUID and UnitGUID("player")) or (UnitName("player")..":"..class)
if RuenUIDB.mouseoverLastCharacter == identity and C_CVar and C_CVar.GetCVar then
local current = C_CVar.GetCVar("enableMouseoverCast")
if current == "0" or current == "1" then enabled = current == "1" end
end
RuenUICharacter.mouseoverMode = enabled and "on" or "off"
end
RuenUICharacter.profileVersion = 1
end
function P.NumberProfile()
local preference = RuenUICharacter and RuenUICharacter.numberProfile
if preference == "pet" or preference == "utility" then return preference end
local _, class = UnitClass("player")
return petClasses[class] and "pet" or "utility"
end
function P.CharacterRole()
local role = RuenUICharacter and RuenUICharacter.rolePreference
if role == "HEALER" or role == "TANK" or role == "DAMAGER" then return role end
role = nil
if GetSpecialization and GetSpecializationRole then
local spec = GetSpecialization()
if spec then role = GetSpecializationRole(spec) end
end
if not role or role == "NONE" then role = UnitGroupRolesAssigned and UnitGroupRolesAssigned("player") end
if role == "HEALER" or role == "TANK" or role == "DAMAGER" then return role end
return "UNKNOWN" -- never infer a healing input scheme from class or form
end
local petSeen = {}
function P.NeedsPetRow()
local _, class = UnitClass("player")
if petClasses[class] or P.NumberProfile() == "pet" then return true end
local identity = (UnitGUID and UnitGUID("player")) or class
local spec = GetSpecialization and GetSpecialization() or 0
local key = tostring(identity)..":"..tostring(spec)
local shown = PetActionBar and PetActionBar:IsShown()
if shown and UnitExists and UnitExists("pet") then petSeen[key] = true end
-- Keep the acquired row stable through dismissal/death this session. A
-- petless Mage does not reserve it merely because of its class name.
return shown or petSeen[key] or false
end
function P.EffectiveBindingKey(action)
for _, key in ipairs(effective[action] or {}) do
if GetBindingAction(key, true) == action then return key end
end
for _, key in ipairs({GetBindingKey(action)}) do
if GetBindingAction(key, true) == action then return key end
end
end
local function label(button, action)
if not button or not button.HotKey then return end
local key = P.EffectiveBindingKey(action)
button.HotKey:SetText(key and GetBindingText(key, true) or "")
if key then button.HotKey:Show() else button.HotKey:Hide() end
end
function P.RefreshProfileHotkeys()
for i = 1, 12 do
local entries = {{_G["MultiBarLeftButton"..i], "MULTIACTIONBAR4BUTTON"..i, "UpdateHotkeys"}}
if i <= 10 then entries[#entries+1] = {_G["PetActionButton"..i], "BONUSACTIONBUTTON"..i, "SetHotkeys"}
else entries[#entries+1] = {_G["RuenPetExtraButton"..i], "CLICK RuenPetExtraButton"..i..":LeftButton"} end
for _, entry in ipairs(entries) do
local button, action, method = unpack(entry)
if button and not hooked[button] and method and type(button[method]) == "function" then
hooksecurefunc(button, method, function() label(button, action) end)
hooked[button] = true
end
label(button, action)
end
end
end
function P.ClearCharacterBindings()
if InCombatLockdown() then return false end
if owner then
UnregisterStateDriver(owner, "ruencontrols")
owner:SetAttribute("bindingCount", 0)
owner:Execute(bindingSnippet)
end
effective, signature = {}, nil
P.RefreshProfileHotkeys()
return true
end
function P.ApplyCharacterBindings(force)
if InCombatLockdown() then return false end
if not RuenUICharacter or not RuenUIDB then return false end
if RuenUIDB.disabled then return P.ClearCharacterBindings() end
P.EnsureCharacterProfile()
if not RegisterStateDriver or not UnregisterStateDriver then
RuenUICharacter.numberProfileStatus = "secure binding support unavailable"
return false
end
if not owner then
owner = CreateFrame("Frame", "RuenCharacterBindings", UIParent, "SecureHandlerStateTemplate")
owner:SetAttribute("_onstate-ruencontrols", bindingSnippet)
owner:HookScript("OnAttributeChanged", function(_, key)
if key == "state-ruencontrols" then P.RefreshProfileHotkeys() end
end)
end
local records = RuenUICharacter.numberBindingBases or {}
RuenUICharacter.numberBindingBases = records
local scope = GetCurrentBindingSet() == 1 and RuenUIDB or RuenUICharacter
local profile, wanted, parts, conflicts = P.NumberProfile(), {}, {}, {}
effective = {}
for i, number in ipairs(numbers) do
local pet = i <= 10 and "BONUSACTIONBUTTON"..i or "CLICK RuenPetExtraButton"..i..":LeftButton"
local utility = "MULTIACTIONBAR4BUTTON"..i
for _, entry in ipairs({{number, profile == "pet" and pet or utility, pet},
{"CTRL-"..number, profile == "pet" and utility or pet, utility}}) do
local key, action, oldDefault = unpack(entry)
local base = GetBindingAction(key) or ""
local record = records[key]
local change = RuenUICharacter.bindingChanges and RuenUICharacter.bindingChanges[key]
if not record or force then
record = {base=base, managed=force or base == "" or
(change and change.applied == base) or (scope.petBindingsVersion == 2 and base == oldDefault) or false}
records[key] = record
elseif record.base ~= base then
record.base, record.managed = base, false -- later user edits win
end
if record.managed then
wanted[#wanted+1] = {key, action}
parts[#parts+1] = key.."="..action
effective[action] = effective[action] or {}
effective[action][#effective[action]+1] = key
else conflicts[#conflicts+1] = key end
end
end
local nextSignature = table.concat(parts, ";")
if signature ~= nextSignature then
owner:SetAttribute("bindingCount", #wanted)
for i, entry in ipairs(wanted) do
owner:SetAttribute("key"..i, entry[1]); owner:SetAttribute("action"..i, entry[2])
end
RegisterStateDriver(owner, "ruencontrols", conditions)
owner:Execute(bindingSnippet)
signature = nextSignature
end
RuenUICharacter.numberProfileStatus = profile
RuenUICharacter.numberBindingConflicts = conflicts
P.RefreshProfileHotkeys()
return true
end
function P.CharacterProfileSummary()
return P.Localized("Numbers: ", "Цифры: ")..P.NumberProfile()
..P.Localized("; role: ", "; роль: ")..P.CharacterRole()
..P.Localized("; custom keys preserved: ", "; сохранены свои клавиши: ")
..table.concat(RuenUICharacter.numberBindingConflicts or {}, ", ")
end
function P.CharacterProfileCommand(command, argument)
P.EnsureCharacterProfile()
if command == "keys" and (argument == "auto" or argument == "pet" or argument == "utility") then
RuenUICharacter.numberProfile = argument
elseif command == "role" then
local roles = {auto="auto", healer="HEALER", tank="TANK", damage="DAMAGER"}
if roles[argument] then
RuenUICharacter.rolePreference = roles[argument]
RuenUICharacter.mouseoverMode = "auto"
P.mouseoverPending = true
end
end
P.ApplyCharacterBindings(false)
if P.ApplyMouseover then P.ApplyMouseover() end
if P.RefreshClassAssist then P.RefreshClassAssist() end
return P.CharacterProfileSummary()..P.Localized(
". /ruen keys auto|pet|utility; /ruen role auto|healer|tank|damage. Changes wait until combat ends.",
". /ruen keys auto|pet|utility; /ruen role auto|healer|tank|damage. Изменения применяются после боя.")
end
function P.InitializeCharacterProfiles()
P.EnsureCharacterProfile()
if listener then return end
listener = CreateFrame("Frame")
for _, event in ipairs({"UPDATE_BINDINGS", "PLAYER_REGEN_ENABLED", "PLAYER_ENTERING_WORLD",
"PLAYER_SPECIALIZATION_CHANGED", "GROUP_ROSTER_UPDATE", "PLAYER_ROLES_ASSIGNED"}) do
pcall(listener.RegisterEvent, listener, event)
end
listener:SetScript("OnEvent", function()
if queued then return end
queued = true
C_Timer.After(.1, function()
queued = false
if RuenUIDB.disabled then P.ClearCharacterBindings(); return end
P.ApplyCharacterBindings(false)
if P.ApplyMouseover then P.ApplyMouseover() end
end)
end)
end