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