comparison HotKey.lua @ 70:131d9190db6b

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