X-Git-Url: https://www.aleksib.fi/git/wowui.git/blobdiff_plain/f2bb49433c3e5edfc81625f21c91557a841ef64e..fdbd89d788a6442f2fe57abb8f2321bd6c452889:/OmaUF/Events.lua diff --git a/OmaUF/Events.lua b/OmaUF/Events.lua index d68affe..fa8965a 100644 --- a/OmaUF/Events.lua +++ b/OmaUF/Events.lua @@ -1,12 +1,10 @@ -- Events.lua --- TODO -- recheck these, pvp functions not added yet local _; local unpack = unpack; local ssub = string.sub; local min = math.min; local ceil = math.ceil; local UnitName, UnitClass, UnitExists = UnitName, UnitClass, UnitExists; -local UnitDebuff, UnitIsCharmed, UnitIsFriend = UnitDebuff, UnitIsCharmed, UnitIsFriend; local UnitPower, UnitPowerMax, UnitPowerType = UnitPower, UnitPowerMax, UnitPowerType; local UnitHealth, UnitHealthMax = UnitHealth, UnitHealthMax; local UnitGetIncomingHeals, UnitGetTotalAbsorbs = UnitGetIncomingHeals, UnitGetTotalAbsorbs; @@ -14,8 +12,14 @@ local UnitThreatSituation, GetThreatStatusColor = UnitThreatSituation, GetThreat local UnitIsDeadOrGhost, UnitIsConnected = UnitIsDeadOrGhost, UnitIsConnected; local UnitGetTotalHealAbsorbs = UnitGetTotalHealAbsorbs; local UnitHasVehicleUI, UnitTargetsVehicleInRaidUI = UnitHasVehicleUI, UnitTargetsVehicleInRaidUI; -local UnitGroupRolesAssigned = UnitGroupRolesAssigned; local UnitLevel, UnitClassification = UnitLevel, UnitClassification; +local UnitAffectingCombat, IsResting = UnitAffectingCombat, IsResting; +local UnitIsPVPFreeForAll, UnitIsPVP = UnitIsPVPFreeForAll, UnitIsPVP; +local UnitFactionGroup, UnitIsMercenary = UnitFactionGroup, UnitIsMercenary; +local UnitIsGroupLeader, UnitIsGroupAssistant = UnitIsGroupLeader, UnitIsGroupAssistant; +local HasLFGRestrictions = HasLFGRestrictions; +local UnitPlayerControlled, UnitIsPlayer = UnitPlayerControlled, UnitIsPlayer; +local UnitIsTapDenied, UnitSelectionColor = UnitIsTapDenied, UnitSelectionColor; local RAID_CLASS_COLORS = RAID_CLASS_COLORS; local updateAuraFrames = OmaUFAuras.UpdateAuras; @@ -23,15 +27,11 @@ local updateAuraFrames = OmaUFAuras.UpdateAuras; local Settings = OmaUFSettings; local baseColor = Settings.BaseColor; local healthColor = Settings.HealthColor; -local overlayColorDispel = Settings.OverlayColorDispel; -local overlayColorCharm = Settings.OverlayColorCharm; -local overlayColorAlert = Settings.OverlayColorAlert; local powerColors = Settings.PowerColors; -local width = 10; local M = {}; OmaUFEvents = M; -function M.RegisterEvents(frame) +function M.RegisterUnitEvents(frame) -- events are taken from FrameXML/CompactUnitFrame.lua -- TODO raid marker support, -- player flags support (/afk, /dnd) @@ -42,6 +42,8 @@ function M.RegisterEvents(frame) frame:RegisterUnitEvent("UNIT_POWER", frame.unit, displayed); frame:RegisterUnitEvent("UNIT_MAXPOWER", frame.unit, displayed); frame:RegisterUnitEvent("UNIT_DISPLAYPOWER", frame.unit, displayed); + frame:RegisterUnitEvent("UNIT_POWER_BAR_SHOW", frame.unit, displayed); + frame:RegisterUnitEvent("UNIT_POWER_BAR_HIDE", frame.unit, displayed); frame:RegisterUnitEvent("UNIT_NAME_UPDATE", frame.unit, displayed); frame:RegisterUnitEvent("UNIT_AURA", frame.unit, displayed); frame:RegisterUnitEvent("UNIT_HEAL_PREDICTION", frame.unit, displayed); @@ -51,74 +53,60 @@ function M.RegisterEvents(frame) frame:RegisterUnitEvent("UNIT_CONNECTION", frame.unit, displayed); frame:RegisterUnitEvent("UNIT_FACTION", frame.unit, displayed); end -local registerEvents = M.RegisterEvents; +local registerUnitEvents = M.RegisterUnitEvents; + +local function updateMaxHealth(frame, unit) + frame.health.max = UnitHealthMax(unit); +end local function updateHealth(frame, unit) local current, max = UnitHealth(unit), frame.health.max; - frame.health:Show(); - -- sanity check, occasionally UnitHealthMax gives zero - if current > max then + if current > max or max <= 0 then -- somehow current health has gone over the maximum (missed maxhealth event) - frame.health.max = UnitHealthMax(unit); - max = frame.health.max; - if current > max then - -- error state, still over maximum - frame.health:SetWidth(width); - return; - end - elseif max > 0 then - frame.health:SetWidth(current/frame.health.max*width); - else - frame.health:SetWidth(width); - return; - end - - if UnitIsDeadOrGhost(unit) then + frame.health:SetWidth(frame.width); + updateMaxHealth(frame, unit); + frame.health:Show(); + elseif current <= 0 or UnitIsDeadOrGhost(unit) then frame.health:Hide(); + else + frame.health:SetWidth(current/max*frame.width); + frame.health:Show(); end end local function updateHealthText(frame, unit) - local current, max = UnitHealth(unit), frame.health.max; if UnitIsDeadOrGhost(unit) then frame.healthText:SetText("Dead"); - frame.healthText:Show(); elseif not UnitIsConnected(unit) then frame.healthText:SetText("DC"); - frame.healthText:Show(); - elseif max > 0 and current < max then - frame.healthText:SetText(ceil(current/max*100)); - frame.healthText:Show(); else - frame.healthText:Hide(); + local current = UnitHealth(unit); + if current > 1200000000 then -- 1.2B + frame.healthText:SetFormattedText("%.1fB", current / 1000000000); + elseif current > 1200000 then -- 1.2M + frame.healthText:SetFormattedText("%.1fM", current / 1000000); + elseif current > 1000 then -- 1K + frame.healthText:SetFormattedText("%.1fK", current / 1000); + else + frame.healthText:SetFormattedText("%d", current) + end end end -local function updateMaxHealth(frame, unit) - frame.health.max = UnitHealthMax(unit); +local function updateMaxPower(frame, unit) + frame.mana.max = UnitPowerMax(unit); end local function updatePower(frame, unit) local current, max = UnitPower(unit), frame.mana.max; - -- sanity check, occasionally UnitPowerMax gives zero - if current == 0 then + if current <= 0 then frame.mana:Hide(); - return; - elseif current > max then - frame.mana:Show(); - frame.mana.max = UnitPowerMax(unit); - max = frame.mana.max; - if current > max then - -- error - frame.mana:SetWidth(width); - return; - end - end - if max > 0 then - frame.mana:SetWidth(UnitPower(unit)/max*width); + elseif current > max or max <= 0 then + frame.mana:SetWidth(frame.width); + updateMaxPower(frame, unit); frame.mana:Show(); else - frame.mana:SetWidth(width); + frame.mana:SetWidth(current/max*frame.width); frame.mana:Show(); end end @@ -126,7 +114,7 @@ end local function updatePowerText(frame, unit) local current, max = UnitPower(unit), frame.mana.max; if UnitIsDeadOrGhost(unit) or not UnitIsConnected(unit) then - frame.healthText:Hide(); + frame.manaText:Hide(); elseif max > 0 and current > 0 and current < max then frame.manaText:SetText(ceil(current/max*100)); frame.manaText:Show(); @@ -135,10 +123,6 @@ local function updatePowerText(frame, unit) end end -local function updateMaxPower(frame, unit) - frame.mana.max = UnitPowerMax(unit); -end - local function updatePowerColor(frame, unit) frame.mana:SetVertexColor(unpack(powerColors[UnitPowerType(unit)])); end @@ -152,10 +136,9 @@ end local function updateHealPred(frame, unit) local incoming = UnitGetIncomingHeals(unit) or 0; if incoming > 0 then - local max = frame.health.max; - local space = width - frame.health:GetWidth() + 1; - local pred = (incoming / max) * width; - frame.healpred:SetWidth(min(space, pred)); + local space = frame.width - frame.health:GetWidth() + 1; + incoming = (incoming / frame.health.max) * frame.width; + frame.healpred:SetWidth(min(space, incoming)); frame.healpred:Show(); else frame.healpred:Hide(); @@ -165,9 +148,8 @@ end local function updateShield(frame, unit) local shield = UnitGetTotalAbsorbs(unit) or 0; if shield > 0 then - local max = frame.health.max; - local space = width - frame.health:GetWidth(); - shield = (shield / max) * width; + local space = frame.width - frame.health:GetWidth(); + shield = (shield / frame.health.max) * frame.width; if space == 0 then frame.shield:Hide(); frame.shieldhl:Show(); @@ -189,9 +171,8 @@ end local function updateHealAbsorb(frame, unit) local absorb = UnitGetTotalHealAbsorbs(unit) or 0; if absorb > 0 then - local max = frame.health.max; local space = frame.health:GetWidth(); - absorb = (absorb / max) * width; + absorb = (absorb / frame.health.max) * frame.width; frame.healabsorb:SetWidth(min(space, absorb)); frame.healabsorb:Show(); else @@ -200,27 +181,7 @@ local function updateHealAbsorb(frame, unit) end local function updateAuras(frame, unit) - updateAuraFrames(frame, unit); - if UnitIsFriend("player", unit) and UnitDebuff(unit, 1, "RAID") ~= nil then - -- something dispellable - if frame.overlay.color ~= overlayColorDispel then - frame.overlay:SetVertexColor(unpack(overlayColorDispel)); - frame.overlay:Show(); - frame.overlay.color = overlayColorDispel; - end - -- don't overlay charmed when in vehicle - elseif UnitIsCharmed(unit) and not frame.inVehicle then - if frame.overlay.color ~= overlayColorCharm then - frame.overlay:SetVertexColor(unpack(overlayColorCharm)); - frame.overlay:Show(); - frame.overlay.color = overlayColorCharm; - end - else - if frame.overlay.color ~= nil then - frame.overlay:Hide(); - frame.overlay.color = nil; - end - end + if frame.auras then updateAuraFrames(frame, unit) end end local function updateAggro(frame, unit) @@ -233,27 +194,18 @@ local function updateAggro(frame, unit) end local function updateVehicle(frame) - local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle); + local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and + UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle); if shouldTargetVehicle then if not frame.inVehicle then frame.inVehicle = true; frame.displayed = frame.vehicle; - registerEvents(frame); + registerUnitEvents(frame); end elseif frame.inVehicle then frame.inVehicle = false; frame.displayed = frame.unit; - registerEvents(frame); - end -end - -local function updateRole(frame, unit) - local role = UnitGroupRolesAssigned(unit); - if role == "HEALER" or role == "TANK" or role == "DAMAGER" then - frame.role:SetTexCoord(GetTexCoordsForRoleSmallCircle(role)); - frame.role:Show(); - else - frame.role:Hide(); + registerUnitEvents(frame); end end @@ -288,7 +240,7 @@ local function updateLevelText(frame, unit, levelup) end local function updateStatus(frame, unit) - -- coords from PlayerFrame + -- coords from FrameXML/PlayerFrame.lua if frame.inCombat or UnitAffectingCombat(unit) then frame.status:SetTexCoord(0.5, 1, 0, 0.484375); frame.status:Show(); @@ -307,7 +259,7 @@ local function updatePVP(frame, unit) elseif UnitIsPVP(unit) then local faction = UnitFactionGroup(unit); if faction and faction ~= "Neutral" then - -- from PlayerFrame, mercenary checks + -- from FrameXML/PlayerFrame.lua, mercenary checks if UnitIsMercenary(unit) then if faction == "Horde" then faction = "Alliance"; @@ -368,7 +320,6 @@ local eventFuncs = { updateHealthText(frame, frame.displayed); updateShield(frame, frame.displayed); updateHealAbsorb(frame, frame.displayed); - -- no heal prediction update, that doesn't overflow too much end, ["UNIT_POWER"] = function(frame) updatePower(frame, frame.displayed); @@ -403,44 +354,46 @@ local eventFuncs = { end, ["UNIT_DISPLAYPOWER"] = function(frame) updatePowerColor(frame, frame.displayed); + updateMaxPower(frame, frame.displayed); + updatePower(frame, frame.displayed); + updatePowerText(frame, frame.displayed); end, ["UNIT_NAME_UPDATE"] = function(frame) - updateName(frame, frame.displayed); - updateHealthColor(frame, frame.unit); + if frame.name then updateName(frame, frame.displayed) end + updateHealthColor(frame, frame.displayed); end, ["UNIT_CONNECTION"] = function(frame) updateHealthText(frame, frame.displayed); updatePowerText(frame, frame.displayed); end, - ["PLAYER_ROLES_ASSIGNED"] = function(frame) - updateRole(frame, frame.unit); - end, ["UNIT_LEVEL"] = function(frame) + -- if this is registered, frame has frame.level updateLevelText(frame, frame.unit); end, ["PLAYER_LEVEL_UP"] = function(frame, arg1) updateLevelText(frame, frame.unit, arg1); end, ["PLAYER_UPDATE_RESTING"] = function(frame) + -- player frame has frame.status updateStatus(frame, frame.unit); end, ["PLAYER_REGEN_DISABLED"] = function(frame) frame.inCombat = true; - updateStatus(frame, frame.unit); + if frame.status then updateStatus(frame, frame.unit) end end, ["PLAYER_REGEN_ENABLED"] = function(frame) frame.inCombat = false; - updateStatus(frame, frame.unit); + if frame.status then updateStatus(frame, frame.unit) end end, ["UNIT_FACTION"] = function(frame) - updatePVP(frame, frame.unit); - updateHealthColor(frame, frame.unit); + if frame.pvp then updatePVP(frame, frame.unit) end + updateHealthColor(frame, frame.displayed); end, ["PARTY_LEADER_CHANGED"] = function(frame) updateLeaderIcon(frame, frame.unit); end, ["UPDATE_ALL_BARS"] = function(frame) - updateVehicle(frame); + if frame.vehicle then updateVehicle(frame) end updateMaxHealth(frame, frame.displayed); updateMaxPower(frame, frame.displayed); updateHealth(frame, frame.displayed); @@ -453,16 +406,17 @@ local eventFuncs = { updateHealAbsorb(frame, frame.displayed); updatePowerColor(frame, frame.displayed); updateAggro(frame, frame.displayed); - updateName(frame, frame.displayed); - updateRole(frame, frame.unit); - updateLevelText(frame, frame.unit); - updateStatus(frame, frame.unit); - updatePVP(frame, frame.unit); - updateLeaderIcon(frame, frame.unit); - updateHealthColor(frame, frame.unit); + if frame.name then updateName(frame, frame.displayed) end + if frame.level then updateLevelText(frame, frame.unit) end + if frame.status then updateStatus(frame, frame.unit) end + if frame.pvp then updatePVP(frame, frame.unit) end + if frame.leader then updateLeaderIcon(frame, frame.unit) end + updateHealthColor(frame, frame.displayed); end, }; eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"]; +eventFuncs["UNIT_POWER_BAR_SHOW"] = eventFuncs["UNIT_DISPLAYPOWER"]; +eventFuncs["UNIT_POWER_BAR_HIDE"] = eventFuncs["UNIT_DISPLAYPOWER"]; eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"]; eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"]; eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"]; @@ -473,7 +427,3 @@ eventFuncs["PLAYER_TARGET_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"]; function M.UnitEvent(self, event, arg1) eventFuncs[event](self, arg1); end - -function M.LoadChar() - width = Settings.Character.Width; -end