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