521041f - Remove OmaUF in favor of standard unit frames
[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
19 local function updateAuras()
20     -- TODO
21     return false;
22 end
23
24 function addon.FrameUpdate(frame)
25     assert(type(frame) == "table", "FrameUpdate received invalid frame parameter!");
26
27     local unit = frame.displayed;
28     local width = frame.barwidth;
29     -- range check (doesn't have an event)
30     local inrange, checked = UnitInRange(unit);
31     if checked and not inrange then
32         frame:SetAlpha(0.55);
33     else
34         frame:SetAlpha(1);
35     end
36     -- states
37     if UnitIsDeadOrGhost(unit) then
38         frame.text:SetText("Dead");
39         if not frame.text:IsShown() then frame.text:Show() end
40         if frame.health:IsShown() then frame.health:Hide() end
41         if frame.shield:IsShown() then frame.shield:Hide() end
42         if frame.shieldhl:IsShown() then frame.shieldhl:Hide() end
43         if frame.healpred:IsShown() then frame.healpred:Hide() end
44         if frame.healabsorb:IsShown() then frame.healabsorb:Hide() end
45         --if frame.auras:IsShown() then frame.auras:Hide() end
46         frame.prev.health = nil;
47         frame.prev.hmax = nil;
48     elseif not UnitIsConnected(unit) then
49         frame.text:SetText("DC");
50         if not frame.text:IsShown() then frame.text:Show() end
51         if frame.health:IsShown() then frame.health:Hide() end
52         if frame.shield:IsShown() then frame.shield:Hide() end
53         if frame.shieldhl:IsShown() then frame.shieldhl:Hide() end
54         if frame.healpred:IsShown() then frame.healpred:Hide() end
55         if frame.healabsorb:IsShown() then frame.healabsorb:Hide() end
56         --if frame.auras:IsShown() then frame.auras:Hide() end
57         frame.prev.health = nil;
58         frame.prev.hmax = nil;
59     else
60         if UnitIsAFK(unit) then
61             frame.text:SetText("afk");
62             if not frame.text:IsShown() then frame.text:Show() end
63         elseif UnitIsDND(unit) then
64             frame.text:SetText("dnd");
65             if not frame.text:IsShown() then frame.text:Show() end
66         else
67             if frame.text:IsShown() then frame.text:Hide() end
68         end
69         -- health
70         local current, hmax = UnitHealth(unit), UnitHealthMax(unit);
71         if frame.prev.health ~= current or frame.prev.hmax ~= hmax then
72             frame.prev.health = current;
73             frame.prev.hmax = hmax;
74             if hmax < current or hmax <= 1 then
75                 frame.health:SetWidth(width);
76                 frame.health.width = width;
77                 if not frame.health:IsShown() then frame.health:Show() end
78             elseif current <= 0 then
79                 if frame.health:IsShown() then frame.health:Hide() end
80             else
81                 local w = current/hmax*width;
82                 frame.health:SetWidth(w);
83                 frame.health.width = w;
84                 if not frame.health:IsShown() then frame.health:Show() end
85             end
86         end
87         -- shield
88         local hwidth = frame.health.width;
89         current = UnitGetTotalAbsorbs(unit) or 0;
90         if current > 0 then
91             local space = width - hwidth;
92             current = (current / hmax) * width;
93             if space == 0 then
94                 if frame.shield:IsShown() then frame.shield:Hide() end
95                 if not frame.shieldhl:IsShown() then frame.shieldhl:Show() end
96             elseif space < current then
97                 frame.shield:SetWidth(space);
98                 if not frame.shield:IsShown() then frame.shield:Show() end
99                 if not frame.shieldhl:IsShown() then frame.shieldhl:Show() end
100             else
101                 frame.shield:SetWidth(current);
102                 if not frame.shield:IsShown() then frame.shield:Show() end
103                 if frame.shieldhl:IsShown() then frame.shieldhl:Hide() end
104             end
105         else
106             if frame.shield:IsShown() then frame.shield:Hide() end
107             if frame.shieldhl:IsShown() then frame.shieldhl:Hide() end
108         end
109         -- heal absorb
110         current = UnitGetTotalHealAbsorbs(unit) or 0;
111         if current > 0 then
112             current = (current / hmax) * width;
113             frame.healabsorb:SetWidth(min(hwidth, current));
114             if not frame.healabsorb:IsShown() then frame.healabsorb:Show() end
115         elseif frame.healabsorb:IsShown() then
116             frame.healabsorb:Hide();
117         end
118         -- heal prediction
119         current = UnitGetIncomingHeals(unit) or 0;
120         if current > 0 then
121             current = (current / hmax) * width;
122             -- at least 1 pixel prediction shown
123             frame.healpred:SetWidth(min(width - hwidth + 1, current));
124             if not frame.healpred:IsShown() then frame.healpred:Show() end
125         elseif frame.healpred:IsShown() then
126             frame.healpred:Hide();
127         end
128         -- auras
129         if frame.alert and next(frame.alert) then
130             -- major
131             if frame.overlay.color ~= majorcolor then
132                 frame.overlay:SetVertexColor(unpack(majorcolor));
133                 frame.overlay.color = majorcolor;
134                 if not frame.overlay:IsShown() then frame.overlay:Show() end
135             end
136         elseif UnitIsCharmed(unit) and frame.unit == frame.displayed then
137             -- charmed
138             if frame.overlay.color ~= charmcolor then
139                 frame.overlay:SetVertexColor(unpack(charmcolor));
140                 frame.overlay.color = charmcolor;
141                 if not frame.overlay:IsShown() then frame.overlay:Show() end
142             end
143         elseif UnitDebuff(unit, 1, "RAID") ~= nil then
144             -- dispellable
145             if frame.overlay.color ~= dispelcolor then
146                 frame.overlay:SetVertexColor(unpack(dispelcolor));
147                 frame.overlay.color = dispelcolor;
148                 if not frame.overlay:IsShown() then frame.overlay:Show() end
149             end
150         else
151             if frame.overlay.color ~= nil then
152                 frame.overlay.color = nil;
153                 if frame.overlay:IsShown() then frame.overlay:Hide() end
154             end
155         end
156     end
157 end