Make Trigger responsible for default data

Trigger now has a new method Trigger:SetData and two new fields
Trigger.defaultData and Trigger.data

Whenever SetData is used it ensures that all the keys and values from
defaultData are present in the new data.

The data argument from Trigger:SetState has been removed and it will now
always use the Trigger.Data field for notifying indicators
This commit is contained in:
2023-04-10 15:06:41 +02:00
parent 3a1cc35a1e
commit 1f95899781
4 changed files with 50 additions and 21 deletions

View File

@@ -38,7 +38,8 @@ function AuraTrigger.CreateFromConfig(unit, config)
config.spellId, config.spellId,
config.own, config.own,
config.requiredCount, config.requiredCount,
config.invert config.invert,
config.defaultData
) )
unit.auras:AddTrigger(trigger) unit.auras:AddTrigger(trigger)
return trigger return trigger
@@ -57,8 +58,9 @@ end
---@param own boolean ---@param own boolean
---@param requiredCount number | nil ---@param requiredCount number | nil
---@param invert boolean | nil ---@param invert boolean | nil
function AuraTrigger:Init(spellId, own, requiredCount, invert) ---@param defaultData table | nil
Trigger.Init(self, invert) function AuraTrigger:Init(spellId, own, requiredCount, invert, defaultData)
Trigger.Init(self, invert, defaultData)
self.spellId = spellId self.spellId = spellId
self.requiredCount = requiredCount or 1 self.requiredCount = requiredCount or 1
self.own = own self.own = own
@@ -68,7 +70,7 @@ end
--- Reset the AuraTrigger to the default state --- Reset the AuraTrigger to the default state
function AuraTrigger:Reset() function AuraTrigger:Reset()
self.count = 0 self.count = 0
self:SetState(self.count >= self.requiredCount, nil, false) self:SetState(self.count >= self.requiredCount, false)
end end
---Check if a given aura matches this AuraTrigger ---Check if a given aura matches this AuraTrigger
@@ -90,15 +92,17 @@ function AuraTrigger:AddAura(aura)
return return
end end
self.count = self.count + 1 self.count = self.count + 1
self:SetState(self.count >= self.requiredCount, {stacks=aura.applications}, false) self:SetData({stacks=aura.applications})
self:SetState(self.count >= self.requiredCount, false)
end end
---Inform the trigger about an updated aura ---Inform the trigger about an updated aura
---@param before UnitAuraInfo ---@param before UnitAuraInfo
---@param after UnitAuraInfo ---@param after UnitAuraInfo
function AuraTrigger:UpdateAura(before, after) function AuraTrigger:UpdateAura(before, after)
self:SetData({stacks=after.applications})
if self.active then if self.active then
self.indicator:Update({stacks=after.applications}) self.indicator:Update(self.data)
end end
end end
@@ -109,5 +113,6 @@ function AuraTrigger:RemoveAura(aura)
return return
end end
self.count = self.count - 1 self.count = self.count - 1
self:SetState(self.count >= self.requiredCount, nil, false) self:SetData({})
self:SetState(self.count >= self.requiredCount, false)
end end

View File

@@ -92,7 +92,8 @@ function MultiTrigger:OnChildActivate(idx, trigger, data)
table.sort(activeChildren) table.sort(activeChildren)
-- The highest priority active trigger has changed -- The highest priority active trigger has changed
if activeChild ~= activeChildren[1] then if activeChild ~= activeChildren[1] then
self:SetState(true, data) self:SetData(data)
self:SetState(true, true)
end end
end end
@@ -113,8 +114,15 @@ function MultiTrigger:OnChildDeactivate(idx, trigger, data)
end end
end end
table.remove(activeChildren, found) table.remove(activeChildren, found)
if activeChild ~= activeChildren[1] then local newActiveChild = activeChildren[1]
self:SetState(activeChildren[1] ~= nil, childrenData[1], true) if activeChild ~= newActiveChild then
if activeChildren[1] ~= nil then
self:SetData(childrenData[newActiveChild])
self:SetState(true, true)
else
self:SetData({})
self:SetState(false, false)
end
end end
end end

