ef91c25 - Fix totem tracker bug when changing spec
[wowui.git] / OmaAB / ActionBars.lua
1 -- ActionBars.lua
2 local _;
3 local pairs = pairs;
4 local format = string.format;
5 local ssub = string.sub;
6 local GetActionInfo, GetActionTexture = GetActionInfo, GetActionTexture;
7 local GetActionLossOfControlCooldown = GetActionLossOfControlCooldown;
8 local GetActionCooldown, GetActionCharges = GetActionCooldown, GetActionCharges;
9 local IsConsumableAction, IsStackableAction = IsConsumableAction, IsStackableAction;
10 local IsItemAction, GetActionCount = IsItemAction, GetActionCount;
11 local IsSpellOverlayed, GetMacroSpell = IsSpellOverlayed, GetMacroSpell;
12 local IsMounted = IsMounted;
13 local HasAction, IsUsableAction = HasAction, IsUsableAction;
14 local IsCurrentAction, IsAutoRepeatAction = IsCurrentAction, IsAutoRepeatAction;
15 local CreateFrame = CreateFrame;
16 local RegisterStateDriver = RegisterStateDriver;
17 local CooldownFrame_Set, CooldownFrame_Clear = CooldownFrame_Set, CooldownFrame_Clear;
18 local CTimerAfter = C_Timer.After;
19 local GameTooltip = GameTooltip;
20 local GameTooltip_SetDefaultAnchor = GameTooltip_SetDefaultAnchor;
21 local COOLDOWN_TYPE_LOSS_OF_CONTROL = COOLDOWN_TYPE_LOSS_OF_CONTROL;
22 local COOLDOWN_TYPE_NORMAL = COOLDOWN_TYPE_NORMAL;
23 local CDTexture = "Interface\\Cooldown\\edge";
24 local locCDTexture = "Interface\\Cooldown\\edge-LoC";
25
26 local BUTTONLOCK = true; -- change to lock button dragging
27
28 local settings = {
29     ["Oma1"] = {
30         bar = 1,
31         start = 1,
32         length = 12,
33         columns = 4,
34         size = 40,
35         x = 580,
36         y = 300,
37         nomouse = true,
38     },
39     ["Oma2"] = {
40         bar = 2,
41         start = 13,
42         length = 12,
43         columns = 4,
44         size = 40,
45         x = 580,
46         y = 180,
47         nomouse = true,
48     },
49     ["Oma3"] = {
50         bar = 3,
51         start = 25,
52         length = 12,
53         columns = 3,
54         x = 1824,
55         y = 128,
56         flyout = "LEFT",
57     },
58     ["Oma4"] = {
59         bar = 4,
60         start = 37,
61         length = 12,
62         columns = 3,
63         x = 1824,
64         y = 256,
65     },
66     ["Oma5"] = {
67         bar = 5,
68         start = 49,
69         length = 12,
70         columns = 3,
71         x = 1000,
72         y = 840,
73     },
74     ["Oma6"] = {
75         bar = 6,
76         start = 61,
77         length = 12,
78         x = 1000,
79         y = 600,
80     },
81     -- used as bonus bars for some classes
82     ["Oma7"] = {
83         bar = 7,
84         start = 73,
85         length = 12,
86         x = 1000,
87         y = 760,
88     },
89     ["Oma8"] = {
90         bar = 8,
91         start = 85,
92         length = 12,
93         x = 1000,
94         y = 720,
95     },
96     ["Oma9"] = {
97         bar = 9,
98         start = 97,
99         length = 12,
100         x = 1000,
101         y = 680,
102     },
103     ["Oma10"] = {
104         bar = 10,
105         start = 109,
106         length = 12,
107         x = 1000,
108         y = 640,
109     },
110 };
111
112 local usingBonusbars = {
113     --["WARRIOR"] = {[7]=true, [8]=true, [9]=true}, -- not using stance separated actionbars
114     ["DRUID"] = {[7]=true, [8]=true, [9]=true}, -- moonkin form page is usable anyway
115     --["DRUID"] = {[7]=true, [8]=true, [9]=true,[10]=true},
116     ["ROGUE"] = {[7]=true},
117     --["PRIEST"] = {[7]=true}, -- shadowform doesn't change abilities
118 };
119
120 local chars = {
121     ["Stormreaver"] = {
122         ["Vildan"] = {1, 2, 3, 4,},
123         ["Gedren"] = {1, 2, 3, 4,},
124         ["Gazden"] = {1, 2, 3, 4,},
125     },
126 };
127
128 local buttons = {};
129 local activeButtons = {};
130
131 local ActionBars = CreateFrame("Frame", "OmaActionBars", UIParent);
132 local inheritedFrames =
133 "SecureActionButtonTemplate,SecureHandlerDragTemplate,SecureHandlerStateTemplate";
134
135 local function showTooltip(secure)
136     GameTooltip_SetDefaultAnchor(GameTooltip, secure);
137     GameTooltip:SetAction(secure:GetAttribute("action"));
138 end
139
140 local function hideTooltip()
141     GameTooltip:Hide();
142 end
143
144 local numChargeCDs = 0;
145 local function createChargeCD(parent)
146     numChargeCDs = numChargeCDs + 1;
147     local frame = CreateFrame("Cooldown", "OmaChargeCD"..numChargeCDs, parent, "CooldownFrameTemplate");
148     frame:SetHideCountdownNumbers(false);
149     frame:SetDrawSwipe(false);
150     frame:SetAllPoints(parent);
151     frame:SetFrameStrata("TOOLTIP");
152     return frame;
153 end
154
155 local function clearChargeCD(parent)
156     if parent.chargecd then CooldownFrame_Clear(parent.chargecd) end
157 end
158
159 local function startChargeCD(parent, start, duration, modrate)
160     if start == 0 then
161         return clearChargeCD(parent);
162     end
163     parent.chargecd = parent.chargecd or createChargeCD(parent);
164     CooldownFrame_Set(parent.chargecd, start, duration, true, true, modrate);
165 end
166
167 local redoCooldown;
168
169 local function updateCooldown(button, slot)
170     -- CD update from FrameXML/ActionButton.lua
171     local locstart, locduration = GetActionLossOfControlCooldown(slot);
172     local start, duration, enable, modrate = GetActionCooldown(slot);
173     local charges, maxcharges, chargestart, chargeduration, chargemodrate = GetActionCharges(slot);
174     if (locstart + locduration) > (start + duration) then
175         if button.cd.currentCooldownType ~= COOLDOWN_TYPE_LOSS_OF_CONTROL then
176             button.cd:SetEdgeTexture(locCDTexture);
177             button.cd:SetSwipeColor(0.17, 0, 0);
178             button.cd:SetHideCountdownNumbers(true);
179             button.cd.currentCooldownType = COOLDOWN_TYPE_LOSS_OF_CONTROL;
180         end
181
182         CooldownFrame_Set(button.cd, locstart, locduration, true, true, modrate);
183         clearChargeCD(button);
184     else
185         if button.cd.currentCooldownType ~= COOLDOWN_TYPE_NORMAL then
186             button.cd:SetEdgeTexture(CDTexture);
187             button.cd:SetSwipeColor(0, 0, 0);
188             button.cd:SetHideCountdownNumbers(false);
189             button.cd.currentCooldownType = COOLDOWN_TYPE_NORMAL;
190         end
191
192         if locstart > 0 then
193             button.cd:SetScript("OnCooldownDone", redoCooldown);
194         end
195         if charges and maxcharges and maxcharges > 1 and charges < maxcharges then
196             startChargeCD(button, chargestart, chargeduration, chargemodrate);
197         else
198             clearChargeCD(button);
199         end
200         CooldownFrame_Set(button.cd, start, duration, enable, false, modrate);
201     end
202 end
203
204 local function redoCooldown(cd)
205     local button = cd:GetParent();
206     cd:SetScript("OnCooldownDone", nil);
207     updateCooldown(button, button.slot);
208 end
209
210 local function updateCount(button, slot)
211     if IsConsumableAction(slot) or IsStackableAction(slot) or
212             (not IsItemAction(slot) and GetActionCount(slot) > 0) then
213         local count = GetActionCount(slot);
214         if count > 99 then
215             button.count:SetText("*");
216         else
217             button.count:SetText(count);
218         end
219         button.count:Show();
220     else
221         local charges, maxcharges = GetActionCharges(slot);
222         if maxcharges > 1 then
223             button.count:SetText(charges);
224             button.count:Show();
225         else
226             button.count:Hide();
227         end
228     end
229 end
230
231 local function updateUsable(button, slot)
232     local isUsable, noMana = IsUsableAction(slot);
233     if isUsable then
234         button.icon:SetVertexColor(1, 1, 1);
235     elseif noMana then
236         button.icon:SetVertexColor(0, 0.5, 1);
237     else
238         button.icon:SetVertexColor(0.4, 0.4, 0.4);
239     end
240 end
241
242 local function updateState(button, slot)
243     button:SetChecked(IsCurrentAction(slot) or IsAutoRepeatAction(slot));
244 end
245
246 local function updateGlow(button, slot)
247     local stype, id, _ = GetActionInfo(slot);
248     if stype == "spell" and IsSpellOverlayed(id) then
249         button.glow:Show();
250     elseif stype == "macro" then
251         local _, _, macroid = GetMacroSpell(id);
252         if macroid and IsSpellOverlayed(macroid) then
253             button.glow:Show();
254         else
255             button.glow:Hide();
256         end
257     else -- TODO FlyoutHasSpell glow
258         button.glow:Hide();
259     end
260 end
261
262 local function startGlow(button, slot, spell)
263     local stype, id, _ = GetActionInfo(slot);
264     if stype == "spell" and id == spell then
265         button.glow:Show();
266     elseif stype == "macro" then
267         local _, _, macroid = GetMacroSpell(id);
268         if macroid and macroid == spell then
269             button.glow:Show();
270         end
271     end
272     -- TODO FlyoutHasSpell glow
273 end
274
275 local function stopGlow(button, slot, spell)
276     local stype, id, _ = GetActionInfo(slot);
277     if stype == "spell" and id == spell then
278         button.glow:Hide();
279     elseif stype == "macro" then
280         local _, _, macroid = GetMacroSpell(id);
281         if macroid and macroid == spell then
282             button.glow:Hide();
283         end
284     end
285     -- TODO FlyoutHasSpell glow
286 end
287
288 local function updateButton(button, slot)
289     if HasAction(slot) then
290         activeButtons[slot] = button;
291         button.base:Show();
292         button.icon:SetTexture(GetActionTexture(slot));
293         updateCooldown(button, slot);
294         updateUsable(button, slot);
295         updateState(button, slot);
296         updateCount(button, slot);
297         updateGlow(button, slot);
298         if not IsConsumableAction(slot) and not IsStackableAction(slot) then
299             button.text:SetText(ssub(GetActionText(slot) or "", 1, 4));
300             button.text:Show();
301         end
302         if button.hotkey.shown then button.hotkey:Show() end
303     else
304         activeButtons[slot] = nil;
305         if not button.grid then button.base:Hide() end
306         button.icon:SetTexture(nil);
307         button.cd:Hide();
308         button.count:Hide();
309         button.hotkey:Hide();
310         button.text:Hide();
311         button.glow:Hide();
312         button:SetChecked(false);
313     end
314 end
315
316 local function updateHotkeys(button)
317     local key = GetBindingKey(format("CLICK %s:LeftButton", button:GetName()));
318     if key and key ~= "" then
319         -- from LibKeyBound-1.0
320         key = key:upper();
321         key = key:gsub(" ", "");
322         key = key:gsub("ALT%-", "a");
323         key = key:gsub("CTRL%-", "c");
324         key = key:gsub("SHIFT%-", "s");
325         key = key:gsub("NUMPAD", "n");
326         button.hotkey:SetText(key);
327         button.hotkey.shown = true;
328         button.hotkey:Show();
329     else
330         button.hotkey.shown = nil;
331         button.hotkey:Hide();
332     end
333 end
334
335 local mainbartoggle = "[overridebar][possessbar][shapeshift]possess;";
336 mainbartoggle = mainbartoggle.."[bonusbar:1,stealth:1]bonusbar2;"; -- prowl
337 mainbartoggle = mainbartoggle.."[bonusbar:1]bonusbar1;[bonusbar:2]bonusbar2;"; -- cat form, unused
338 mainbartoggle = mainbartoggle.."[bonusbar:3]bonusbar3;[bonusbar:4]bonusbar4;"; -- bear form, moonkin form
339 mainbartoggle = mainbartoggle.."normal";
340 local function setupSnippets(secure, slot)
341     -- FrameXML/SecureHandlers.lua has arguments and return value
342     -- args: self, button, kind, value, ... (kind, value, ... from GetCursorInfo())
343     -- returns: kind, target, detail
344     -- or: "clear", kind, target, detail
345     -- used for Pickup* functions
346     -- some of these snippets based on LibActionButton-1.0
347     secure:SetAttribute("_ondragstart", [=[
348         return "action", self:GetAttribute("action");
349     ]=]);
350     secure:SetAttribute("_onreceivedrag", [=[
351         if not kind or not value then return nil end
352         return "action", self:GetAttribute("action");
353     ]=]);
354     -- pre-wrapper can pass a message to post-wrapper
355     secure:WrapScript(secure, "OnDragStart", [=[
356         local kind, value = GetActionInfo(self:GetAttribute("action"));
357         return "message", format("%s|%s", tostring(kind), tostring(value));
358     ]=], [=[
359         local kind, value = GetActionInfo(self:GetAttribute("action"));
360         if message ~= format("%s|%s", tostring(kind), tostring(value)) then
361             self:CallMethod("ActionChanged");
362         end
363     ]=]);
364     secure:WrapScript(secure, "OnReceiveDrag", [=[
365         local kind, value = GetActionInfo(self:GetAttribute("action"));
366         return "message", format("%s|%s", tostring(kind), tostring(value));
367     ]=], [=[
368         local kind, value = GetActionInfo(self:GetAttribute("action"));
369         if message ~= format("%s|%s", tostring(kind), tostring(value)) then
370             self:CallMethod("ActionChanged");
371         end
372     ]=]);
373     function secure:UpdateState()
374         return updateState(self, self.slot);
375     end
376     secure:WrapScript(secure, "OnClick", [=[
377         local kind, value = GetActionInfo(self:GetAttribute("action"));
378         return nil, format("%s|%s", tostring(kind), tostring(value));
379     ]=], [=[
380         local kind, value = GetActionInfo(self:GetAttribute("action"));
381         if message ~= format("%s|%s", tostring(kind), tostring(value)) then
382             self:CallMethod("ActionChanged");
383         else
384             self:CallMethod("UpdateState");
385         end
386     ]=]);
387     if slot < 13 then
388         -- first action bar has possible states based on vehicle/possess etc.
389         secure:SetAttribute("origaction", slot);
390         secure:SetAttribute("_onstate-possess", [=[
391             local oldslot = self:GetAttribute("action");
392             if newstate == "possess" then
393                 local slot;
394                 if HasVehicleActionBar() then
395                     slot = (GetVehicleBarIndex()-1)*12+self:GetAttribute("origaction");
396                 elseif HasOverrideActionBar() then
397                     slot = (GetOverrideBarIndex()-1)*12+self:GetAttribute("origaction");
398                 elseif HasTempShapeshiftActionBar() then
399                     slot = (GetTempShapeshiftBarIndex()-1)*12+self:GetAttribute("origaction");
400                 else
401                     -- something wrong, just revert to normal
402                     print("Possess bar index not found");
403                     slot = self:GetAttribute("origaction");
404                 end
405                 self:SetAttribute("action", slot);
406             elseif newstate == "bonusbar1" then
407                 self:SetAttribute("action", 72+self:GetAttribute("origaction"));
408             elseif newstate == "bonusbar2" then
409                 self:SetAttribute("action", 84+self:GetAttribute("origaction"));
410             elseif newstate == "bonusbar3" then
411                 self:SetAttribute("action", 96+self:GetAttribute("origaction"));
412             elseif newstate == "bonusbar4" then
413                 --self:SetAttribute("action", 108+self:GetAttribute("origaction"));
414                 -- moonkin form, don't change actionbar
415                 self:SetAttribute("action", self:GetAttribute("origaction"));
416             else
417                 self:SetAttribute("action", self:GetAttribute("origaction"));
418             end
419             self:CallMethod("ActionChanged", oldslot);
420         ]=]);
421         RegisterStateDriver(secure, "possess", mainbartoggle);
422     else
423         function secure:ShowButton() if HasAction(slot) then activeButtons[slot] = self end end
424         function secure:HideButton() activeButtons[slot] = nil end
425         -- all other action bar are hidden if with overridebar or vehicleui (not shapeshift, possessbar)
426         -- default Bartender4 options
427         secure:SetAttribute("_onstate-possess", [=[
428             if newstate == "possess" then
429                 self:Hide();
430                 self:CallMethod("HideButton");
431             else
432                 self:Show();
433                 self:CallMethod("ShowButton");
434             end
435         ]=]);
436         RegisterStateDriver(secure, "possess", "[overridebar][vehicleui] possess; normal");
437     end
438 end
439
440 local function createActionBar(parent, config)
441     local prev;
442     local i = 0;
443     local bar = CreateFrame("Frame", "OmaBTBar"..config.bar, parent, "SecureFrameTemplate");
444     bar:SetPoint("TOPLEFT", parent, "BOTTOMLEFT", config.x, config.y);
445     bar:SetWidth(1);
446     bar:SetHeight(1);
447     if config.hidden then
448         bar:Hide();
449     end
450     for slot = config.start, config.start+config.length-1 do
451         local secure = CreateFrame("CheckButton", "OmaBT"..slot, bar, inheritedFrames);
452         secure.slot = slot;
453         if slot == config.start then
454             secure:SetPoint("TOPLEFT");
455         elseif config.columns and i % config.columns == 0 then
456             secure:SetPoint("TOPLEFT", _G["OmaBT"..(slot-config.columns)], "BOTTOMLEFT");
457         else
458             secure:SetPoint("TOPLEFT", prev, "TOPRIGHT");
459         end
460         secure:RegisterForClicks("AnyUp");
461         if not BUTTONLOCK then
462             secure:RegisterForDrag("LeftButton", "RightButton");
463         end
464         if config.nomouse then
465             secure:EnableMouse(false);
466         else
467             -- only show tooltips for bars with mouse interaction
468             secure:SetScript("OnEnter", showTooltip);
469             secure:SetScript("OnLeave", hideTooltip);
470         end
471         secure:SetWidth(config.size or 32);
472         secure:SetHeight(config.size or 32);
473         secure.base = secure:CreateTexture(nil, "BACKGROUND");
474         secure.base:SetAllPoints();
475         secure.base:SetColorTexture(0, 0, 0, 0.5);
476         secure.iconbase = secure:CreateTexture(nil, "BORDER");
477         secure.iconbase:SetPoint("TOPLEFT", secure.base, "TOPLEFT", 1, -1);
478         secure.iconbase:SetPoint("BOTTOMRIGHT", secure.base, "BOTTOMRIGHT", -1, 1);
479         secure.iconbase:SetColorTexture(0, 0, 0, 0.5);
480         secure.iconbase:Hide();
481         secure.icon = secure:CreateTexture(nil, "ARTWORK");
482         secure.icon:SetPoint("TOPLEFT", secure.iconbase, "TOPLEFT");
483         secure.icon:SetPoint("BOTTOMRIGHT", secure.iconbase, "BOTTOMRIGHT");
484         secure.icon:SetTexCoord(0.07, 0.93, 0.07, 0.93);
485         secure:SetCheckedTexture("Interface\\Buttons\\CheckButtonHilight");
486         secure.autocastable = secure:CreateTexture(nil, "OVERLAY");
487         secure.autocastable:SetPoint("CENTER");
488         secure.autocastable:SetWidth(58);
489         secure.autocastable:SetHeight(58);
490         secure.autocastable:SetTexture("Interface\\Buttons\\UI-AutoCastableOverlay");
491         secure.autocastable:Hide();
492         secure.glow = secure:CreateTexture(nil, "OVERLAY", nil, 1);
493         secure.glow:SetPoint("CENTER");
494         secure.glow:SetWidth(config.size and config.size+26 or 53);
495         secure.glow:SetHeight(config.size and config.size+26 or 53);
496         secure.glow:SetTexture("Interface\\SpellActivationOverlay\\IconAlert");
497         secure.glow:SetTexCoord(0.00781250, 0.50781250, 0.27734375, 0.52634375);
498         secure.glow:Hide();
499         secure.hotkey = secure:CreateFontString(nil, "OVERLAY", "NumberFontNormalGray");
500         secure.hotkey:SetPoint("TOPRIGHT", secure, "TOPRIGHT", 2, -1);
501         secure.count = secure:CreateFontString(nil, "OVERLAY", "NumberFontNormal");
502         secure.count:SetPoint("BOTTOMRIGHT", secure, "BOTTOMRIGHT", 2, -1);
503         secure.text = secure:CreateFontString(nil, "OVERLAY", "NumberFontNormal");
504         secure.text:SetPoint("BOTTOMLEFT", secure, "BOTTOMLEFT", 2, -1);
505         secure.text:Hide();
506         secure.cd = CreateFrame("Cooldown", "OmaBTCD"..slot, secure, "CooldownFrameTemplate");
507         secure.cd:SetAllPoints();
508         secure:SetAttribute("type", "action");
509         secure:SetAttribute("action", slot);
510         if config.flyout then
511             secure:SetAttribute("flyoutDirection", config.flyout);
512         end
513         function secure:ActionChanged(oldslot)
514             if oldslot then activeButtons[oldslot] = nil end
515             self.slot = self:GetAttribute("action");
516             return updateButton(self, self.slot);
517         end
518         secure:ActionChanged(); -- initial update
519         setupSnippets(secure, slot);
520         updateHotkeys(secure);
521         buttons[slot] = secure;
522         prev = secure;
523         i = i + 1;
524     end
525 end
526
527 local function initialize()
528     local _, class = UnitClass("player");
529     local name, realm = UnitFullName("player");
530     ActionBars:SetFrameStrata("LOW");
531     ActionBars:SetPoint("BOTTOMLEFT");
532     ActionBars:SetWidth(1);
533     ActionBars:SetHeight(1);
534     for _, config in pairs(settings) do
535         if (not usingBonusbars[class] or not usingBonusbars[class][config.bar]) and
536            (not chars[realm] or not chars[realm][name] or chars[realm][name][config.bar]) then
537             createActionBar(ActionBars, config);
538         end
539     end
540 end
541
542 local function setupBindings()
543     BINDING_HEADER_OmaAB = "Oma Action Bar";
544     for i = 1,10 do
545         _G["BINDING_HEADER_OMAABBLANK"..i] = "Bar "..i;
546         for j = 1,12 do
547             _G[format("BINDING_NAME_CLICK OmaBT%d:LeftButton", (i-1)*12+j)] = format("Bar %d Button %d", i, j);
548         end
549     end
550 end
551
552 local mounted = false;
553 -- throttleCD 3-state, nil -> false -> true -> nil
554 -- this way there's not double update each time the first
555 -- update event comes, instead the extra throttled update comes
556 -- if there are >2 ACTIONBAR_UPDATE_COOLDOWN events in one frame
557 local throttleCD = nil;
558 local function throttleCDDone()
559     -- update CD once more to confirm newest CD change is taken in even with some throttling
560     for _, button in pairs(activeButtons) do
561         updateCooldown(button, button.slot);
562     end
563 end
564 local function throttleCDReset()
565     throttleCD = nil;
566 end
567
568 local events = {
569     ["ACTIONBAR_UPDATE_COOLDOWN"] = function()
570         if not throttleCD then -- only update at most once/frame
571             throttleCD = throttleCD == false and true or false;
572             if throttleCD then
573                 CTimerAfter(0.01, throttleCDDone); -- wait one frame
574             else
575                 for _, button in pairs(activeButtons) do
576                     updateCooldown(button, button.slot);
577                 end
578                 CTimerAfter(0.01, throttleCDReset); -- wait one frame
579             end
580         end
581     end,
582     ["SPELL_UPDATE_CHARGES"] = function()
583         for _, button in pairs(activeButtons) do
584             updateCount(button, button.slot);
585         end
586     end,
587     ["ACTIONBAR_SLOT_CHANGED"] = function(slot)
588         if buttons[slot] then buttons[slot]:ActionChanged() end
589     end,
590     ["ACTIONBAR_SHOWGRID"] = function()
591         for _, button in pairs(buttons) do
592             button.grid = true;
593             button.iconbase:Show();
594             if not activeButtons[button.slot] then button.base:Show() end
595         end
596     end,
597     ["ACTIONBAR_HIDEGRID"] = function()
598         for _, button in pairs(buttons) do
599             button.grid = nil;
600             button.iconbase:Hide();
601             if not activeButtons[button.slot] then button.base:Hide() end
602         end
603     end,
604     ["ACTIONBAR_UPDATE_STATE"] = function()
605         for _, button in pairs(activeButtons) do
606             updateState(button, button.slot);
607         end
608     end,
609     ["ACTIONBAR_UPDATE_USABLE"] = function()
610         for _, button in pairs(activeButtons) do
611             updateUsable(button, button.slot);
612         end
613     end,
614     ["UPDATE_OVERRIDE_ACTIONBAR"] = function()
615         if buttons[1] then -- called before PLAYER_LOGIN
616             for _, button in pairs(buttons) do
617                 updateButton(button, button.slot);
618             end
619         end
620     end,
621     ["START_AUTOREPEAT_SPELL"] = function()
622         for _, button in pairs(activeButtons) do
623             if IsAutoRepeatAction(button.slot) then
624                 button.autorepeating = true;
625                 button.icon:SetVertexColor(0, 1, 0.5);
626             end
627         end
628     end,
629     ["STOP_AUTOREPEAT_SPELL"] = function()
630         for _, button in pairs(activeButtons) do
631             if button.autorepeating then
632                 button.autorepeating = nil;
633                 updateUsable(button, button.slot);
634             end
635         end
636     end,
637     ["SPELL_ACTIVATION_OVERLAY_GLOW_SHOW"] = function(spell)
638         -- TODO create mapping from spellIDs to buttons
639         for _, button in pairs(activeButtons) do
640             startGlow(button, button.slot, spell);
641         end
642     end,
643     ["SPELL_ACTIVATION_OVERLAY_GLOW_HIDE"] = function(spell)
644         -- TODO create mapping from spellIDs to buttons
645         for _, button in pairs(activeButtons) do
646             stopGlow(button, button.slot, spell);
647         end
648     end,
649     ["UPDATE_BINDINGS"] = function()
650         for _, button in pairs(buttons) do
651             updateHotkeys(button);
652         end
653     end,
654     ["UNIT_AURA"] = function(unit)
655         -- using UNIT_AURA instead of COMPANION_UPDATE to not update every time
656         -- someone mounts, tracking player mount status with COMPANION_UPDATE is
657         -- inconsistent
658         if (not mounted and IsMounted()) or (mounted and not IsMounted()) then
659             mounted = not mounted;
660             for _, button in pairs(activeButtons) do
661                 updateState(button, button.slot);
662             end
663         end
664     end,
665     ["UPDATE_ALL_BUTTONS"] = function()
666         for _, button in pairs(buttons) do
667             updateButton(button, button.slot);
668         end
669     end,
670     ["PLAYER_LOGIN"] = function()
671         GameTooltip = _G["GameTooltip"];
672         initialize();
673     end,
674     ["ADDON_LOADED"] = function(addon)
675         if addon == "OmaAB" then
676             setupBindings();
677             ActionBars:UnregisterEvent("ADDON_LOADED");
678         end
679     end,
680 };
681 events["LOSS_OF_CONTROL_ADDED"] = events["ACTIONBAR_UPDATE_COOLDOWN"];
682 events["LOSS_OF_CONTROL_UPDATE"] = events["ACTIONBAR_UPDATE_COOLDOWN"]; -- TODO might change once tooltips are in
683 events["PLAYER_MOUNT_DISPLAY_CHANGED"] = events["ACTIONBAR_UPDATE_USABLE"];
684 events["TRADE_SKILL_SHOW"] = events["ACTIONBAR_UPDATE_STATE"];
685 events["TRADE_SKILL_CLOSE"] = events["ACTIONBAR_UPDATE_STATE"];
686 events["ARCHAEOLOGY_CLOSED"] = events["ACTIONBAR_UPDATE_STATE"];
687 events["PLAYER_ENTERING_WORLD"] = events["UPDATE_ALL_BUTTONS"];
688 events["UPDATE_VEHICLE_ACTIONBAR"] = events["UPDATE_ALL_BUTTONS"];
689 events["UPDATE_SHAPESHIFT_FORM"] = events["UPDATE_ALL_BUTTONS"];
690 events["SPELL_UPDATE_ICON"] = events["UPDATE_ALL_BUTTONS"];
691 events["PET_STABLE_UPDATE"] = events["UPDATE_ALL_BUTTONS"];
692 events["PET_STABLE_SHOW"] = events["UPDATE_ALL_BUTTONS"];
693 events["PLAYER_SPECIALIZATION_CHANGED"] = events["UPDATE_ALL_BUTTONS"];
694 events["UNIT_ENTERED_VEHICLE"] = function(unit)
695     if unit == "player" then events["ACTIONBAR_UPDATE_STATE"]() end
696 end
697 events["UNIT_EXITED_VEHICLE"] = events["UNIT_ENTERED_VEHICLE"];
698
699 ActionBars:RegisterEvent("ADDON_LOADED");
700 ActionBars:RegisterEvent("PLAYER_LOGIN");
701 ActionBars:RegisterEvent("ACTIONBAR_UPDATE_COOLDOWN");
702 ActionBars:RegisterEvent("ACTIONBAR_UPDATE_USABLE");
703 ActionBars:RegisterEvent("ACTIONBAR_UPDATE_STATE");
704 ActionBars:RegisterEvent("ACTIONBAR_SLOT_CHANGED");
705 ActionBars:RegisterEvent("ACTIONBAR_SHOWGRID");
706 ActionBars:RegisterEvent("ACTIONBAR_HIDEGRID");
707 ActionBars:RegisterEvent("SPELL_UPDATE_ICON");
708 ActionBars:RegisterEvent("SPELL_UPDATE_CHARGES");
709 ActionBars:RegisterEvent("SPELL_ACTIVATION_OVERLAY_GLOW_SHOW");
710 ActionBars:RegisterEvent("SPELL_ACTIVATION_OVERLAY_GLOW_HIDE");
711 ActionBars:RegisterEvent("UPDATE_VEHICLE_ACTIONBAR");
712 ActionBars:RegisterEvent("UPDATE_OVERRIDE_ACTIONBAR");
713 ActionBars:RegisterEvent("PLAYER_MOUNT_DISPLAY_CHANGED");
714 ActionBars:RegisterEvent("PLAYER_SPECIALIZATION_CHANGED");
715 ActionBars:RegisterEvent("UNIT_ENTERED_VEHICLE");
716 ActionBars:RegisterEvent("UNIT_EXITED_VEHICLE");
717 ActionBars:RegisterEvent("PET_STABLE_UPDATE");
718 ActionBars:RegisterEvent("PET_STABLE_SHOW");
719 ActionBars:RegisterEvent("UPDATE_BINDINGS");
720 ActionBars:RegisterUnitEvent("UNIT_AURA", "player");
721 ActionBars:SetScript("OnEvent", function(self, event, arg1)
722     events[event](arg1);
723 end);