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 Settings = OmaUFSettings;
22 local baseColor = Settings.BaseColor;
23 local overlayColorDispel = Settings.OverlayColorDispel;
24 local overlayColorCharm = Settings.OverlayColorCharm;
25 local overlayColorAlert = Settings.OverlayColorAlert;
26 local powerColors = Settings.PowerColors;
31 function M.RegisterEvents(frame)
32 -- events are taken from FrameXML/CompactUnitFrame.lua
33 -- TODO raid marker support,
34 -- player flags support (/afk, /dnd)
35 local displayed = frame.unit ~= frame.displayed and frame.displayed or nil;
36 frame:RegisterUnitEvent("UNIT_HEALTH", frame.unit, displayed);
37 frame:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", frame.unit, displayed);
38 frame:RegisterUnitEvent("UNIT_MAXHEALTH", frame.unit, displayed);
39 frame:RegisterUnitEvent("UNIT_POWER", frame.unit, displayed);
40 frame:RegisterUnitEvent("UNIT_MAXPOWER", frame.unit, displayed);
41 frame:RegisterUnitEvent("UNIT_DISPLAYPOWER", frame.unit, displayed);
42 frame:RegisterUnitEvent("UNIT_NAME_UPDATE", frame.unit, displayed);
43 frame:RegisterUnitEvent("UNIT_AURA", frame.unit, displayed);
44 frame:RegisterUnitEvent("UNIT_HEAL_PREDICTION", frame.unit, displayed);
45 frame:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
46 frame:RegisterUnitEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
47 frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", frame.unit, displayed);
48 frame:RegisterUnitEvent("UNIT_CONNECTION", frame.unit, displayed);
49 frame:RegisterUnitEvent("UNIT_FACTION", frame.unit, displayed);
51 local registerEvents = M.RegisterEvents;
53 local function updateHealth(frame, unit)
54 local current, max = UnitHealth(unit), frame.health.max;
56 -- sanity check, occasionally UnitHealthMax gives zero
58 -- somehow current health has gone over the maximum (missed maxhealth event)
59 frame.health.max = UnitHealthMax(unit);
60 max = frame.health.max;
62 -- error state, still over maximum
63 frame.health:SetWidth(width);
67 frame.health:SetWidth(current/frame.health.max*width);
69 frame.health:SetWidth(width);
73 if UnitIsDeadOrGhost(unit) then
78 local function updateHealthText(frame, unit)
79 local current, max = UnitHealth(unit), frame.health.max;
80 if UnitIsDeadOrGhost(unit) then
81 frame.healthText:SetText("Dead");
82 frame.healthText:Show();
83 elseif not UnitIsConnected(unit) then
84 frame.healthText:SetText("DC");
85 frame.healthText:Show();
86 elseif max > 0 and current < max then
87 frame.healthText:SetText(ceil(current/max*100));
88 frame.healthText:Show();
90 frame.healthText:Hide();
94 local function updateMaxHealth(frame, unit)
95 frame.health.max = UnitHealthMax(unit);
98 local function updatePower(frame, unit)
99 local current, max = UnitPower(unit), frame.mana.max;
100 -- sanity check, occasionally UnitPowerMax gives zero
104 elseif current > max then
106 frame.mana.max = UnitPowerMax(unit);
107 max = frame.mana.max;
108 if current > max then
110 frame.mana:SetWidth(width);
115 frame.mana:SetWidth(UnitPower(unit)/max*width);
118 frame.mana:SetWidth(width);
123 local function updatePowerText(frame, unit)
124 local current, max = UnitPower(unit), frame.mana.max;
125 if UnitIsDeadOrGhost(unit) or not UnitIsConnected(unit) then
126 frame.healthText:Hide();
127 elseif max > 0 and current > 0 and current < max then
128 frame.manaText:SetText(ceil(current/max*100));
129 frame.manaText:Show();
131 frame.manaText:Hide();
135 local function updateMaxPower(frame, unit)
136 frame.mana.max = UnitPowerMax(unit);
139 local function updatePowerColor(frame, unit)
140 frame.mana:SetVertexColor(unpack(powerColors[UnitPowerType(unit)]));
143 local function updateName(frame, unit)
144 local name = UnitName(unit);
145 if not name then return end
146 frame.name:SetText(ssub(name, 1, 10));
148 local _, class = UnitClass(unit);
149 local color = RAID_CLASS_COLORS[class];
150 if color then frame.name:SetVertexColor(color.r, color.g, color.b) end
153 local function updateHealPred(frame, unit)
154 local incoming = UnitGetIncomingHeals(unit) or 0;
156 local max = frame.health.max;
157 local space = width - frame.health:GetWidth() + 1;
158 local pred = (incoming / max) * width;
159 frame.healpred:SetWidth(min(space, pred));
160 frame.healpred:Show();
162 frame.healpred:Hide();
166 local function updateShield(frame, unit)
167 local shield = UnitGetTotalAbsorbs(unit) or 0;
169 local max = frame.health.max;
170 local space = width - frame.health:GetWidth();
171 shield = (shield / max) * width;
174 frame.shieldhl:Show();
175 elseif space < shield then
176 frame.shield:SetWidth(space);
178 frame.shieldhl:Show();
180 frame.shield:SetWidth(shield);
182 frame.shieldhl:Hide();
186 frame.shieldhl:Hide();
190 local function updateHealAbsorb(frame, unit)
191 local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
193 local max = frame.health.max;
194 local space = frame.health:GetWidth();
195 absorb = (absorb / max) * width;
196 frame.healabsorb:SetWidth(min(space, absorb));
197 frame.healabsorb:Show();
199 frame.healabsorb:Hide();
203 local function updateAuras(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 unit == frame.unit 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);
348 ["UNIT_HEALTH"] = function(frame)
349 updateHealth(frame, frame.displayed);
350 updateHealthText(frame, frame.displayed);
351 updateShield(frame, frame.displayed);
352 updateHealAbsorb(frame, frame.displayed);
353 -- no heal prediction update, that doesn't overflow too much
355 ["UNIT_POWER"] = function(frame)
356 updatePower(frame, frame.displayed);
357 updatePowerText(frame, frame.displayed);
359 ["UNIT_AURA"] = function(frame)
360 updateAuras(frame, frame.displayed);
362 ["UNIT_HEAL_PREDICTION"] = function(frame)
363 updateHealPred(frame, frame.displayed);
365 ["UNIT_ABSORB_AMOUNT_CHANGED"] = function(frame)
366 updateShield(frame, frame.displayed);
368 ["UNIT_HEAL_ABSORB_AMOUNT_CHANGED"] = function(frame)
369 updateHealAbsorb(frame, frame.displayed);
371 ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
372 updateAggro(frame, frame.displayed);
374 ["UNIT_MAXHEALTH"] = function(frame)
375 updateMaxHealth(frame, frame.displayed);
376 updateHealth(frame, frame.displayed);
377 updateHealthText(frame, frame.displayed);
378 updateShield(frame, frame.displayed);
379 updateHealAbsorb(frame, frame.displayed);
381 ["UNIT_MAXPOWER"] = function(frame)
382 updateMaxPower(frame, frame.displayed);
383 updatePower(frame, frame.displayed);
384 updatePowerText(frame, frame.displayed);
386 ["UNIT_DISPLAYPOWER"] = function(frame)
387 updatePowerColor(frame, frame.displayed);
389 ["UNIT_NAME_UPDATE"] = function(frame)
390 updateName(frame, frame.displayed);
392 ["UNIT_CONNECTION"] = function(frame)
393 updateHealthText(frame, frame.displayed);
394 updatePowerText(frame, frame.displayed);
396 ["PLAYER_ROLES_ASSIGNED"] = function(frame)
397 updateRole(frame, frame.unit);
399 ["UNIT_LEVEL"] = function(frame)
400 updateLevelText(frame, frame.unit);
402 ["PLAYER_LEVEL_UP"] = function(frame, arg1)
403 updateLevelText(frame, frame.unit, arg1);
405 ["PLAYER_UPDATE_RESTING"] = function(frame)
406 updateStatus(frame, frame.unit);
408 ["PLAYER_REGEN_DISABLED"] = function(frame)
409 frame.inCombat = true;
410 updateStatus(frame, frame.unit);
412 ["PLAYER_REGEN_ENABLED"] = function(frame)
413 frame.inCombat = false;
414 updateStatus(frame, frame.unit);
416 ["UNIT_FACTION"] = function(frame)
417 updatePVP(frame, frame.unit);
419 ["PARTY_LEADER_CHANGED"] = function(frame)
420 updateLeaderIcon(frame, frame.unit);
422 ["UPDATE_ALL_BARS"] = function(frame)
423 updateVehicle(frame);
424 updateMaxHealth(frame, frame.displayed);
425 updateMaxPower(frame, frame.displayed);
426 updateHealth(frame, frame.displayed);
427 updateHealthText(frame, frame.displayed);
428 updatePower(frame, frame.displayed);
429 updatePowerText(frame, frame.displayed);
430 updateAuras(frame, frame.displayed);
431 updateShield(frame, frame.displayed);
432 updateHealPred(frame, frame.displayed);
433 updateHealAbsorb(frame, frame.displayed);
434 updatePowerColor(frame, frame.displayed);
435 updateAggro(frame, frame.displayed);
436 updateName(frame, frame.displayed);
437 updateRole(frame, frame.unit);
438 updateLevelText(frame, frame.unit);
439 updateStatus(frame, frame.unit);
440 updatePVP(frame, frame.unit);
441 updateLeaderIcon(frame, frame.unit);
444 eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"];
445 eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
446 eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
447 eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"];
448 eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"];
449 eventFuncs["PLAYER_ENTERING_WORLD"] = eventFuncs["UPDATE_ALL_BARS"];
450 eventFuncs["PLAYER_TARGET_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
452 function M.UnitEvent(self, event, arg1)
453 eventFuncs[event](self, arg1);
456 function M.LoadChar()
457 width = Settings.Character.Width;