Rework the way triggers and indicators work

Triggers are now a link between link between a datasource (for now only
AuraList) and an indicator (for now only SquareIndicator).
This commit is contained in:
2023-03-19 06:54:02 +01:00
parent 5d21f1030c
commit 825738a040
4 changed files with 152 additions and 80 deletions

View File

@@ -17,47 +17,52 @@
local omi = select(2, ...)
local types = omi.GetModule("types")
---
-- Trigger objects provide a link between indicators and some data source. They
-- can pass data from the data source into the trigger. This is just a
-- supertype for all indicators and should not be constructed on its own. It
-- has no functionality other than providing default implementations that do
-- nothing.
types.Trigger = types.CreateClass("Trigger")
local Trigger = types.Trigger
-- Constructor for Trigger.
-- fn:
-- The callback function that gets called every time this trigger changes
-- states between active and inactive. Function takes one boolean argument.
function Trigger:Init(fn)
self.fn = fn
-- Initialize a new Trigger object
-- indicator:
-- The indicator that gets activated by this trigger.
function Trigger:Init(indicator)
self.indicator = indicator
end
-- Returns whether the trigger is active
-- Returns whether or not the trigger is active
function Trigger:IsActive()
return false
end
-- Reset the trigger to the default state. Does run the untrigger callback if
-- the trigger is active when Reset is called.
-- Reset the trigger to the default state. Deactivates the indicator if it is
-- active when Reset is called.
function Trigger:Reset()
end
---
-- AuraTrigger is a trigger that can be attached to AuraList as datasource
types.AuraTrigger = types.CreateClass("AuraTrigger", Trigger)
local AuraTrigger = types.AuraTrigger
-- Constructor for AuraTrigger
-- fn:
-- The callback function that gets called every time this trigger changes
-- states between active and inactive. Function takes one boolean argument.
--
-- requiredCount = 1:
-- The minimum number of conditions that must be met before the trigger
-- activates. What exactly this means depends on the trigger, examples are
-- # of matching auras, # of stacks.
--
-- invert = false:
-- If the trigger is inverted it will activate when count < requiredCount.
-- If the trigger is not inverted it will activate when count >= requiredCount.
function AuraTrigger:Init(fn, spellId, own, requiredCount, invert)
Trigger.Init(self, fn)
--- Initialize a new AuraTrigger object
-- indicator
-- Indicator that gets controlled by this trigger.
-- spellId
-- Spell id to trigger on
-- own
-- Only trigger on auras by the player
-- requiredCount=1
-- Number of aura applications to activate trigger
-- invert=false
-- Whether to invert trigger activation
function AuraTrigger:Init(indicator, spellId, own, requiredCount, invert)
Trigger.Init(self, indicator)
self.spellId = spellId
self.requiredCount = requiredCount or 1
self.own = own
@@ -65,18 +70,19 @@ function AuraTrigger:Init(fn, spellId, own, requiredCount, invert)
self.invert = invert or false
end
-- Reset the trigger to the default state. Does run the untrigger callback if
-- the trigger is active when it is called
--- See Trigger:Reset
function AuraTrigger:Reset()
local before = self:IsActive()
self.count = 0
local after = self:IsActive()
if before ~= after then
self.fn(after)
if not before and after then
self.indicator:Show()
elseif before and not after then
self.indicator:Hide()
end
end
-- Return true if the aura matches the trigger
--- Return true if the aura matches the trigger
-- aura:
-- Must be a valid UnitAuraInfo structure.
function AuraTrigger:IsMatching(aura)
@@ -86,10 +92,10 @@ function AuraTrigger:IsMatching(aura)
if self.own and aura.sourceUnit ~= "player" then
return false
end
return true
return true
end
-- Inform the trigger about an added aura
--- Inform the trigger about an added aura
-- aura:
-- Must be a valid UnitAuraInfo
function AuraTrigger:AddAura(aura)
@@ -98,13 +104,17 @@ function AuraTrigger:AddAura(aura)
end
self.count = self.count + 1
-- Be mindful, this works only if count always changes by 1.
-- Be mindful, this works only if count always changes by 1.
if self.count == self.requiredCount then
self.fn(not self.invert)
if self.invert then
self.indicator:Hide()
else
self.indicator:Show()
end
end
end
-- Inform the trigger about an updated aura
--- Inform the trigger about an updated aura
-- before:
-- Must be a valid UnitAuraInfo for the aura before the update
-- after:
@@ -113,20 +123,25 @@ function AuraTrigger:UpdateAura(before, after)
--
end
-- Inform the trigger about an aura that got removed
--- Inform the trigger about an aura that got removed
-- aura:
-- Must be a valid UnitAuraInfo for the aura before it got removed
function AuraTrigger:RemoveAura(aura)
if not self:IsMatching(aura) then
return
end
self.count = self.count - 1
-- Be mindful, this works only if count always changes by 1.
self.count = self.count - 1
-- Be mindful, this works only if count always changes by 1.
if self.count == self.requiredCount - 1 then
self.fn(self.invert)
if self.invert then
self.indicator:Show()
else
self.indicator:Hide()
end
end
end
-- Returns true if the trigger is active, false otherwise
--- Returns true if the trigger is active, false otherwise
function AuraTrigger:IsActive()
if self.invert then
return self.count < self.requiredCount