annotate SkeletonKey/HotKey.lua @ 63:2409fe9b81e1

- check macro spells when considering whether a binding should be treated as a talent - clean up talent binding data when releasing/unbinding slots with a related macro/spell
author Nenue
date Thu, 08 Sep 2016 16:52:55 -0400
parents 04c23ceaf9e0
children
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@63 116 if hasAction then
Nenue@63 117 local indexKey = kb.FormatActionID(actionType, actionID)
Nenue@63 118 kprint(frame:GetName(), '|cFF88FF00'..indexKey..'|r', hasAction)
Nenue@27 119
Nenue@50 120
Nenue@63 121 local actionName
Nenue@63 122 if actionType == 'spell' then
Nenue@63 123 actionName = GetSpellInfo(actionID)
Nenue@63 124 elseif actionType == 'macro' then
Nenue@63 125 actionName = GetMacroInfo(actionID)
Nenue@63 126 elseif actionType == 'summonmount' then
Nenue@63 127 actionName = C_MountJournal.GetMountInfoByID(actionID)
Nenue@63 128 elseif actionType == 'item' then
Nenue@63 129 actionName = GetItemInfo(actionID)
Nenue@63 130 end
Nenue@63 131 kprint(' ', actionName)
Nenue@63 132
Nenue@50 133 local binds = bindings[indexKey]
Nenue@63 134 kprint(binds)
Nenue@57 135 if binds and (not frame.HotKey:IsVisible()) then
Nenue@63 136 local bindingsText = kb.BindingString(unpack(binds))
Nenue@50 137
Nenue@63 138 if not hotkeyText[frame] then
Nenue@63 139 kprint('-new hotkey element')
Nenue@63 140 hotkeyText[frame] = frame:CreateFontString(frame:GetName()..'SkeletonKey', 'OVERLAY')
Nenue@63 141 hotkeyText[frame]:SetFont(frame.HotKey:GetFont())
Nenue@63 142 hotkeyText[frame]:SetTextColor(frame.HotKey:GetTextColor())
Nenue@63 143 hotkeyText[frame]:SetPoint('TOPRIGHT', frame.HotKey, 'TOPRIGHT')
Nenue@57 144
Nenue@63 145 hooksecurefunc(frame.HotKey, 'SetVertexColor', function(self, r,g,b,a)
Nenue@63 146 hotkeyText[frame]:SetTextColor(r,g,b,a)
Nenue@63 147 end)
Nenue@63 148 end
Nenue@6 149
Nenue@63 150 hotkeyText[frame]:SetText(bindingsText)
Nenue@63 151 hotkeyText[frame]:Show()
Nenue@63 152 kprint(' |cFF00FFFF', frame:GetName(), '|cFFFFFF00'..tostring(bindingsText)..'|r')
Nenue@50 153
Nenue@63 154 return
Nenue@6 155 end
Nenue@6 156 end
Nenue@6 157
Nenue@6 158 if hotkeyText[frame] then
Nenue@63 159 local oldText = hotkeyText[frame]:GetText()
Nenue@63 160 if oldText then
Nenue@6 161 hotkeyText[frame]:SetText(nil)
Nenue@63 162 print('|cFFFF4400' .. frame:GetName() .. '|r', 'removed text', oldText)
Nenue@63 163 end
Nenue@63 164
Nenue@6 165 end
Nenue@6 166 end
Nenue@6 167
Nenue@6 168 hotkey.actionbar = function()
Nenue@50 169 -- reset frames list
Nenue@61 170 print('actionbar')
Nenue@63 171 wipe(actionFrames)
Nenue@6 172 if ActionBarButtonEventsFrame.frames then
Nenue@6 173 for index, frame in ipairs(ActionBarButtonEventsFrame.frames) do
Nenue@50 174 local slot = frame.action
Nenue@50 175 local actionType, actionID = GetActionInfo(slot)
Nenue@50 176 local hasAction = HasAction(slot)
Nenue@62 177
Nenue@50 178 actionSlots[slot] = frame
Nenue@63 179 if hasAction then
Nenue@63 180 local indexKey = kb.FormatActionID(actionType, actionID)
Nenue@63 181
Nenue@63 182 actionFrames[indexKey] = actionFrames[indexKey] or {}
Nenue@63 183 local result = ''
Nenue@63 184 if not tContains(actionFrames[indexKey], frame) then
Nenue@63 185 tinsert(actionFrames[indexKey], frame)
Nenue@63 186 actionIndex[slot] = indexKey
Nenue@63 187 result = 'added'
Nenue@63 188 end
Nenue@63 189 print('#'..index, frame:GetName(), indexKey, result)
Nenue@50 190 end
Nenue@50 191
Nenue@63 192
Nenue@50 193 hotkey.UpdateHotKey(frame, actionType, actionID, hasAction)
Nenue@6 194 end
Nenue@6 195 end
Nenue@6 196 end
Nenue@6 197
Nenue@6 198 hotkey.actionslot = function(self, event, slot)
Nenue@27 199 print(actionSlots[slot], event, slot, GetActionInfo(slot))
Nenue@27 200 local atype, aid = GetActionInfo(slot)
Nenue@50 201 local indexKey = kb.FormatActionID(atype, aid)
Nenue@50 202 local frame = actionSlots[slot]
Nenue@62 203 actionFrames[indexKey] = actionFrames[indexKey] or {}
Nenue@62 204 if not tContains(actionFrames[indexKey]) then
Nenue@62 205 tinsert(actionFrames[indexKey], frame)
Nenue@62 206 actionIndex[slot] = indexKey
Nenue@62 207 end
Nenue@50 208 if frame then
Nenue@50 209 hotkey.UpdateHotKey(frame, atype, aid, HasAction(slot))
Nenue@29 210 end
Nenue@29 211
Nenue@6 212 end
Nenue@6 213
Nenue@6 214 hotkey.player = function()
Nenue@61 215 print('player')
Nenue@6 216 hotkey.actionbar()
Nenue@6 217 end
Nenue@6 218
Nenue@6 219
Nenue@27 220
Nenue@6 221 hotkey.pet = function(self, event, arg1)
Nenue@6 222 if event == 'UNIT_PET' and arg1 == 'player' then
Nenue@6 223 if PetHasActionBar() and UnitIsVisible("pet") then
Nenue@6 224 hotkey.wrapEvent('PET_UI_CLOSE', 'pet')
Nenue@6 225 hotkey.wrapEvent('PET_BAR_UPDATE', 'pet')
Nenue@6 226 else
Nenue@6 227 hotkey.unwrapEvent('PET_UI_CLOSE')
Nenue@6 228 hotkey.unwrapEvent('PET_BAR_UPDATE')
Nenue@6 229 return
Nenue@6 230 end
Nenue@6 231 end
Nenue@6 232
Nenue@6 233 for i=1, NUM_PET_ACTION_SLOTS, 1 do
Nenue@6 234 local button = _G['PetActionButton'.. i]
Nenue@6 235 --print(button:GetName())
Nenue@6 236 for k, v in pairs(button) do
Nenue@6 237 --print(' ', k, type(v))
Nenue@6 238 end
Nenue@6 239 end
Nenue@6 240 end
Nenue@6 241
Nenue@6 242
Nenue@27 243 --- used to pick up changes from user interaction
Nenue@27 244 local changeIndex = 1
Nenue@27 245 hotkey.binding = function()
Nenue@27 246 print('|cFFFF0088BindingsUpdate')
Nenue@27 247 local changeNum = #kb.ChangedBindings
Nenue@27 248 if changeNum >= changeIndex then
Nenue@50 249
Nenue@50 250
Nenue@27 251 for i = changeIndex, changeNum do
Nenue@27 252 local actionType, actionID, name = unpack(kb.ChangedBindings[i])
Nenue@50 253 local actionKey = kb.FormatActionID(actionType, actionID)
Nenue@50 254 local frames = actionFrames[actionKey]
Nenue@50 255 if frames then
Nenue@50 256 for i, frame in ipairs(frames) do
Nenue@50 257 print('|cFFFF0088'..actionKey..'|r', frame)
Nenue@50 258 if frame then
Nenue@50 259 hotkey.UpdateHotKey(frame , actionType, actionID, HasAction(frame.action))
Nenue@50 260 end
Nenue@50 261 end
Nenue@50 262 else
Nenue@50 263 print('no frames picked up, rebuild')
Nenue@50 264 hotkey.actionbar()
Nenue@27 265 end
Nenue@27 266
Nenue@27 267 changeIndex = i + 1
Nenue@27 268 end
Nenue@27 269
Nenue@27 270 end
Nenue@27 271 end