annotate SkeletonKey/HotKey.lua @ 57:9eebce04e69b

- Prevent bind settings from being updated in combat. - Close the UI during combat. - Resolve conflicting keybinds and inform the user of any results. - Resolve conflicts for talents, but only when they are active.
author Nenue
date Mon, 22 Aug 2016 09:24:02 -0400
parents a545933ddf3d
children d8bb2629fea8
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@57 114 if binds and (not frame.HotKey:IsVisible()) 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@57 124
Nenue@57 125 hooksecurefunc(frame.HotKey, 'SetVertexColor', function(self, r,g,b,a)
Nenue@57 126 hotkeyText[frame]:SetTextColor(r,g,b,a)
Nenue@57 127 end)
Nenue@6 128 end
Nenue@6 129
Nenue@50 130 hotkeyText[frame]:SetText(bindingsText)
Nenue@50 131 hotkeyText[frame]:Show()
Nenue@50 132 print('|cFF00FFFFUpdate text for', frame:GetName(), '|cFFFFFF00'..tostring(bindingsText)..'|r')
Nenue@50 133
Nenue@6 134 return
Nenue@6 135 end
Nenue@6 136 end
Nenue@6 137 end
Nenue@6 138
Nenue@6 139 if hotkeyText[frame] then
Nenue@6 140 hotkeyText[frame]:SetText(nil)
Nenue@6 141 --print('|cFFFF4400cleared text from', frame:GetName())
Nenue@6 142 end
Nenue@6 143 end
Nenue@6 144
Nenue@6 145 hotkey.actionbar = function()
Nenue@50 146 wipe(actionFrames)
Nenue@50 147 -- reset frames list
Nenue@50 148
Nenue@6 149 if ActionBarButtonEventsFrame.frames then
Nenue@6 150 for index, frame in ipairs(ActionBarButtonEventsFrame.frames) do
Nenue@50 151 local slot = frame.action
Nenue@50 152 local actionType, actionID = GetActionInfo(slot)
Nenue@50 153 local hasAction = HasAction(slot)
Nenue@50 154 local indexKey = kb.FormatActionID(actionType, actionID)
Nenue@50 155 actionSlots[slot] = frame
Nenue@50 156 actionFrames[indexKey] = actionFrames[indexKey] or {}
Nenue@50 157 if not tContains(actionFrames[indexKey]) then
Nenue@50 158 tinsert(actionFrames[indexKey], frame)
Nenue@50 159 actionIndex[slot] = indexKey
Nenue@50 160 end
Nenue@50 161
Nenue@50 162 hotkey.UpdateHotKey(frame, actionType, actionID, hasAction)
Nenue@6 163 end
Nenue@6 164 end
Nenue@6 165 end
Nenue@6 166
Nenue@6 167 hotkey.actionslot = function(self, event, slot)
Nenue@27 168 print(actionSlots[slot], event, slot, GetActionInfo(slot))
Nenue@6 169 --print(GetActionButtonForID(slot))
Nenue@27 170 local atype, aid = GetActionInfo(slot)
Nenue@50 171 local indexKey = kb.FormatActionID(atype, aid)
Nenue@50 172 local frame = actionSlots[slot]
Nenue@50 173 if frame then
Nenue@50 174 hotkey.UpdateHotKey(frame, atype, aid, HasAction(slot))
Nenue@29 175 end
Nenue@29 176
Nenue@6 177 end
Nenue@6 178
Nenue@6 179 hotkey.player = function()
Nenue@6 180 hotkey.actionbar()
Nenue@6 181 end
Nenue@6 182
Nenue@6 183
Nenue@27 184
Nenue@6 185 hotkey.pet = function(self, event, arg1)
Nenue@6 186 if event == 'UNIT_PET' and arg1 == 'player' then
Nenue@6 187 if PetHasActionBar() and UnitIsVisible("pet") then
Nenue@6 188 hotkey.wrapEvent('PET_UI_CLOSE', 'pet')
Nenue@6 189 hotkey.wrapEvent('PET_BAR_UPDATE', 'pet')
Nenue@6 190 else
Nenue@6 191 hotkey.unwrapEvent('PET_UI_CLOSE')
Nenue@6 192 hotkey.unwrapEvent('PET_BAR_UPDATE')
Nenue@6 193 return
Nenue@6 194 end
Nenue@6 195 end
Nenue@6 196
Nenue@6 197 for i=1, NUM_PET_ACTION_SLOTS, 1 do
Nenue@6 198 local button = _G['PetActionButton'.. i]
Nenue@6 199 --print(button:GetName())
Nenue@6 200 for k, v in pairs(button) do
Nenue@6 201 --print(' ', k, type(v))
Nenue@6 202 end
Nenue@6 203 end
Nenue@6 204 end
Nenue@6 205
Nenue@6 206
Nenue@27 207 --- used to pick up changes from user interaction
Nenue@27 208 local changeIndex = 1
Nenue@27 209 hotkey.binding = function()
Nenue@27 210 print('|cFFFF0088BindingsUpdate')
Nenue@27 211 local changeNum = #kb.ChangedBindings
Nenue@27 212 if changeNum >= changeIndex then
Nenue@50 213
Nenue@50 214
Nenue@27 215 for i = changeIndex, changeNum do
Nenue@27 216 local actionType, actionID, name = unpack(kb.ChangedBindings[i])
Nenue@50 217 local actionKey = kb.FormatActionID(actionType, actionID)
Nenue@50 218 local frames = actionFrames[actionKey]
Nenue@50 219 if frames then
Nenue@50 220 for i, frame in ipairs(frames) do
Nenue@50 221 print('|cFFFF0088'..actionKey..'|r', frame)
Nenue@50 222 if frame then
Nenue@50 223 hotkey.UpdateHotKey(frame , actionType, actionID, HasAction(frame.action))
Nenue@50 224 end
Nenue@50 225 end
Nenue@50 226 else
Nenue@50 227 print('no frames picked up, rebuild')
Nenue@50 228 hotkey.actionbar()
Nenue@27 229 end
Nenue@27 230
Nenue@27 231 changeIndex = i + 1
Nenue@27 232 end
Nenue@27 233
Nenue@27 234 end
Nenue@27 235 end