4 local ssub = string.sub;
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 RAID_CLASS_COLORS = RAID_CLASS_COLORS;
20 local READY_CHECK_READY_TEXTURE = READY_CHECK_READY_TEXTURE;
21 local READY_CHECK_NOT_READY_TEXTURE = READY_CHECK_NOT_READY_TEXTURE;
22 local READY_CHECK_WAITING_TEXTURE = READY_CHECK_WAITING_TEXTURE;
24 local checkIndicators = OmaRFIndicators.CheckIndicators;
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 powerColors = Settings.PowerColors;
32 local width = Settings.Width;
36 function M.RegisterEvents(frame)
37 frame:RegisterEvent("PLAYER_ENTERING_WORLD");
38 frame:RegisterEvent("PLAYER_ROLES_ASSIGNED");
39 frame:RegisterEvent("READY_CHECK");
40 frame:RegisterEvent("READY_CHECK_FINISHED");
41 frame:RegisterEvent("PARTY_MEMBER_ENABLE");
42 frame:RegisterEvent("PARTY_MEMBER_DISABLE");
43 frame:RegisterEvent("GROUP_ROSTER_UPDATE");
44 if frame.unit == "focus" then frame:RegisterEvent("PLAYER_FOCUS_CHANGED") end
47 local function unregisterPower(frame)
48 frame.health:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT");
49 frame.overlay:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT");
51 frame:UnregisterEvent("UNIT_POWER");
52 frame:UnregisterEvent("UNIT_MAXPOWER");
53 frame:UnregisterEvent("UNIT_DISPLAYPOWER");
54 frame:UnregisterEvent("UNIT_POWER_BAR_SHOW");
55 frame:UnregisterEvent("UNIT_POWER_BAR_HIDE");
58 local function registerPower(frame)
59 frame.health:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT", 0, 2);
60 frame.overlay:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT", 0, 2);
62 frame:RegisterUnitEvent("UNIT_POWER", frame.unit);
63 frame:RegisterUnitEvent("UNIT_MAXPOWER", frame.unit);
64 frame:RegisterUnitEvent("UNIT_DISPLAYPOWER", frame.unit);
65 frame:RegisterUnitEvent("UNIT_POWER_BAR_SHOW", frame.unit);
66 frame:RegisterUnitEvent("UNIT_POWER_BAR_HIDE", frame.unit);
69 function M.RegisterUnitEvents(frame)
70 -- events are taken from FrameXML/CompactUnitFrame.lua
71 -- TODO raid marker support
72 local displayed = frame.unit ~= frame.displayed and frame.displayed or nil;
73 frame:RegisterUnitEvent("UNIT_HEALTH", frame.unit, displayed);
74 frame:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", frame.unit, displayed);
75 frame:RegisterUnitEvent("UNIT_MAXHEALTH", frame.unit, displayed);
76 frame:RegisterUnitEvent("UNIT_NAME_UPDATE", frame.unit, displayed);
77 frame:RegisterUnitEvent("UNIT_AURA", frame.unit, displayed);
78 frame:RegisterUnitEvent("UNIT_HEAL_PREDICTION", frame.unit, displayed);
79 frame:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
80 frame:RegisterUnitEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
81 frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", frame.unit, displayed);
82 frame:RegisterUnitEvent("UNIT_CONNECTION", frame.unit, displayed);
83 frame:RegisterUnitEvent("INCOMING_RESURRECT_CHANGED", frame.unit, displayed);
84 frame:RegisterUnitEvent("PLAYER_FLAGS_CHANGED", frame.unit, displayed);
85 frame:RegisterUnitEvent("READY_CHECK_CONFIRM", frame.unit, displayed);
86 frame:RegisterUnitEvent("UNIT_ENTERED_VEHICLE", frame.unit, displayed);
87 frame:RegisterUnitEvent("UNIT_EXITED_VEHICLE", frame.unit, displayed);
88 frame:RegisterUnitEvent("UNIT_PET", frame.unit, displayed);
89 frame:RegisterUnitEvent("RAID_TARGET_UPDATE", frame.unit, displayed);
91 local registerUnitEvents = M.RegisterUnitEvents;
93 local function updateMaxHealth(frame, unit)
94 frame.health.max = UnitHealthMax(unit);
97 local function updateHealth(frame, unit)
98 local current, max = UnitHealth(unit), frame.health.max;
99 if current > max or max <= 0 then
100 -- somehow current health has gone over the maximum (missed maxhealth event possibly)
101 -- just put health bar full and update max health for next event
102 frame.health:SetWidth(width);
103 updateMaxHealth(frame, unit);
105 elseif current <= 0 or UnitIsDeadOrGhost(unit) then
108 frame.health:SetWidth(current/max*width);
113 local function updateText(frame, unit)
114 local healthLost = frame.health.max - UnitHealth(unit);
115 if UnitIsDeadOrGhost(unit) then
116 frame.text:SetText("Dead");
118 elseif not UnitIsConnected(unit) then
119 frame.text:SetText("DC");
121 elseif UnitIsAFK(unit) then
122 frame.text:SetText("afk");
124 elseif UnitIsDND(unit) and healthLost == 0 then
125 frame.text:SetText("dnd");
127 elseif healthLost > 0 then
128 if healthLost > 1200000000 then -- 1.2B
129 frame.text:SetFormattedText("-%.1fB", healthLost / 1000000000);
130 elseif healthLost > 1200000 then -- 1.2M
131 frame.text:SetFormattedText("-%.1fM", healthLost / 1000000);
132 elseif healthLost > 1000 then -- 1K
133 frame.text:SetFormattedText("-%dK", healthLost / 1000);
135 frame.text:SetFormattedText("-%d", healthLost)
143 local function updateIncomingRes(frame, unit)
144 if UnitHasIncomingResurrection(unit) then frame.rez:Show();
145 else frame.rez:Hide(); end
148 local function updateMaxPower(frame, unit)
149 frame.mana.max = UnitPowerMax(unit);
152 local function updatePower(frame, unit)
153 local current, max = UnitPower(unit), frame.mana.max;
156 elseif current > max or max <= 0 then
157 frame.mana:SetWidth(width);
158 updateMaxPower(frame, unit);
161 frame.mana:SetWidth(current/max*width);
166 local function updatePowerColor(frame, unit)
167 frame.mana:SetVertexColor(unpack(powerColors[UnitPowerType(unit)]));
170 local function updateName(frame, unit)
171 local name = UnitName(unit);
172 if not name then return end
173 frame.name:SetText(ssub(name, 1, 6));
175 local _, class = UnitClass(unit);
176 local color = RAID_CLASS_COLORS[class];
177 if color then frame.name:SetVertexColor(color.r, color.g, color.b) end
180 local function updateHealPred(frame, unit)
181 local incoming = UnitGetIncomingHeals(unit) or 0;
183 -- always at least 1 pixel space for heal prediction
184 local space = width - frame.health:GetWidth() + 1;
185 incoming = (incoming / frame.health.max) * width;
186 frame.healpred:SetWidth(min(space, incoming));
187 frame.healpred:Show();
189 frame.healpred:Hide();
193 local function updateShield(frame, unit)
194 local shield = UnitGetTotalAbsorbs(unit) or 0;
196 local space = width - frame.health:GetWidth();
197 shield = (shield / frame.health.max) * width;
200 frame.shieldhl:Show();
201 elseif space < shield then
202 frame.shield:SetWidth(space);
204 frame.shieldhl:Show();
206 frame.shield:SetWidth(shield);
208 frame.shieldhl:Hide();
212 frame.shieldhl:Hide();
216 local function updateHealAbsorb(frame, unit)
217 local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
219 local space = frame.health:GetWidth();
220 absorb = (absorb / frame.health.max) * width;
221 frame.healabsorb:SetWidth(min(space, absorb));
222 frame.healabsorb:Show();
224 frame.healabsorb:Hide();
228 local function updateAuras(frame, unit)
229 local alert = checkIndicators(frame, unit);
231 if frame.overlay.color ~= overlayColorAlert then
232 frame.overlay:SetVertexColor(unpack(overlayColorAlert));
233 frame.overlay.color = overlayColorAlert;
234 frame.overlay:Show();
236 elseif UnitDebuff(unit, 1, "RAID") ~= nil then
237 -- something dispellable
238 if frame.overlay.color ~= overlayColorDispel then
239 frame.overlay:SetVertexColor(unpack(overlayColorDispel));
240 frame.overlay.color = overlayColorDispel;
241 frame.overlay:Show();
243 -- don't overlay charmed when in vehicle
244 elseif UnitIsCharmed(unit) and unit == frame.unit then
245 if frame.overlay.color ~= overlayColorCharm then
246 frame.overlay:SetVertexColor(unpack(overlayColorCharm));
247 frame.overlay.color = overlayColorCharm;
248 frame.overlay:Show();
251 if frame.overlay.color ~= nil then
252 frame.overlay.color = nil;
253 frame.overlay:Hide();
258 local function updateAggro(frame, unit)
259 local status = UnitThreatSituation(unit);
260 if status and status > 0 then
261 frame.base:SetVertexColor(GetThreatStatusColor(status));
263 frame.base:SetVertexColor(unpack(baseColor));
267 local function updateVehicle(frame)
268 local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and
269 UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle);
270 if shouldTargetVehicle then
271 if not frame.inVehicle then
272 frame.inVehicle = true;
273 frame.displayed = frame.vehicle;
274 registerUnitEvents(frame);
276 elseif frame.inVehicle then
277 frame.inVehicle = false;
278 frame.displayed = frame.unit;
279 registerUnitEvents(frame);
283 local function updateRole(frame, unit)
284 local role = UnitGroupRolesAssigned(unit);
285 if role == "HEALER" then
286 frame.role:SetTexCoord(0.75, 1, 0, 1);
288 if not frame.role.healer then
289 registerPower(frame);
290 frame.role.healer = true;
292 elseif role == "TANK" then
293 frame.role:SetTexCoord(0.5, 0.75, 0, 1);
295 if frame.role.healer then
296 unregisterPower(frame);
297 frame.role.healer = false;
301 if frame.role.healer then
302 unregisterPower(frame);
303 frame.role.healer = false;
308 local function updateReadyCheck(frame, unit)
309 local status = GetReadyCheckStatus(unit);
310 if status == "ready" then
311 frame.ready:SetTexture(READY_CHECK_READY_TEXTURE);
313 elseif status == "notready" then
314 frame.ready:SetTexture(READY_CHECK_NOT_READY_TEXTURE);
316 elseif status == "waiting" then
317 frame.ready:SetTexture(READY_CHECK_WAITING_TEXTURE);
324 local function updateRaidMarker(frame, unit)
325 --print(unit, "marker");
329 ["UNIT_HEALTH"] = function(frame)
330 updateHealth(frame, frame.displayed);
331 updateText(frame, frame.displayed);
332 updateShield(frame, frame.displayed);
333 updateHealAbsorb(frame, frame.displayed);
334 -- no heal prediction update, that doesn't overflow too much
335 -- raid marker update here, because marker is removed when unit dies
336 -- without a RAID_TARGET_UPDATE event
337 updateRaidMarker(frame, frame.unit);
339 ["UNIT_POWER"] = function(frame)
340 updatePower(frame, frame.displayed);
342 ["UNIT_AURA"] = function(frame)
343 updateAuras(frame, frame.displayed);
345 ["UNIT_HEAL_PREDICTION"] = function(frame)
346 updateHealPred(frame, frame.displayed);
348 ["UNIT_ABSORB_AMOUNT_CHANGED"] = function(frame)
349 updateShield(frame, frame.displayed);
351 ["UNIT_HEAL_ABSORB_AMOUNT_CHANGED"] = function(frame)
352 updateHealAbsorb(frame, frame.displayed);
354 ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
355 updateAggro(frame, frame.displayed);
357 ["UNIT_MAXHEALTH"] = function(frame)
358 updateMaxHealth(frame, frame.displayed);
359 updateHealth(frame, frame.displayed);
360 updateText(frame, frame.displayed);
361 updateShield(frame, frame.displayed);
362 updateHealAbsorb(frame, frame.displayed);
364 ["UNIT_MAXPOWER"] = function(frame)
365 updateMaxPower(frame, frame.displayed);
366 updatePower(frame, frame.displayed);
368 ["UNIT_DISPLAYPOWER"] = function(frame)
369 updatePowerColor(frame, frame.displayed);
370 updateMaxPower(frame, frame.displayed);
371 updatePower(frame, frame.displayed);
373 ["UNIT_NAME_UPDATE"] = function(frame)
374 updateName(frame, frame.displayed);
376 ["UNIT_CONNECTION"] = function(frame)
377 updateText(frame, frame.displayed);
379 ["INCOMING_RESURRECT_CHANGED"] = function(frame)
380 updateIncomingRes(frame, frame.unit);
382 ["PARTY_MEMBER_ENABLE"] = function(frame)
383 -- new power info possibly (FrameXML/CompactUnitFrame.lua)
384 updateMaxPower(frame, frame.displayed);
385 updatePowerColor(frame, frame.displayed);
387 ["PLAYER_ROLES_ASSIGNED"] = function(frame)
388 updateRole(frame, frame.unit);
390 ["READY_CHECK"] = function(frame)
391 updateReadyCheck(frame, frame.unit);
393 ["RAID_TARGET_UPDATE"] = function(frame)
394 updateRaidMarker(frame, frame.unit);
396 ["UPDATE_ALL_BARS"] = function(frame)
397 updateRole(frame, frame.unit);
398 updateVehicle(frame);
399 updateMaxHealth(frame, frame.displayed);
400 updateMaxPower(frame, frame.displayed);
401 updateHealth(frame, frame.displayed);
402 updateText(frame, frame.displayed);
403 updatePower(frame, frame.displayed);
404 updateAuras(frame, frame.displayed);
405 updateShield(frame, frame.displayed);
406 updateHealPred(frame, frame.displayed);
407 updateHealAbsorb(frame, frame.displayed);
408 updatePowerColor(frame, frame.displayed);
409 updateAggro(frame, frame.displayed);
410 updateName(frame, frame.displayed);
411 updateIncomingRes(frame, frame.unit);
412 updateReadyCheck(frame, frame.unit);
413 updateRaidMarker(frame, frame.unit);
416 eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"];
417 eventFuncs["PARTY_MEMBER_DISABLE"] = eventFuncs["PARTY_MEMBER_ENABLE"];
418 eventFuncs["PLAYER_FLAGS_CHANGED"] = eventFuncs["UNIT_CONNECTION"];
419 eventFuncs["UNIT_POWER_BAR_SHOW"] = eventFuncs["UNIT_DISPLAYPOWER"];
420 eventFuncs["UNIT_POWER_BAR_HIDE"] = eventFuncs["UNIT_DISPLAYPOWER"];
421 eventFuncs["READY_CHECK_CONFIRM"] = eventFuncs["READY_CHECK"];
422 eventFuncs["READY_CHECK_FINISHED"] = eventFuncs["READY_CHECK"];
423 eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
424 eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
425 eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"];
426 eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"];
427 eventFuncs["PLAYER_ENTERING_WORLD"] = eventFuncs["UPDATE_ALL_BARS"];
428 eventFuncs["PLAYER_FOCUS_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
430 function M.UnitEvent(self, event)
431 eventFuncs[event](self);