504949772c03ed687d03a3b42518f4e68301d76e
[wowui.git] / OmaRF / CFrame.lua
1 -- CFrame.lua
2 local _;
3 local unpack, pairs, ipairs = unpack, pairs, ipairs;
4 local ssub = string.sub;
5 local min = math.min;
6 local UnitName, UnitInRange, UnitDebuff = UnitName, UnitInRange, UnitDebuff;
7 local UnitPower, UnitPowerMax, UnitPowerType = UnitPower, UnitPowerMax, UnitPowerType;
8 local UnitHealth, UnitHealthMax = UnitHealth, UnitHealthMax;
9 local UnitInParty, UnitInRaid = UnitInParty, UnitInRaid;
10 local UnitGetIncomingHeals, UnitGetTotalAbsorbs = UnitGetIncomingHeals, UnitGetTotalAbsorbs;
11 local UnitGetTotalHealAbsorbs = UnitGetTotalHealAbsorbs;
12 local IsInGroup, IsInRaid = IsInGroup, IsInRaid;
13 local UnitIsCharmed = UnitIsCharmed;
14 local CreateFrame, RegisterStateDriver, RegisterUnitWatch = CreateFrame, RegisterStateDriver, RegisterUnitWatch;
15 local SPELL_POWER_MANA = SPELL_POWER_MANA;
16
17 local CFrame = CreateFrame("Frame", "OmaCFrame", UIParent);
18 local party = {};
19 local raid = {};
20 local positions = {"TOPRIGHT", "BOTTOMLEFT"};
21
22 -- configurable settings
23 local width, height = 80, 40;
24 local anchorX, anchorY = 0, -200;
25 local indSize = 14;
26 local bgColor = {0.7, 0.7, 0.7};
27 local bgColorDispel = {1, 0.5, 0};
28 local healthColor = {0.3, 0.3, 0.3};
29 local healthColorDispel = {0.8, 0.4, 0};
30 local shieldColor = {0, 0.7, 1};
31 local shieldhlColor = {0.5, 0.8, 1};
32 local healpredColor = {0.5, 0.6, 0.5};
33 local healabsorbColor = {0.1, 0.1, 0.1};
34 local powerColors = {
35     [SPELL_POWER_MANA] = {0, 0.5, 1},
36     [SPELL_POWER_RAGE] = {1, 0.2, 0},
37     [SPELL_POWER_FOCUS] = {1, 0.5, 0},
38     [SPELL_POWER_ENERGY] = {1, 0.8, 0},
39     [SPELL_POWER_RUNIC_POWER] = {0.9, 0, 0.1},
40 };
41 -- watch to not remove mana entry
42 setmetatable(powerColors, {__index = function(t) return t[SPELL_POWER_MANA] end});
43
44 local function updateHealth(frame, unit)
45     local current = UnitHealth(unit);
46     local _, max = frame.health:GetMinMaxValues();
47     local healthLost = max - current;
48     frame.health:SetValue(current);
49     if healthLost > 0 then
50         if healthLost > 1200000000 then -- 1.2B
51             frame.text:SetFormattedText("-%.1fB", healthLost / 1000000000);
52         elseif healthLost > 1200000 then -- 1.2M
53             frame.text:SetFormattedText("-%.1fM", healthLost / 1000000);
54         elseif healthLost > 1000 then -- 1K
55             frame.text:SetFormattedText("-%dK", healthLost / 1000);
56         else
57             frame.text:SetFormattedText("-%d", healthLost)
58         end
59         if not frame.text:IsShown() then frame.text:Show() end
60     else
61         if frame.text:IsShown() then frame.text:Hide() end
62     end
63 end
64
65 local function updateMaxHealth(frame, unit)
66     frame.health:SetMinMaxValues(0, UnitHealthMax(unit));
67 end
68
69 local function updatePower(frame, unit)
70     frame.mana:SetValue(UnitPower(unit));
71 end
72
73 local function updateMaxPower(frame, unit)
74     frame.mana:SetMinMaxValues(0, UnitPowerMax(unit));
75 end
76
77 local function updatePowerColor(frame, unit)
78     frame.mana:SetStatusBarColor(unpack(powerColors[UnitPowerType(unit)]));
79 end
80
81 local function updateName(frame, unit)
82     frame.name:SetText(ssub(UnitName(unit), 1, 6));
83 end
84
85 local function updateHealPred(frame, unit)
86     local incoming = UnitGetIncomingHeals(unit) or 0;
87     if incoming > 0 then
88         local _, max = frame.health:GetMinMaxValues();
89         local space = width - frame.health.bar:GetWidth() + 1;
90         local pred = (incoming / max) * width;
91         frame.healpred:SetWidth(min(space, pred));
92         if not frame.healpred:IsShown() then frame.healpred:Show() end
93     else
94         if frame.healpred:IsShown() then frame.healpred:Hide() end
95     end
96 end
97
98 local function updateShield(frame, unit)
99     local shield = UnitGetTotalAbsorbs(unit) or 0;
100     if shield > 0 then
101         local _, max = frame.health:GetMinMaxValues();
102         local space = width - frame.health.bar:GetWidth();
103         shield = (shield / max) * width;
104         frame.shield:SetWidth(min(space, shield));
105         if not frame.shield:IsShown() then
106             frame.shield:Show();
107             frame.shieldhl:Show();
108         end
109     else
110         if frame.shield:IsShown() then
111             frame.shield:Hide();
112             frame.shieldhl:Hide();
113         end
114     end
115 end
116
117 local function updateHealAbsorb(frame, unit)
118     local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
119     if absorb > 0 then
120         local _, max = frame.health:GetMinMaxValues();
121         local space = frame.health.bar:GetWidth();
122         absorb = (absorb / max) * width;
123         frame.healabsorb:SetWidth(min(space, absorb));
124         if not frame.healabsorb:IsShown() then frame.healabsorb:Show() end
125     else
126         if frame.healabsorb:IsShown() then frame.healabsorb:Hide() end
127     end
128 end
129
130 local function updateAuras(frame, unit)
131     if UnitDebuff(unit, 1, "RAID") ~= nil then
132         -- something dispellable
133         if frame.health.color ~= healthColorDispel then
134             -- TODO change to a texture layered on top tinting whole bar
135             frame.health:SetStatusBarColor(unpack(healthColorDispel));
136             frame.background:SetColorTexture(unpack(bgColorDispel));
137             frame.health.color = healthColorDispel;
138         end
139     elseif UnitIsCharmed(unit) then
140         -- TODO purple
141     else
142         if frame.health.color ~= healthColor then
143             frame.health:SetStatusBarColor(unpack(healthColor));
144             frame.background:SetColorTexture(unpack(bgColor));
145             frame.health.color = healthColor;
146         end
147     end
148 end
149
150 local function unitEvent(self, event, ...)
151     local arg1, arg2, arg3, arg4 = ...;
152     if event == "UNIT_HEALTH" or event == "UNIT_HEALTH_FREQUENT" then
153         updateHealth(self, arg1);
154     elseif event == "UNIT_POWER" then
155         updatePower(self, arg1);
156     elseif event == "UNIT_AURA" then
157         updateAuras(self, arg1);
158     elseif event == "UNIT_ABSORB_AMOUNT_CHANGED" then
159         updateShield(self, arg1);
160     elseif event == "UNIT_HEAL_PREDICTION" then
161         updateHealPred(self, arg1);
162     elseif event == "UNIT_HEAL_ABSORB_AMOUNT_CHANGED" then
163         updateHealAbsorb(self, arg1);
164     elseif event == "UNIT_MAXHEALTH" then
165         updateMaxHealth(self, arg1);
166     elseif event == "UNIT_MAXPOWER" then
167         updateMaxPower(self, arg1);
168     elseif event == "UNIT_DISPLAYPOWER" then
169         updatePowerColor(self, arg1);
170     elseif event == "UNIT_NAME_UPDATE" then
171         updateName(self, arg1);
172     elseif event == "UPDATE_ALL_BARS" then
173         updateHealth(self, arg1);
174         updatePower(self, arg1);
175         updateAuras(self, arg1);
176         updateShield(self, arg1);
177         updateHealPred(self, arg1);
178         updateMaxHealth(self, arg1);
179         updateMaxPower(self, arg1);
180         updatePowerColor(self, arg1);
181         updateName(self, arg1);
182     end
183 end
184
185 local function unitUpdate(self, elapsed)
186     -- there's no in/out of range event, have to check each frame
187     -- indicator update here?
188     local inRange, checked = UnitInRange(self.unit);
189     if checked and not inRange then
190         self:SetAlpha(0.55);
191     else
192         self:SetAlpha(1);
193     end
194 end
195
196 local function registerEvents(frame, unit)
197     frame:RegisterUnitEvent("UNIT_HEALTH", unit);
198     frame:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", unit);
199     frame:RegisterUnitEvent("UNIT_MAXHEALTH", unit);
200     frame:RegisterUnitEvent("UNIT_POWER", unit);
201     frame:RegisterUnitEvent("UNIT_MAXPOWER", unit);
202     frame:RegisterUnitEvent("UNIT_DISPLAYPOWER", unit);
203     frame:RegisterUnitEvent("UNIT_NAME_UPDATE", unit);
204     frame:RegisterUnitEvent("UNIT_AURA", unit);
205     frame:RegisterUnitEvent("UNIT_HEAL_PREDICTION", unit);
206     frame:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", unit);
207     frame:RegisterUnitEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED", unit);
208 end
209
210 local function vis(frame, vis)
211     if vis then
212         frame:Show();
213         frame:SetScript("OnUpdate", unitUpdate);
214         frame:UnregisterAllEvents();
215         registerEvents(frame, frame.unit);
216         unitEvent(frame, "UPDATE_ALL_BARS", frame.unit);
217     else
218         frame:Hide();
219         frame:SetScript("OnUpdate", nil);
220         frame:UnregisterAllEvents();
221     end;
222 end
223
224 local function updateGroup()
225     if IsInGroup() then
226         if IsInRaid() then
227             -- show raid
228             for _, val in ipairs(raid) do
229                 if UnitInRaid(val.frame.unit) then
230                     if not val.frame:IsShown() then vis(val.frame, true) end
231                 elseif val.frame:IsShown() then
232                     vis(val.frame, false);
233                 end
234             end
235             -- hide player + party (use pairs, not ipairs)
236             for _, val in pairs(party) do
237                 if val.frame:IsShown() then vis(val.frame, false) end
238             end
239         else
240             -- show player + party
241             for _, val in pairs(party) do
242                 if UnitInParty(val.frame.unit) then
243                     if not val.frame:IsShown() then vis(val.frame, true) end
244                 elseif val.frame:IsShown() then
245                     vis(val.frame, false);
246                 end
247             end
248             -- hide raid
249             for _, val in ipairs(raid) do
250                 if val.frame:IsShown() then vis(val.frame, false) end
251             end
252         end
253     else
254         -- show player
255         if not party[0].frame:IsShown() then vis(party[0].frame, true) end
256         -- hide all other frames
257         for _, val in ipairs(party) do
258             if val.frame:IsShown() then vis(val.frame, false) end
259         end
260         for _, val in ipairs(raid) do
261             if val.frame:IsShown() then vis(val.frame, false) end
262         end
263     end
264 end
265
266 local function setupIndicators(frame)
267     frame.inds = CreateFrame("Frame", nil, frame);
268     frame.inds:SetAllPoints();
269     for _, pos in pairs(positions) do
270         frame.inds[pos] = frame.inds:CreateTexture(nil, "OVERLAY");
271         frame.inds[pos]:SetPoint(pos, frame.inds, pos);
272         frame.inds[pos]:SetWidth(indSize);
273         frame.inds[pos]:SetHeight(indSize);
274         frame.inds[pos]:SetTexture("Interface\\AddOns\\OmaRF\\images\\rhomb");
275     end
276 end
277
278 local function setupFrame(frame, secure, unit)
279     -- create visuals
280     secure:SetWidth(width+2);
281     frame:SetWidth(width+2);
282     frame.base = frame:CreateTexture(nil, "BACKGROUND");
283     frame.base:SetAllPoints();
284     frame.base:SetColorTexture(0, 0, 0);
285     frame.background = frame:CreateTexture(nil, "BACKGROUND", nil, 1);
286     frame.background:SetPoint("TOPLEFT", frame, "TOPLEFT", 1, -1);
287     frame.background:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -1, 1);
288     frame.background:SetColorTexture(unpack(bgColor));
289     frame.health = CreateFrame("StatusBar", nil, frame);
290     frame.health:SetPoint("TOPLEFT", frame.background, "TOPLEFT");
291     frame.health:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT", 0, 2);
292     frame.health.bar = frame.health:CreateTexture(nil, "ARTWORK");
293     frame.health.bar:SetTexture("Interface\\RaidFrame\\Raid-Bar-Hp-Fill");
294     frame.health:SetStatusBarTexture(frame.health.bar);
295     frame.health:SetStatusBarColor(unpack(healthColor));
296     frame.health.color = healthColor;
297     frame.shield = frame.health:CreateTexture(nil, "ARTWORK");
298     frame.shield:SetPoint("TOPLEFT", frame.health.bar, "TOPRIGHT");
299     frame.shield:SetPoint("BOTTOMLEFT", frame.health.bar, "BOTTOMRIGHT");
300     frame.shield:SetColorTexture(unpack(shieldColor));
301     frame.shield:Hide();
302     frame.shieldhl = frame:CreateTexture(nil, "ARTWORK");
303     frame.shieldhl:SetPoint("TOPLEFT", frame.shield, "TOPRIGHT", -1, 0);
304     frame.shieldhl:SetPoint("BOTTOMRIGHT", frame.shield, "BOTTOMRIGHT", 1, 0);
305     frame.shieldhl:SetColorTexture(unpack(shieldhlColor));
306     frame.shieldhl:Hide();
307     frame.healpred = frame.health:CreateTexture(nil, "ARTWORK", nil, 1);
308     frame.healpred:SetPoint("TOPLEFT", frame.health.bar, "TOPRIGHT");
309     frame.healpred:SetPoint("BOTTOMLEFT", frame.health.bar, "BOTTOMRIGHT");
310     frame.healpred:SetColorTexture(unpack(healpredColor));
311     frame.healpred:Hide();
312     frame.healabsorb = frame.health:CreateTexture(nil, "ARTWORK", nil, 1);
313     frame.healabsorb:SetPoint("TOPRIGHT", frame.health.bar, "TOPRIGHT");
314     frame.healabsorb:SetPoint("BOTTOMRIGHT", frame.health.bar, "BOTTOMRIGHT");
315     frame.healabsorb:SetColorTexture(unpack(healabsorbColor));
316     frame.healabsorb:Hide();
317     frame.mana = CreateFrame("StatusBar", nil, frame);
318     frame.mana:SetPoint("TOPLEFT", frame.background, "BOTTOMLEFT", 0, 2);
319     frame.mana:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT");
320     frame.mana.bar = frame.mana:CreateTexture(nil, "ARTWORK");
321     frame.mana.bar:SetColorTexture(1, 1, 1);
322     frame.mana:SetStatusBarTexture(frame.mana.bar);
323     frame.name = frame.health:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
324     frame.name:SetPoint("TOPLEFT", frame.background, "TOPLEFT");
325     local name = UnitName(unit);
326     frame.name:SetText(name and ssub(name, 1, 6) or "");
327     frame.text = frame.health:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
328     frame.text:SetFont(STANDARD_TEXT_FONT, 13);
329     frame.text:SetPoint("CENTER", frame.background, "CENTER");
330     frame.text:Hide();
331     setupIndicators(frame);
332     -- set attributes
333     secure:RegisterForClicks("AnyDown");
334     secure:SetAttribute("type1", "spell"); -- left click
335     secure:SetAttribute("type2", "spell"); -- right click
336     secure:SetAttribute("shift-type1", "spell"); -- shift left click
337     secure:SetAttribute("shift-type2", "spell"); -- shift right click
338     secure:SetAttribute("ctrl-type1", "spell"); -- ctrl left click
339     secure:SetAttribute("alt-type2", "spell"); -- alt right click
340     secure:SetAttribute("alt-shift-type1", "spell"); -- alt+shift left click
341     secure:SetAttribute("alt-shift-type2", "spell"); -- alt+shift right click
342     secure:SetAttribute("spell1", "Holy Light");
343     secure:SetAttribute("spell2", "Bestow Faith");
344     secure:SetAttribute("shift-spell1", "Flash of Light");
345     secure:SetAttribute("shift-spell2", "Light of the Martyr");
346     secure:SetAttribute("ctrl-spell1", "Cleanse");
347     secure:SetAttribute("alt-spell2", "Lay on Hands");
348     secure:SetAttribute("alt-shift-spell1", "Beacon of Light");
349     secure:SetAttribute("alt-shift-spell2", "Beacon of Faith");
350     -- rest give target and menu
351     secure:SetAttribute("*type1", "target");
352     secure:SetAttribute("*type2", "togglemenu");
353 end
354
355 local function initializeParty()
356     local secure = CreateFrame("Button", "OmaPartySecure0", CFrame, "SecureUnitButtonTemplate");
357     local frame = CreateFrame("Frame", "OmaParty0", CFrame);
358     local unit = "player";
359     secure:SetAttribute("unit", unit);
360     secure:SetPoint("TOPLEFT", CFrame, "TOPLEFT");
361     secure:SetHeight(height+2);
362     frame.unit = unit;
363     frame:SetPoint("TOPLEFT", CFrame, "TOPLEFT");
364     frame:SetHeight(height+2);
365     frame:SetScript("OnEvent", unitEvent);
366     frame:Hide();
367     setupFrame(frame, secure, unit);
368     RegisterStateDriver(secure, "visibility", "[@player,group:raid] hide; show");
369     party[0] = {secure=secure, frame=frame};
370     for i = 1,4 do
371         local secure = CreateFrame("Button", "OmaPartySecure"..i, CFrame, "SecureUnitButtonTemplate");
372         local frame = CreateFrame("Frame", "OmaParty"..i, CFrame);
373         local unit = "party"..i;
374         secure:SetAttribute("unit", unit);
375         secure:SetPoint("TOPLEFT", party[i-1].secure, "TOPRIGHT");
376         secure:SetPoint("BOTTOMLEFT", party[i-1].secure, "BOTTOMRIGHT");
377         frame.unit = unit;
378         frame:SetPoint("TOPLEFT", party[i-1].frame, "TOPRIGHT");
379         frame:SetPoint("BOTTOMLEFT", party[i-1].frame, "BOTTOMRIGHT");
380         frame:SetScript("OnEvent", unitEvent);
381         frame:Hide();
382         setupFrame(frame, secure, unit);
383         RegisterUnitWatch(secure);
384         party[i] = {secure=secure, frame=frame};
385     end
386 end
387
388 local function initializeRaid()
389     local secure = CreateFrame("Button", "OmaRaidSecure1", CFrame, "SecureUnitButtonTemplate");
390     local frame = CreateFrame("Frame", "OmaRaid1", CFrame);
391     local unit = "raid1";
392     secure:SetAttribute("unit", unit);
393     secure:SetPoint("TOPLEFT", CFrame, "TOPLEFT");
394     secure:SetHeight(height+2);
395     frame.unit = unit;
396     frame:SetPoint("TOPLEFT", CFrame, "TOPLEFT");
397     frame:SetHeight(height+2);
398     frame:SetScript("OnEvent", unitEvent);
399     frame:Hide();
400     setupFrame(frame, secure, unit);
401     RegisterUnitWatch(secure);
402     raid[1] = {secure=secure, frame=frame};
403     for y = 1,7 do
404         local i = y*5+1;
405         local secure = CreateFrame("Button", "OmaRaidSecure"..i, CFrame, "SecureUnitButtonTemplate");
406         local frame = CreateFrame("Frame", "OmaRaid"..i, CFrame);
407         local unit = "raid"..i;
408         secure:SetAttribute("unit", unit);
409         secure:SetPoint("TOPLEFT", raid[i-5].secure, "BOTTOMLEFT");
410         secure:SetHeight(height+2);
411         frame.unit = unit;
412         frame:SetPoint("TOPLEFT", raid[i-5].frame, "BOTTOMLEFT");
413         frame:SetHeight(height+2);
414         frame:SetScript("OnEvent", unitEvent);
415         frame:Hide();
416         setupFrame(frame, secure, unit);
417         RegisterUnitWatch(secure);
418         raid[i] = {secure=secure, frame=frame};
419     end
420     for y = 0,7 do
421         for x = 1,4 do
422             local i = y*5+x+1;
423             local secure = CreateFrame("Button", "OmaRaidSecure"..i, CFrame, "SecureUnitButtonTemplate");
424             local frame = CreateFrame("Frame", "OmaRaid"..i, CFrame);
425             local unit = "raid"..i;
426             secure:SetAttribute("unit", unit);
427             secure:SetPoint("TOPLEFT", raid[i-1].secure, "TOPRIGHT");
428             secure:SetPoint("BOTTOMLEFT", raid[i-1].secure, "BOTTOMRIGHT");
429             frame.unit = unit;
430             frame:SetPoint("TOPLEFT", raid[i-1].frame, "TOPRIGHT");
431             frame:SetPoint("BOTTOMLEFT", raid[i-1].frame, "BOTTOMRIGHT");
432             frame:SetScript("OnEvent", unitEvent);
433             frame:Hide();
434             setupFrame(frame, secure, unit);
435             RegisterUnitWatch(secure);
436             raid[i] = {secure=secure, frame=frame};
437         end
438     end
439 end
440
441 local function initialize()
442     CFrame:SetPoint("CENTER", nil, "CENTER", anchorX, anchorY);
443     CFrame:SetHeight((height+2)*8);
444     CFrame:SetWidth((width+2)*5+1); -- extra pixel for shield overflow
445     initializeParty();
446     initializeRaid();
447     updateGroup();
448 end
449
450 CFrame:RegisterEvent("PLAYER_LOGIN");
451 CFrame:RegisterEvent("GROUP_ROSTER_UPDATE");
452 CFrame:SetScript("OnEvent", function(self, event, ...)
453     if event == "GROUP_ROSTER_UPDATE" then
454         updateGroup();
455     elseif event == "PLAYER_LOGIN" then
456         initialize();
457     end
458 end);