Move ivert logic and trigger logic into Trigger

The base Trigger class is now responsible for inverting the trigger
state and also for signaling trigger activation to the target/indicator.

In addition the following non-function changes also occured:
 - The triggers moved into their own directory
 - The trigger code has been annotated to help lua-language-server
   deduce type information
 - AuraTrigger was moved into its own file

All these changes should make future development of other triggers easier
This commit is contained in:
omicron committed 2023-04-04 06:40:29 +02:00
1 parent d85a1c3579
commit 8c4cc405aa
5 files changed
+173 -158

No files matched your search

+94
View File
@@ -0,0 +1,94 @@
-- 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")
---@class Trigger
local Trigger = types.Trigger
---AuraTrigger is a Trigger that can be attached to AuraList as datasource
---@class AuraTrigger: Trigger
---@field spellId number
---@field count number
---@field requiredCount number
local AuraTrigger = types.CreateClass("AuraTrigger", Trigger)
types.AuraTrigger = AuraTrigger
---@return AuraTrigger
function AuraTrigger.new(cls, ...)
--- I really dislike duplicating this everywhere but it makes
--lua-language-server able to deduce the type of Object:new calls and that
--is honestly worth it
return types.Object.new(cls, ...)
end
--- Initialize a new AuraTrigger object
---@param spellId number
---@param own boolean
---@param requiredCount number | nil
---@param invert boolean | nil
function AuraTrigger:Init(spellId, own, requiredCount, invert)
Trigger.Init(self, invert)
self.spellId = spellId
self.requiredCount = requiredCount or 1
self.own = own
self.count = 0
end
--- Reset the AuraTrigger to the default state
function AuraTrigger:Reset()
self.count = 0
self:SetState(self.count >= self.requiredCount)
end
---Check if a given aura matches this AuraTrigger
---@param aura UnitAuraInfo
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
---@param aura UnitAuraInfo
function AuraTrigger:AddAura(aura)
if not self:IsMatching(aura) then
return
end
self.count = self.count + 1
self:SetState(self.count >= self.requiredCount)
end
---Inform the trigger about an updated aura
---@param before UnitAuraInfo
---@param after UnitAuraInfo
function AuraTrigger:UpdateAura(before, after)
--
end
---Inform the trigger about an aura that got removed
---@param aura UnitAuraInfo
function AuraTrigger:RemoveAura(aura)
if not self:IsMatching(aura) then
return
end
self.count = self.count - 1
self:SetState(self.count >= self.requiredCount)
end
+67
View File
@@ -0,0 +1,67 @@
-- 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")
--- Triggers objects provide a link between indicators and some data source. They
-- can pass data from the data source into the indicator. This is a supertype for
-- all triggers and should not be constructed on its own.
---@class Trigger: Object
---@field active boolean
---@field invert boolean
local Trigger = types.CreateClass("Trigger")
types.Trigger = Trigger
--- Initialize a new Trigger object
---@param invert boolean|nil
function Trigger:Init(invert)
invert = invert or false
self.invert = invert
self.active = invert
end
--- Set the target of the Trigger.
-- The target will get notified every time the trigger changes state. If the trigger
-- is currently active when the target is set, it will get notified immediately.
---@param target Indicator
function Trigger:SetTarget(target)
self.indicator = target
end
---Set the state of the trigger. Will activate the indicator if needed.
---@param data table<string,any> | nil
---@param state boolean
function Trigger:SetState(state, data)
state = state ~= self.invert -- state xor inverted
if self.active ~= state then
if state then
self.indicator:Show(data)
else
self.indicator:Hide()
end
self.active = state
end
end
-- Reset the trigger to the default state. If this changes the activation of the
-- trigger then the target will be notified.
function Trigger:Reset()
self:SetState(false)
end