f8f03f1 - Fix inspect caching, should now properly cache for 60 sec
[wowui.git] / OmaUF / Events.lua
index fa8965a..28b8672 100644 (file)
@@ -10,7 +10,6 @@ local UnitHealth, UnitHealthMax = UnitHealth, UnitHealthMax;
 local UnitGetIncomingHeals, UnitGetTotalAbsorbs = UnitGetIncomingHeals, UnitGetTotalAbsorbs;
 local UnitThreatSituation, GetThreatStatusColor = UnitThreatSituation, GetThreatStatusColor;
 local UnitIsDeadOrGhost, UnitIsConnected = UnitIsDeadOrGhost, UnitIsConnected;
 local UnitGetIncomingHeals, UnitGetTotalAbsorbs = UnitGetIncomingHeals, UnitGetTotalAbsorbs;
 local UnitThreatSituation, GetThreatStatusColor = UnitThreatSituation, GetThreatStatusColor;
 local UnitIsDeadOrGhost, UnitIsConnected = UnitIsDeadOrGhost, UnitIsConnected;
-local UnitGetTotalHealAbsorbs = UnitGetTotalHealAbsorbs;
 local UnitHasVehicleUI, UnitTargetsVehicleInRaidUI = UnitHasVehicleUI, UnitTargetsVehicleInRaidUI;
 local UnitLevel, UnitClassification = UnitLevel, UnitClassification;
 local UnitAffectingCombat, IsResting = UnitAffectingCombat, IsResting;
 local UnitHasVehicleUI, UnitTargetsVehicleInRaidUI = UnitHasVehicleUI, UnitTargetsVehicleInRaidUI;
 local UnitLevel, UnitClassification = UnitLevel, UnitClassification;
 local UnitAffectingCombat, IsResting = UnitAffectingCombat, IsResting;
@@ -20,6 +19,7 @@ local UnitIsGroupLeader, UnitIsGroupAssistant = UnitIsGroupLeader, UnitIsGroupAs
 local HasLFGRestrictions = HasLFGRestrictions;
 local UnitPlayerControlled, UnitIsPlayer = UnitPlayerControlled, UnitIsPlayer;
 local UnitIsTapDenied, UnitSelectionColor = UnitIsTapDenied, UnitSelectionColor;
 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 RAID_CLASS_COLORS = RAID_CLASS_COLORS;
 
 local updateAuraFrames = OmaUFAuras.UpdateAuras;
@@ -32,23 +32,27 @@ local powerColors = Settings.PowerColors;
 local M = {};
 OmaUFEvents = M;
 function M.RegisterUnitEvents(frame)
 local M = {};
 OmaUFEvents = M;
 function M.RegisterUnitEvents(frame)
-    -- events are taken from FrameXML/CompactUnitFrame.lua
-    -- TODO raid marker support,
-    -- player flags support (/afk, /dnd)
+    -- 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);
     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);
-    frame:RegisterUnitEvent("UNIT_POWER_BAR_SHOW", frame.unit, displayed);
-    frame:RegisterUnitEvent("UNIT_POWER_BAR_HIDE", frame.unit, displayed);
+    if frame.mana then
+        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);
+    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_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);
     frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", frame.unit, displayed);
     frame:RegisterUnitEvent("UNIT_CONNECTION", frame.unit, displayed);
     frame:RegisterUnitEvent("UNIT_FACTION", frame.unit, displayed);
@@ -58,6 +62,7 @@ local registerUnitEvents = M.RegisterUnitEvents;
 local function updateMaxHealth(frame, unit)
     frame.health.max = UnitHealthMax(unit);
 end
 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;
 
 local function updateHealth(frame, unit)
     local current, max = UnitHealth(unit), frame.health.max;
@@ -73,18 +78,21 @@ local function updateHealth(frame, unit)
         frame.health:Show();
     end
 end
         frame.health:Show();
     end
 end
+M.UpdateHealth = updateHealth;
 
 local function updateHealthText(frame, unit)
     if UnitIsDeadOrGhost(unit) then
         frame.healthText:SetText("Dead");
     elseif not UnitIsConnected(unit) then
         frame.healthText:SetText("DC");
 
 local function updateHealthText(frame, unit)
     if UnitIsDeadOrGhost(unit) then
         frame.healthText:SetText("Dead");
     elseif not UnitIsConnected(unit) then
         frame.healthText:SetText("DC");
