facac67f5ea753372eae2c04a94a31076f2ebdcd
[wowui.git] / OmaAB / ActionBars.lua
1 -- ActionBars.lua
2 local _;
3 local min = math.min;
4 local CreateFrame, IsResting = CreateFrame, IsResting;
5 local UnitXP, UnitXPMax, GetXPExhaustion = UnitXP, UnitXPMax, GetXPExhaustion;
6 local CTimerAfter = C_Timer.After;
7 local ExhaustionToolTipText = ExhaustionToolTipText;
8 local GameTooltip = nil;
9
10 local width = 300;
11 local running = false;
12
13 local ActionBars = CreateFrame("Frame", "OmaActionBars");
14
15 local function expBar(parent)
16     local frame = CreateFrame("Frame", "OmaExpBar", parent);
17     frame:SetPoint("BOTTOM");
18     frame:SetWidth(width);
19     frame:SetHeight(10);
20     frame.base = frame:CreateTexture(nil, "BACKGROUND");
21     frame.base:SetAllPoints();
22     frame.base:SetColorTexture(0, 0, 0, 0.5);
23     frame.bar = frame:CreateTexture(nil, "BORDER");
24     frame.bar:SetPoint("TOPLEFT", frame.base, "TOPLEFT");
25     frame.bar:SetPoint("BOTTOMLEFT", frame.base, "BOTTOMLEFT");
26     frame.bar:SetColorTexture(1, 1, 1);
27     frame.rest = frame:CreateTexture(nil, "BORDER");
28     frame.rest:SetPoint("TOPLEFT", frame.bar, "TOPRIGHT");
29     frame.rest:SetPoint("BOTTOMLEFT", frame.bar, "BOTTOMRIGHT");
30     frame.rest:SetColorTexture(0, 0.3, 1, 0.7);
31     frame.text = frame:CreateFontString(nil, "ARTWORK", "GameFontHighlight");
32     frame.text:SetPoint("BOTTOM");
33     frame.text:Hide();
34
35     local function updateXP()
36         local xp, xpmax = UnitXP("player"), UnitXPMax("player");
37         local rested = GetXPExhaustion();
38         local current = xp/xpmax*width;
39         local space = width - current;
40         frame.bar:SetWidth(current);
41         if rested then
42             local restw = rested/xpmax*width;
43             frame.rest:SetWidth(min(space, restw));
44             frame.rest:Show();
45             frame.text:SetFormattedText("%d / %d (+%d)", xp, xpmax, rested/2);
46             frame.bar:SetVertexColor(0, 0.5, 1, 0.9);
47         else
48             frame.rest:Hide();
49             frame.text:SetFormattedText("%d / %d", xp, xpmax);
50             frame.bar:SetVertexColor(0.6, 0.2, 1, 0.9);
51         end
52     end
53     local function updater()
54         updateXP();
55         if running then CTimerAfter(10, updater) end
56     end
57     updateXP();
58
59     frame:SetScript("OnEvent", function(self, event)
60         if event == "PLAYER_XP_UPDATE" or event == "PLAYER_LEVEL_UP" then
61             updateXP();
62         elseif event == "PLAYER_UPDATE_RESTING" then
63             if IsResting() then
64                 running = true;
65                 CTimerAfter(6, updater);
66             else
67                 running = false;
68             end
69         end
70     end);
71     frame:RegisterEvent("PLAYER_XP_UPDATE");
72     frame:RegisterEvent("PLAYER_LEVEL_UP");
73     frame:RegisterEvent("PLAYER_UPDATE_RESTING");
74     -- from FrameXML/MainMenuBar.lua
75     frame:SetScript("OnEnter", function(frame) frame.text:Show(); ExhaustionToolTipText(); end);
76     frame:SetScript("OnLeave", function(frame) frame.text:Hide(); GameTooltip:Hide(); end);
77 end
78
79 local function initialize()
80     -- let other addons hook this to anchor tooltip elsewhere
81     GameTooltip = _G["GameTooltip"];
82     if UnitLevel("player") < 110 and not IsXPUserDisabled() then
83         expBar(UIParent);
84     end
85 end
86
87 ActionBars:RegisterEvent("PLAYER_LOGIN");
88 ActionBars:SetScript("OnEvent", function(self, event)
89     if event == "PLAYER_LOGIN" then
90         initialize();
91     end
92 end);