f61b9c7d2cf7a5dd0731a7936d8a632e293cbc85
[wowui.git] / OmaAB / ActionBars.lua
1 -- ActionBars.lua
2 local _;
3 local min = math.min;
4 local CreateFrame = CreateFrame;
5 local UnitXP, UnitXPMax, GetXPExhaustion = UnitXP, UnitXPMax, GetXPExhaustion;
6 local ExhaustionToolTipText = ExhaustionToolTipText;
7 local GameTooltip = nil;
8
9 local width = 300;
10
11 local ActionBars = CreateFrame("Frame", "OmaActionBars");
12
13 local function expBar(parent)
14     local frame = CreateFrame("Frame", "OmaExpBar", parent);
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, restw = xp/xpmax*width, rested/xpmax*width;
37         local space = width - current;
38         frame.bar:SetWidth(current);
39         if rested then
40             frame.rest:SetWidth(min(space, restw));
41             frame.rest:Show();
42             frame.text:SetFormattedText("%d / %d (+%d)", xp, xpmax, rested/2);
43             frame.bar:SetVertexColor(0, 0.5, 1, 0.9);
44         else
45             frame.rest:Hide();
46             frame.text:SetFormattedText("%d / %d", xp, xpmax);
47             frame.bar:SetVertexColor(0.6, 0.2, 1, 0.9);
48         end
49     end
50     updateXP();
51
52     frame:SetScript("OnEvent", function(self, event)
53         if event == "PLAYER_XP_UPDATE" or event == "PLAYER_LEVEL_UP" then
54             updateXP();
55         end
56     end);
57     frame:RegisterEvent("PLAYER_XP_UPDATE");
58     frame:RegisterEvent("PLAYER_LEVEL_UP");
59     -- from FrameXML/MainMenuBar.lua
60     frame:SetScript("OnEnter", function(frame) frame.text:Show(); ExhaustionToolTipText(); end);
61     frame:SetScript("OnLeave", function(frame) frame.text:Hide(); GameTooltip:Hide(); end);
62 end
63
64 local function initialize()
65     -- let other addons hook this to anchor tooltip elsewhere
66     GameTooltip = _G["GameTooltip"];
67     if UnitLevel("player") < 110 and not IsXPUserDisabled() then
68         expBar(UIParent);
69     end
70 end
71
72 ActionBars:RegisterEvent("PLAYER_LOGIN");
73 ActionBars:SetScript("OnEvent", function(self, event)
74     if event == "PLAYER_LOGIN" then
75         initialize();
76     end
77 end);