+    elseif frame.healthText.percent then
+        frame.healthText:SetFormattedText("%.1f", UnitHealth(unit)/frame.health.max*100);
     else
         local current = UnitHealth(unit);
     else
         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);
+        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
         elseif current > 1000 then -- 1K
             frame.healthText:SetFormattedText("%.1fK", current / 1000);
         else
@@ -92,10 +100,12 @@ local function updateHealthText(frame, unit)
         end
     end
 end
         end
     end
 end
+M.UpdateHealthText = updateHealthText;
 
 local function updateMaxPower(frame, unit)
     frame.mana.max = UnitPowerMax(unit);
 end
 
 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;
 
 local function updatePower(frame, unit)
     local current, max = UnitPower(unit), frame.mana.max;
@@ -110,6 +120,7 @@ local function updatePower(frame, unit)
         frame.mana:Show();
     end
 end
         frame.mana:Show();
     end
 end
+M.UpdatePower = updatePower;
 
 local function updatePowerText(frame, unit)
     local current, max = UnitPower(unit), frame.mana.max;
 
 local function updatePowerText(frame, unit)
     local current, max = UnitPower(unit), frame.mana.max;
@@ -122,28 +133,19 @@ local function updatePowerText(frame, unit)
         frame.manaText:Hide();
     end
 end
         frame.manaText:Hide();
     end
 end
+M.UpdatePowerText = updatePowerText;
 
 local function updatePowerColor(frame, unit)
     frame.mana:SetVertexColor(unpack(powerColors[UnitPowerType(unit)]));
 end
 
 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
 
 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 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();
-    end
+    frame.name:SetText(ssub(name, 1, frame.name.count));
 end
 end
+M.UpdateName = updateName;
 
 local function updateShield(frame, unit)
     local shield = UnitGetTotalAbsorbs(unit) or 0;
 
 local function updateShield(frame, unit)
     local shield = UnitGetTotalAbsorbs(unit) or 0;
@@ -167,22 +169,12 @@ local function updateShield(frame, unit)
         frame.shieldhl:Hide();
     end
 end
         frame.shieldhl:Hide();
     end
 end
-
-local function updateHealAbsorb(frame, unit)
-    local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
-    if absorb > 0 then
-        local space = frame.health:GetWidth();
-        absorb = (absorb / frame.health.max) * frame.width;
-        frame.healabsorb:SetWidth(min(space, absorb));
-        frame.healabsorb:Show();
-    else
-        frame.healabsorb:Hide();
-    end
-end
+M.UpdateShield = updateShield;
 
 local function updateAuras(frame, unit)
 
 local function updateAuras(frame, unit)
-    if frame.auras then updateAuraFrames(frame, unit) end
+    updateAuraFrames(frame, unit);
 end
 end
+M.UpdateAuras = updateAuras;
 
 local function updateAggro(frame, unit)
     local status = UnitThreatSituation(unit);
 
 local function updateAggro(frame, unit)
     local status = UnitThreatSituation(unit);
@@ -192,10 +184,12 @@ local function updateAggro(frame, unit)
         frame.base:SetVertexColor(unpack(baseColor));
     end
 end
         frame.base:SetVertexColor(unpack(baseColor));
     end
 end
