2 -- TODO -- recheck these, pvp functions not added yet
5 local ssub = string.sub;
7 local ceil = math.ceil;
8 local UnitName, UnitClass, UnitExists = UnitName, UnitClass, UnitExists;
9 local UnitDebuff, UnitIsCharmed, UnitIsFriend = UnitDebuff, UnitIsCharmed, UnitIsFriend;
10 local UnitPower, UnitPowerMax, UnitPowerType = UnitPower, UnitPowerMax, UnitPowerType;
11 local UnitHealth, UnitHealthMax = UnitHealth, UnitHealthMax;
12 local UnitGetIncomingHeals, UnitGetTotalAbsorbs = UnitGetIncomingHeals, UnitGetTotalAbsorbs;
13 local UnitThreatSituation, GetThreatStatusColor = UnitThreatSituation, GetThreatStatusColor;
14 local UnitIsDeadOrGhost, UnitIsConnected = UnitIsDeadOrGhost, UnitIsConnected;
15 local UnitGetTotalHealAbsorbs = UnitGetTotalHealAbsorbs;
16 local UnitHasVehicleUI, UnitTargetsVehicleInRaidUI = UnitHasVehicleUI, UnitTargetsVehicleInRaidUI;
17 local UnitGroupRolesAssigned = UnitGroupRolesAssigned;
18 local UnitLevel, UnitClassification = UnitLevel, UnitClassification;
19 local RAID_CLASS_COLORS = RAID_CLASS_COLORS;
21 local updateAuraFrames = OmaUFAuras.UpdateAuras;
23 local Settings = OmaUFSettings;
24 local baseColor = Settings.BaseColor;
25 local healthColor = Settings.HealthColor;
26 local overlayColorDispel = Settings.OverlayColorDispel;
27 local overlayColorCharm = Settings.OverlayColorCharm;
28 local overlayColorAlert = Settings.OverlayColorAlert;
29 local powerColors = Settings.PowerColors;
30 local width = Settings.Width;
34 function M.RegisterEvents(frame)
35 -- events are taken from FrameXML/CompactUnitFrame.lua
36 -- TODO raid marker support,
37 -- player flags support (/afk, /dnd)
38 local displayed = frame.unit ~= frame.displayed and frame.displayed or nil;
39 frame:RegisterUnitEvent("UNIT_HEALTH", frame.unit, displayed);
40 frame:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", frame.unit, displayed);
41 frame:RegisterUnitEvent("UNIT_MAXHEALTH", frame.unit, displayed);
42 frame:RegisterUnitEvent("UNIT_POWER", frame.unit, displayed);
43 frame:RegisterUnitEvent("UNIT_MAXPOWER", frame.unit, displayed);
44 frame:RegisterUnitEvent("UNIT_DISPLAYPOWER", frame.unit, displayed);
45 frame:RegisterUnitEvent("UNIT_NAME_UPDATE", frame.unit, displayed);
46 frame:RegisterUnitEvent("UNIT_AURA", frame.unit, displayed);
47 frame:RegisterUnitEvent("UNIT_HEAL_PREDICTION", frame.unit, displayed);
48 frame:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
49 frame:RegisterUnitEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
50 frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", frame.unit, displayed);
51 frame:RegisterUnitEvent("UNIT_CONNECTION", frame.unit, displayed);
52 frame:RegisterUnitEvent("UNIT_FACTION", frame.unit, displayed);
54 local registerEvents = M.RegisterEvents;
56 local function updateHealth(frame, unit)
57 local current, max = UnitHealth(unit), frame.health.max;
59 -- sanity check, occasionally UnitHealthMax gives zero
61 -- somehow current health has gone over the maximum (missed maxhealth event)
62 frame.health.max = UnitHealthMax(unit);
63 max = frame.health.max;
65 -- error state, still over maximum
66 frame.health:SetWidth(width);
70 frame.health:SetWidth(current/frame.health.max*width);
72 frame.health:SetWidth(width);
76 if UnitIsDeadOrGhost(unit) then
81 local function updateHealthText(frame, unit)
82 local current, max = UnitHealth(unit), frame.health.max;
83 if UnitIsDeadOrGhost(unit) then
84 frame.healthText:SetText("Dead");
85 frame.healthText:Show();
86 elseif not UnitIsConnected(unit) then
87 frame.healthText:SetText("DC");
88 frame.healthText:Show();
89 elseif max > 0 and current < max then
90 frame.healthText:SetText(ceil(current/max*100));
91 frame.healthText:Show();
93 frame.healthText:Hide();
97 local function updateMaxHealth(frame, unit)
98 frame.health.max = UnitHealthMax(unit);
101 local function updatePower(frame, unit)
102 local current, max = UnitPower(unit), frame.mana.max;
103 -- sanity check, occasionally UnitPowerMax gives zero
107 elseif current > max then
109 frame.mana.max = UnitPowerMax(unit);
110 max = frame.mana.max;
111 if current > max then
113 frame.mana:SetWidth(width);
118 frame.mana:SetWidth(UnitPower(unit)/max*width);
121 frame.mana:SetWidth(width);
126 local function updatePowerText(frame, unit)
127 local current, max = UnitPower(unit), frame.mana.max;
128 if UnitIsDeadOrGhost(unit) or not UnitIsConnected(unit) then
129 frame.healthText:Hide();
130 elseif max > 0 and current > 0 and current < max then
131 frame.manaText:SetText(ceil(current/max*100));
132 frame.manaText:Show();
134 frame.manaText:Hide();
138 local function updateMaxPower(frame, unit)
139 frame.mana.max = UnitPowerMax(unit);
142 local function updatePowerColor(frame, unit)
143 frame.mana:SetVertexColor(unpack(powerColors[UnitPowerType(unit)]));
146 local function updateName(frame, unit)
147 local name = UnitName(unit);
148 if not name then return end
149 frame.name:SetText(ssub(name, 1, 10));
152 local function updateHealPred(frame, unit)
153 local incoming = UnitGetIncomingHeals(unit) or 0;
155 local max = frame.health.max;
156 local space = width - frame.health:GetWidth() + 1;
157 local pred = (incoming / max) * width;
158 frame.healpred:SetWidth(min(space, pred));
159 frame.healpred:Show();
161 frame.healpred:Hide();
165 local function updateShield(frame, unit)
166 local shield = UnitGetTotalAbsorbs(unit) or 0;
168 local max = frame.health.max;
169 local space = width - frame.health:GetWidth();
170 shield = (shield / max) * width;
173 frame.shieldhl:Show();
174 elseif space < shield then
175 frame.shield:SetWidth(space);
177 frame.shieldhl:Show();
179 frame.shield:SetWidth(shield);
181 frame.shieldhl:Hide();
185 frame.shieldhl:Hide();
189 local function updateHealAbsorb(frame, unit)
190 local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
192 local max = frame.health.max;
193 local space = frame.health:GetWidth();
194 absorb = (absorb / max) * width;
195 frame.healabsorb:SetWidth(min(space, absorb));
196 frame.healabsorb:Show();
198 frame.healabsorb:Hide();
202 local function updateAuras(frame, unit)
203 updateAuraFrames(frame, unit);
204 if UnitIsFriend("player", unit) and UnitDebuff(unit, 1, "RAID") ~= nil then
205 -- something dispellable
206 if frame.overlay.color ~= overlayColorDispel then
207 frame.overlay:SetVertexColor(unpack(overlayColorDispel));
208 frame.overlay:Show();
209 frame.overlay.color = overlayColorDispel;
211 -- don't overlay charmed when in vehicle
212 elseif UnitIsCharmed(unit) and not frame.inVehicle then
213 if frame.overlay.color ~= overlayColorCharm then
214 frame.overlay:SetVertexColor(unpack(overlayColorCharm));
215 frame.overlay:Show();
216 frame.overlay.color = overlayColorCharm;
219 if frame.overlay.color ~= nil then
220 frame.overlay:Hide();
221 frame.overlay.color = nil;
226 local function updateAggro(frame, unit)
227 local status = UnitThreatSituation(unit);
228 if status and status > 0 then
229 frame.base:SetVertexColor(GetThreatStatusColor(status));
231 frame.base:SetVertexColor(unpack(baseColor));
235 local function updateVehicle(frame)
236 local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle);
237 if shouldTargetVehicle then
238 if not frame.inVehicle then
239 frame.inVehicle = true;
240 frame.displayed = frame.vehicle;
241 registerEvents(frame);
243 elseif frame.inVehicle then
244 frame.inVehicle = false;
245 frame.displayed = frame.unit;
246 registerEvents(frame);
250 local function updateRole(frame, unit)
251 local role = UnitGroupRolesAssigned(unit);
252 if role == "HEALER" or role == "TANK" or role == "DAMAGER" then
253 frame.role:SetTexCoord(GetTexCoordsForRoleSmallCircle(role));
260 local function updateLevelText(frame, unit, levelup)
263 frame.level:SetText(levelup);
265 local level = UnitLevel(unit);
266 local class = UnitClassification(unit);
267 local leveltext, classtext;
269 if class == "worldboss" then
277 if class == "rareelite" then
278 classtext = " Rare Elite";
279 elseif class == "elite" then
280 classtext = " Elite";
281 elseif class == "rare" then
286 frame.level:SetFormattedText("%s%s", leveltext, classtext);
290 local function updateStatus(frame, unit)
291 -- coords from PlayerFrame
292 if frame.inCombat or UnitAffectingCombat(unit) then
293 frame.status:SetTexCoord(0.5, 1, 0, 0.484375);
295 elseif unit == "player" and IsResting() then
296 frame.status:SetTexCoord(0, 0.5, 0, 0.421875);
303 local function updatePVP(frame, unit)
304 if UnitIsPVPFreeForAll(unit) then
305 frame.pvp:SetTexture("Interface\\TARGETINGFRAME\\UI-PVP-FFA");
307 elseif UnitIsPVP(unit) then
308 local faction = UnitFactionGroup(unit);
309 if faction and faction ~= "Neutral" then
310 -- from PlayerFrame, mercenary checks
311 if UnitIsMercenary(unit) then
312 if faction == "Horde" then
313 faction = "Alliance";
314 elseif faction == "Alliance" then
318 frame.pvp:SetTexture("Interface\\TARGETINGFRAME\\UI-PVP-"..faction);
328 local function updateLeaderIcon(frame, unit)
329 if UnitIsGroupLeader(frame.unit) then
330 if HasLFGRestrictions() then
331 frame.leader:SetTexture("Interface\\LFGFrame\\UI-LFG-ICON-PORTRAITROLES");
332 frame.leader:SetTexCoord(0, 0.296875, 0.015625, 0.3125);
334 frame.leader:SetTexture("Interface\\GROUPFRAME\\UI-Group-LeaderIcon");
335 frame.leader:SetTexCoord(0, 1, 0, 1);
338 elseif UnitIsGroupAssistant(frame.unit) then
339 frame.leader:SetTexture("Interface\\GROUPFRAME\\UI-Group-AssistantIcon");
340 frame.leader:SetTexCoord(0, 1, 0, 1);
347 local function updateHealthColor(frame, unit)
348 if not UnitPlayerControlled(unit) and UnitIsTapDenied(unit) then
349 frame.health:SetVertexColor(0.5, 0.5, 0.5);
350 elseif UnitIsPlayer(unit) then
351 local _, class = UnitClass(unit);
352 local color = RAID_CLASS_COLORS[class];
354 frame.health:SetVertexColor(color.r, color.g, color.b)
356 frame.health:SetVertexColor(unpack(healthColor))
358 elseif UnitPlayerControlled(unit) then
359 frame.health:SetVertexColor(0, 1, 0);
361 frame.health:SetVertexColor(UnitSelectionColor(unit));
366 ["UNIT_HEALTH"] = function(frame)
367 updateHealth(frame, frame.displayed);
368 updateHealthText(frame, frame.displayed);
369 updateShield(frame, frame.displayed);
370 updateHealAbsorb(frame, frame.displayed);
371 -- no heal prediction update, that doesn't overflow too much
373 ["UNIT_POWER"] = function(frame)
374 updatePower(frame, frame.displayed);
375 updatePowerText(frame, frame.displayed);
377 ["UNIT_AURA"] = function(frame)
378 updateAuras(frame, frame.displayed);
380 ["UNIT_HEAL_PREDICTION"] = function(frame)
381 updateHealPred(frame, frame.displayed);
383 ["UNIT_ABSORB_AMOUNT_CHANGED"] = function(frame)
384 updateShield(frame, frame.displayed);
386 ["UNIT_HEAL_ABSORB_AMOUNT_CHANGED"] = function(frame)
387 updateHealAbsorb(frame, frame.displayed);
389 ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
390 updateAggro(frame, frame.displayed);
392 ["UNIT_MAXHEALTH"] = function(frame)
393 updateMaxHealth(frame, frame.displayed);
394 updateHealth(frame, frame.displayed);
395 updateHealthText(frame, frame.displayed);
396 updateShield(frame, frame.displayed);
397 updateHealAbsorb(frame, frame.displayed);
399 ["UNIT_MAXPOWER"] = function(frame)
400 updateMaxPower(frame, frame.displayed);
401 updatePower(frame, frame.displayed);
402 updatePowerText(frame, frame.displayed);
404 ["UNIT_DISPLAYPOWER"] = function(frame)
405 updatePowerColor(frame, frame.displayed);
407 ["UNIT_NAME_UPDATE"] = function(frame)
408 updateName(frame, frame.displayed);
409 updateHealthColor(frame, frame.unit);
411 ["UNIT_CONNECTION"] = function(frame)
412 updateHealthText(frame, frame.displayed);
413 updatePowerText(frame, frame.displayed);
415 ["PLAYER_ROLES_ASSIGNED"] = function(frame)
416 updateRole(frame, frame.unit);
418 ["UNIT_LEVEL"] = function(frame)
419 updateLevelText(frame, frame.unit);
421 ["PLAYER_LEVEL_UP"] = function(frame, arg1)
422 updateLevelText(frame, frame.unit, arg1);
424 ["PLAYER_UPDATE_RESTING"] = function(frame)
425 updateStatus(frame, frame.unit);
427 ["PLAYER_REGEN_DISABLED"] = function(frame)
428 frame.inCombat = true;
429 updateStatus(frame, frame.unit);
431 ["PLAYER_REGEN_ENABLED"] = function(frame)
432 frame.inCombat = false;
433 updateStatus(frame, frame.unit);
435 ["UNIT_FACTION"] = function(frame)
436 updatePVP(frame, frame.unit);
437 updateHealthColor(frame, frame.unit);
439 ["PARTY_LEADER_CHANGED"] = function(frame)
440 updateLeaderIcon(frame, frame.unit);
442 ["UPDATE_ALL_BARS"] = function(frame)
443 updateVehicle(frame);
444 updateMaxHealth(frame, frame.displayed);
445 updateMaxPower(frame, frame.displayed);
446 updateHealth(frame, frame.displayed);
447 updateHealthText(frame, frame.displayed);
448 updatePower(frame, frame.displayed);
449 updatePowerText(frame, frame.displayed);
450 updateAuras(frame, frame.displayed);
451 updateShield(frame, frame.displayed);
452 updateHealPred(frame, frame.displayed);
453 updateHealAbsorb(frame, frame.displayed);
454 updatePowerColor(frame, frame.displayed);
455 updateAggro(frame, frame.displayed);
456 updateName(frame, frame.displayed);
457 updateRole(frame, frame.unit);
458 updateLevelText(frame, frame.unit);
459 updateStatus(frame, frame.unit);
460 updatePVP(frame, frame.unit);
461 updateLeaderIcon(frame, frame.unit);
462 updateHealthColor(frame, frame.unit);
465 eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"];
466 eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
467 eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
468 eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"];
469 eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"];
470 eventFuncs["PLAYER_ENTERING_WORLD"] = eventFuncs["UPDATE_ALL_BARS"];
471 eventFuncs["PLAYER_TARGET_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
473 function M.UnitEvent(self, event, arg1)
474 eventFuncs[event](self, arg1);