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 UnitGetTotalHealAbsorbs = UnitGetTotalHealAbsorbs;
15 local UnitHasVehicleUI, UnitTargetsVehicleInRaidUI = UnitHasVehicleUI, UnitTargetsVehicleInRaidUI;
16 local GetReadyCheckTimeLeft, GetReadyCheckStatus = GetReadyCheckTimeLeft, GetReadyCheckStatus;
17 local UnitGroupRolesAssigned = UnitGroupRolesAssigned;
18 local GetRaidTargetIndex, SetRaidTargetIconTexture = GetRaidTargetIndex, SetRaidTargetIconTexture;
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("GROUP_ROSTER_UPDATE");
42 frame:RegisterEvent("RAID_TARGET_UPDATE");
43 if frame.unit == "focus" then frame:RegisterEvent("PLAYER_FOCUS_CHANGED") end
46 local function unregisterPower(frame)
47 frame.health:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT");
48 frame.overlay:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT");
50 frame:UnregisterEvent("PARTY_MEMBER_ENABLE");
51 frame:UnregisterEvent("PARTY_MEMBER_DISABLE");
52 frame:UnregisterEvent("UNIT_POWER");
53 frame:UnregisterEvent("UNIT_MAXPOWER");
54 frame:UnregisterEvent("UNIT_DISPLAYPOWER");
55 frame:UnregisterEvent("UNIT_POWER_BAR_SHOW");
56 frame:UnregisterEvent("UNIT_POWER_BAR_HIDE");
59 local function registerPower(frame)
60 frame.health:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT", 0, 2);
61 frame.overlay:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT", 0, 2);
63 frame:RegisterEvent("PARTY_MEMBER_ENABLE");
64 frame:RegisterEvent("PARTY_MEMBER_DISABLE");
65 frame:RegisterUnitEvent("UNIT_POWER", frame.unit);
66 frame:RegisterUnitEvent("UNIT_MAXPOWER", frame.unit);
67 frame:RegisterUnitEvent("UNIT_DISPLAYPOWER", frame.unit);
68 frame:RegisterUnitEvent("UNIT_POWER_BAR_SHOW", frame.unit);
69 frame:RegisterUnitEvent("UNIT_POWER_BAR_HIDE", frame.unit);
72 function M.RegisterUnitEvents(frame)
73 -- events are taken from FrameXML/CompactUnitFrame.lua
74 local displayed = frame.unit ~= frame.displayed and frame.displayed or nil;
75 frame:RegisterUnitEvent("UNIT_HEALTH", frame.unit, displayed);
76 frame:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", frame.unit, displayed);
77 frame:RegisterUnitEvent("UNIT_MAXHEALTH", frame.unit, displayed);
78 frame:RegisterUnitEvent("UNIT_NAME_UPDATE", frame.unit, displayed);
79 frame:RegisterUnitEvent("UNIT_AURA", frame.unit, displayed);
80 frame:RegisterUnitEvent("UNIT_HEAL_PREDICTION", frame.unit, displayed);
81 frame:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
82 frame:RegisterUnitEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
83 frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", frame.unit, displayed);
84 frame:RegisterUnitEvent("UNIT_CONNECTION", frame.unit, displayed);
85 frame:RegisterUnitEvent("PLAYER_FLAGS_CHANGED", frame.unit, displayed);
86 frame:RegisterUnitEvent("READY_CHECK_CONFIRM", frame.unit, displayed);
87 frame:RegisterUnitEvent("UNIT_ENTERED_VEHICLE", frame.unit, displayed);
88 frame:RegisterUnitEvent("UNIT_EXITED_VEHICLE", frame.unit, displayed);
89 frame:RegisterUnitEvent("UNIT_PET", 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 updateMaxPower(frame, unit)
144 frame.mana.max = UnitPowerMax(unit);
147 local function updatePower(frame, unit)
148 local current, max = UnitPower(unit), frame.mana.max;
151 elseif current > max or max <= 0 then
152 frame.mana:SetWidth(width);
153 updateMaxPower(frame, unit);
156 frame.mana:SetWidth(current/max*width);
161 local function updatePowerColor(frame, unit)
162 frame.mana:SetVertexColor(unpack(powerColors[UnitPowerType(unit)]));
165 -- TODO maybe add a prefix when in vehicle
166 local function updateName(frame, unit)
167 local name = UnitName(unit);
168 if not name then return end
169 frame.name:SetText(ssub(name, 1, 6));
171 local _, class = UnitClass(unit);
172 local color = RAID_CLASS_COLORS[class];
173 if color then frame.name:SetVertexColor(color.r, color.g, color.b) end
176 local function updateHealPred(frame, unit)
177 local incoming = UnitGetIncomingHeals(unit) or 0;
179 -- always at least 1 pixel space for heal prediction
180 local space = width - frame.health:GetWidth() + 1;
181 incoming = (incoming / frame.health.max) * width;
182 frame.healpred:SetWidth(min(space, incoming));
183 frame.healpred:Show();
185 frame.healpred:Hide();
189 local function updateShield(frame, unit)
190 local shield = UnitGetTotalAbsorbs(unit) or 0;
192 local space = width - frame.health:GetWidth();
193 shield = (shield / frame.health.max) * width;
196 frame.shieldhl:Show();
197 elseif space < shield then
198 frame.shield:SetWidth(space);
200 frame.shieldhl:Show();
202 frame.shield:SetWidth(shield);
204 frame.shieldhl:Hide();
208 frame.shieldhl:Hide();
212 local function updateHealAbsorb(frame, unit)
213 local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
215 local space = frame.health:GetWidth();
216 absorb = (absorb / frame.health.max) * width;
217 frame.healabsorb:SetWidth(min(space, absorb));
218 frame.healabsorb:Show();
220 frame.healabsorb:Hide();
224 local function updateAuras(frame, unit)
225 local alert = checkIndicators(frame, unit);
227 if frame.overlay.color ~= overlayColorAlert then
228 frame.overlay:SetVertexColor(unpack(overlayColorAlert));
229 frame.overlay.color = overlayColorAlert;
230 frame.overlay:Show();
232 elseif UnitDebuff(unit, 1, "RAID") ~= nil then
233 -- something dispellable
234 if frame.overlay.color ~= overlayColorDispel then
235 frame.overlay:SetVertexColor(unpack(overlayColorDispel));
236 frame.overlay.color = overlayColorDispel;
237 frame.overlay:Show();
239 -- don't overlay charmed when in vehicle
240 elseif UnitIsCharmed(unit) and unit == frame.unit then
241 if frame.overlay.color ~= overlayColorCharm then
242 frame.overlay:SetVertexColor(unpack(overlayColorCharm));
243 frame.overlay.color = overlayColorCharm;
244 frame.overlay:Show();
247 if frame.overlay.color ~= nil then
248 frame.overlay.color = nil;
249 frame.overlay:Hide();
254 local function updateAggro(frame, unit)
255 local status = UnitThreatSituation(unit);
256 if status and status > 0 then
257 frame.base:SetVertexColor(GetThreatStatusColor(status));
259 frame.base:SetVertexColor(unpack(baseColor));
263 local function updateVehicle(frame)
264 local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and
265 UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle);
266 if shouldTargetVehicle then
267 if not frame.inVehicle then
268 frame.inVehicle = true;
269 frame.displayed = frame.vehicle;
270 registerUnitEvents(frame);
272 elseif frame.inVehicle then
273 frame.inVehicle = false;
274 frame.displayed = frame.unit;
275 registerUnitEvents(frame);
279 local function updateRole(frame, unit)
280 local role = UnitGroupRolesAssigned(unit);
281 if role == "HEALER" then
282 frame.role:SetTexCoord(0.75, 1, 0, 1);
284 registerPower(frame);
285 frame.role.healer = true;
286 elseif role == "TANK" then
287 frame.role:SetTexCoord(0.5, 0.75, 0, 1);
289 if frame.role.healer then
290 frame.role.healer = false;
291 unregisterPower(frame);
295 if frame.role.healer then
296 frame.role.healer = false;
297 unregisterPower(frame);
302 local function updateReadyCheck(frame, unit)
303 local status = GetReadyCheckStatus(unit);
304 if status == "ready" then
305 frame.ready:SetTexture(READY_CHECK_READY_TEXTURE);
307 elseif status == "notready" then
308 frame.ready:SetTexture(READY_CHECK_NOT_READY_TEXTURE);
310 elseif status == "waiting" then
311 frame.ready:SetTexture(READY_CHECK_WAITING_TEXTURE);
318 local function updateRaidMarker(frame, unit)
319 local index = GetRaidTargetIndex(unit);
321 SetRaidTargetIconTexture(frame.targeticon, index);
322 frame.targeticon:Show();
324 frame.targeticon:Hide();
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, if needed
336 -- because marker is removed when unit dies
337 -- without a RAID_TARGET_UPDATE event
338 --updateRaidMarker(frame, frame.unit);
340 ["UNIT_POWER"] = function(frame)
341 updatePower(frame, frame.displayed);
343 ["UNIT_AURA"] = function(frame)
344 updateAuras(frame, frame.displayed);
346 ["UNIT_HEAL_PREDICTION"] = function(frame)
347 updateHealPred(frame, frame.displayed);
349 ["UNIT_ABSORB_AMOUNT_CHANGED"] = function(frame)
350 updateShield(frame, frame.displayed);
352 ["UNIT_HEAL_ABSORB_AMOUNT_CHANGED"] = function(frame)
353 updateHealAbsorb(frame, frame.displayed);
355 ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
356 updateAggro(frame, frame.displayed);
358 ["UNIT_MAXHEALTH"] = function(frame)
359 updateMaxHealth(frame, frame.displayed);
360 updateHealth(frame, frame.displayed);
361 updateText(frame, frame.displayed);
362 updateShield(frame, frame.displayed);
363 updateHealAbsorb(frame, frame.displayed);
365 ["UNIT_MAXPOWER"] = function(frame)
366 updateMaxPower(frame, frame.displayed);
367 updatePower(frame, frame.displayed);
369 ["UNIT_DISPLAYPOWER"] = function(frame)
370 updatePowerColor(frame, frame.displayed);
371 updateMaxPower(frame, frame.displayed);
372 updatePower(frame, frame.displayed);
374 ["UNIT_NAME_UPDATE"] = function(frame)
375 updateName(frame, frame.unit);
377 ["UNIT_CONNECTION"] = function(frame)
378 updateText(frame, frame.displayed);
380 ["PARTY_MEMBER_ENABLE"] = function(frame)
381 -- new power info possibly (FrameXML/CompactUnitFrame.lua)
382 updateMaxPower(frame, frame.displayed);
383 updatePowerColor(frame, frame.displayed);
385 ["PLAYER_ROLES_ASSIGNED"] = function(frame)
386 updateRole(frame, frame.unit);
388 ["READY_CHECK"] = function(frame)
389 updateReadyCheck(frame, frame.unit);
391 ["RAID_TARGET_UPDATE"] = function(frame)
392 updateRaidMarker(frame, frame.displayed);
394 ["UPDATE_ALL_BARS"] = function(frame)
395 updateRole(frame, frame.unit);
396 updateVehicle(frame);
397 updateMaxHealth(frame, frame.displayed);
398 updateHealth(frame, frame.displayed);
399 updateText(frame, frame.displayed);
400 if frame.role.healer then
401 updateMaxPower(frame, frame.displayed);
402 updatePower(frame, frame.displayed);
403 updatePowerColor(frame, frame.displayed);
405 updateAuras(frame, frame.displayed);
406 updateShield(frame, frame.displayed);
407 updateHealPred(frame, frame.displayed);
408 updateHealAbsorb(frame, frame.displayed);
409 updateAggro(frame, frame.displayed);
410 updateName(frame, frame.unit);
411 updateReadyCheck(frame, frame.unit);
412 updateRaidMarker(frame, frame.displayed);
415 eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"];
416 eventFuncs["PARTY_MEMBER_DISABLE"] = eventFuncs["PARTY_MEMBER_ENABLE"];
417 eventFuncs["PLAYER_FLAGS_CHANGED"] = eventFuncs["UNIT_CONNECTION"];
418 eventFuncs["UNIT_POWER_BAR_SHOW"] = eventFuncs["UNIT_DISPLAYPOWER"];
419 eventFuncs["UNIT_POWER_BAR_HIDE"] = eventFuncs["UNIT_DISPLAYPOWER"];
420 eventFuncs["READY_CHECK_CONFIRM"] = eventFuncs["READY_CHECK"];
421 eventFuncs["READY_CHECK_FINISHED"] = eventFuncs["READY_CHECK"];
422 eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
423 eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
424 eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"];
425 eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"];
426 eventFuncs["PLAYER_ENTERING_WORLD"] = eventFuncs["UPDATE_ALL_BARS"];
427 eventFuncs["PLAYER_FOCUS_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
429 function M.UnitEvent(self, event)
430 eventFuncs[event](self);