annotate SkeletonKey/HotKey.lua @ 62:04c23ceaf9e0

- fix taint caused by loading in combat
author Nenue
date Mon, 05 Sep 2016 14:56:38 -0400
parents b7be92f62b05
children 2409fe9b81e1
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@61 8
Nenue@61 9 local hotkey = CreateFrame('Frame', 'SkeletonHotKeys', UIParent)
Nenue@61 10 local kb, print, wrap = LibStub("LibKraken").register(KeyBinder, hotkey)
Nenue@6 11 local hotkeyText = {}
Nenue@6 12 local blizHotKey = {}
Nenue@6 13 local bindings
Nenue@6 14
Nenue@6 15 -- frames obtained via post-load hooks, created by addons like Dominos or BarTender4
Nenue@6 16 local loadedFrames = {}
Nenue@6 17 -- frames divided by update categories
Nenue@6 18 local categoryFrames = {}
Nenue@6 19 -- frames indexed by action slot ID (just the action bar, for... reasons)
Nenue@27 20 local actionSlots = {}
Nenue@6 21 local actionFrames = {}
Nenue@27 22 local actionIndex = {}
Nenue@6 23
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@62 34 ["PLAYER_SPECIALIZATION_CHANGED"] = {"player"},
Nenue@62 35 ["PLAYER_TALENTS_UPDATED"] = {"player"},
Nenue@6 36 }
Nenue@6 37
Nenue@6 38
Nenue@6 39 hotkey.wrapEvent = function(event, ...)
Nenue@62 40 hotkey:RegisterEvent(event)
Nenue@6 41 hotkeyEvents[event] = {...}
Nenue@6 42 hotkey[event] = hotkey.UpdateFromEvent
Nenue@6 43 end
Nenue@6 44
Nenue@6 45 hotkey.unwrapEvent = function(event)
Nenue@6 46 if not kb[event] then
Nenue@6 47 kb:UnregisterEvent(event)
Nenue@6 48 end
Nenue@6 49 hotkeyEvents[event] = nil
Nenue@6 50 hotkey[event] = nil
Nenue@6 51 end
Nenue@6 52
Nenue@6 53
Nenue@6 54 hotkey.RegisterFrame = function(frame)
Nenue@14 55 --wrap(function()
Nenue@61 56 print('ActionBarButtonEventsFrame_RegisterFrame(', frame:GetName(), frame.action, frame:IsVisible(), frame:IsShown())
Nenue@6 57 --end)
Nenue@6 58 blizHotKey[frame] = frame.HotKey
Nenue@6 59 loadedFrames[frame] = true
Nenue@6 60 end
Nenue@6 61
Nenue@6 62 hotkey.UpdateFromEvent = function(self, event, ...)
Nenue@6 63 if hotkeyEvents[event] then
Nenue@6 64 --print('call batch', event, ...)
Nenue@6 65 for i, func in ipairs(hotkeyEvents[event]) do
Nenue@6 66
Nenue@6 67 if hotkey[func] then
Nenue@62 68 print('->|cFF88FF00', func)
Nenue@6 69 hotkey[func](self, event, ...)
Nenue@6 70 end
Nenue@6 71 end
Nenue@6 72 end
Nenue@6 73 return true
Nenue@6 74 end
Nenue@6 75
Nenue@6 76 hotkey.variables = function()
Nenue@61 77 print('variables')
Nenue@6 78 bindings = kb.GetBindings()
Nenue@6 79 for event, manifest in pairs(hotkeyEvents) do
Nenue@62 80 print('-', event, table.concat(manifest, ', '))
Nenue@62 81 hotkey:RegisterEvent(event)
Nenue@6 82 hotkey[event] = hotkey.UpdateFromEvent
Nenue@6 83 end
Nenue@6 84 hotkey.wrapEvent('UNIT_PET', 'pet')
Nenue@61 85 hotkey.player()
Nenue@6 86 end
Nenue@6 87
Nenue@54 88
Nenue@6 89 hotkey.init = function()
Nenue@61 90
Nenue@6 91 hooksecurefunc("ActionBarButtonEventsFrame_RegisterFrame", hotkey.RegisterFrame)
Nenue@61 92
Nenue@6 93 end
Nenue@6 94
Nenue@13 95 hotkey.ui = function()
Nenue@62 96 hotkey.player()
Nenue@62 97 hotkey.pet()
Nenue@13 98 end
Nenue@13 99
Nenue@61 100
Nenue@6 101 hotkey.world = function()
Nenue@62 102 -- needs to be delayed so it isn't fired 50 times at login
Nenue@27 103 hotkeyEvents["UPDATE_BINDINGS"] = {"binding"}
Nenue@6 104 hotkey.UPDATE_BINDINGS = hotkey.UpdateFromEvent
Nenue@61 105 hotkey:RegisterEvent("UPDATE_BINDINGS")
Nenue@6 106
Nenue@6 107 hotkey.player()
Nenue@6 108 hotkey.pet()
Nenue@6 109 end
Nenue@6 110
Nenue@6 111 -- requires all these arguments since non-actionbar buttons don't have all of said methods
Nenue@62 112 local kprint = (DEVIAN_WORKSPACE and function(...) _G.print('HotKeyUpdate', ...) end) or function() end
Nenue@50 113 hotkey.UpdateHotKey = function(frame, actionType, actionID, hasAction)
Nenue@13 114 bindings = kb.GetBindings()
Nenue@27 115
Nenue@50 116 local indexKey = kb.FormatActionID(actionType, actionID)
Nenue@62 117 --kprint(frame:GetName(), '|cFF88FF00'..indexKey..'|r', hasAction)
Nenue@27 118
Nenue@50 119
Nenue@50 120 if bindings[indexKey] then
Nenue@62 121 kprint(' |cFFFF0088', actionType ..'|r', actionID, hasAction)
Nenue@50 122 local binds = bindings[indexKey]
Nenue@57 123 if binds and (not frame.HotKey:IsVisible()) then
Nenue@50 124 if hasAction then
Nenue@50 125 local bindingsText = kb.BindingString(unpack(binds))
Nenue@50 126
Nenue@6 127 if not hotkeyText[frame] then
Nenue@62 128 kprint('-new hotkey element')
Nenue@50 129 hotkeyText[frame] = frame:CreateFontString(frame:GetName()..'SkeletonKey', 'OVERLAY')
Nenue@6 130 hotkeyText[frame]:SetFont(frame.HotKey:GetFont())
Nenue@6 131 hotkeyText[frame]:SetTextColor(frame.HotKey:GetTextColor())
Nenue@6 132 hotkeyText[frame]:SetPoint('TOPRIGHT', frame.HotKey, 'TOPRIGHT')
Nenue@57 133
Nenue@57 134 hooksecurefunc(frame.HotKey, 'SetVertexColor', function(self, r,g,b,a)
Nenue@57 135 hotkeyText[frame]:SetTextColor(r,g,b,a)
Nenue@57 136 end)
Nenue@6 137 end
Nenue@6 138
Nenue@50 139 hotkeyText[frame]:SetText(bindingsText)
Nenue@50 140 hotkeyText[frame]:Show()
Nenue@62 141 kprint(' |cFF00FFFF', frame:GetName(), '|cFFFFFF00'..tostring(bindingsText)..'|r')
Nenue@50 142
Nenue@6 143 return
Nenue@6 144 end
Nenue@6 145 end
Nenue@6 146 end
Nenue@6 147
Nenue@6 148 if hotkeyText[frame] then
Nenue@6 149 hotkeyText[frame]:SetText(nil)
Nenue@62 150 print('|cFFFF4400' .. frame:GetName() .. '|r', 'remove text')
Nenue@6 151 end
Nenue@6 152 end
Nenue@6 153
Nenue@6 154 hotkey.actionbar = function()
Nenue@50 155 wipe(actionFrames)
Nenue@50 156 -- reset frames list
Nenue@61 157 print('actionbar')
Nenue@6 158 if ActionBarButtonEventsFrame.frames then
Nenue@6 159 for index, frame in ipairs(ActionBarButtonEventsFrame.frames) do
Nenue@61 160
Nenue@50 161 local slot = frame.action
Nenue@50 162 local actionType, actionID = GetActionInfo(slot)
Nenue@50 163 local hasAction = HasAction(slot)
Nenue@50 164 local indexKey = kb.FormatActionID(actionType, actionID)
Nenue@62 165
Nenue@50 166 actionSlots[slot] = frame
Nenue@50 167 actionFrames[indexKey] = actionFrames[indexKey] or {}
Nenue@50 168 if not tContains(actionFrames[indexKey]) then
Nenue@50 169 tinsert(actionFrames[indexKey], frame)
Nenue@50 170 actionIndex[slot] = indexKey
Nenue@50 171 end
Nenue@50 172
Nenue@50 173 hotkey.UpdateHotKey(frame, actionType, actionID, hasAction)
Nenue@6 174 end
Nenue@6 175 end
Nenue@6 176 end
Nenue@6 177
Nenue@6 178 hotkey.actionslot = function(self, event, slot)
Nenue@27 179 print(actionSlots[slot], event, slot, GetActionInfo(slot))
Nenue@27 180 local atype, aid = GetActionInfo(slot)
Nenue@50 181 local indexKey = kb.FormatActionID(atype, aid)
Nenue@50 182 local frame = actionSlots[slot]
Nenue@62 183 actionFrames[indexKey] = actionFrames[indexKey] or {}
Nenue@62 184 if not tContains(actionFrames[indexKey]) then
Nenue@62 185 tinsert(actionFrames[indexKey], frame)
Nenue@62 186 actionIndex[slot] = indexKey
Nenue@62 187 end
Nenue@50 188 if frame then
Nenue@50 189 hotkey.UpdateHotKey(frame, atype, aid, HasAction(slot))
Nenue@29 190 end
Nenue@29 191
Nenue@6 192 end
Nenue@6 193
Nenue@6 194 hotkey.player = function()
Nenue@61 195 print('player')
Nenue@6 196 hotkey.actionbar()
Nenue@6 197 end
Nenue@6 198
Nenue@6 199
Nenue@27 200
Nenue@6 201 hotkey.pet = function(self, event, arg1)
Nenue@6 202 if event == 'UNIT_PET' and arg1 == 'player' then
Nenue@6 203 if PetHasActionBar() and UnitIsVisible("pet") then
Nenue@6 204 hotkey.wrapEvent('PET_UI_CLOSE', 'pet')
Nenue@6 205 hotkey.wrapEvent('PET_BAR_UPDATE', 'pet')
Nenue@6 206 else
Nenue@6 207 hotkey.unwrapEvent('PET_UI_CLOSE')
Nenue@6 208 hotkey.unwrapEvent('PET_BAR_UPDATE')
Nenue@6 209 return
Nenue@6 210 end
Nenue@6 211 end
Nenue@6 212
Nenue@6 213 for i=1, NUM_PET_ACTION_SLOTS, 1 do
Nenue@6 214 local button = _G['PetActionButton'.. i]
Nenue@6 215 --print(button:GetName())
Nenue@6 216 for k, v in pairs(button) do
Nenue@6 217 --print(' ', k, type(v))
Nenue@6 218 end
Nenue@6 219 end
Nenue@6 220 end
Nenue@6 221
Nenue@6 222
Nenue@27 223 --- used to pick up changes from user interaction
Nenue@27 224 local changeIndex = 1
Nenue@27 225 hotkey.binding = function()
Nenue@27 226 print('|cFFFF0088BindingsUpdate')
Nenue@27 227 local changeNum = #kb.ChangedBindings
Nenue@27 228 if changeNum >= changeIndex then
Nenue@50 229
Nenue@50 230
Nenue@27 231 for i = changeIndex, changeNum do
Nenue@27 232 local actionType, actionID, name = unpack(kb.ChangedBindings[i])
Nenue@50 233 local actionKey = kb.FormatActionID(actionType, actionID)
Nenue@50 234 local frames = actionFrames[actionKey]
Nenue@50 235 if frames then
Nenue@50 236 for i, frame in ipairs(frames) do
Nenue@50 237 print('|cFFFF0088'..actionKey..'|r', frame)
Nenue@50 238 if frame then
Nenue@50 239 hotkey.UpdateHotKey(frame , actionType, actionID, HasAction(frame.action))
Nenue@50 240 end
Nenue@50 241 end
Nenue@50 242 else
Nenue@50 243 print('no frames picked up, rebuild')
Nenue@50 244 hotkey.actionbar()
Nenue@27 245 end
Nenue@27 246
Nenue@27 247 changeIndex = i + 1
Nenue@27 248 end
Nenue@27 249
Nenue@27 250 end
Nenue@27 251 end