View File

@@ -30,7 +30,7 @@ types.StatusTrigger = StatusTrigger
---@param config table ---@param config table
function StatusTrigger.CreateFromConfig(unit, config) function StatusTrigger.CreateFromConfig(unit, config)
local trigger = StatusTrigger:new(config.status, config.requiredCount, local trigger = StatusTrigger:new(config.status, config.requiredCount,
config.invert) config.invert, config.defaultData)
unit.auras:AddTrigger(trigger) unit.auras:AddTrigger(trigger)
return trigger return trigger
end end
@@ -47,8 +47,8 @@ end
---@param status string The kind of status to trigger on ---@param status string The kind of status to trigger on
---@param requiredCount number | nil ---@param requiredCount number | nil
---@param invert boolean | nil ---@param invert boolean | nil
function StatusTrigger:Init(status, requiredCount, invert) function StatusTrigger:Init(status, requiredCount, invert, defaultData)
Trigger.Init(self, invert) Trigger.Init(self, invert, defaultData)
self.status = status self.status = status
self.requiredCount = requiredCount or 1 self.requiredCount = requiredCount or 1
self.invert = invert or false self.invert = invert or false
@@ -59,5 +59,5 @@ end
-- status: -- status:
-- Must be a valid UnitAuraInfo -- Must be a valid UnitAuraInfo
function StatusTrigger:UpdateStatus(count) function StatusTrigger:UpdateStatus(count)
self:SetState(count >= self.requiredCount, nil, false) self:SetState(count >= self.requiredCount, false)
end end

View File

@@ -23,6 +23,8 @@ local types = omi.GetModule("types")
---@class Trigger: Object ---@class Trigger: Object
---@field active boolean ---@field active boolean
---@field invert boolean ---@field invert boolean
---@field indicator Indicator
---@field data table The most recent data updated by the data source
local Trigger = types.CreateClass("Trigger") local Trigger = types.CreateClass("Trigger")
types.Trigger = Trigger types.Trigger = Trigger
@@ -36,10 +38,25 @@ end
--- Initialize a new Trigger object --- Initialize a new Trigger object
---@param invert boolean|nil ---@param invert boolean|nil
function Trigger:Init(invert) ---@param defaultData table|nil default data that is always present when this trigger updates an indicator
function Trigger:Init(invert, defaultData)
invert = invert or false invert = invert or false
self.invert = invert self.invert = invert
self.active = invert self.active = invert
self.defaultData = defaultData
self:SetData({})
end
---Save new data from the datasource into the trigger. This method will make
---sure the default data is always present
---@param data table
function Trigger:SetData(data)
if self.defaultData then
for k, v in pairs(self.defaultData) do
data[k] = v
end
end
self.data = data
end end
--- Set the target of the Trigger. --- Set the target of the Trigger.
@@ -49,30 +66,29 @@ end
function Trigger:SetTarget(target) function Trigger:SetTarget(target)
self.indicator = target self.indicator = target
if self.active then if self.active then
target:Show() target:Show(self.data)
end end
end end
---Set the state of the trigger. Will activate the indicator if needed. ---Set the state of the trigger. Will activate the indicator if needed.
---@param data table<string,any> | nil
---@param state boolean ---@param state boolean
function Trigger:SetState(state, data, doUpdate) function Trigger:SetState(state, doUpdate)
state = state ~= self.invert -- state xor inverted state = state ~= self.invert -- state xor inverted
if self.active ~= state then if self.active ~= state then
if state then if state then
self.indicator:Show(data) self.indicator:Show(self.data)
else else
self.indicator:Hide() self.indicator:Hide()
end end
self.active = state self.active = state
elseif doUpdate and self.active then elseif doUpdate and self.active then
self.indicator:Update(data) self.indicator:Update(self.data)
end end
end end
-- Reset the trigger to the default state. If this changes the activation of the -- Reset the trigger to the default state. If this changes the activation of the
-- trigger then the target will be notified. -- trigger then the target will be notified.
function Trigger:Reset() function Trigger:Reset()
self:SetState(false, nil, false) self:SetState(false, false)
end end