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 -- events are taken from FrameXML/CompactUnitFrame.lua
38 -- TODO raid marker support
39 local displayed = frame.unit ~= frame.displayed and frame.displayed or nil;
40 frame:RegisterUnitEvent("UNIT_HEALTH", frame.unit, displayed);
41 frame:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", frame.unit, displayed);
42 frame:RegisterUnitEvent("UNIT_MAXHEALTH", frame.unit, displayed);
43 frame:RegisterUnitEvent("UNIT_POWER", frame.unit, displayed);
44 frame:RegisterUnitEvent("UNIT_MAXPOWER", frame.unit, displayed);
45 frame:RegisterUnitEvent("UNIT_DISPLAYPOWER", frame.unit, displayed);
46 frame:RegisterUnitEvent("UNIT_NAME_UPDATE", frame.unit, displayed);
47 frame:RegisterUnitEvent("UNIT_AURA", frame.unit, displayed);
48 frame:RegisterUnitEvent("UNIT_HEAL_PREDICTION", frame.unit, displayed);
49 frame:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
50 frame:RegisterUnitEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
51 frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", frame.unit, displayed);
52 frame:RegisterUnitEvent("UNIT_CONNECTION", frame.unit, displayed);
53 frame:RegisterUnitEvent("INCOMING_RESURRECT_CHANGED", frame.unit, displayed);
54 frame:RegisterUnitEvent("PLAYER_FLAGS_CHANGED", frame.unit, displayed);
56 local registerEvents = M.RegisterEvents;
58 local function updateHealth(frame, unit)
59 local current, max = UnitHealth(unit), frame.health.max;
61 -- sanity check, occasionally UnitHealthMax gives zero
63 -- somehow current health has gone over the maximum (missed maxhealth event)
64 frame.health.max = UnitHealthMax(unit);
65 max = frame.health.max;
67 -- error state, still over maximum
68 frame.health:SetWidth(width);
72 frame.health:SetWidth(current/frame.health.max*width);
74 frame.health:SetWidth(width);
78 if UnitIsDeadOrGhost(unit) then
83 local function updateText(frame, unit)
84 local current, max = UnitHealth(unit), frame.health.max;
85 local healthLost = max - current;
86 if UnitIsDeadOrGhost(unit) then
87 frame.text:SetText("Dead");
89 elseif not UnitIsConnected(unit) then
90 frame.text:SetText("DC");
92 elseif UnitIsAFK(unit) then
93 frame.text:SetText("afk");
95 elseif UnitIsDND(unit) and healthLost == 0 then
96 frame.text:SetText("dnd");
98 elseif healthLost > 0 then
99 if healthLost > 1200000000 then -- 1.2B
100 frame.text:SetFormattedText("-%.1fB", healthLost / 1000000000);
101 elseif healthLost > 1200000 then -- 1.2M
102 frame.text:SetFormattedText("-%.1fM", healthLost / 1000000);
103 elseif healthLost > 1000 then -- 1K
104 frame.text:SetFormattedText("-%dK", healthLost / 1000);
106 frame.text:SetFormattedText("-%d", healthLost)
114 local function updateIncomingRes(frame, unit)
115 if UnitHasIncomingResurrection(unit) then
122 local function updateMaxHealth(frame, unit)
123 frame.health.max = UnitHealthMax(unit);
126 local function updatePower(frame, unit)
127 local current, max = UnitPower(unit), frame.mana.max;
128 -- sanity check, occasionally UnitPowerMax gives zero
129 if current > max then
130 frame.mana.max = UnitPowerMax(unit);
131 max = frame.mana.max;
132 if current > max then
134 frame.mana:SetWidth(width);
138 frame.mana:SetWidth(UnitPower(unit)/max*width);
140 frame.mana:SetWidth(width);
144 local function updateMaxPower(frame, unit)
145 frame.mana.max = UnitPowerMax(unit);
148 local function updatePowerColor(frame, unit)
149 frame.mana:SetVertexColor(unpack(powerColors[UnitPowerType(unit)]));
152 local function updateName(frame, unit)
153 local name = UnitName(unit);
154 if not name then return end
155 frame.name:SetText(ssub(name, 1, 6));
157 local _, class = UnitClass(unit);
158 local color = RAID_CLASS_COLORS[class];
159 if color then frame.name:SetVertexColor(color.r, color.g, color.b) end
162 local function updateHealPred(frame, unit)
163 local incoming = UnitGetIncomingHeals(unit) or 0;
165 local max = frame.health.max;
166 local space = width - frame.health:GetWidth() + 1;
167 local pred = (incoming / max) * width;
168 frame.healpred:SetWidth(min(space, pred));
169 frame.healpred:Show();
171 frame.healpred:Hide();
175 local function updateShield(frame, unit)
176 local shield = UnitGetTotalAbsorbs(unit) or 0;
178 local max = frame.health.max;
179 local space = width - frame.health:GetWidth();
180 shield = (shield / max) * width;
181 if space < shield then
182 frame.shield:SetWidth(space);
183 frame.shieldhl:Show();
185 frame.shield:SetWidth(shield);
186 frame.shieldhl:Hide();
191 frame.shieldhl:Hide();
195 local function updateHealAbsorb(frame, unit)
196 local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
198 local max = frame.health.max;
199 local space = frame.health:GetWidth();
200 absorb = (absorb / max) * width;
201 frame.healabsorb:SetWidth(min(space, absorb));
202 frame.healabsorb:Show();
204 frame.healabsorb:Hide();
208 local function updateAuras(frame, unit)
209 checkIndicators(frame, unit);
210 if UnitDebuff(unit, 1, "RAID") ~= nil then
211 -- something dispellable
212 if frame.overlay.color ~= overlayColorDispel then
213 frame.overlay:SetVertexColor(unpack(overlayColorDispel));
214 frame.overlay:Show();
215 frame.overlay.color = overlayColorDispel;
217 -- don't overlay charmed when in vehicle
218 elseif UnitIsCharmed(unit) and unit == frame.unit then
219 if frame.overlay.color ~= overlayColorCharm then
220 frame.overlay:SetVertexColor(unpack(overlayColorCharm));
221 frame.overlay:Show();
222 frame.overlay.color = overlayColorCharm;
225 if frame.overlay.color ~= nil then
226 frame.overlay:Hide();
227 frame.overlay.color = nil;
232 local function updateAggro(frame, unit)
233 local status = UnitThreatSituation(unit);
234 if status and status > 0 then
235 frame.base:SetVertexColor(GetThreatStatusColor(status));
237 frame.base:SetVertexColor(unpack(baseColor));
241 local function updateVehicle(frame)
242 local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle);
243 if shouldTargetVehicle then
244 if not frame.inVehicle then
245 frame.inVehicle = true;
246 frame.displayed = frame.vehicle;
247 registerEvents(frame);
249 elseif frame.inVehicle then
250 frame.inVehicle = false;
251 frame.displayed = frame.unit;
252 registerEvents(frame);
256 local function updateRole(frame, unit)
257 local role = UnitGroupRolesAssigned(unit);
258 if role == "HEALER" then
259 frame.role:SetTexCoord(0.75, 1, 0, 1);
261 elseif role == "TANK" then
262 frame.role:SetTexCoord(0.5, 0.75, 0, 1);
269 local function updateReadyCheck(frame, unit)
270 local status = GetReadyCheckStatus(unit);
271 if status == "ready" then
272 frame.ready:SetTexture(READY_CHECK_READY_TEXTURE);
274 elseif status == "notready" then
275 frame.ready:SetTexture(READY_CHECK_NOT_READY_TEXTURE);
277 elseif status == "waiting" then
278 frame.ready:SetTexture(READY_CHECK_WAITING_TEXTURE);
286 ["UNIT_HEALTH"] = function(frame)
287 updateHealth(frame, frame.displayed);
288 updateText(frame, frame.displayed);
289 updateShield(frame, frame.displayed);
290 updateHealAbsorb(frame, frame.displayed);
291 -- no heal prediction update, that doesn't overflow too much
293 ["UNIT_POWER"] = function(frame)
294 updatePower(frame, frame.displayed);
296 ["UNIT_AURA"] = function(frame)
297 updateAuras(frame, frame.displayed);
299 ["UNIT_HEAL_PREDICTION"] = function(frame)
300 updateHealPred(frame, frame.displayed);
302 ["UNIT_ABSORB_AMOUNT_CHANGED"] = function(frame)
303 updateShield(frame, frame.displayed);
305 ["UNIT_HEAL_ABSORB_AMOUNT_CHANGED"] = function(frame)
306 updateHealAbsorb(frame, frame.displayed);
308 ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
309 updateAggro(frame, frame.displayed);
311 ["UNIT_MAXHEALTH"] = function(frame)
312 updateMaxHealth(frame, frame.displayed);
313 updateHealth(frame, frame.displayed);
314 updateText(frame, frame.displayed);
315 updateShield(frame, frame.displayed);
316 updateHealAbsorb(frame, frame.displayed);
318 ["UNIT_MAXPOWER"] = function(frame)
319 updateMaxPower(frame, frame.displayed);
320 updatePower(frame, frame.displayed);
322 ["UNIT_DISPLAYPOWER"] = function(frame)
323 updatePowerColor(frame, frame.displayed);
325 ["UNIT_NAME_UPDATE"] = function(frame)
326 updateName(frame, frame.displayed);
328 ["UNIT_CONNECTION"] = function(frame)
329 updateText(frame, frame.displayed);
331 ["INCOMING_RESURRECT_CHANGED"] = function(frame)
332 updateIncomingRes(frame, frame.unit);
334 ["PARTY_MEMBER_ENABLE"] = function(frame)
335 -- new power info possibly (FrameXML/CompactUnitFrame.lua)
336 updateMaxPower(frame, frame.displayed);
337 updatePowerColor(frame, frame.displayed);
339 ["PLAYER_ROLES_ASSIGNED"] = function(frame)
340 updateRole(frame, frame.unit);
342 ["READY_CHECK"] = function(frame)
343 updateReadyCheck(frame, frame.unit);
345 ["UPDATE_ALL_BARS"] = function(frame)
346 updateVehicle(frame);
347 updateMaxHealth(frame, frame.displayed);
348 updateMaxPower(frame, frame.displayed);
349 updateHealth(frame, frame.displayed);
350 updateText(frame, frame.displayed);
351 updatePower(frame, frame.displayed);
352 updateAuras(frame, frame.displayed);
353 updateShield(frame, frame.displayed);
354 updateHealPred(frame, frame.displayed);
355 updateHealAbsorb(frame, frame.displayed);
356 updatePowerColor(frame, frame.displayed);
357 updateIncomingRes(frame, frame.unit);
358 updateReadyCheck(frame, frame.unit);
359 updateAggro(frame, frame.displayed);
360 updateName(frame, frame.displayed);
361 updateRole(frame, frame.unit);
364 eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"];
365 eventFuncs["PARTY_MEMBER_DISABLE"] = eventFuncs["PARTY_MEMBER_ENABLE"];
366 eventFuncs["PLAYER_FLAGS_CHANGED"] = eventFuncs["UNIT_CONNECTION"];
367 eventFuncs["READY_CHECK_CONFIRM"] = eventFuncs["READY_CHECK"];
368 eventFuncs["READY_CHECK_FINISHED"] = eventFuncs["READY_CHECK"];
369 eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
370 eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
371 eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"];
372 eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"];
373 eventFuncs["PLAYER_ENTERING_WORLD"] = eventFuncs["UPDATE_ALL_BARS"];
374 eventFuncs["PLAYER_FOCUS_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
376 function M.UnitEvent(self, event)
377 eventFuncs[event](self);