df084ca - Only show 8 characters in target frame, some comment removal
[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 UnitIsAFK, UnitIsDND = UnitIsAFK, UnitIsDND;
11 local UnitGetIncomingHeals, UnitGetTotalAbsorbs = UnitGetIncomingHeals, UnitGetTotalAbsorbs;
12 local UnitThreatSituation, GetThreatStatusColor = UnitThreatSituation, GetThreatStatusColor;
13 local UnitIsDeadOrGhost, UnitIsConnected = UnitIsDeadOrGhost, UnitIsConnected;
14 local UnitHasIncomingResurrection = UnitHasIncomingResurrection;
15 local UnitGetTotalHealAbsorbs = UnitGetTotalHealAbsorbs;
16 local UnitHasVehicleUI, UnitTargetsVehicleInRaidUI = UnitHasVehicleUI, UnitTargetsVehicleInRaidUI;
17 local GetReadyCheckTimeLeft, GetReadyCheckStatus = GetReadyCheckTimeLeft, GetReadyCheckStatus;
18 local UnitGroupRolesAssigned = UnitGroupRolesAssigned;
19 local GetRaidTargetIndex, SetRaidTargetIconTexture = GetRaidTargetIndex, SetRaidTargetIconTexture;
20 local RAID_CLASS_COLORS = RAID_CLASS_COLORS;
21 local READY_CHECK_READY_TEXTURE = READY_CHECK_READY_TEXTURE;
22 local READY_CHECK_NOT_READY_TEXTURE = READY_CHECK_NOT_READY_TEXTURE;
23 local READY_CHECK_WAITING_TEXTURE = READY_CHECK_WAITING_TEXTURE;
24
25 local checkIndicators = OmaRFIndicators.CheckIndicators;
26
27 local Settings = OmaRFSettings;
28 local baseColor = Settings.BaseColor;
29 local overlayColorDispel = Settings.OverlayColorDispel;
30 local overlayColorCharm = Settings.OverlayColorCharm;
31 local overlayColorAlert = Settings.OverlayColorAlert;
32 local powerColors = Settings.PowerColors;
33 local width = Settings.Width;
34
35 local M = {};
36 OmaRFEvents = M;
37 function M.RegisterEvents(frame)
38     frame:RegisterEvent("PLAYER_ENTERING_WORLD");
39     frame:RegisterEvent("PLAYER_ROLES_ASSIGNED");
40     frame:RegisterEvent("READY_CHECK");
41     frame:RegisterEvent("READY_CHECK_FINISHED");
42     frame:RegisterEvent("GROUP_ROSTER_UPDATE");
43     frame:RegisterEvent("RAID_TARGET_UPDATE");
44     if frame.unit == "focus" then frame:RegisterEvent("PLAYER_FOCUS_CHANGED") end
45 end
46
47 local function unregisterPower(frame)
48     frame.health:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT");
49     frame.overlay:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT");
50     frame.mana:Hide();
51     frame:UnregisterEvent("PARTY_MEMBER_ENABLE");
52     frame:UnregisterEvent("PARTY_MEMBER_DISABLE");
53     frame:UnregisterEvent("UNIT_POWER");
54     frame:UnregisterEvent("UNIT_MAXPOWER");
55     frame:UnregisterEvent("UNIT_DISPLAYPOWER");
56     frame:UnregisterEvent("UNIT_POWER_BAR_SHOW");
57     frame:UnregisterEvent("UNIT_POWER_BAR_HIDE");
58 end
59
60 local function registerPower(frame)
61     frame.health:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT", 0, 2);
62     frame.overlay:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT", 0, 2);
63     frame.mana:Show();
64     frame:RegisterEvent("PARTY_MEMBER_ENABLE");
65     frame:RegisterEvent("PARTY_MEMBER_DISABLE");
66     frame:RegisterUnitEvent("UNIT_POWER", frame.unit);
67     frame:RegisterUnitEvent("UNIT_MAXPOWER", frame.unit);
68     frame:RegisterUnitEvent("UNIT_DISPLAYPOWER", frame.unit);
69     frame:RegisterUnitEvent("UNIT_POWER_BAR_SHOW", frame.unit);
70     frame:RegisterUnitEvent("UNIT_POWER_BAR_HIDE", frame.unit);
71 end
72
73 function M.RegisterUnitEvents(frame)
74     -- events are taken from FrameXML/CompactUnitFrame.lua
75     local displayed = frame.unit ~= frame.displayed and frame.displayed or nil;
76     frame:RegisterUnitEvent("UNIT_HEALTH", frame.unit, displayed);
77     frame:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", frame.unit, displayed);
78     frame:RegisterUnitEvent("UNIT_MAXHEALTH", frame.unit, displayed);
79     frame:RegisterUnitEvent("UNIT_NAME_UPDATE", frame.unit, displayed);
80     frame:RegisterUnitEvent("UNIT_AURA", frame.unit, displayed);
81     frame:RegisterUnitEvent("UNIT_HEAL_PREDICTION", frame.unit, displayed);
82     frame:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
83     frame:RegisterUnitEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
84     frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", frame.unit, displayed);
85     frame:RegisterUnitEvent("UNIT_CONNECTION", frame.unit, displayed);
86     frame:RegisterUnitEvent("INCOMING_RESURRECT_CHANGED", frame.unit, displayed);
87     frame:RegisterUnitEvent("PLAYER_FLAGS_CHANGED", frame.unit, displayed);
88     frame:RegisterUnitEvent("READY_CHECK_CONFIRM", frame.unit, displayed);
89     frame:RegisterUnitEvent("UNIT_ENTERED_VEHICLE", frame.unit, displayed);
90     frame:RegisterUnitEvent("UNIT_EXITED_VEHICLE", frame.unit, displayed);
91     frame:RegisterUnitEvent("UNIT_PET", frame.unit, displayed);
92 end
93 local registerUnitEvents = M.RegisterUnitEvents;
94
95 local function updateMaxHealth(frame, unit)
96     frame.health.max = UnitHealthMax(unit);
97 end
98
99 local function updateHealth(frame, unit)
100     local current, max = UnitHealth(unit), frame.health.max;
101     if current > max or max <= 0 then
102         -- somehow current health has gone over the maximum (missed maxhealth event possibly)
103         -- just put health bar full and update max health for next event
104         frame.health:SetWidth(width);
105         updateMaxHealth(frame, unit);
106         frame.health:Show();
107     elseif current <= 0 or UnitIsDeadOrGhost(unit) then
108         frame.health:Hide();
109     else
110         frame.health:SetWidth(current/max*width);
111         frame.health:Show();
112     end
113 end
114
115 local function updateText(frame, unit)
116     local healthLost = frame.health.max - UnitHealth(unit);
117     if UnitIsDeadOrGhost(unit) then
118         frame.text:SetText("Dead");
119         frame.text:Show();
120     elseif not UnitIsConnected(unit) then
121         frame.text:SetText("DC");
122         frame.text:Show();
123     elseif UnitIsAFK(unit) then
124         frame.text:SetText("afk");
125         frame.text:Show();
126     elseif UnitIsDND(unit) and healthLost == 0 then
127         frame.text:SetText("dnd");
128         frame.text:Show();
129     elseif healthLost > 0 then
130         if healthLost > 1200000000 then -- 1.2B
131             frame.text:SetFormattedText("-%.1fB", healthLost / 1000000000);
132         elseif healthLost > 1200000 then -- 1.2M
133             frame.text:SetFormattedText("-%.1fM", healthLost / 1000000);
134         elseif healthLost > 1000 then -- 1K
135             frame.text:SetFormattedText("-%dK", healthLost / 1000);
136         else
137             frame.text:SetFormattedText("-%d", healthLost)
138         end
139         frame.text:Show();
140     else
141         frame.text:Hide();
142     end
143 end
144
145 local function updateIncomingRes(frame, unit)
146     if UnitHasIncomingResurrection(unit) then frame.rez:Show();
147     else frame.rez:Hide(); end
148 end
149
150 local function updateMaxPower(frame, unit)
151     frame.mana.max = UnitPowerMax(unit);
152 end
153
154 local function updatePower(frame, unit)
155     local current, max = UnitPower(unit), frame.mana.max;
156     if current <= 0 then
157         frame.mana:Hide();
158     elseif current > max or max <= 0 then
159         frame.mana:SetWidth(width);
160         updateMaxPower(frame, unit);
161         frame.mana:Show();
162     else
163         frame.mana:SetWidth(current/max*width);
164         frame.mana:Show();
165     end
166 end
167
168 local function updatePowerColor(frame, unit)
169     frame.mana:SetVertexColor(unpack(powerColors[UnitPowerType(unit)]));
170 end
171
172 local function updateName(frame, unit)
173     local name = UnitName(unit);
174     if not name then return end
175     frame.name:SetText(ssub(name, 1, 6));
176
177     local _, class = UnitClass(unit);
178     local color = RAID_CLASS_COLORS[class];
179     if color then frame.name:SetVertexColor(color.r, color.g, color.b) end
180 end
181
182 local function updateHealPred(frame, unit)
183     local incoming = UnitGetIncomingHeals(unit) or 0;
184     if incoming > 0 then
185         -- always at least 1 pixel space for heal prediction
186         local space = width - frame.health:GetWidth() + 1;
187         incoming = (incoming / frame.health.max) * width;
188         frame.healpred:SetWidth(min(space, incoming));
189         frame.healpred:Show();
190     else
191         frame.healpred:Hide();
192     end
193 end
194
195 local function updateShield(frame, unit)
196     local shield = UnitGetTotalAbsorbs(unit) or 0;
197     if shield > 0 then
198         local space = width - frame.health:GetWidth();
199         shield = (shield / frame.health.max) * width;
200         if space == 0 then
201             frame.shield:Hide();
202             frame.shieldhl:Show();
203         elseif space < shield then
204             frame.shield:SetWidth(space);
205             frame.shield:Show();
206             frame.shieldhl:Show();
207         else
208             frame.shield:SetWidth(shield);
209             frame.shield:Show();
210             frame.shieldhl:Hide();
211         end
212     else
213         frame.shield:Hide();
214         frame.shieldhl:Hide();
215     end
216 end
217
218 local function updateHealAbsorb(frame, unit)
219     local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
220     if absorb > 0 then
221         local space = frame.health:GetWidth();
222         absorb = (absorb / frame.health.max) * width;
223         frame.healabsorb:SetWidth(min(space, absorb));
224         frame.healabsorb:Show();
225     else
226         frame.healabsorb:Hide();
227     end
228 end
229
230 local function updateAuras(frame, unit)
231     local alert = checkIndicators(frame, unit);
232     if alert then
233         if frame.overlay.color ~= overlayColorAlert then
234             frame.overlay:SetVertexColor(unpack(overlayColorAlert));
235             frame.overlay.color = overlayColorAlert;
236             frame.overlay:Show();
237         end
238     elseif UnitDebuff(unit, 1, "RAID") ~= nil then
239         -- something dispellable
240         if frame.overlay.color ~= overlayColorDispel then
241             frame.overlay:SetVertexColor(unpack(overlayColorDispel));
242             frame.overlay.color = overlayColorDispel;
243             frame.overlay:Show();
244         end
245     -- don't overlay charmed when in vehicle
246     elseif UnitIsCharmed(unit) and unit == frame.unit then
247         if frame.overlay.color ~= overlayColorCharm then
248             frame.overlay:SetVertexColor(unpack(overlayColorCharm));
249             frame.overlay.color = overlayColorCharm;
250             frame.overlay:Show();
251         end
252     else
253         if frame.overlay.color ~= nil then
254             frame.overlay.color = nil;
255             frame.overlay:Hide();
256         end
257     end
258 end
259
260 local function updateAggro(frame, unit)
261     local status = UnitThreatSituation(unit);
262     if status and status > 0 then
263         frame.base:SetVertexColor(GetThreatStatusColor(status));
264     else
265         frame.base:SetVertexColor(unpack(baseColor));
266     end
267 end
268
269 local function updateVehicle(frame)
270     local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and
271         UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle);
272     if shouldTargetVehicle then
273         if not frame.inVehicle then
274             frame.inVehicle = true;
275             frame.displayed = frame.vehicle;
276             registerUnitEvents(frame);
277         end
278     elseif frame.inVehicle then
279         frame.inVehicle = false;
280         frame.displayed = frame.unit;
281         registerUnitEvents(frame);
282     end
283 end
284
285 local function updateRole(frame, unit)
286     local role = UnitGroupRolesAssigned(unit);
287     if role == "HEALER" then
288         frame.role:SetTexCoord(0.75, 1, 0, 1);
289         frame.role:Show();
290         registerPower(frame);
291         frame.role.healer = true;
292     elseif role == "TANK" then
293         frame.role:SetTexCoord(0.5, 0.75, 0, 1);
294         frame.role:Show();
295         if frame.role.healer then
296             frame.role.healer = false;
297             unregisterPower(frame);
298         end
299     else
300         frame.role:Hide();
301         if frame.role.healer then
302             frame.role.healer = false;
303             unregisterPower(frame);
304         end
305     end
306 end
307
308 local function updateReadyCheck(frame, unit)
309     local status = GetReadyCheckStatus(unit);
310     if status == "ready" then
311         frame.ready:SetTexture(READY_CHECK_READY_TEXTURE);
312         frame.ready:Show()
313     elseif status == "notready" then
314         frame.ready:SetTexture(READY_CHECK_NOT_READY_TEXTURE);
315         frame.ready:Show()
316     elseif status == "waiting" then
317         frame.ready:SetTexture(READY_CHECK_WAITING_TEXTURE);
318         frame.ready:Show()
319     else
320         frame.ready:Hide()
321     end
322 end
323
324 local function updateRaidMarker(frame, unit)
325     local index = GetRaidTargetIndex(unit);
326     if index then
327         SetRaidTargetIconTexture(frame.targeticon, index);
328         frame.targeticon:Show();
329     else
330         frame.targeticon:Hide();
331     end
332 end
333
334 local eventFuncs = {
335     ["UNIT_HEALTH"] = function(frame)
336         updateHealth(frame, frame.displayed);
337         updateText(frame, frame.displayed);
338         updateShield(frame, frame.displayed);
339         updateHealAbsorb(frame, frame.displayed);
340         -- no heal prediction update, that doesn't overflow too much
341         -- raid marker update here, because marker is removed when unit dies
342         -- without a RAID_TARGET_UPDATE event
343         updateRaidMarker(frame, frame.unit);
344     end,
345     ["UNIT_POWER"] = function(frame)
346         updatePower(frame, frame.displayed);
347     end,
348     ["UNIT_AURA"] = function(frame)
349         updateAuras(frame, frame.displayed);
350     end,
351     ["UNIT_HEAL_PREDICTION"] = function(frame)
352         updateHealPred(frame, frame.displayed);
353     end,
354     ["UNIT_ABSORB_AMOUNT_CHANGED"] = function(frame)
355         updateShield(frame, frame.displayed);
356     end,
357     ["UNIT_HEAL_ABSORB_AMOUNT_CHANGED"] = function(frame)
358         updateHealAbsorb(frame, frame.displayed);
359     end,
360     ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
361         updateAggro(frame, frame.displayed);
362     end,
363     ["UNIT_MAXHEALTH"] = function(frame)
364         updateMaxHealth(frame, frame.displayed);
365         updateHealth(frame, frame.displayed);
366         updateText(frame, frame.displayed);
367         updateShield(frame, frame.displayed);
368         updateHealAbsorb(frame, frame.displayed);
369     end,
370     ["UNIT_MAXPOWER"] = function(frame)
371         updateMaxPower(frame, frame.displayed);
372         updatePower(frame, frame.displayed);
373     end,
374     ["UNIT_DISPLAYPOWER"] = function(frame)
375         updatePowerColor(frame, frame.displayed);
376         updateMaxPower(frame, frame.displayed);
377         updatePower(frame, frame.displayed);
378     end,
379     ["UNIT_NAME_UPDATE"] = function(frame)
380         updateName(frame, frame.displayed);
381     end,
382     ["UNIT_CONNECTION"] = function(frame)
383         updateText(frame, frame.displayed);
384     end,
385     ["INCOMING_RESURRECT_CHANGED"] = function(frame)
386         updateIncomingRes(frame, frame.unit);
387     end,
388     ["PARTY_MEMBER_ENABLE"] = function(frame)
389         -- new power info possibly (FrameXML/CompactUnitFrame.lua)
390         updateMaxPower(frame, frame.displayed);
391         updatePowerColor(frame, frame.displayed);
392     end,
393     ["PLAYER_ROLES_ASSIGNED"] = function(frame)
394         updateRole(frame, frame.unit);
395     end,
396     ["READY_CHECK"] = function(frame)
397         updateReadyCheck(frame, frame.unit);
398     end,
399     ["RAID_TARGET_UPDATE"] = function(frame)
400         updateRaidMarker(frame, frame.displayed);
401     end,
402     ["UPDATE_ALL_BARS"] = function(frame)
403         updateRole(frame, frame.unit);
404         updateVehicle(frame);
405         updateMaxHealth(frame, frame.displayed);
406         updateHealth(frame, frame.displayed);
407         updateText(frame, frame.displayed);
408         if frame.role.healer then
409             updateMaxPower(frame, frame.displayed);
410             updatePower(frame, frame.displayed);
411             updatePowerColor(frame, frame.displayed);
412         end
413         updateAuras(frame, frame.displayed);
414         updateShield(frame, frame.displayed);
415         updateHealPred(frame, frame.displayed);
416         updateHealAbsorb(frame, frame.displayed);
417         updateAggro(frame, frame.displayed);
418         updateName(frame, frame.displayed);
419         updateIncomingRes(frame, frame.unit);
420         updateReadyCheck(frame, frame.unit);
421         updateRaidMarker(frame, frame.displayed);
422     end,
423 };
424 eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"];
425 eventFuncs["PARTY_MEMBER_DISABLE"] = eventFuncs["PARTY_MEMBER_ENABLE"];
426 eventFuncs["PLAYER_FLAGS_CHANGED"] = eventFuncs["UNIT_CONNECTION"];
427 eventFuncs["UNIT_POWER_BAR_SHOW"] = eventFuncs["UNIT_DISPLAYPOWER"];
428 eventFuncs["UNIT_POWER_BAR_HIDE"] = eventFuncs["UNIT_DISPLAYPOWER"];
429 eventFuncs["READY_CHECK_CONFIRM"] = eventFuncs["READY_CHECK"];
430 eventFuncs["READY_CHECK_FINISHED"] = eventFuncs["READY_CHECK"];
431 eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
432 eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
433 eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"];
434 eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"];
435 eventFuncs["PLAYER_ENTERING_WORLD"] = eventFuncs["UPDATE_ALL_BARS"];
436 eventFuncs["PLAYER_FOCUS_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
437
438 function M.UnitEvent(self, event)
439     eventFuncs[event](self);
440 end