annotate HotKey.lua @ 70:131d9190db6b

Curseforge migration
author Nenue
date Wed, 28 Dec 2016 16:31:15 -0500
parents
children c48913c5924c
rev   line source
Nenue@70 1 -- KrakTool
Nenue@70 2 -- HotKey.lua
Nenue@70 3 -- Created: 7/22/2016 10:28 PM
Nenue@70 4 -- %file-revision%
Nenue@70 5 -- Module for fixing actionbar hotkey text
Nenue@70 6
Nenue@70 7 SkeletonHotKeyMixin = {}
Nenue@70 8 local _G, wipe, tContains, tinsert = _G, table.wipe, tContains, tinsert
Nenue@70 9 local hotkeyText = {}
Nenue@70 10 local blizHotKey = {}
Nenue@70 11 local bindings
Nenue@70 12
Nenue@70 13 local _, kb = ...
Nenue@70 14 local print = (DEVIAN_PNAME == 'SkeletonKey') and function(...) _G.print('HotKey', ...) end or nop
Nenue@70 15 local cprint = (DEVIAN_PNAME == 'SkeletonKey') and function(...) _G.print('Cfg', ...) end or nop
Nenue@70 16
Nenue@70 17 -- frames obtained via post-load hooks, created by addons like Dominos or BarTender4
Nenue@70 18 local loadedFrames = {}
Nenue@70 19 -- frames divided by update categories
Nenue@70 20 local categoryFrames = {}
Nenue@70 21 -- frames indexed by action slot ID (just the action bar, for... reasons)
Nenue@70 22 local actionSlots = {}
Nenue@70 23 local actionFrames = {}
Nenue@70 24 local actionIndex = {}
Nenue@70 25 local hotkey = SkeletonHotKeyMixin
Nenue@70 26
Nenue@70 27 --- Used to determine which groups of action buttons need updating
Nenue@70 28 local hotkeyEvents = {
Nenue@70 29 ["UPDATE_BONUS_ACTIONBAR"] = {"bonus"},
Nenue@70 30 ["UPDATE_VEHICLE_ACTIONBAR"] = {"vehicle"},
Nenue@70 31 ["UPDATE_OVERRIDE_ACTIONBAR"] = {"override"},
Nenue@70 32 ["ACTIONBAR_PAGE_CHANGED"] = {"actionbar"},
Nenue@70 33 ["ACTIONBAR_SLOT_CHANGED"] = {"actionslot"},
Nenue@70 34 ["PLAYER_ENTERING_WORLD"] = {"world","all"},
Nenue@70 35 ["PET_UI_UPDATE"] = {"pet"},
Nenue@70 36 ["PLAYER_SPECIALIZATION_CHANGED"] = {"player"},
Nenue@70 37 ["PLAYER_TALENTS_UPDATED"] = {"player"},
Nenue@70 38 }
Nenue@70 39
Nenue@70 40
Nenue@70 41 function hotkey:wrapEvent (event, ...)
Nenue@70 42 self:RegisterEvent(event)
Nenue@70 43 hotkeyEvents[event] = {...}
Nenue@70 44 self[event] = self.UpdateFromEvent
Nenue@70 45 end
Nenue@70 46
Nenue@70 47 function hotkey:unwrapEvent (event)
Nenue@70 48 if not self[event] then
Nenue@70 49 self:UnregisterEvent(event)
Nenue@70 50 end
Nenue@70 51 hotkeyEvents[event] = nil
Nenue@70 52 self[event] = nil
Nenue@70 53 end
Nenue@70 54
Nenue@70 55
Nenue@70 56 hotkey.RegisterFrame = function(frame)
Nenue@70 57 --wrap(function()
Nenue@70 58 print('ActionBarButtonEventsFrame_RegisterFrame(', frame:GetName(), frame.action, frame:IsVisible(), frame:IsShown())
Nenue@70 59 --end)
Nenue@70 60 blizHotKey[frame] = frame.HotKey
Nenue@70 61 loadedFrames[frame] = true
Nenue@70 62 end
Nenue@70 63
Nenue@70 64 function hotkey:OnEvent (event, ...)
Nenue@70 65 print('|cFFFF8888'..self:GetName()..':OnEvent()|r', event, ...)
Nenue@70 66 if hotkeyEvents[event] then
Nenue@70 67 for i, func in ipairs(hotkeyEvents[event]) do
Nenue@70 68
Nenue@70 69 if self[func] then
Nenue@70 70 print('->|cFF88FF00', func)
Nenue@70 71 self[func](self, event, ...)
Nenue@70 72 end
Nenue@70 73 end
Nenue@70 74 end
Nenue@70 75 return true
Nenue@70 76 end
Nenue@70 77
Nenue@70 78 function hotkey:Setup ()
Nenue@70 79 print('variables')
Nenue@70 80 bindings = kb.GetBindings()
Nenue@70 81 self:player()
Nenue@70 82 for event, triggers in pairs(hotkeyEvents) do
Nenue@70 83 print('setup', event, triggers)
Nenue@70 84 self:RegisterEvent(event)
Nenue@70 85 end
Nenue@70 86
Nenue@70 87 end
Nenue@70 88
Nenue@70 89 function hotkey:OnLoad()
Nenue@70 90 hooksecurefunc("ActionBarButtonEventsFrame_RegisterFrame", hotkey.RegisterFrame)
Nenue@70 91
Nenue@70 92 end
Nenue@70 93
Nenue@70 94 function hotkey:Update()
Nenue@70 95 print('--|cFF00FF00ui|r')
Nenue@70 96 self:player()
Nenue@70 97 hotkey:pet()
Nenue@70 98 hotkey:binding()
Nenue@70 99 end
Nenue@70 100
Nenue@70 101
Nenue@70 102 function hotkey:world()
Nenue@70 103 print('--|cFF00FF00world|r')
Nenue@70 104 -- needs to be delayed so it isn't fired 50 times at login
Nenue@70 105 if not hotkeyEvents["UPDATE_BINDINGS"] then
Nenue@70 106 hotkeyEvents["UPDATE_BINDINGS"] = {"binding"}
Nenue@70 107 self.UPDATE_BINDINGS = self.UpdateFromEvent
Nenue@70 108 self:RegisterEvent("UPDATE_BINDINGS")
Nenue@70 109 end
Nenue@70 110
Nenue@70 111 self:player()
Nenue@70 112 self:pet()
Nenue@70 113 end
Nenue@70 114
Nenue@70 115 -- requires all these arguments since non-actionbar buttons don't have all of said methods
Nenue@70 116 local kprint = (DEVIAN_WORKSPACE and function(...) _G.print('HotKeyUpdate', ...) end) or function() end
Nenue@70 117 function hotkey:UpdateHotKey(frame, actionType, actionID, hasAction)
Nenue@70 118 bindings = kb.GetBindings()
Nenue@70 119
Nenue@70 120 if hasAction then
Nenue@70 121 local indexKey = kb.FormatActionID(actionType, actionID)
Nenue@70 122
Nenue@70 123
Nenue@70 124
Nenue@70 125 local actionName
Nenue@70 126 if actionType == 'spell' then
Nenue@70 127 actionName = GetSpellInfo(actionID)
Nenue@70 128 elseif actionType == 'macro' then
Nenue@70 129 actionName = GetMacroInfo(actionID)
Nenue@70 130 elseif actionType == 'summonmount' then
Nenue@70 131 actionName = C_MountJournal.GetMountInfoByID(actionID)
Nenue@70 132 elseif actionType == 'item' then
Nenue@70 133 actionName = GetItemInfo(actionID)
Nenue@70 134 end
Nenue@70 135 kprint(' actionKey:', indexKey)
Nenue@70 136
Nenue@70 137 local binds = bindings[indexKey]
Nenue@70 138 if binds and (not frame.HotKey:IsVisible()) then
Nenue@70 139 kprint(frame:GetName(), '|cFF88FF00'..indexKey..'|r', hasAction, actionName)
Nenue@70 140 local bindingsText = kb.BindingString(unpack(binds))
Nenue@70 141
Nenue@70 142 if not hotkeyText[frame] then
Nenue@70 143 kprint('-new hotkey element')
Nenue@70 144 hotkeyText[frame] = frame:CreateFontString(frame:GetName()..'SkeletonKey', 'OVERLAY')
Nenue@70 145 hotkeyText[frame]:SetFont(frame.HotKey:GetFont())
Nenue@70 146 hotkeyText[frame]:SetTextColor(frame.HotKey:GetTextColor())
Nenue@70 147 hotkeyText[frame]:SetPoint('TOPRIGHT', frame.HotKey, 'TOPRIGHT')
Nenue@70 148
Nenue@70 149 hooksecurefunc(frame.HotKey, 'SetVertexColor', function(self, r,g,b,a)
Nenue@70 150 hotkeyText[frame]:SetTextColor(r,g,b,a)
Nenue@70 151 end)
Nenue@70 152 end
Nenue@70 153
Nenue@70 154 if actionType == 'macro' then
Nenue@70 155 local name, rank, spellID = GetMacroSpell(actionID)
Nenue@70 156 if spellID and bindings['spell_'..spellID] then
Nenue@70 157 bindingsText = kb.BindingString(unpack(bindings['spell_'..spellID]))
Nenue@70 158 end
Nenue@70 159
Nenue@70 160 end
Nenue@70 161
Nenue@70 162
Nenue@70 163 hotkeyText[frame]:SetText(bindingsText)
Nenue@70 164 hotkeyText[frame]:Show()
Nenue@70 165 kprint(' |cFF00FFFF', frame:GetName(), '|cFFFFFF00'..tostring(bindingsText)..'|r')
Nenue@70 166
Nenue@70 167 return
Nenue@70 168 end
Nenue@70 169 end
Nenue@70 170
Nenue@70 171 if hotkeyText[frame] then
Nenue@70 172 local oldText = hotkeyText[frame]:GetText()
Nenue@70 173 if oldText then
Nenue@70 174 hotkeyText[frame]:SetText(nil)
Nenue@70 175 print('|cFFFF4400' .. frame:GetName() .. '|r', 'removed text', oldText)
Nenue@70 176 end
Nenue@70 177 end
Nenue@70 178 end
Nenue@70 179
Nenue@70 180 local foundSlots = {}
Nenue@70 181 function hotkey:actionbar()
Nenue@70 182 print('--|cFF00FF00actionbar|r')
Nenue@70 183 wipe(actionFrames)
Nenue@70 184 wipe(foundSlots)
Nenue@70 185
Nenue@70 186
Nenue@70 187 bindings = kb.GetBindings()
Nenue@70 188 for k,v in pairs(bindings) do
Nenue@70 189 print(k, #v)
Nenue@70 190 end
Nenue@70 191
Nenue@70 192
Nenue@70 193 if ActionBarButtonEventsFrame.frames then
Nenue@70 194 for index, frame in ipairs(ActionBarButtonEventsFrame.frames) do
Nenue@70 195 local slot = frame.action
Nenue@70 196 local actionType, actionID = GetActionInfo(slot)
Nenue@70 197 local hasAction = HasAction(slot)
Nenue@70 198
Nenue@70 199 actionSlots[slot] = frame
Nenue@70 200 if hasAction then
Nenue@70 201 local indexKey = kb.FormatActionID(actionType, actionID)
Nenue@70 202
Nenue@70 203 actionFrames[indexKey] = actionFrames[indexKey] or {}
Nenue@70 204 if not tContains(actionFrames[indexKey], frame) then
Nenue@70 205 tinsert(actionFrames[indexKey], frame)
Nenue@70 206 actionIndex[slot] = indexKey
Nenue@70 207 end
Nenue@70 208 local actionName = ''
Nenue@70 209 if actionType == 'macro' then
Nenue@70 210 actionName = GetMacroInfo(actionID)
Nenue@70 211 elseif actionType == 'spell' then
Nenue@70 212 actionName = GetSpellInfo(actionID)
Nenue@70 213 end
Nenue@70 214
Nenue@70 215 if bindings[indexKey] then
Nenue@70 216 print(indexKey, actionName)
Nenue@70 217 for k,v in pairs(bindings[indexKey]) do
Nenue@70 218 print(k,v)
Nenue@70 219 end
Nenue@70 220 end
Nenue@70 221
Nenue@70 222
Nenue@70 223 --tinsert(foundSlots, '|cFFFFFF00#'..index .. '|r ' .. tostring(indexKey).. '('..(bindings[indexKey] and table.concat(bindings[indexKey],' ') or '-')..')')
Nenue@70 224 end
Nenue@70 225
Nenue@70 226
Nenue@70 227 self:UpdateHotKey(frame, actionType, actionID, hasAction)
Nenue@70 228 end
Nenue@70 229 end
Nenue@70 230
Nenue@70 231 --print('found slots: ', table.concat(foundSlots, ', '))
Nenue@70 232 end
Nenue@70 233
Nenue@70 234 function hotkey:actionslot (event, slot)
Nenue@70 235 local actionType, actionID = GetActionInfo(slot)
Nenue@70 236 print(actionSlots[slot], event, actionType, actionID, HasAction(slot))
Nenue@70 237 local frame = actionSlots[slot]
Nenue@70 238 if frame then
Nenue@70 239 self:UpdateHotKey(frame, actionType, actionID, HasAction(slot))
Nenue@70 240 end
Nenue@70 241
Nenue@70 242 end
Nenue@70 243
Nenue@70 244 function hotkey:player()
Nenue@70 245 print('player')
Nenue@70 246 self:actionbar()
Nenue@70 247 end
Nenue@70 248
Nenue@70 249
Nenue@70 250
Nenue@70 251 function hotkey:pet(event, arg1)
Nenue@70 252 print('--|cFF00FF00pet|r')
Nenue@70 253 if event == 'UNIT_PET' and arg1 == 'player' then
Nenue@70 254 if PetHasActionBar() and UnitIsVisible("pet") then
Nenue@70 255 self:wrapEvent('PET_UI_CLOSE', 'pet')
Nenue@70 256 self:wrapEvent('PET_BAR_UPDATE', 'pet')
Nenue@70 257 else
Nenue@70 258 self:unwrapEvent('PET_UI_CLOSE')
Nenue@70 259 self:unwrapEvent('PET_BAR_UPDATE')
Nenue@70 260 return
Nenue@70 261 end
Nenue@70 262 end
Nenue@70 263
Nenue@70 264 for i=1, NUM_PET_ACTION_SLOTS, 1 do
Nenue@70 265 local button = _G['PetActionButton'.. i]
Nenue@70 266 --print(button:GetName())
Nenue@70 267 for k, v in pairs(button) do
Nenue@70 268 --print(' ', k, type(v))
Nenue@70 269 end
Nenue@70 270 end
Nenue@70 271 end
Nenue@70 272
Nenue@70 273
Nenue@70 274 --- used to pick up changes from user interaction
Nenue@70 275 local changeIndex = 1
Nenue@70 276 function hotkey:binding ()
Nenue@70 277 local changeNum = #kb.ChangedBindings
Nenue@70 278 cprint('--|cFF00FF00binding|r')
Nenue@70 279 if changeNum >= changeIndex then
Nenue@70 280
Nenue@70 281
Nenue@70 282 for i = changeIndex, changeNum do
Nenue@70 283 cprint(changeIndex,'of', changeNum)
Nenue@70 284 local actionType, actionID, name = unpack(kb.ChangedBindings[i])
Nenue@70 285 local actionKey = kb.FormatActionID(actionType, actionID)
Nenue@70 286 local frames = actionFrames[actionKey]
Nenue@70 287 if frames then
Nenue@70 288 for i, frame in ipairs(frames) do
Nenue@70 289 cprint('|cFFFF0088'..actionKey..'|r', frame)
Nenue@70 290 if frame then
Nenue@70 291 self:UpdateHotKey(frame , actionType, actionID, HasAction(frame.action))
Nenue@70 292 end
Nenue@70 293 end
Nenue@70 294 else
Nenue@70 295 cprint('no frames picked up, rebuild')
Nenue@70 296 self:actionbar()
Nenue@70 297 end
Nenue@70 298
Nenue@70 299 changeIndex = i + 1
Nenue@70 300 end
Nenue@70 301
Nenue@70 302 end
Nenue@70 303 end