Add methods to the states/allstates table that helps with creating,
updating or remove states in an optimized way

## Advantage of using this function instead of doing a states[key] = {
... }

- If already created, update existing state, and return true if any
value was changed, this can help reduce amount of resources an aura use

- Automatically `return true` when using these functions and any change
was made

## Examples

```Lua
function(states, event, ...)
    if event == "PLAYER_TARGET_CHANGED" then
        if UnitExists("target") then
            -- if state exists it's updated, not replaced
            -- show & changed fields can be skipped
            states:Update("", {
                    name = UnitName("target"),
                    duration = 5,
                    expirationTime = GetTime() + 5,
                    progressType = "timed",
                    autoHide = true
            })
        else
            -- wipe
            states:RemoveAll()
        end
    end
    -- no need to return true
end
```

with clones

```Lua
function(states, event, ...)
    local currentEssence = UnitPower("player", Enum.PowerType.Essence)
    local maxEssence = UnitPowerMax("player", Enum.PowerType.Essence)
    for i = 1, 6 do
        if i > maxEssence then
            states:Remove(i) -- wipe allstates[6]
        else
            local value = currentEssence >= i and 1 or 0
            local newState = {
                progressType = "static",
                value = value,
                total = 1
            }
            states:Update(i, newState)
        end
    end
    -- no need to return true
end
```
This commit is contained in:
NoM0Re
2025-02-24 22:10:45 +01:00
committed by GitHub
parent 0e0c49ec70
commit 7c7869494e
22 changed files with 140 additions and 1689 deletions
+1 -2
View File
@@ -16,7 +16,6 @@ Private.DiscordList = {
[=[Burlis]=],
[=[Causese]=],
[=[Chab]=],
[=[cheswick]=],
[=[Darian]=],
[=[Desik]=],
[=[DjinnFish]=],
@@ -35,7 +34,7 @@ Private.DiscordList = {
[=[Luckyone]=],
[=[Luxthos]=],
[=[m33shoq]=],
[=[Marcelian]=],
[=[Manabanana]=],
[=[MetalMusicMan]=],
[=[Murph]=],
[=[Mynze]=],
+5 -4
View File
@@ -667,11 +667,12 @@ local function RunTriggerFunc(allStates, data, id, triggernum, event, arg1, arg2
else
ok, returnValue = pcall(data.triggerFunc, allStates, event, arg1, arg2, ...);
end
if not ok then
errorHandler(returnValue)
elseif ok and returnValue then
if (ok and (returnValue or (returnValue ~= false and allStates.__changed))) then
updateTriggerState = true;
elseif not ok then
errorHandler(returnValue)
end
allStates.__changed = nil
for key, state in pairs(allStates) do
if (type(state) ~= "table") then
errorHandler(string.format(L["All States table contains a non table at key: '%s'."], key))
@@ -3852,7 +3853,7 @@ function GenericTrigger.GetOverlayInfo(data, triggernum)
count = variables.additionalProgress;
end
else
local allStates = {};
local allStates = setmetatable({}, Private.allstatesMetatable)
Private.ActivateAuraEnvironment(data.id);
RunTriggerFunc(allStates, events[data.id][triggernum], data.id, triggernum, "OPTIONS");
Private.ActivateAuraEnvironment(nil);
+2 -2
View File
@@ -8,8 +8,8 @@ WeakAuras.halfWidth = WeakAuras.normalWidth / 2
WeakAuras.doubleWidth = WeakAuras.normalWidth * 2
local versionStringFromToc = GetAddOnMetadata("WeakAuras", "Version")
local versionString = "5.19.2 Beta"
local buildTime = "20250222203033"
local versionString = "5.19.3 Beta"
local buildTime = "20250224214450"
local isAwesomeEnabled = C_NamePlate and C_NamePlate.GetNamePlateForUnit and true or false
WeakAuras.versionString = versionString
@@ -684,16 +684,12 @@ function lib.GetUnitNameplate(unit)
local nameplate = C_NamePlate.GetNamePlateForUnit(unit)
if nameplate then
-- credit to Exality for https://wago.io/explosiveorbs
if nameplate.UnitFrame and nameplate.UnitFrame.Health then
-- elvui bunny
if nameplate.UnitFrame and nameplate.UnitFrame.Health and nameplate.UnitFrame.Health:IsShown() then
return nameplate.UnitFrame.Health
elseif nameplate.UnitFrame and nameplate.UnitFrame.Name and nameplate.UnitFrame.Name:IsShown() then
return nameplate.UnitFrame.Name
elseif nameplate.unitFrame and nameplate.unitFrame.Health then
-- elvui someday
elseif nameplate.unitFrame and nameplate.unitFrame.Health and nameplate.unitFrame.Health:IsShown() then
return nameplate.unitFrame.Health
elseif nameplate.unitFrame and nameplate.unitFrame.Name and nameplate.unitFrame.Name:IsShown() then
return nameplate.unitFrame.Name
elseif nameplate.unitFramePlater and nameplate.unitFramePlater.healthBar then
-- plater
-- fallback to default nameplate in case plater is not on screen and uses blizzard default (module disabled, force-blizzard functionality)
+1 -1
View File
@@ -262,7 +262,7 @@ local function UpdatePosition(self)
local yOffset = self.yOffset + (self.yOffsetAnim or 0) + (self.yOffsetRelative or 0)
self:RealClearAllPoints();
local ok, ret = pcall(self.SetPoint, self, self.anchorPoint, self.relativeTo, self.relativePoint, xOffset, yOffset);
local ok = pcall(self.SetPoint, self, self.anchorPoint, self.relativeTo, self.relativePoint, xOffset, yOffset);
if not ok then
Private.GetErrorHandlerId(self.id, L["Update Position"])
end
+91
View File
@@ -0,0 +1,91 @@
if not WeakAuras.IsLibsOK() then return end
local AddonName, Private = ...
local function fixMissingFields(state)
if type(state) ~= "table" then return end
-- set show
if state.show == nil then
state.show = true
end
end
local remove = function(states, key)
local changed = false
local state = states[key]
if state then
state.show = false
state.changed = true
states.__changed = true
changed = true
end
return changed
end
local removeAll = function(states)
local changed = false
for _, state in pairs(states) do
state.show = false
state.changed = true
changed = true
end
if changed then
states.__changed = true
end
return changed
end
local function recurseUpdate(t1, t2)
local changed = false
for k, v in pairs(t2) do
if type(v) == "table" and type(t1[k]) == "table" then
if recurseUpdate(t1[k], v) then
changed = true
end
else
if t1[k] ~= v then
t1[k] = v
changed = true
end
end
end
return changed
end
local update = function(states, key, newState)
local changed = false
local state = states[key]
if state then
fixMissingFields(newState)
changed = recurseUpdate(state, newState)
if changed then
state.changed = true
states.__changed = true
end
end
return changed
end
local create = function(states, key, newState)
states[key] = newState
states[key].changed = true
states.__changed = true
fixMissingFields(states[key])
return true
end
local createOrUpdate = function(states, key, newState)
key = key or ""
if states[key] then
return update(states, key, newState)
else
return create(states, key, newState)
end
end
Private.allstatesMetatable = {
__index = {
Update = createOrUpdate,
Remove = remove,
RemoveAll = removeAll
}
}
+7 -4
View File
@@ -18,6 +18,8 @@ local SendChatMessage, UnitInBattleground, UnitInRaid, UnitInParty, GetTime
local CreateFrame, IsShiftKeyDown, GetScreenWidth, GetScreenHeight, GetCursorPosition, UpdateAddOnCPUUsage, GetFrameCPUUsage, debugprofilestop
= CreateFrame, IsShiftKeyDown, GetScreenWidth, GetScreenHeight, GetCursorPosition, UpdateAddOnCPUUsage, GetFrameCPUUsage, debugprofilestop
local debugstack = debugstack
local GetNumTalentTabs, GetNumTalents = GetNumTalentTabs, GetNumTalents
local MAX_NUM_TALENTS = MAX_NUM_TALENTS or 40
local ADDON_NAME = "WeakAuras"
local WeakAuras = WeakAuras
@@ -4036,7 +4038,9 @@ function WeakAuras.GetTriggerStateForTrigger(id, triggernum)
if (triggernum == -1) then
return Private.GetGlobalConditionState();
end
triggerState[id][triggernum] = triggerState[id][triggernum] or {}
if triggerState[id][triggernum] == nil then
triggerState[id][triggernum] = setmetatable({}, Private.allstatesMetatable)
end
return triggerState[id][triggernum];
end
@@ -4365,7 +4369,7 @@ function Private.UpdatedTriggerState(id)
local changed = false;
for triggernum = 1, triggerState[id].numTriggers do
triggerState[id][triggernum] = triggerState[id][triggernum] or {};
triggerState[id][triggernum] = triggerState[id][triggernum] or setmetatable({}, Private.allstatesMetatable)
local anyStateShown = false;
@@ -4456,7 +4460,6 @@ function Private.UpdatedTriggerState(id)
end
for triggernum = 1, triggerState[id].numTriggers do
triggerState[id][triggernum] = triggerState[id][triggernum] or {};
for cloneId, state in pairs(triggerState[id][triggernum]) do
if (not state.show) then
triggerState[id][triggernum][cloneId] = nil;
@@ -5392,7 +5395,7 @@ function Private.AnchorFrame(data, region, parent, force)
local anchorParent = GetAnchorFrame(data, region, parent);
if not anchorParent then return end
if (data.anchorFrameParent or data.anchorFrameParent == nil
or data.anchorFrameType == "SCREEN" or data.anchorFrameType == "UIPARENT" or data.anchorFrameType == "MOUSE") then
or data.anchorFrameType == "SCREEN" or data.anchorFrameType == "UIPARENT" or data.anchorFrameType == "MOUSE") then
local ok = pcall(region.SetParent, region, anchorParent);
if not ok then
Private.GetErrorHandlerId(data.id, L["Anchoring"])
+2 -1
View File
@@ -1,7 +1,7 @@
## Interface: 30300
## Title: WeakAuras
## Author: The WeakAuras Team
## Version: 5.19.2
## Version: 5.19.3
## X-Flavor: 3.3.5
## Notes: A powerful, comprehensive utility for displaying graphics and information based on buffs, debuffs, and other triggers.
## Notes-esES: Potente y completa aplicación que te permitirá mostrar por pantalla múltiples diseños, basados en beneficios, perjuicios y otros activadores.
@@ -54,6 +54,7 @@ GenericTrigger.lua
BossMods.lua
# Helper Systems
TSUHelpers.lua
AuraWarnings.lua
AuraEnvironment.lua
DebugLog.lua