79feebe - Add boss frames
[wowui.git] / kehys / boss.lua
1 -- boss.lua
2 local _, addon = ...;
3 local unpack = unpack;
4 local format = string.format;
5 local CFrame = CreateFrame("Frame", "kehysBossInit", UIParent);
6 local CTimerAfter = C_Timer.After;
7
8 local barTexture = "Interface\\AddOns\\kehys\\images\\minimalist";
9
10 local function showTooltip(frame)
11     GameTooltip_SetDefaultAnchor(GameTooltip, frame);
12     GameTooltip:SetUnit(frame:GetAttribute("unit"));
13 end
14
15 local function hideTooltip()
16     GameTooltip:FadeOut();
17 end
18
19 local id = 1;
20 local updaters = {};
21 local function initBoss(parent, y, width, height, update, event)
22     assert(type(parent) == "table", "Boss frame creation missing parent!");
23     assert(type(y) == "number", "Boss frame creation missing Y offset!");
24     assert(type(width) == "number", "Boss frame creation missing width!");
25     assert(type(height) == "number", "Boss frame creation missing height!");
26     assert(type(update) == "function",
27            "Boss frame creation missing update function!");
28     assert(type(event) == "function",
29            "Boss frame creation missing event function!");
30
31     local unit = "boss"..id;
32     local f = CreateFrame("Button", "kehysBoss"..id, parent,
33                           "SecureUnitButtonTemplate,SecureHandlerStateTemplate");
34     id = id + 1;
35     f:Hide();
36     f:SetPoint("CENTER", parent, "CENTER", 540, y+200);
37     f:SetWidth(width);
38     f:SetHeight(height);
39     f.barwidth = width - 2; -- 1px padding
40     f:SetAttribute("unit", unit);
41     f:SetAttribute("displayed", unit);
42     f.unit = unit;
43     f.displayed = unit;
44     f.boss = true;
45     f.prev = {};
46
47     updaters[f] = function()
48         if f.updating then
49             CTimerAfter(0.1, updaters[f]);
50             return update(f);
51         end
52     end
53     f:SetScript("OnEvent", event);
54     f:SetScript("OnHide", function()
55         f:UnregisterAllEvents();
56         f.updating = false;
57         f.prev = {};
58     end);
59     f:SetScript("OnShow", function()
60         addon.RegisterEvents(f);
61         addon.RegisterUnitEvents(f);
62         event(f, "UPDATE_ALL_BARS");
63         f.updating = true;
64         updaters[f]();
65     end);
66     f:SetScript("OnEnter", showTooltip);
67     f:SetScript("OnLeave", hideTooltip);
68     f:RegisterForClicks("AnyDown");
69     f:SetAttribute("*type1", "target");
70     f:SetAttribute("*type2", "togglemenu");
71     f:SetAttribute("toggleForVehicle", false);
72
73     -- create visuals
74     f.base = f:CreateTexture(nil, "BACKGROUND");
75     f.base:SetAllPoints();
76     f.base:SetColorTexture(0, 0, 0, 0.5);
77     f.background = f:CreateTexture(nil, "BACKGROUND", nil, 1);
78     f.background:SetPoint("TOPLEFT", f, "TOPLEFT", 1, -1);
79     f.background:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", -1, 1);
80     f.health = f:CreateTexture(nil, "BORDER");
81     f.health:SetPoint("TOPLEFT", f.background, "TOPLEFT");
82     f.health:SetPoint("BOTTOMLEFT", f.background, "LEFT", 0, -height/8);
83     f.health:SetTexture(barTexture);
84     f.health:SetVertexColor(0.7, 0.7, 0.7);
85     f.health:Hide();
86     f.mana = f:CreateTexture(nil, "BORDER");
87     f.mana:SetPoint("TOPLEFT", f.background, "LEFT", 0, -height/8);
88     f.mana:SetPoint("BOTTOMLEFT", f.background, "BOTTOMLEFT");
89     f.mana:SetTexture(barTexture);
90     f.mana:SetVertexColor(0.1, 0.5, 0.9);
91     f.mana:Hide();
92     f.manatext = f:CreateFontString(nil, "ARTWORK", "GameFontHighlight");
93     f.manatext:SetPoint("BOTTOMRIGHT", f.background, "BOTTOMRIGHT", -2, 1);
94     f.manatext:SetFont(STANDARD_TEXT_FONT, 10);
95     f.manatext:Hide();
96     f.shield = f:CreateTexture(nil, "BORDER");
97     f.shield:SetPoint("TOPLEFT", f.health, "TOPRIGHT");
98     f.shield:SetPoint("BOTTOMLEFT", f.health, "BOTTOMRIGHT");
99     f.shield:SetTexture(barTexture);
100     f.shield:SetVertexColor(0, 0.7, 1);
101     f.shield:Hide();
102     f.shieldhl = f:CreateTexture(nil, "ARTWORK");
103     f.shieldhl:SetPoint("TOPLEFT", f, "TOPRIGHT", -2, 0);
104     f.shieldhl:SetPoint("BOTTOMRIGHT", f, "RIGHT", 0, -height/8);
105     f.shieldhl:SetColorTexture(0.5, 0.8, 1);
106     f.shieldhl:Hide();
107     f.healpred = f:CreateTexture(nil, "ARTWORK");
108     f.healpred:SetPoint("TOPLEFT", f.health, "TOPRIGHT");
109     f.healpred:SetPoint("BOTTOMLEFT", f.health, "BOTTOMRIGHT");
110     f.healpred:SetColorTexture(0.5, 0.6, 0.5);
111     f.healpred:Hide();
112     f.healabsorb = f:CreateTexture(nil, "ARTWORK");
113     f.healabsorb:SetPoint("TOPRIGHT", f.health, "TOPRIGHT");
114     f.healabsorb:SetPoint("BOTTOMRIGHT", f.health, "BOTTOMRIGHT");
115     f.healabsorb:SetColorTexture(0.1, 0.1, 0.1);
116     f.healabsorb:Hide();
117     f.name = f:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
118     f.name:SetPoint("LEFT", f, "LEFT", 2, 6);
119     f.text = f:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
120     f.text:SetPoint("RIGHT", f, "RIGHT", -2, 6);
121     f.text:SetFont(STANDARD_TEXT_FONT, 13);
122     f.text:Hide();
123     f.targeticon = f:CreateTexture(nil, "OVERLAY");
124     f.targeticon:SetPoint("CENTER", f, "TOP", 0, -1);
125     f.targeticon:SetWidth(12);
126     f.targeticon:SetHeight(12);
127     f.targeticon:SetTexture("Interface\\TARGETINGFRAME\\UI-RaidTargetingIcons");
128     f.targeticon:Hide();
129     -- Use Blizzard Alt Power Bar
130     local powerbar = _G[format("Boss%iTargetFramePowerBarAlt", string.sub(unit, 5, 5))];
131     powerbar:SetParent(f);
132     powerbar:ClearAllPoints();
133     powerbar:SetPoint("RIGHT", f, "LEFT");
134
135     RegisterUnitWatch(f);
136     f:Show();
137     return f;
138 end
139
140 CFrame:SetScript("OnEvent", function(self)
141     self:UnregisterAllEvents();
142     CFrame:SetFrameStrata("LOW");
143     CFrame:SetPoint("CENTER", nil, "CENTER");
144     CFrame:SetWidth(2);
145     CFrame:SetHeight(2);
146     for i =1,MAX_BOSS_FRAMES do
147         initBoss(self, -(i*36), 160, 32, addon.FrameUpdate, addon.UnitEvent);
148     end
149 end);
150 CFrame:RegisterEvent("PLAYER_LOGIN");