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