3c2cef6 - Decrease high healing overlay opacity
[wowui.git] / kehys / updater.lua
1 -- updater.lua
2 -- 2019 Aleksi Blinnikka
3 --
4 -- Main update function for all frames. Gets around issues with events.
5 local _, addon = ...;
6 local unpack = unpack;
7 local min = math.min;
8 local UnitDebuff, UnitIsCharmed = UnitDebuff, UnitIsCharmed;
9 local UnitHealth, UnitHealthMax = UnitHealth, UnitHealthMax;
10 local UnitIsAFK, UnitIsDND = UnitIsAFK, UnitIsDND;
11 local UnitGetIncomingHeals, UnitGetTotalAbsorbs = UnitGetIncomingHeals, UnitGetTotalAbsorbs;
12 local UnitIsDeadOrGhost, UnitIsConnected = UnitIsDeadOrGhost, UnitIsConnected;
13 local UnitGetTotalHealAbsorbs = UnitGetTotalHealAbsorbs;
14
15 local dispelcolor = addon.Colors.OverlayColorDispel;
16 local charmcolor = addon.Colors.OverlayColorCharm;
17 local majorcolor = addon.Colors.OverlayColorAlert;
18 local healcolor = addon.Colors.OverlayColorHeal;
19
20 local function updateAuras()
21     -- TODO
22     return false;
23 end
24
25 function addon.FrameUpdate(frame)
26     assert(type(frame) == "table", "FrameUpdate received invalid frame parameter!");
27
28     local unit = frame.displayed;
29     local width = frame.barwidth;
30     -- range check (doesn't have an event)
31     local inrange, checked = UnitInRange(unit);
32     if checked and not inrange then
33         frame:SetAlpha(0.55);
34     else
35         frame:SetAlpha(1);
36     end
37     -- states
38     if UnitIsDeadOrGhost(unit) then
39         frame.text:SetText("Dead");
40         if not frame.text:IsShown() then frame.text:Show() end
41         if frame.health:IsShown() then frame.health:Hide() end
42         if frame.shield:IsShown() then frame.shield:Hide() end
43         if frame.shieldhl:IsShown() then frame.shieldhl:Hide() end
44         if frame.healpred:IsShown() then frame.healpred:Hide() end
45         if frame.healabsorb:IsShown() then frame.healabsorb:Hide() end
46         --if frame.auras:IsShown() then frame.auras:Hide() end
47         frame.prev.health = nil;
48         frame.prev.hmax = nil;
49     elseif not UnitIsConnected(unit) then
50         frame.text:SetText("DC");
51         if not frame.text:IsShown() then frame.text:Show() end
52         if frame.health:IsShown() then frame.health:Hide() end
53         if frame.shield:IsShown() then frame.shield:Hide() end
54         if frame.shieldhl:IsShown() then frame.shieldhl:Hide() end
55         if frame.healpred:IsShown() then frame.healpred:Hide() end
56         if frame.healabsorb:IsShown() then frame.healabsorb:Hide() end
57         --if frame.auras:IsShown() then frame.auras:Hide() end
58         frame.prev.health = nil;
59         frame.prev.hmax = nil;
60     else
61         if UnitIsAFK(unit) then
62             frame.text:SetText("afk");
63             if not frame.text:IsShown() then frame.text:Show() end
64         elseif UnitIsDND(unit) then
65             frame.text:SetText("dnd");
66             if not frame.text:IsShown() then frame.text:Show() end
67         else
68             if frame.text:IsShown() then frame.text:Hide() end
69         end
70         -- health
71         local current, hmax = UnitHealth(unit), UnitHealthMax(unit);
72         if frame.prev.health ~= current or frame.prev.hmax ~= hmax then
73             frame.prev.health = current;
74             frame.prev.hmax = hmax;
75             if hmax < current or hmax <= 1 then
76                 frame.health:SetWidth(width);
77                 frame.health.width = width;
78                 if not frame.health:IsShown() then frame.health:Show() end
79             elseif current <= 0 then
80                 if frame.health:IsShown() then frame.health:Hide() end
81             else
82                 local w = current/hmax*width;
83                 frame.health:SetWidth(w);
84                 frame.health.width = w;
85                 if not frame.health:IsShown() then frame.health:Show() end
86             end
87         end
88         -- shield
89         local hwidth = frame.health.width;
90         current = UnitGetTotalAbsorbs(unit) or 0;
91         if current > 0 then
92             local space = width - hwidth;
93             current = (current / hmax) * width;
94             if space == 0 then
95                 if frame.shield:IsShown() then frame.shield:Hide() end
96                 if not frame.shieldhl:IsShown() then frame.shieldhl:Show() end
97             elseif space < current then
98                 frame.shield:SetWidth(space);
99                 if not frame.shield:IsShown() then frame.shield:Show() end
100                 if not frame.shieldhl:IsShown() then frame.shieldhl:Show() end
101             else
102                 frame.shield:SetWidth(current);
103                 if not frame.shield:IsShown() then frame.shield:Show() end
104                 if frame.shieldhl:IsShown() then frame.shieldhl:Hide() end
105             end
106         else
107             if frame.shield:IsShown() then frame.shield:Hide() end
108             if frame.shieldhl:IsShown() then frame.shieldhl:Hide() end
109         end
110         -- heal absorb
111         current = UnitGetTotalHealAbsorbs(unit) or 0;
112         if current > 0 then
113             current = (current / hmax) * width;
114             frame.healabsorb:SetWidth(min(hwidth, current));
115             if not frame.healabsorb:IsShown() then frame.healabsorb:Show() end
116         elseif frame.healabsorb:IsShown() then
117             frame.healabsorb:Hide();
118         end
119         -- heal prediction
120         current = UnitGetIncomingHeals(unit) or 0;
121         if current > 0 then
122             current = (current / hmax) * width;
123             -- at least 1 pixel prediction shown
124             frame.healpred:SetWidth(min(width - hwidth + 1, current));
125             if not frame.healpred:IsShown() then frame.healpred:Show() end
126         elseif frame.healpred:IsShown() then
127             frame.healpred:Hide();
128         end
129         -- auras
130         if next(frame.alert) then
131             -- major
132             if frame.overlay.color ~= majorcolor then
133                 frame.overlay:SetVertexColor(unpack(majorcolor));
134                 frame.overlay.color = majorcolor;
135                 if not frame.overlay:IsShown() then frame.overlay:Show() end
136             end
137         elseif next(frame.heal) then
138             -- major heals needed
139             if frame.overlay.color ~= healcolor then
140                 frame.overlay:SetVertexColor(unpack(healcolor));
141                 frame.overlay.color = healcolor;
142                 if not frame.overlay:IsShown() then frame.overlay:Show() end
143             end
144         elseif UnitIsCharmed(unit) and frame.unit == frame.displayed then
145             -- charmed
146             if frame.overlay.color ~= charmcolor then
147                 frame.overlay:SetVertexColor(unpack(charmcolor));
148                 frame.overlay.color = charmcolor;
149                 if not frame.overlay:IsShown() then frame.overlay:Show() end
150             end
151         elseif UnitDebuff(unit, 1, "RAID") ~= nil then
152             -- dispellable
153             if frame.overlay.color ~= dispelcolor then
154                 frame.overlay:SetVertexColor(unpack(dispelcolor));
155                 frame.overlay.color = dispelcolor;
156                 if not frame.overlay:IsShown() then frame.overlay:Show() end
157             end
158         else
159             if frame.overlay.color ~= nil then
160                 frame.overlay.color = nil;
161                 if frame.overlay:IsShown() then frame.overlay:Hide() end
162             end
163         end
164     end
165 end