X-Git-Url: https://www.aleksib.fi/git/wowui.git/blobdiff_plain/d76a52e46fc339db35e6a068c0f1564db7b9cf46..bb359caabbc6b3142f02677a11eaf7507a2582bb:/OmaUF/Events.lua diff --git a/OmaUF/Events.lua b/OmaUF/Events.lua index f745434..03e790c 100644 --- a/OmaUF/Events.lua +++ b/OmaUF/Events.lua @@ -1,132 +1,131 @@ -- 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; local UnitThreatSituation, GetThreatStatusColor = UnitThreatSituation, GetThreatStatusColor; 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 GetRaidTargetIndex, SetRaidTargetIconTexture = GetRaidTargetIndex, SetRaidTargetIconTexture; local RAID_CLASS_COLORS = RAID_CLASS_COLORS; -local updateAuraFrames = OmaUFAuras.UpdateAuras; +local updateAuras = 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 = Settings.Width; local M = {}; OmaUFEvents = M; -function M.RegisterEvents(frame) - -- events are taken from FrameXML/CompactUnitFrame.lua - -- TODO raid marker support, - -- player flags support (/afk, /dnd) +function M.RegisterUnitEvents(frame) + -- events are taken from FrameXML/CompactUnitFrame.lua and FrameXML/TargetFrame.lua + -- TODO player flags support (/afk, /dnd) + frame:RegisterEvent("RAID_TARGET_UPDATE"); -- have to register all and just check local displayed = frame.unit ~= frame.displayed and frame.displayed or nil; frame:RegisterUnitEvent("UNIT_HEALTH", frame.unit, displayed); frame:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", frame.unit, displayed); frame:RegisterUnitEvent("UNIT_MAXHEALTH", frame.unit, displayed); - frame:RegisterUnitEvent("UNIT_POWER", frame.unit, displayed); - frame:RegisterUnitEvent("UNIT_MAXPOWER", frame.unit, displayed); - frame:RegisterUnitEvent("UNIT_DISPLAYPOWER", frame.unit, displayed); + if frame.mana then + frame:RegisterUnitEvent("UNIT_POWER_UPDATE", 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); + end + if frame.shield then + frame:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", frame.unit, displayed); + end + if frame.auras then + frame:RegisterUnitEvent("UNIT_AURA", frame.unit, displayed); + end frame:RegisterUnitEvent("UNIT_NAME_UPDATE", frame.unit, displayed); - frame:RegisterUnitEvent("UNIT_AURA", frame.unit, displayed); - frame:RegisterUnitEvent("UNIT_HEAL_PREDICTION", frame.unit, displayed); - frame:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", frame.unit, displayed); - frame:RegisterUnitEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED", frame.unit, displayed); frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", frame.unit, displayed); 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 +M.UpdateMaxHealth = updateMaxHealth; 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 +M.UpdateHealth = updateHealth; 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(); + elseif frame.healthText.percent then + frame.healthText:SetFormattedText("%.1f", UnitHealth(unit)/frame.health.max*100); else - frame.healthText:Hide(); + local current = UnitHealth(unit); + if current > 1000000000 then -- 1.0B + frame.healthText:SetFormattedText("%.2fB", current / 1000000000); + elseif current > 1000000 then -- 1.0M + frame.healthText:SetFormattedText("%.2fM", current / 1000000); + elseif current > 1000 then -- 1K + frame.healthText:SetFormattedText("%.1fK", current / 1000); + else + frame.healthText:SetFormattedText("%d", current) + end end end +M.UpdateHealthText = updateHealthText; -local function updateMaxHealth(frame, unit) - frame.health.max = UnitHealthMax(unit); +local function updateMaxPower(frame, unit) + frame.mana.max = UnitPowerMax(unit); end +M.UpdateMaxPower = updateMaxPower; 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 +M.UpdatePower = updatePower; 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(); @@ -134,40 +133,25 @@ local function updatePowerText(frame, unit) frame.manaText:Hide(); end end - -local function updateMaxPower(frame, unit) - frame.mana.max = UnitPowerMax(unit); -end +M.UpdatePowerText = updatePowerText; local function updatePowerColor(frame, unit) frame.mana:SetVertexColor(unpack(powerColors[UnitPowerType(unit)])); end +M.UpdatePowerColor = updatePowerColor; local function updateName(frame, unit) local name = UnitName(unit); if not name then return end - frame.name:SetText(ssub(name, 1, 10)); -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)); - frame.healpred:Show(); - else - frame.healpred:Hide(); - end + frame.name:SetText(ssub(name, 1, frame.name.count)); end +M.UpdateName = updateName; 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(); @@ -185,43 +169,7 @@ local function updateShield(frame, unit) frame.shieldhl:Hide(); end 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; - frame.healabsorb:SetWidth(min(space, absorb)); - frame.healabsorb:Show(); - else - frame.healabsorb:Hide(); - end -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 -end +M.UpdateShield = updateShield; local function updateAggro(frame, unit) local status = UnitThreatSituation(unit); @@ -231,31 +179,25 @@ local function updateAggro(frame, unit) frame.base:SetVertexColor(unpack(baseColor)); end end +M.UpdateAggro = updateAggro; +-- only works for player frame local function updateVehicle(frame) - local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle); + local shouldTargetVehicle = UnitHasVehicleUI("player") and + UnitTargetsVehicleInRaidUI("player") and UnitExists("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 +M.UpdateVehicle = updateVehicle; local function updateLevelText(frame, unit, levelup) if levelup then @@ -286,9 +228,10 @@ local function updateLevelText(frame, unit, levelup) frame.level:SetFormattedText("%s%s", leveltext, classtext); end end +M.UpdateLevelText = updateLevelText; 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(); @@ -299,7 +242,12 @@ local function updateStatus(frame, unit) frame.status:Hide(); end end +M.UpdateStatus = updateStatus; +local pvpIcons = { + Alliance = "Interface\\TARGETINGFRAME\\UI-PVP-Alliance", + Horde = "Interface\\TARGETINGFRAME\\UI-PVP-Horde" +}; local function updatePVP(frame, unit) if UnitIsPVPFreeForAll(unit) then frame.pvp:SetTexture("Interface\\TARGETINGFRAME\\UI-PVP-FFA"); @@ -307,7 +255,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"; @@ -315,7 +263,7 @@ local function updatePVP(frame, unit) faction = "Horde"; end end - frame.pvp:SetTexture("Interface\\TARGETINGFRAME\\UI-PVP-"..faction); + frame.pvp:SetTexture(pvpIcons[faction]); frame.pvp:Show(); else frame.pvp:Hide(); @@ -324,6 +272,7 @@ local function updatePVP(frame, unit) frame.pvp:Hide(); end end +M.UpdatePVP = updatePVP; local function updateLeaderIcon(frame, unit) if UnitIsGroupLeader(frame.unit) then @@ -343,6 +292,7 @@ local function updateLeaderIcon(frame, unit) frame.leader:Hide(); end end +M.UpdateLeaderIcon = updateLeaderIcon; local function updateHealthColor(frame, unit) if not UnitPlayerControlled(unit) and UnitIsTapDenied(unit) then @@ -361,31 +311,35 @@ local function updateHealthColor(frame, unit) frame.health:SetVertexColor(UnitSelectionColor(unit)); end end +M.UpdateHealthColor = updateHealthColor; + +local function updateRaidMarker(frame, unit) + local index = GetRaidTargetIndex(unit); + if index then + SetRaidTargetIconTexture(frame.targeticon, index); + frame.targeticon:Show(); + else + frame.targeticon:Hide(); + end +end +M.UpdateRaidMarker = updateRaidMarker; local eventFuncs = { ["UNIT_HEALTH"] = function(frame) updateHealth(frame, frame.displayed); updateHealthText(frame, frame.displayed); - updateShield(frame, frame.displayed); - updateHealAbsorb(frame, frame.displayed); - -- no heal prediction update, that doesn't overflow too much + if frame.shield then updateShield(frame, frame.displayed) end end, - ["UNIT_POWER"] = function(frame) + ["UNIT_POWER_UPDATE"] = function(frame) updatePower(frame, frame.displayed); updatePowerText(frame, frame.displayed); end, ["UNIT_AURA"] = function(frame) updateAuras(frame, frame.displayed); end, - ["UNIT_HEAL_PREDICTION"] = function(frame) - updateHealPred(frame, frame.displayed); - end, ["UNIT_ABSORB_AMOUNT_CHANGED"] = function(frame) updateShield(frame, frame.displayed); end, - ["UNIT_HEAL_ABSORB_AMOUNT_CHANGED"] = function(frame) - updateHealAbsorb(frame, frame.displayed); - end, ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame) updateAggro(frame, frame.displayed); end, @@ -393,8 +347,7 @@ local eventFuncs = { updateMaxHealth(frame, frame.displayed); updateHealth(frame, frame.displayed); updateHealthText(frame, frame.displayed); - updateShield(frame, frame.displayed); - updateHealAbsorb(frame, frame.displayed); + if frame.shield then updateShield(frame, frame.displayed) end end, ["UNIT_MAXPOWER"] = function(frame) updateMaxPower(frame, frame.displayed); @@ -403,73 +356,82 @@ 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, + ["RAID_TARGET_UPDATE"] = function(frame) + updateRaidMarker(frame, frame.displayed); + 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); updateHealthText(frame, frame.displayed); - updatePower(frame, frame.displayed); - updatePowerText(frame, frame.displayed); - updateAuras(frame, frame.displayed); - updateShield(frame, frame.displayed); - updateHealPred(frame, frame.displayed); - updateHealAbsorb(frame, frame.displayed); - updatePowerColor(frame, frame.displayed); + updateHealthColor(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); + updateRaidMarker(frame, frame.displayed); + if frame.mana then + updateMaxPower(frame, frame.displayed); + updatePower(frame, frame.displayed); + updatePowerText(frame, frame.displayed); + updatePowerColor(frame, frame.displayed); + end + if frame.auras then updateAuras(frame, frame.displayed) end + if frame.shield then updateShield(frame, frame.displayed) end + 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 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"]; eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"]; eventFuncs["PLAYER_ENTERING_WORLD"] = eventFuncs["UPDATE_ALL_BARS"]; eventFuncs["PLAYER_TARGET_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"]; +eventFuncs["INSTANCE_ENCOUNTER_ENGAGE_UNIT"] = eventFuncs["UPDATE_ALL_BARS"]; +eventFuncs["UNIT_TARGETABLE_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"]; function M.UnitEvent(self, event, arg1) - eventFuncs[event](self, arg1); + return eventFuncs[event](self, arg1); end