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 updateHealth(frame, unit)
94 local current, max = UnitHealth(unit), frame.health.max;
96 -- sanity check, occasionally UnitHealthMax gives zero
98 -- somehow current health has gone over the maximum (missed maxhealth event)
99 frame.health.max = UnitHealthMax(unit);
100 max = frame.health.max;
101 if current > max then
102 -- error state, still over maximum
103 frame.health:SetWidth(width);
107 frame.health:SetWidth(current/frame.health.max*width);
109 frame.health:SetWidth(width);
113 if UnitIsDeadOrGhost(unit) then
118 local function updateText(frame, unit)
119 local current, max = UnitHealth(unit), frame.health.max;
120 local healthLost = max - current;
121 if UnitIsDeadOrGhost(unit) then
122 frame.text:SetText("Dead");
124 elseif not UnitIsConnected(unit) then
125 frame.text:SetText("DC");
127 elseif UnitIsAFK(unit) then
128 frame.text:SetText("afk");
130 elseif UnitIsDND(unit) and healthLost == 0 then
131 frame.text:SetText("dnd");
133 elseif healthLost > 0 then
134 if healthLost > 1200000000 then -- 1.2B
135 frame.text:SetFormattedText("-%.1fB", healthLost / 1000000000);
136 elseif healthLost > 1200000 then -- 1.2M
137 frame.text:SetFormattedText("-%.1fM", healthLost / 1000000);
138 elseif healthLost > 1000 then -- 1K
139 frame.text:SetFormattedText("-%dK", healthLost / 1000);
141 frame.text:SetFormattedText("-%d", healthLost)
149 local function updateIncomingRes(frame, unit)
150 if UnitHasIncomingResurrection(unit) then
157 local function updateMaxHealth(frame, unit)
158 frame.health.max = UnitHealthMax(unit);
161 local function updatePower(frame, unit)
162 local current, max = UnitPower(unit), frame.mana.max;
163 -- sanity check, occasionally UnitPowerMax gives zero
164 if current > max then
165 frame.mana.max = UnitPowerMax(unit);
166 max = frame.mana.max;
167 if current > max then
169 frame.mana:SetWidth(width);
173 frame.mana:SetWidth(UnitPower(unit)/max*width);
175 frame.mana:SetWidth(width);
179 local function updateMaxPower(frame, unit)
180 frame.mana.max = UnitPowerMax(unit);
183 local function updatePowerColor(frame, unit)
184 frame.mana:SetVertexColor(unpack(powerColors[UnitPowerType(unit)]));
187 local function updateName(frame, unit)
188 local name = UnitName(unit);
189 if not name then return end
190 frame.name:SetText(ssub(name, 1, 6));
192 local _, class = UnitClass(unit);
193 local color = RAID_CLASS_COLORS[class];
194 if color then frame.name:SetVertexColor(color.r, color.g, color.b) end
197 local function updateHealPred(frame, unit)
198 local incoming = UnitGetIncomingHeals(unit) or 0;
200 local max = frame.health.max;
201 local space = width - frame.health:GetWidth() + 1;
202 local pred = (incoming / max) * width;
203 frame.healpred:SetWidth(min(space, pred));
204 frame.healpred:Show();
206 frame.healpred:Hide();
210 local function updateShield(frame, unit)
211 local shield = UnitGetTotalAbsorbs(unit) or 0;
213 local max = frame.health.max;
214 local space = width - frame.health:GetWidth();
215 shield = (shield / max) * width;
216 if space < shield then
217 frame.shield:SetWidth(space);
218 frame.shieldhl:Show();
220 frame.shield:SetWidth(shield);
221 frame.shieldhl:Hide();
226 frame.shieldhl:Hide();
230 local function updateHealAbsorb(frame, unit)
231 local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
233 local max = frame.health.max;
234 local space = frame.health:GetWidth();
235 absorb = (absorb / max) * width;
236 frame.healabsorb:SetWidth(min(space, absorb));
237 frame.healabsorb:Show();
239 frame.healabsorb:Hide();
243 local function updateAuras(frame, unit)
244 local alert = checkIndicators(frame, unit);
246 if frame.overlay.color ~= overlayColorAlert then
247 frame.overlay:SetVertexColor(unpack(overlayColorAlert));
248 frame.overlay:Show();
249 frame.overlay.color = overlayColorAlert;
251 elseif UnitDebuff(unit, 1, "RAID") ~= nil then
252 -- something dispellable
253 if frame.overlay.color ~= overlayColorDispel then
254 frame.overlay:SetVertexColor(unpack(overlayColorDispel));
255 frame.overlay:Show();
256 frame.overlay.color = overlayColorDispel;
258 -- don't overlay charmed when in vehicle
259 elseif UnitIsCharmed(unit) and unit == frame.unit then
260 if frame.overlay.color ~= overlayColorCharm then
261 frame.overlay:SetVertexColor(unpack(overlayColorCharm));
262 frame.overlay:Show();
263 frame.overlay.color = overlayColorCharm;
266 if frame.overlay.color ~= nil then
267 frame.overlay:Hide();
268 frame.overlay.color = nil;
273 local function updateAggro(frame, unit)
274 local status = UnitThreatSituation(unit);
275 if status and status > 0 then
276 frame.base:SetVertexColor(GetThreatStatusColor(status));
278 frame.base:SetVertexColor(unpack(baseColor));
282 local function updateVehicle(frame)
283 local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle);
284 if shouldTargetVehicle then
285 if not frame.inVehicle then
286 frame.inVehicle = true;
287 frame.displayed = frame.vehicle;
288 registerUnitEvents(frame);
290 elseif frame.inVehicle then
291 frame.inVehicle = false;
292 frame.displayed = frame.unit;
293 registerUnitEvents(frame);
297 local function updateRole(frame, unit)
298 local role = UnitGroupRolesAssigned(unit);
299 if role == "HEALER" then
300 frame.role:SetTexCoord(0.75, 1, 0, 1);
302 if not frame.role.healer then
303 registerPower(frame);
304 frame.role.healer = true;
306 elseif role == "TANK" then
307 frame.role:SetTexCoord(0.5, 0.75, 0, 1);
309 if frame.role.healer then
310 unregisterPower(frame);
311 frame.role.healer = false;
315 if frame.role.healer then
316 unregisterPower(frame);
317 frame.role.healer = false;
322 local function updateReadyCheck(frame, unit)
323 local status = GetReadyCheckStatus(unit);
324 if status == "ready" then
325 frame.ready:SetTexture(READY_CHECK_READY_TEXTURE);
327 elseif status == "notready" then
328 frame.ready:SetTexture(READY_CHECK_NOT_READY_TEXTURE);
330 elseif status == "waiting" then
331 frame.ready:SetTexture(READY_CHECK_WAITING_TEXTURE);
338 local function updateRaidMarker(frame, unit)
339 --print(unit, "marker");
343 ["UNIT_HEALTH"] = function(frame)
344 updateHealth(frame, frame.displayed);
345 updateText(frame, frame.displayed);
346 updateShield(frame, frame.displayed);
347 updateHealAbsorb(frame, frame.displayed);
348 -- no heal prediction update, that doesn't overflow too much
349 -- raid marker update here, because marker is removed when unit dies
350 -- without a RAID_TARGET_UPDATE event
351 updateRaidMarker(frame, frame.unit);
353 ["UNIT_POWER"] = function(frame)
354 updatePower(frame, frame.displayed);
356 ["UNIT_AURA"] = function(frame)
357 updateAuras(frame, frame.displayed);
359 ["UNIT_HEAL_PREDICTION"] = function(frame)
360 updateHealPred(frame, frame.displayed);
362 ["UNIT_ABSORB_AMOUNT_CHANGED"] = function(frame)
363 updateShield(frame, frame.displayed);
365 ["UNIT_HEAL_ABSORB_AMOUNT_CHANGED"] = function(frame)
366 updateHealAbsorb(frame, frame.displayed);
368 ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
369 updateAggro(frame, frame.displayed);
371 ["UNIT_MAXHEALTH"] = function(frame)
372 updateMaxHealth(frame, frame.displayed);
373 updateHealth(frame, frame.displayed);
374 updateText(frame, frame.displayed);
375 updateShield(frame, frame.displayed);
376 updateHealAbsorb(frame, frame.displayed);
378 ["UNIT_MAXPOWER"] = function(frame)
379 updateMaxPower(frame, frame.displayed);
380 updatePower(frame, frame.displayed);
382 ["UNIT_DISPLAYPOWER"] = function(frame)
383 updatePowerColor(frame, frame.displayed);
384 updateMaxPower(frame, frame.displayed);
385 updatePower(frame, frame.displayed);
387 ["UNIT_NAME_UPDATE"] = function(frame)
388 updateName(frame, frame.displayed);
390 ["UNIT_CONNECTION"] = function(frame)
391 updateText(frame, frame.displayed);
393 ["INCOMING_RESURRECT_CHANGED"] = function(frame)
394 updateIncomingRes(frame, frame.unit);
396 ["PARTY_MEMBER_ENABLE"] = function(frame)
397 -- new power info possibly (FrameXML/CompactUnitFrame.lua)
398 updateMaxPower(frame, frame.displayed);
399 updatePowerColor(frame, frame.displayed);
401 ["PLAYER_ROLES_ASSIGNED"] = function(frame)
402 updateRole(frame, frame.unit);
404 ["READY_CHECK"] = function(frame)
405 updateReadyCheck(frame, frame.unit);
407 ["RAID_TARGET_UPDATE"] = function(frame)
408 updateRaidMarker(frame, frame.unit);
410 ["UPDATE_ALL_BARS"] = function(frame)
411 updateRole(frame, frame.unit);
412 updateVehicle(frame);
413 updateMaxHealth(frame, frame.displayed);
414 updateMaxPower(frame, frame.displayed);
415 updateHealth(frame, frame.displayed);
416 updateText(frame, frame.displayed);
417 updatePower(frame, frame.displayed);
418 updateAuras(frame, frame.displayed);
419 updateShield(frame, frame.displayed);
420 updateHealPred(frame, frame.displayed);
421 updateHealAbsorb(frame, frame.displayed);
422 updatePowerColor(frame, frame.displayed);
423 updateAggro(frame, frame.displayed);
424 updateName(frame, frame.displayed);
425 updateIncomingRes(frame, frame.unit);
426 updateReadyCheck(frame, frame.unit);
427 updateRaidMarker(frame, frame.unit);
430 eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"];
431 eventFuncs["PARTY_MEMBER_DISABLE"] = eventFuncs["PARTY_MEMBER_ENABLE"];
432 eventFuncs["PLAYER_FLAGS_CHANGED"] = eventFuncs["UNIT_CONNECTION"];
433 eventFuncs["UNIT_POWER_BAR_SHOW"] = eventFuncs["UNIT_DISPLAYPOWER"];
434 eventFuncs["UNIT_POWER_BAR_HIDE"] = eventFuncs["UNIT_DISPLAYPOWER"];
435 eventFuncs["READY_CHECK_CONFIRM"] = eventFuncs["READY_CHECK"];
436 eventFuncs["READY_CHECK_FINISHED"] = eventFuncs["READY_CHECK"];
437 eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
438 eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
439 eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"];
440 eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"];
441 eventFuncs["PLAYER_ENTERING_WORLD"] = eventFuncs["UPDATE_ALL_BARS"];
442 eventFuncs["PLAYER_FOCUS_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
444 function M.UnitEvent(self, event)
445 eventFuncs[event](self);