annotate SkeletonKey/HotKey.lua @ 54:a545933ddf3d

- pet ability binding macro now contains a /cast line for all known abilities with the same subtext, fixing an issue where swapping pets during combat breaks the key binding
author Nenue
date Sun, 21 Aug 2016 03:13:32 -0400
parents 1aba8a6fd4a9
children 9eebce04e69b
rev   line source
Nenue@6 1 -- KrakTool
Nenue@6 2 -- HotKey.lua
Nenue@6 3 -- Created: 7/22/2016 10:28 PM
Nenue@6 4 -- %file-revision%
Nenue@6 5 -- Module for fixing actionbar hotkey text
Nenue@6 6
Nenue@50 7 local _G, wipe, tContains, tinsert = _G, table.wipe, tContains, tinsert
Nenue@14 8 local kb, print, wrap = LibStub("LibKraken").register(KeyBinder, 'HotKey')
Nenue@6 9 local hotkey = {}
Nenue@6 10 local hotkeyText = {}
Nenue@6 11 local blizHotKey = {}
Nenue@6 12 local bindings
Nenue@6 13
Nenue@6 14 -- frames obtained via post-load hooks, created by addons like Dominos or BarTender4
Nenue@6 15 local loadedFrames = {}
Nenue@6 16 -- frames divided by update categories
Nenue@6 17 local categoryFrames = {}
Nenue@6 18 -- frames indexed by action slot ID (just the action bar, for... reasons)
Nenue@27 19 local actionSlots = {}
Nenue@6 20 local actionFrames = {}
Nenue@27 21 local actionIndex = {}
Nenue@6 22
Nenue@6 23 kb.wrap(hotkey)
Nenue@6 24
Nenue@6 25 --- Used to determine which groups of action buttons need updating
Nenue@6 26 local hotkeyEvents = {
Nenue@6 27 ["UPDATE_BONUS_ACTIONBAR"] = {"bonus"},
Nenue@6 28 ["UPDATE_VEHICLE_ACTIONBAR"] = {"vehicle"},
Nenue@6 29 ["UPDATE_OVERRIDE_ACTIONBAR"] = {"override"},
Nenue@6 30 ["ACTIONBAR_PAGE_CHANGED"] = {"actionbar"},
Nenue@27 31 ["ACTIONBAR_SLOT_CHANGED"] = {"actionslot"},
Nenue@6 32 ["PLAYER_ENTERING_WORLD"] = {"world","all"},
Nenue@6 33 ["PET_UI_UPDATE"] = {"pet"},
Nenue@6 34 }
Nenue@6 35
Nenue@6 36
Nenue@6 37 hotkey.wrapEvent = function(event, ...)
Nenue@6 38 kb:RegisterEvent(event)
Nenue@6 39 hotkeyEvents[event] = {...}
Nenue@6 40 hotkey[event] = hotkey.UpdateFromEvent
Nenue@6 41 end
Nenue@6 42
Nenue@6 43 hotkey.unwrapEvent = function(event)
Nenue@6 44 if not kb[event] then
Nenue@6 45 kb:UnregisterEvent(event)
Nenue@6 46 end
Nenue@6 47 hotkeyEvents[event] = nil
Nenue@6 48 hotkey[event] = nil
Nenue@6 49 end
Nenue@6 50
Nenue@6 51
Nenue@6 52 hotkey.RegisterFrame = function(frame)
Nenue@14 53 --wrap(function()
Nenue@6 54 --print('ActionBarButtonEventsFrame_RegisterFrame(', frame:GetName(), frame.action, frame:IsVisible(), frame:IsShown())
Nenue@6 55 --end)
Nenue@6 56 blizHotKey[frame] = frame.HotKey
Nenue@6 57 loadedFrames[frame] = true
Nenue@6 58 end
Nenue@6 59
Nenue@6 60 hotkey.UpdateFromEvent = function(self, event, ...)
Nenue@6 61 if hotkeyEvents[event] then
Nenue@6 62 --print('call batch', event, ...)
Nenue@6 63 for i, func in ipairs(hotkeyEvents[event]) do
Nenue@6 64
Nenue@6 65 if hotkey[func] then
Nenue@6 66 --print(' ', func)
Nenue@6 67 hotkey[func](self, event, ...)
Nenue@6 68 end
Nenue@6 69 end
Nenue@6 70 end
Nenue@6 71 return true
Nenue@6 72 end
Nenue@6 73
Nenue@6 74 hotkey.variables = function()
Nenue@6 75 bindings = kb.GetBindings()
Nenue@6 76 for event, manifest in pairs(hotkeyEvents) do
Nenue@6 77 kb:RegisterEvent(event)
Nenue@6 78 hotkey[event] = hotkey.UpdateFromEvent
Nenue@6 79 end
Nenue@6 80 hotkey.wrapEvent('UNIT_PET', 'pet')
Nenue@6 81 end
Nenue@6 82
Nenue@54 83
Nenue@6 84 hotkey.init = function()
Nenue@6 85 hooksecurefunc("ActionBarButtonEventsFrame_RegisterFrame", hotkey.RegisterFrame)
Nenue@6 86 end
Nenue@6 87
Nenue@13 88 hotkey.ui = function()
Nenue@27 89 --hotkey.player()
Nenue@27 90 --hotkey.pet()
Nenue@13 91 end
Nenue@13 92
Nenue@6 93 hotkey.world = function()
Nenue@27 94 hotkeyEvents["UPDATE_BINDINGS"] = {"binding"}
Nenue@6 95 hotkey.UPDATE_BINDINGS = hotkey.UpdateFromEvent
Nenue@6 96 kb:RegisterEvent("UPDATE_BINDINGS")
Nenue@6 97
Nenue@6 98 hotkey.player()
Nenue@6 99 hotkey.pet()
Nenue@6 100
Nenue@6 101 end
Nenue@6 102
Nenue@6 103 -- requires all these arguments since non-actionbar buttons don't have all of said methods
Nenue@50 104 hotkey.UpdateHotKey = function(frame, actionType, actionID, hasAction)
Nenue@13 105 bindings = kb.GetBindings()
Nenue@27 106
Nenue@50 107 local indexKey = kb.FormatActionID(actionType, actionID)
Nenue@50 108 print('|cFF00FFFFUpdate|r', frame, indexKey, hasAction)
Nenue@27 109
Nenue@50 110
Nenue@50 111 if bindings[indexKey] then
Nenue@6 112 --print('|cFFFFFF00'..frame:GetName(), actionType, actionID, hasAction)
Nenue@50 113 local binds = bindings[indexKey]
Nenue@6 114 if binds then
Nenue@50 115 if hasAction then
Nenue@50 116 local bindingsText = kb.BindingString(unpack(binds))
Nenue@50 117
Nenue@6 118 if not hotkeyText[frame] then
Nenue@50 119 print('-new hotkey element')
Nenue@50 120 hotkeyText[frame] = frame:CreateFontString(frame:GetName()..'SkeletonKey', 'OVERLAY')
Nenue@6 121 hotkeyText[frame]:SetFont(frame.HotKey:GetFont())
Nenue@6 122 hotkeyText[frame]:SetTextColor(frame.HotKey:GetTextColor())
Nenue@6 123 hotkeyText[frame]:SetPoint('TOPRIGHT', frame.HotKey, 'TOPRIGHT')
Nenue@6 124 end
Nenue@6 125
Nenue@50 126 hotkeyText[frame]:SetText(bindingsText)
Nenue@50 127 hotkeyText[frame]:Show()
Nenue@50 128 print('|cFF00FFFFUpdate text for', frame:GetName(), '|cFFFFFF00'..tostring(bindingsText)..'|r')
Nenue@50 129
Nenue@6 130 return
Nenue@6 131 end
Nenue@6 132 end
Nenue@6 133 end
Nenue@6 134
Nenue@6 135 if hotkeyText[frame] then
Nenue@6 136 hotkeyText[frame]:SetText(nil)
Nenue@6 137 --print('|cFFFF4400cleared text from', frame:GetName())
Nenue@6 138 end
Nenue@6 139 end
Nenue@6 140
Nenue@6 141 hotkey.actionbar = function()
Nenue@50 142 wipe(actionFrames)
Nenue@50 143 -- reset frames list
Nenue@50 144
Nenue@6 145 if ActionBarButtonEventsFrame.frames then
Nenue@6 146 for index, frame in ipairs(ActionBarButtonEventsFrame.frames) do
Nenue@50 147 local slot = frame.action
Nenue@50 148 local actionType, actionID = GetActionInfo(slot)
Nenue@50 149 local hasAction = HasAction(slot)
Nenue@50 150 local indexKey = kb.FormatActionID(actionType, actionID)
Nenue@50 151 actionSlots[slot] = frame
Nenue@50 152 actionFrames[indexKey] = actionFrames[indexKey] or {}
Nenue@50 153 if not tContains(actionFrames[indexKey]) then
Nenue@50 154 tinsert(actionFrames[indexKey], frame)
Nenue@50 155 actionIndex[slot] = indexKey
Nenue@50 156 end
Nenue@50 157
Nenue@50 158 hotkey.UpdateHotKey(frame, actionType, actionID, hasAction)
Nenue@6 159 end
Nenue@6 160 end
Nenue@6 161 end
Nenue@6 162
Nenue@6 163 hotkey.actionslot = function(self, event, slot)
Nenue@27 164 print(actionSlots[slot], event, slot, GetActionInfo(slot))
Nenue@6 165 --print(GetActionButtonForID(slot))
Nenue@27 166 local atype, aid = GetActionInfo(slot)
Nenue@50 167 local indexKey = kb.FormatActionID(atype, aid)
Nenue@50 168 local frame = actionSlots[slot]
Nenue@50 169 if frame then
Nenue@50 170 hotkey.UpdateHotKey(frame, atype, aid, HasAction(slot))
Nenue@29 171 end
Nenue@29 172
Nenue@6 173 end
Nenue@6 174
Nenue@6 175 hotkey.player = function()
Nenue@6 176 hotkey.actionbar()
Nenue@6 177 end
Nenue@6 178
Nenue@6 179
Nenue@27 180
Nenue@6 181 hotkey.pet = function(self, event, arg1)
Nenue@6 182 if event == 'UNIT_PET' and arg1 == 'player' then
Nenue@6 183 if PetHasActionBar() and UnitIsVisible("pet") then
Nenue@6 184 hotkey.wrapEvent('PET_UI_CLOSE', 'pet')
Nenue@6 185 hotkey.wrapEvent('PET_BAR_UPDATE', 'pet')
Nenue@6 186 else
Nenue@6 187 hotkey.unwrapEvent('PET_UI_CLOSE')
Nenue@6 188 hotkey.unwrapEvent('PET_BAR_UPDATE')
Nenue@6 189 return
Nenue@6 190 end
Nenue@6 191 end
Nenue@6 192
Nenue@6 193 for i=1, NUM_PET_ACTION_SLOTS, 1 do
Nenue@6 194 local button = _G['PetActionButton'.. i]
Nenue@6 195 --print(button:GetName())
Nenue@6 196 for k, v in pairs(button) do
Nenue@6 197 --print(' ', k, type(v))
Nenue@6 198 end
Nenue@6 199 end
Nenue@6 200 end
Nenue@6 201
Nenue@6 202
Nenue@27 203 --- used to pick up changes from user interaction
Nenue@27 204 local changeIndex = 1
Nenue@27 205 hotkey.binding = function()
Nenue@27 206 print('|cFFFF0088BindingsUpdate')
Nenue@27 207 local changeNum = #kb.ChangedBindings
Nenue@27 208 if changeNum >= changeIndex then
Nenue@50 209
Nenue@50 210
Nenue@27 211 for i = changeIndex, changeNum do
Nenue@27 212 local actionType, actionID, name = unpack(kb.ChangedBindings[i])
Nenue@50 213 local actionKey = kb.FormatActionID(actionType, actionID)
Nenue@50 214 local frames = actionFrames[actionKey]
Nenue@50 215 if frames then
Nenue@50 216 for i, frame in ipairs(frames) do
Nenue@50 217 print('|cFFFF0088'..actionKey..'|r', frame)
Nenue@50 218 if frame then
Nenue@50 219 hotkey.UpdateHotKey(frame , actionType, actionID, HasAction(frame.action))
Nenue@50 220 end
Nenue@50 221 end
Nenue@50 222 else
Nenue@50 223 print('no frames picked up, rebuild')
Nenue@50 224 hotkey.actionbar()
Nenue@27 225 end
Nenue@27 226
Nenue@27 227 changeIndex = i + 1
Nenue@27 228 end
Nenue@27 229
Nenue@27 230 end
Nenue@27 231 end