Nenue@6: -- KrakTool Nenue@6: -- HotKey.lua Nenue@6: -- Created: 7/22/2016 10:28 PM Nenue@6: -- %file-revision% Nenue@6: -- Module for fixing actionbar hotkey text Nenue@6: Nenue@50: local _G, wipe, tContains, tinsert = _G, table.wipe, tContains, tinsert Nenue@14: local kb, print, wrap = LibStub("LibKraken").register(KeyBinder, 'HotKey') Nenue@6: local hotkey = {} Nenue@6: local hotkeyText = {} Nenue@6: local blizHotKey = {} Nenue@6: local bindings Nenue@6: Nenue@6: -- frames obtained via post-load hooks, created by addons like Dominos or BarTender4 Nenue@6: local loadedFrames = {} Nenue@6: -- frames divided by update categories Nenue@6: local categoryFrames = {} Nenue@6: -- frames indexed by action slot ID (just the action bar, for... reasons) Nenue@27: local actionSlots = {} Nenue@6: local actionFrames = {} Nenue@27: local actionIndex = {} Nenue@6: Nenue@6: kb.wrap(hotkey) Nenue@6: Nenue@6: --- Used to determine which groups of action buttons need updating Nenue@6: local hotkeyEvents = { Nenue@6: ["UPDATE_BONUS_ACTIONBAR"] = {"bonus"}, Nenue@6: ["UPDATE_VEHICLE_ACTIONBAR"] = {"vehicle"}, Nenue@6: ["UPDATE_OVERRIDE_ACTIONBAR"] = {"override"}, Nenue@6: ["ACTIONBAR_PAGE_CHANGED"] = {"actionbar"}, Nenue@27: ["ACTIONBAR_SLOT_CHANGED"] = {"actionslot"}, Nenue@6: ["PLAYER_ENTERING_WORLD"] = {"world","all"}, Nenue@6: ["PET_UI_UPDATE"] = {"pet"}, Nenue@6: } Nenue@6: Nenue@6: Nenue@6: hotkey.wrapEvent = function(event, ...) Nenue@6: kb:RegisterEvent(event) Nenue@6: hotkeyEvents[event] = {...} Nenue@6: hotkey[event] = hotkey.UpdateFromEvent Nenue@6: end Nenue@6: Nenue@6: hotkey.unwrapEvent = function(event) Nenue@6: if not kb[event] then Nenue@6: kb:UnregisterEvent(event) Nenue@6: end Nenue@6: hotkeyEvents[event] = nil Nenue@6: hotkey[event] = nil Nenue@6: end Nenue@6: Nenue@6: Nenue@6: hotkey.RegisterFrame = function(frame) Nenue@14: --wrap(function() Nenue@6: --print('ActionBarButtonEventsFrame_RegisterFrame(', frame:GetName(), frame.action, frame:IsVisible(), frame:IsShown()) Nenue@6: --end) Nenue@6: blizHotKey[frame] = frame.HotKey Nenue@6: loadedFrames[frame] = true Nenue@6: end Nenue@6: Nenue@6: hotkey.UpdateFromEvent = function(self, event, ...) Nenue@6: if hotkeyEvents[event] then Nenue@6: --print('call batch', event, ...) Nenue@6: for i, func in ipairs(hotkeyEvents[event]) do Nenue@6: Nenue@6: if hotkey[func] then Nenue@6: --print(' ', func) Nenue@6: hotkey[func](self, event, ...) Nenue@6: end Nenue@6: end Nenue@6: end Nenue@6: return true Nenue@6: end Nenue@6: Nenue@6: hotkey.variables = function() Nenue@6: bindings = kb.GetBindings() Nenue@6: for event, manifest in pairs(hotkeyEvents) do Nenue@6: kb:RegisterEvent(event) Nenue@6: hotkey[event] = hotkey.UpdateFromEvent Nenue@6: end Nenue@6: hotkey.wrapEvent('UNIT_PET', 'pet') Nenue@6: end Nenue@6: Nenue@54: Nenue@6: hotkey.init = function() Nenue@6: hooksecurefunc("ActionBarButtonEventsFrame_RegisterFrame", hotkey.RegisterFrame) Nenue@6: end Nenue@6: Nenue@13: hotkey.ui = function() Nenue@27: --hotkey.player() Nenue@27: --hotkey.pet() Nenue@13: end Nenue@13: Nenue@6: hotkey.world = function() Nenue@27: hotkeyEvents["UPDATE_BINDINGS"] = {"binding"} Nenue@6: hotkey.UPDATE_BINDINGS = hotkey.UpdateFromEvent Nenue@6: kb:RegisterEvent("UPDATE_BINDINGS") Nenue@6: Nenue@6: hotkey.player() Nenue@6: hotkey.pet() Nenue@6: Nenue@6: end Nenue@6: Nenue@6: -- requires all these arguments since non-actionbar buttons don't have all of said methods Nenue@50: hotkey.UpdateHotKey = function(frame, actionType, actionID, hasAction) Nenue@13: bindings = kb.GetBindings() Nenue@27: Nenue@50: local indexKey = kb.FormatActionID(actionType, actionID) Nenue@50: print('|cFF00FFFFUpdate|r', frame, indexKey, hasAction) Nenue@27: Nenue@50: Nenue@50: if bindings[indexKey] then Nenue@6: --print('|cFFFFFF00'..frame:GetName(), actionType, actionID, hasAction) Nenue@50: local binds = bindings[indexKey] Nenue@6: if binds then Nenue@50: if hasAction then Nenue@50: local bindingsText = kb.BindingString(unpack(binds)) Nenue@50: Nenue@6: if not hotkeyText[frame] then Nenue@50: print('-new hotkey element') Nenue@50: hotkeyText[frame] = frame:CreateFontString(frame:GetName()..'SkeletonKey', 'OVERLAY') Nenue@6: hotkeyText[frame]:SetFont(frame.HotKey:GetFont()) Nenue@6: hotkeyText[frame]:SetTextColor(frame.HotKey:GetTextColor()) Nenue@6: hotkeyText[frame]:SetPoint('TOPRIGHT', frame.HotKey, 'TOPRIGHT') Nenue@6: end Nenue@6: Nenue@50: hotkeyText[frame]:SetText(bindingsText) Nenue@50: hotkeyText[frame]:Show() Nenue@50: print('|cFF00FFFFUpdate text for', frame:GetName(), '|cFFFFFF00'..tostring(bindingsText)..'|r') Nenue@50: Nenue@6: return Nenue@6: end Nenue@6: end Nenue@6: end Nenue@6: Nenue@6: if hotkeyText[frame] then Nenue@6: hotkeyText[frame]:SetText(nil) Nenue@6: --print('|cFFFF4400cleared text from', frame:GetName()) Nenue@6: end Nenue@6: end Nenue@6: Nenue@6: hotkey.actionbar = function() Nenue@50: wipe(actionFrames) Nenue@50: -- reset frames list Nenue@50: Nenue@6: if ActionBarButtonEventsFrame.frames then Nenue@6: for index, frame in ipairs(ActionBarButtonEventsFrame.frames) do Nenue@50: local slot = frame.action Nenue@50: local actionType, actionID = GetActionInfo(slot) Nenue@50: local hasAction = HasAction(slot) Nenue@50: local indexKey = kb.FormatActionID(actionType, actionID) Nenue@50: actionSlots[slot] = frame Nenue@50: actionFrames[indexKey] = actionFrames[indexKey] or {} Nenue@50: if not tContains(actionFrames[indexKey]) then Nenue@50: tinsert(actionFrames[indexKey], frame) Nenue@50: actionIndex[slot] = indexKey Nenue@50: end Nenue@50: Nenue@50: hotkey.UpdateHotKey(frame, actionType, actionID, hasAction) Nenue@6: end Nenue@6: end Nenue@6: end Nenue@6: Nenue@6: hotkey.actionslot = function(self, event, slot) Nenue@27: print(actionSlots[slot], event, slot, GetActionInfo(slot)) Nenue@6: --print(GetActionButtonForID(slot)) Nenue@27: local atype, aid = GetActionInfo(slot) Nenue@50: local indexKey = kb.FormatActionID(atype, aid) Nenue@50: local frame = actionSlots[slot] Nenue@50: if frame then Nenue@50: hotkey.UpdateHotKey(frame, atype, aid, HasAction(slot)) Nenue@29: end Nenue@29: Nenue@6: end Nenue@6: Nenue@6: hotkey.player = function() Nenue@6: hotkey.actionbar() Nenue@6: end Nenue@6: Nenue@6: Nenue@27: Nenue@6: hotkey.pet = function(self, event, arg1) Nenue@6: if event == 'UNIT_PET' and arg1 == 'player' then Nenue@6: if PetHasActionBar() and UnitIsVisible("pet") then Nenue@6: hotkey.wrapEvent('PET_UI_CLOSE', 'pet') Nenue@6: hotkey.wrapEvent('PET_BAR_UPDATE', 'pet') Nenue@6: else Nenue@6: hotkey.unwrapEvent('PET_UI_CLOSE') Nenue@6: hotkey.unwrapEvent('PET_BAR_UPDATE') Nenue@6: return Nenue@6: end Nenue@6: end Nenue@6: Nenue@6: for i=1, NUM_PET_ACTION_SLOTS, 1 do Nenue@6: local button = _G['PetActionButton'.. i] Nenue@6: --print(button:GetName()) Nenue@6: for k, v in pairs(button) do Nenue@6: --print(' ', k, type(v)) Nenue@6: end Nenue@6: end Nenue@6: end Nenue@6: Nenue@6: Nenue@27: --- used to pick up changes from user interaction Nenue@27: local changeIndex = 1 Nenue@27: hotkey.binding = function() Nenue@27: print('|cFFFF0088BindingsUpdate') Nenue@27: local changeNum = #kb.ChangedBindings Nenue@27: if changeNum >= changeIndex then Nenue@50: Nenue@50: Nenue@27: for i = changeIndex, changeNum do Nenue@27: local actionType, actionID, name = unpack(kb.ChangedBindings[i]) Nenue@50: local actionKey = kb.FormatActionID(actionType, actionID) Nenue@50: local frames = actionFrames[actionKey] Nenue@50: if frames then Nenue@50: for i, frame in ipairs(frames) do Nenue@50: print('|cFFFF0088'..actionKey..'|r', frame) Nenue@50: if frame then Nenue@50: hotkey.UpdateHotKey(frame , actionType, actionID, HasAction(frame.action)) Nenue@50: end Nenue@50: end Nenue@50: else Nenue@50: print('no frames picked up, rebuild') Nenue@50: hotkey.actionbar() Nenue@27: end Nenue@27: Nenue@27: changeIndex = i + 1 Nenue@27: end Nenue@27: Nenue@27: end Nenue@27: end