d180f82 - Add customizable settings, remove extra code
[wowui.git] / OmaRF / Events.lua
1 -- Events.lua
2 local _;
3 local unpack = unpack;
4 local ssub = string.sub;
5 local min = math.min;
6 local UnitName, UnitClass, UnitExists = UnitName, UnitClass, UnitExists;
7 local UnitDebuff, UnitIsCharmed = UnitDebuff, UnitIsCharmed;
8 local UnitPower, UnitPowerMax, UnitPowerType = UnitPower, UnitPowerMax, UnitPowerType;
9 local UnitHealth, UnitHealthMax = UnitHealth, UnitHealthMax;
10 local UnitGetIncomingHeals, UnitGetTotalAbsorbs = UnitGetIncomingHeals, UnitGetTotalAbsorbs;
11 local UnitThreatSituation, GetThreatStatusColor = UnitThreatSituation, GetThreatStatusColor;
12 local UnitIsDeadOrGhost, UnitIsConnected = UnitIsDeadOrGhost, UnitIsConnected;
13 local UnitHasIncomingResurrection = UnitHasIncomingResurrection;
14 local UnitGetTotalHealAbsorbs = UnitGetTotalHealAbsorbs;
15 local UnitHasVehicleUI, UnitTargetsVehicleInRaidUI = UnitHasVehicleUI, UnitTargetsVehicleInRaidUI;
16 local UnitGroupRolesAssigned = UnitGroupRolesAssigned;
17 local RAID_CLASS_COLORS = RAID_CLASS_COLORS;
18
19 local checkIndicators = OmaRFIndicators.CheckIndicators;
20
21 local Settings = OmaRFSettings;
22 local width = Settings.Character.Width;
23 local baseColor = Settings.BaseColor;
24 local overlayColorDispel = Settings.OverlayColorDispel;
25 local overlayColorCharm = Settings.OverlayColorCharm;
26 local overlayColorAlert = Settings.OverlayColorAlert;
27 local powerColors = Settings.PowerColors;
28
29 local M = {};
30 OmaRFEvents = M;
31 function M.RegisterEvents(frame)
32     -- events are taken from FrameXML/CompactUnitFrame.lua
33     -- TODO ready check support, raid marker support,
34     -- player flags support (/afk, /dnd)
35     local displayed = frame.unit ~= frame.displayed and frame.displayed or nil;
36     frame:RegisterUnitEvent("UNIT_HEALTH", frame.unit, displayed);
37     frame:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", frame.unit, displayed);
38     frame:RegisterUnitEvent("UNIT_MAXHEALTH", frame.unit, displayed);
39     frame:RegisterUnitEvent("UNIT_POWER", frame.unit, displayed);
40     frame:RegisterUnitEvent("UNIT_MAXPOWER", frame.unit, displayed);
41     frame:RegisterUnitEvent("UNIT_DISPLAYPOWER", frame.unit, displayed);
42     frame:RegisterUnitEvent("UNIT_NAME_UPDATE", frame.unit, displayed);
43     frame:RegisterUnitEvent("UNIT_AURA", frame.unit, displayed);
44     frame:RegisterUnitEvent("UNIT_HEAL_PREDICTION", frame.unit, displayed);
45     frame:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
46     frame:RegisterUnitEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
47     frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", frame.unit, displayed);
48     frame:RegisterUnitEvent("UNIT_CONNECTION", frame.unit, displayed);
49     frame:RegisterUnitEvent("INCOMING_RESURRECT_CHANGED", frame.unit, displayed);
50 end
51 local registerEvents = M.RegisterEvents;
52
53 local function updateHealth(frame, unit)
54     local current, max = UnitHealth(unit), frame.health.max;
55     frame.health:Show();
56     -- sanity check, occasionally UnitHealthMax gives zero
57     if current > max then
58         -- somehow current health has gone over the maximum (missed maxhealth event)
59         frame.health.max = UnitHealthMax(unit);
60         max = frame.health.max;
61         if current > max then
62             -- error state, still over maximum
63             frame.health:SetWidth(width);
64             return;
65         end
66     elseif max > 0 then
67         frame.health:SetWidth(current/frame.health.max*width);
68     else
69         frame.health:SetWidth(width);
70         return;
71     end
72
73     if UnitIsDeadOrGhost(unit) then
74         frame.health:Hide();
75     end
76 end
77
78 local function updateText(frame, unit)
79     local current, max = UnitHealth(unit), frame.health.max;
80     local healthLost = max - current;
81     if UnitIsDeadOrGhost(unit) then
82         frame.text:SetText("Dead");
83     elseif not UnitIsConnected(unit) then
84         frame.text:SetText("DC");
85     elseif healthLost > 0 then
86         if healthLost > 1200000000 then -- 1.2B
87             frame.text:SetFormattedText("-%.1fB", healthLost / 1000000000);
88         elseif healthLost > 1200000 then -- 1.2M
89             frame.text:SetFormattedText("-%.1fM", healthLost / 1000000);
90         elseif healthLost > 1000 then -- 1K
91             frame.text:SetFormattedText("-%dK", healthLost / 1000);
92         else
93             frame.text:SetFormattedText("-%d", healthLost)
94         end
95         frame.text:Show();
96     else
97         frame.text:Hide();
98     end
99 end
100
101 local function updateIncomingRes(frame, unit)
102     if UnitHasIncomingResurrection(unit) then
103         frame.rez:Show();
104     else
105         frame.rez:Hide();
106     end
107 end
108
109 local function updateMaxHealth(frame, unit)
110     frame.health.max = UnitHealthMax(unit);
111 end
112
113 local function updatePower(frame, unit)
114     local current, max = UnitPower(unit), frame.mana.max;
115     -- sanity check, occasionally UnitPowerMax gives zero
116     if current > max then
117         frame.mana.max = UnitPowerMax(unit);
118         max = frame.mana.max;
119         if current > max then
120             -- error
121             frame.mana:SetWidth(width);
122             return;
123         end
124     elseif max > 0 then
125         frame.mana:SetWidth(UnitPower(unit)/max*width);
126     else
127         frame.mana:SetWidth(width);
128     end
129 end
130
131 local function updateMaxPower(frame, unit)
132     frame.mana.max = UnitPowerMax(unit);
133 end
134
135 local function updatePowerColor(frame, unit)
136     frame.mana:SetVertexColor(unpack(powerColors[UnitPowerType(unit)]));
137 end
138
139 local function updateName(frame, unit)
140     local name = UnitName(unit);
141     if not name then return end
142     frame.name:SetText(ssub(name, 1, 6));
143
144     local _, class = UnitClass(unit);
145     local color = RAID_CLASS_COLORS[class];
146     if color then frame.name:SetVertexColor(color.r, color.g, color.b) end
147 end
148
149 local function updateHealPred(frame, unit)
150     local incoming = UnitGetIncomingHeals(unit) or 0;
151     if incoming > 0 then
152         local max = frame.health.max;
153         local space = width - frame.health:GetWidth() + 1;
154         local pred = (incoming / max) * width;
155         frame.healpred:SetWidth(min(space, pred));
156         frame.healpred:Show();
157     else
158         frame.healpred:Hide();
159     end
160 end
161
162 local function updateShield(frame, unit)
163     local shield = UnitGetTotalAbsorbs(unit) or 0;
164     if shield > 0 then
165         local max = frame.health.max;
166         local space = width - frame.health:GetWidth();
167         shield = (shield / max) * width;
168         if space < shield then
169             frame.shield:SetWidth(space);
170             frame.shieldhl:Show();
171         else
172             frame.shield:SetWidth(shield);
173             frame.shieldhl:Hide();
174         end
175         frame.shield:Show();
176     else
177         frame.shield:Hide();
178         frame.shieldhl:Hide();
179     end
180 end
181
182 local function updateHealAbsorb(frame, unit)
183     local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
184     if absorb > 0 then
185         local max = frame.health.max;
186         local space = frame.health:GetWidth();
187         absorb = (absorb / max) * width;
188         frame.healabsorb:SetWidth(min(space, absorb));
189         frame.healabsorb:Show();
190     else
191         frame.healabsorb:Hide();
192     end
193 end
194
195 local function updateAuras(frame, unit)
196     checkIndicators(frame, unit);
197     if UnitDebuff(unit, 1, "RAID") ~= nil then
198         -- something dispellable
199         if frame.overlay.color ~= overlayColorDispel then
200             frame.overlay:SetVertexColor(unpack(overlayColorDispel));
201             frame.overlay:Show();
202             frame.overlay.color = overlayColorDispel;
203         end
204     -- don't overlay charmed when in vehicle
205     elseif UnitIsCharmed(unit) and unit == frame.unit then
206         if frame.overlay.color ~= overlayColorCharm then
207             frame.overlay:SetVertexColor(unpack(overlayColorCharm));
208             frame.overlay:Show();
209             frame.overlay.color = overlayColorCharm;
210         end
211     else
212         if frame.overlay.color ~= nil then
213             frame.overlay:Hide();
214             frame.overlay.color = nil;
215         end
216     end
217 end
218
219 local function updateAggro(frame, unit)
220     local status = UnitThreatSituation(unit);
221     if status and status > 0 then
222         frame.base:SetVertexColor(GetThreatStatusColor(status));
223     else
224         frame.base:SetVertexColor(unpack(baseColor));
225     end
226 end
227
228 local function updateVehicle(frame)
229     local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle);
230     if shouldTargetVehicle then
231         if not frame.inVehicle then
232             frame.inVehicle = true;
233             frame.displayed = frame.vehicle;
234             registerEvents(frame);
235         end
236     elseif frame.inVehicle then
237         frame.inVehicle = false;
238         frame.displayed = frame.unit;
239         registerEvents(frame);
240     end
241 end
242
243 local function updateRole(frame, unit)
244     local role = UnitGroupRolesAssigned(unit);
245     if role == "HEALER" then
246         frame.role:SetTexCoord(0.75, 1, 0, 1);
247         frame.role:Show();
248     elseif role == "TANK" then
249         frame.role:SetTexCoord(0.5, 0.75, 0, 1);
250         frame.role:Show();
251     else
252         frame.role:Hide();
253     end
254 end
255
256 local eventFuncs = {
257     ["UNIT_HEALTH"] = function(frame)
258         updateHealth(frame, frame.displayed);
259         updateText(frame, frame.displayed);
260         updateShield(frame, frame.displayed);
261         updateHealAbsorb(frame, frame.displayed);
262         -- no heal prediction update, that doesn't overflow too much
263     end,
264     ["UNIT_POWER"] = function(frame)
265         updatePower(frame, frame.displayed);
266     end,
267     ["UNIT_AURA"] = function(frame)
268         updateAuras(frame, frame.displayed);
269     end,
270     ["UNIT_HEAL_PREDICTION"] = function(frame)
271         updateHealPred(frame, frame.displayed);
272     end,
273     ["UNIT_ABSORB_AMOUNT_CHANGED"] = function(frame)
274         updateShield(frame, frame.displayed);
275     end,
276     ["UNIT_HEAL_ABSORB_AMOUNT_CHANGED"] = function(frame)
277         updateHealAbsorb(frame, frame.displayed);
278     end,
279     ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
280         updateAggro(frame, frame.displayed);
281     end,
282     ["UNIT_MAXHEALTH"] = function(frame)
283         updateMaxHealth(frame, frame.displayed);
284         updateHealth(frame, frame.displayed);
285         updateText(frame, frame.displayed);
286         updateShield(frame, frame.displayed);
287         updateHealAbsorb(frame, frame.displayed);
288     end,
289     ["UNIT_MAXPOWER"] = function(frame)
290         updateMaxPower(frame, frame.displayed);
291         updatePower(frame, frame.displayed);
292     end,
293     ["UNIT_DISPLAYPOWER"] = function(frame)
294         updatePowerColor(frame, frame.displayed);
295     end,
296     ["UNIT_NAME_UPDATE"] = function(frame)
297         updateName(frame, frame.displayed);
298     end,
299     ["UNIT_CONNECTION"] = function(frame)
300         updateText(frame, frame.displayed);
301     end,
302     ["INCOMING_RESURRECT_CHANGED"] = function(frame)
303         updateIncomingRes(frame, frame.unit);
304     end,
305     ["PARTY_MEMBER_ENABLE"] = function(frame)
306         -- new power info possibly (FrameXML/CompactUnitFrame.lua)
307         updateMaxPower(frame, frame.displayed);
308         updatePowerColor(frame, frame.displayed);
309     end,
310     ["PLAYER_ROLES_ASSIGNED"] = function(frame)
311         updateRole(frame, frame.unit);
312     end,
313     ["UPDATE_ALL_BARS"] = function(frame)
314         updateVehicle(frame);
315         updateMaxHealth(frame, frame.displayed);
316         updateMaxPower(frame, frame.displayed);
317         updateHealth(frame, frame.displayed);
318         updateText(frame, frame.displayed);
319         updatePower(frame, frame.displayed);
320         updateAuras(frame, frame.displayed);
321         updateShield(frame, frame.displayed);
322         updateHealPred(frame, frame.displayed);
323         updateHealAbsorb(frame, frame.displayed);
324         updatePowerColor(frame, frame.displayed);
325         updateIncomingRes(frame, frame.unit);
326         updateAggro(frame, frame.displayed);
327         updateName(frame, frame.displayed);
328         updateRole(frame, frame.unit);
329     end,
330 };
331 eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"];
332 eventFuncs["PARTY_MEMBER_DISABLE"] = eventFuncs["PARTY_MEMBER_ENABLE"];
333 eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
334 eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
335 eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"];
336 eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"];
337 function M.UnitEvent(self, event)
338     eventFuncs[event](self);
339 end