Files
OmicronFrames/src/types/trigger.lua
omicron 825738a040 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).
2023-03-19 06:54:02 +01:00

152 lines
4.4 KiB
Lua

-- Copyright 2023 <omicron.me@protonmail.com>
--
-- This file is part of Omicron Frames
--
-- Omicron Frames is free software: you can redistribute it and/or modify it
-- under the terms of the GNU General Public License as published by the Free
-- Software Foundation, either version 3 of the License, or (at your option)
-- any later version.
--
-- Omicron Frames is distributed in the hope that it will be useful, but
-- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
-- more details.
--
-- You should have received a copy of the GNU General Public License along with
-- Omicron Frames. If not, see <https://www.gnu.org/licenses/>.
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
-- Initialize a new Trigger object
-- indicator:
-- The indicator that gets activated by this trigger.
function Trigger:Init(indicator)
self.indicator = indicator
end
-- Returns whether or not the trigger is active
function Trigger:IsActive()
return false
end
-- 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
--- 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
self.count = 0
self.invert = invert or false
end
--- See Trigger:Reset
function AuraTrigger:Reset()
local before = self:IsActive()
self.count = 0
local after = self:IsActive()
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
-- aura:
-- Must be a valid UnitAuraInfo structure.
function AuraTrigger:IsMatching(aura)
if aura.spellId ~= aura.spellId then
return false
end
if self.own and aura.sourceUnit ~= "player" then
return false
end
return true
end
--- Inform the trigger about an added aura
-- aura:
-- Must be a valid UnitAuraInfo
function AuraTrigger:AddAura(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.
if self.count == self.requiredCount then
if self.invert then
self.indicator:Hide()
else
self.indicator:Show()
end
end
end
--- Inform the trigger about an updated aura
-- before:
-- Must be a valid UnitAuraInfo for the aura before the update
-- after:
-- Must be a valid UnitAuraInfo for the aura after the update
function AuraTrigger:UpdateAura(before, after)
--
end
--- 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.
if self.count == self.requiredCount - 1 then
if self.invert then
self.indicator:Show()
else
self.indicator:Hide()
end
end
end
--- Returns true if the trigger is active, false otherwise
function AuraTrigger:IsActive()
if self.invert then
return self.count < self.requiredCount
else
return self.count >= self.requiredCount
end
end