+M.UpdateAggro = updateAggro;
 
 
+-- only works for player frame
 local function updateVehicle(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;
     if shouldTargetVehicle then
         if not frame.inVehicle then
             frame.inVehicle = true;
@@ -208,6 +202,7 @@ local function updateVehicle(frame)
         registerUnitEvents(frame);
     end
 end
         registerUnitEvents(frame);
     end
 end
+M.UpdateVehicle = updateVehicle;
 
 local function updateLevelText(frame, unit, levelup)
     if levelup then
 
 local function updateLevelText(frame, unit, levelup)
     if levelup then
@@ -238,6 +233,7 @@ local function updateLevelText(frame, unit, levelup)
         frame.level:SetFormattedText("%s%s", leveltext, classtext);
     end
 end
         frame.level:SetFormattedText("%s%s", leveltext, classtext);
     end
 end
+M.UpdateLevelText = updateLevelText;
 
 local function updateStatus(frame, unit)
     -- coords from FrameXML/PlayerFrame.lua
 
 local function updateStatus(frame, unit)
     -- coords from FrameXML/PlayerFrame.lua
@@ -251,7 +247,12 @@ local function updateStatus(frame, unit)
         frame.status:Hide();
     end
 end
         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");
 local function updatePVP(frame, unit)
     if UnitIsPVPFreeForAll(unit) then
         frame.pvp:SetTexture("Interface\\TARGETINGFRAME\\UI-PVP-FFA");
@@ -267,7 +268,7 @@ local function updatePVP(frame, unit)
                     faction = "Horde";
                 end
             end
                     faction = "Horde";
                 end
             end
-            frame.pvp:SetTexture("Interface\\TARGETINGFRAME\\UI-PVP-"..faction);
+            frame.pvp:SetTexture(pvpIcons[faction]);
             frame.pvp:Show();
         else
             frame.pvp:Hide();
             frame.pvp:Show();
         else
             frame.pvp:Hide();
@@ -276,6 +277,7 @@ local function updatePVP(frame, unit)
         frame.pvp:Hide();
     end
 end
         frame.pvp:Hide();
     end
 end
+M.UpdatePVP = updatePVP;
 
 local function updateLeaderIcon(frame, unit)
     if UnitIsGroupLeader(frame.unit) then
 
 local function updateLeaderIcon(frame, unit)
     if UnitIsGroupLeader(frame.unit) then
@@ -295,6 +297,7 @@ local function updateLeaderIcon(frame, unit)
         frame.leader:Hide();
     end
 end
         frame.leader:Hide();
     end
 end
+M.UpdateLeaderIcon = updateLeaderIcon;
 
 local function updateHealthColor(frame, unit)
     if not UnitPlayerControlled(unit) and UnitIsTapDenied(unit) then
 
 local function updateHealthColor(frame, unit)
     if not UnitPlayerControlled(unit) and UnitIsTapDenied(unit) then
@@ -313,13 +316,24 @@ local function updateHealthColor(frame, unit)
         frame.health:SetVertexColor(UnitSelectionColor(unit));
     end
 end
         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);
 
 local eventFuncs = {
     ["UNIT_HEALTH"] = function(frame)
         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_POWER"] = function(frame)
         updatePower(frame, frame.displayed);
     end,
     ["UNIT_POWER"] = function(frame)
         updatePower(frame, frame.displayed);
@@ -328,15 +342,9 @@ local eventFuncs = {
     ["UNIT_AURA"] = function(frame)
         updateAuras(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_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,
     ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
         updateAggro(frame, frame.displayed);
     end,
@@ -344,8 +352,7 @@ local eventFuncs = {
         updateMaxHealth(frame, frame.displayed);
         updateHealth(frame, frame.displayed);
         updateHealthText(frame, frame.displayed);
         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);
     end,
     ["UNIT_MAXPOWER"] = function(frame)
         updateMaxPower(frame, frame.displayed);
@@ -392,26 +399,30 @@ local eventFuncs = {
     ["PARTY_LEADER_CHANGED"] = function(frame)
         updateLeaderIcon(frame, frame.unit);
     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)
         if frame.vehicle then updateVehicle(frame) end
         updateMaxHealth(frame, frame.displayed);
     ["UPDATE_ALL_BARS"] = function(frame)
         if frame.vehicle then updateVehicle(frame) end
         updateMaxHealth(frame, frame.displayed);
-        updateMaxPower(frame, frame.displayed);
         updateHealth(frame, frame.displayed);
         updateHealthText(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);
         updateAggro(frame, frame.displayed);
+        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
         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"];
     end,
 };
 eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"];
@@ -423,7 +434,9 @@ 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["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)
 
 function M.UnitEvent(self, event, arg1)
-    eventFuncs[event](self, arg1);
+    return eventFuncs[event](self, arg1);
 end
 end