02986a2 - Add disc priest
[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 UnitHealth, UnitHealthMax = UnitHealth, UnitHealthMax;
9 local UnitIsAFK, UnitIsDND = UnitIsAFK, UnitIsDND;
10 local UnitGetIncomingHeals, UnitGetTotalAbsorbs = UnitGetIncomingHeals, UnitGetTotalAbsorbs;
11 local UnitThreatSituation, GetThreatStatusColor = UnitThreatSituation, GetThreatStatusColor;
12 local UnitIsDeadOrGhost, UnitIsConnected = UnitIsDeadOrGhost, UnitIsConnected;
13 local UnitGetTotalHealAbsorbs = UnitGetTotalHealAbsorbs;
14 local UnitHasVehicleUI, UnitTargetsVehicleInRaidUI = UnitHasVehicleUI, UnitTargetsVehicleInRaidUI;
15 local GetReadyCheckTimeLeft, GetReadyCheckStatus = GetReadyCheckTimeLeft, GetReadyCheckStatus;
16 local UnitGroupRolesAssigned = UnitGroupRolesAssigned;
17 local GetRaidTargetIndex, SetRaidTargetIconTexture = GetRaidTargetIndex, SetRaidTargetIconTexture;
18 local RAID_CLASS_COLORS = RAID_CLASS_COLORS;
19 local READY_CHECK_READY_TEXTURE = READY_CHECK_READY_TEXTURE;
20 local READY_CHECK_NOT_READY_TEXTURE = READY_CHECK_NOT_READY_TEXTURE;
21 local READY_CHECK_WAITING_TEXTURE = READY_CHECK_WAITING_TEXTURE;
22
23 local updateIndicatorAuras = OmaRFIndicators.UpdateAuras;
24 local updateMajorAuras = OmaRFIndicators.UpdateMajorAuras;
25
26 local Settings = OmaRFSettings;
27 local baseColor = Settings.BaseColor;
28 local overlayColorDispel = Settings.OverlayColorDispel;
29 local overlayColorCharm = Settings.OverlayColorCharm;
30 local overlayColorAlert = Settings.OverlayColorAlert;
31 local width = Settings.Width;
32
33 local M = {};
34 OmaRFEvents = M;
35 function M.RegisterEvents(frame)
36     frame:RegisterEvent("PLAYER_ENTERING_WORLD");
37     frame:RegisterEvent("PLAYER_ROLES_ASSIGNED");
38     frame:RegisterEvent("READY_CHECK");
39     frame:RegisterEvent("READY_CHECK_FINISHED");
40     frame:RegisterEvent("GROUP_ROSTER_UPDATE");
41     frame:RegisterEvent("RAID_TARGET_UPDATE");
42     if frame.unit == "focus" then frame:RegisterEvent("PLAYER_FOCUS_CHANGED") end
43 end
44
45 function M.RegisterUnitEvents(frame)
46     -- events are taken from FrameXML/CompactUnitFrame.lua
47     local displayed = frame.unit ~= frame.displayed and frame.displayed or nil;
48     frame:RegisterUnitEvent("UNIT_HEALTH", frame.unit, displayed);
49     frame:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", frame.unit, displayed);
50     frame:RegisterUnitEvent("UNIT_MAXHEALTH", frame.unit, displayed);
51     frame:RegisterUnitEvent("UNIT_NAME_UPDATE", frame.unit, displayed);
52     frame:RegisterUnitEvent("UNIT_AURA", frame.unit, displayed);
53     frame:RegisterUnitEvent("UNIT_HEAL_PREDICTION", frame.unit, displayed);
54     frame:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
55     frame:RegisterUnitEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
56     frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", frame.unit, displayed);
57     frame:RegisterUnitEvent("UNIT_CONNECTION", frame.unit, displayed);
58     frame:RegisterUnitEvent("PLAYER_FLAGS_CHANGED", frame.unit, displayed);
59     frame:RegisterUnitEvent("READY_CHECK_CONFIRM", frame.unit, displayed);
60     frame:RegisterUnitEvent("UNIT_ENTERED_VEHICLE", frame.unit, displayed);
61     frame:RegisterUnitEvent("UNIT_EXITED_VEHICLE", frame.unit, displayed);
62     frame:RegisterUnitEvent("UNIT_PET", frame.unit, displayed);
63 end
64 local registerUnitEvents = M.RegisterUnitEvents;
65
66 local function updateText(frame, unit)
67     if UnitIsDeadOrGhost(unit) then
68         frame.dead = true;
69         frame.text:SetText("Dead");
70         frame.text:Show();
71     elseif not UnitIsConnected(unit) then
72         frame.text:SetText("DC");
73         frame.text:Show();
74     elseif UnitIsAFK(unit) then
75         frame.text:SetText("afk");
76         frame.text:Show();
77     elseif UnitIsDND(unit) then
78         frame.text:SetText("dnd");
79         frame.text:Show();
80     else
81         frame.text:Hide();
82     end
83 end
84 M.UpdateText = updateText;
85
86 local function updateMaxHealth(frame, unit)
87     frame.health.max = UnitHealthMax(unit);
88 end
89 M.UpdateMaxHealth = updateMaxHealth;
90
91 local function updateHealth(frame, unit)
92     local current, max = UnitHealth(unit), frame.health.max;
93     if current > max or max <= 0 then
94         -- somehow current health has gone over the maximum (missed maxhealth event possibly)
95         -- just put health bar full and update max health for next event
96         frame.health:SetWidth(width);
97         frame.health.width = width;
98         updateMaxHealth(frame, unit);
99         frame.health:Show();
100     elseif current <= 0 or UnitIsDeadOrGhost(unit) then
101         frame.health:Hide();
102         return updateText(frame, unit); -- update death
103     else
104         local w = current/max*width;
105         frame.health:SetWidth(w);
106         frame.health.width = w;
107         frame.health:Show();
108     end
109
110     if frame.dead and current > 0 then
111         frame.dead = nil;
112         updateText(frame, unit); -- update revive
113     end
114 end
115 M.UpdateHealth = updateHealth;
116
117 local function updateName(frame, unit)
118     local name = UnitName(unit);
119     if not name then return end
120     name = ssub(name, 1, 6);
121     if frame.unit == unit then
122         frame.name:SetText(name);
123     else
124         frame.name:SetFormattedText("-%s", name);
125     end
126
127     local _, class = UnitClass(unit);
128     local color = RAID_CLASS_COLORS[class];
129     if color then frame.name:SetVertexColor(color.r, color.g, color.b) end
130 end
131 M.UpdateName = updateName;
132
133 local function updateHealPred(frame, unit)
134     local incoming = UnitGetIncomingHeals(unit) or 0;
135     if incoming > 0 then
136         incoming = (incoming / frame.health.max) * width;
137         -- always at least 1 pixel space for heal prediction
138         frame.healpred:SetWidth(min(width - frame.health.width + 1, incoming));
139         frame.healpred:Show();
140     else
141         frame.healpred:Hide();
142     end
143 end
144 M.UpdateHealPred = updateHealPred;
145
146 local function updateShield(frame, unit)
147     local shield = UnitGetTotalAbsorbs(unit) or 0;
148     if shield > 0 then
149         local space = width - frame.health.width;
150         shield = (shield / frame.health.max) * width;
151         if space == 0 then
152             frame.shield:Hide();
153             frame.shieldhl:Show();
154         elseif space < shield then
155             frame.shield:SetWidth(space);
156             frame.shield:Show();
157             frame.shieldhl:Show();
158         else
159             frame.shield:SetWidth(shield);
160             frame.shield:Show();
161             frame.shieldhl:Hide();
162         end
163     else
164         frame.shield:Hide();
165         frame.shieldhl:Hide();
166     end
167 end
168 M.UpdateShield = updateShield;
169
170 local function updateHealAbsorb(frame, unit)
171     local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
172     if absorb > 0 then
173         absorb = (absorb / frame.health.max) * width;
174         frame.healabsorb:SetWidth(min(frame.health.width, absorb));
175         frame.healabsorb:Show();
176     else
177         frame.healabsorb:Hide();
178     end
179 end
180 M.UpdateHealAbsorb = updateHealAbsorb;
181
182 local function updateAuras(frame, unit)
183     updateIndicatorAuras(frame, unit); -- this is throttled
184     local barColor = updateMajorAuras(frame, unit);
185     if barColor then
186         if barColor == true and frame.overlay.color ~= overlayColorAlert then
187             frame.overlay:SetVertexColor(unpack(overlayColorAlert));
188             frame.overlay.color = overlayColorAlert;
189             frame.overlay:Show();
190         elseif barColor ~= true and frame.overlay.color ~= barColor then
191             frame.overlay:SetVertexColor(unpack(barColor));
192             frame.overlay.color = barColor;
193             frame.overlay:Show();
194         end
195     elseif UnitDebuff(unit, 1, "RAID") ~= nil then
196         -- something dispellable
197         if frame.overlay.color ~= overlayColorDispel then
198             frame.overlay:SetVertexColor(unpack(overlayColorDispel));
199             frame.overlay.color = overlayColorDispel;
200             frame.overlay:Show();
201         end
202     -- don't overlay charmed when in vehicle
203     elseif UnitIsCharmed(unit) and unit == frame.unit then
204         if frame.overlay.color ~= overlayColorCharm then
205             frame.overlay:SetVertexColor(unpack(overlayColorCharm));
206             frame.overlay.color = overlayColorCharm;
207             frame.overlay:Show();
208         end
209     else
210         if frame.overlay.color ~= nil then
211             frame.overlay.color = nil;
212             frame.overlay:Hide();
213         end
214     end
215 end
216 M.UpdateAuras = updateAuras;
217
218 local function updateAggro(frame, unit)
219     local status = UnitThreatSituation(unit);
220     if status and status > 0 then
221         frame.base:SetVertexColor(GetThreatStatusColor(status));
222     else
223         frame.base:SetVertexColor(unpack(baseColor));
224     end
225 end
226 M.UpdateAggro = updateAggro;
227
228 local function updateVehicle(frame)
229     local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and
230         UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle);
231     if shouldTargetVehicle then
232         if not frame.inVehicle then
233             frame.inVehicle = true;
234             frame.displayed = frame.vehicle;
235             registerUnitEvents(frame);
236         end
237     elseif frame.inVehicle then
238         frame.inVehicle = false;
239         frame.displayed = frame.unit;
240         registerUnitEvents(frame);
241     end
242 end
243 M.UpdateVehicle = updateVehicle;
244
245 local function updateRole(frame, unit)
246     local role = UnitGroupRolesAssigned(unit);
247     if role == "HEALER" then
248         frame.role:SetTexCoord(0.75, 1, 0, 1);
249         frame.role:Show();
250     elseif role == "TANK" then
251         frame.role:SetTexCoord(0.5, 0.75, 0, 1);
252         frame.role:Show();
253     else
254         frame.role:Hide();
255     end
256 end
257 M.UpdateRole = updateRole;
258
259 local function updateReadyCheck(frame, unit)
260     local status = GetReadyCheckStatus(unit);
261     if status == "ready" then
262         frame.ready:SetTexture(READY_CHECK_READY_TEXTURE);
263         frame.ready:Show()
264     elseif status == "notready" then
265         frame.ready:SetTexture(READY_CHECK_NOT_READY_TEXTURE);
266         frame.ready:Show()
267     elseif status == "waiting" then
268         frame.ready:SetTexture(READY_CHECK_WAITING_TEXTURE);
269         frame.ready:Show()
270     else
271         frame.ready:Hide()
272     end
273 end
274 M.UpdateReadyCheck = updateReadyCheck;
275
276 local function updateRaidMarker(frame, unit)
277     local index = GetRaidTargetIndex(unit);
278     if index then
279         SetRaidTargetIconTexture(frame.targeticon, index);
280         frame.targeticon:Show();
281     else
282         frame.targeticon:Hide();
283     end
284 end
285 M.UpdateRaidMarker = updateRaidMarker;
286
287 local eventFuncs = {
288     ["UNIT_HEALTH"] = function(frame)
289         updateHealth(frame, frame.displayed);
290         updateShield(frame, frame.displayed);
291         updateHealAbsorb(frame, frame.displayed);
292         -- no heal prediction update, that doesn't overflow too much
293     end,
294     ["UNIT_AURA"] = function(frame)
295         updateAuras(frame, frame.displayed);
296     end,
297     ["UNIT_HEAL_PREDICTION"] = function(frame)
298         updateHealPred(frame, frame.displayed);
299     end,
300     ["UNIT_ABSORB_AMOUNT_CHANGED"] = function(frame)
301         updateShield(frame, frame.displayed);
302     end,
303     ["UNIT_HEAL_ABSORB_AMOUNT_CHANGED"] = function(frame)
304         updateHealAbsorb(frame, frame.displayed);
305     end,
306     ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
307         updateAggro(frame, frame.displayed);
308     end,
309     ["UNIT_MAXHEALTH"] = function(frame)
310         updateMaxHealth(frame, frame.displayed);
311         updateHealth(frame, frame.displayed);
312         updateShield(frame, frame.displayed);
313         updateHealAbsorb(frame, frame.displayed);
314     end,
315     ["UNIT_NAME_UPDATE"] = function(frame)
316         updateName(frame, frame.unit);
317     end,
318     ["UNIT_CONNECTION"] = function(frame)
319         updateText(frame, frame.displayed);
320     end,
321     ["PLAYER_ROLES_ASSIGNED"] = function(frame)
322         updateRole(frame, frame.unit);
323     end,
324     ["READY_CHECK"] = function(frame)
325         updateReadyCheck(frame, frame.unit);
326     end,
327     ["RAID_TARGET_UPDATE"] = function(frame)
328         updateRaidMarker(frame, frame.displayed);
329     end,
330     ["UPDATE_ALL_BARS"] = function(frame)
331         updateRole(frame, frame.unit);
332         updateVehicle(frame);
333         updateMaxHealth(frame, frame.displayed);
334         updateHealth(frame, frame.displayed);
335         updateText(frame, frame.displayed);
336         updateAuras(frame, frame.displayed);
337         updateShield(frame, frame.displayed);
338         updateHealPred(frame, frame.displayed);
339         updateHealAbsorb(frame, frame.displayed);
340         updateAggro(frame, frame.displayed);
341         updateName(frame, frame.unit);
342         updateReadyCheck(frame, frame.unit);
343         updateRaidMarker(frame, frame.displayed);
344     end,
345 };
346 eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"];
347 eventFuncs["PLAYER_FLAGS_CHANGED"] = eventFuncs["UNIT_CONNECTION"];
348 eventFuncs["READY_CHECK_CONFIRM"] = eventFuncs["READY_CHECK"];
349 eventFuncs["READY_CHECK_FINISHED"] = eventFuncs["READY_CHECK"];
350 eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
351 eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
352 eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"];
353 eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"];
354 eventFuncs["PLAYER_ENTERING_WORLD"] = eventFuncs["UPDATE_ALL_BARS"];
355 eventFuncs["PLAYER_FOCUS_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
356
357 function M.UnitEvent(self, event)
358     return eventFuncs[event](self);
359 end