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