diff --git a/src/types/triggers/auratrigger.lua b/src/types/triggers/auratrigger.lua index 73dcace..3c33eb3 100644 --- a/src/types/triggers/auratrigger.lua +++ b/src/types/triggers/auratrigger.lua @@ -27,6 +27,23 @@ local Trigger = types.Trigger local AuraTrigger = types.CreateClass("AuraTrigger", Trigger) types.AuraTrigger = AuraTrigger + +---Creates a new AuraTrigger from a config description and attaches it to the +---correct data source +---@param unit UnitFrame +---@param config table +---@return AuraTrigger +function AuraTrigger.CreateFromConfig(unit, config) + local trigger = AuraTrigger:new( + config.spellId, + config.own, + config.requiredCount, + config.invert + ) + unit.auras:AddTrigger(trigger) + return trigger +end + ---@return AuraTrigger function AuraTrigger.new(cls, ...) --- I really dislike duplicating this everywhere but it makes diff --git a/src/types/triggers/multitrigger.lua b/src/types/triggers/multitrigger.lua index db4571f..025ee92 100644 --- a/src/types/triggers/multitrigger.lua +++ b/src/types/triggers/multitrigger.lua @@ -30,6 +30,20 @@ local Trigger = types.Trigger local MultiTrigger = types.CreateClass("MultiTrigger", Trigger) types.MultiTrigger = MultiTrigger +---Creates a new MultiTrigger from a config description +---@param unit UnitFrame +---@param config table +---@return MultiTrigger +function MultiTrigger.CreateFromConfig(unit, config) + local trigger = MultiTrigger:new(config.invert) + for _, childConfig in ipairs(config.children) do + local childType = types[childConfig.kind] + local child = childType.CreateFromConfig(unit, childConfig) + trigger:AddTrigger(child) + end + return trigger +end + --- Initialize a new Trigger object ---@param invert boolean|nil function MultiTrigger:Init(invert) diff --git a/src/types/triggers/statustrigger.lua b/src/types/triggers/statustrigger.lua index 7c2e188..c04249a 100644 --- a/src/types/triggers/statustrigger.lua +++ b/src/types/triggers/statustrigger.lua @@ -25,6 +25,16 @@ local Trigger = types.Trigger local StatusTrigger = types.CreateClass("StatusTrigger", Trigger) types.StatusTrigger = StatusTrigger +---Creates a new StatusTrigger from a config description +---@param unit UnitFrame +---@param config table +function StatusTrigger.CreateFromConfig(unit, config) + local trigger = StatusTrigger:new(config.status, config.requiredCount, + config.invert) + unit.auras:AddTrigger(trigger) + return trigger +end + ---@return StatusTrigger function StatusTrigger.new(cls, ...) --- I really dislike duplicating this everywhere but it makes diff --git a/src/types/triggers/trigger.lua b/src/types/triggers/trigger.lua index cb13da4..700684b 100644 --- a/src/types/triggers/trigger.lua +++ b/src/types/triggers/trigger.lua @@ -26,6 +26,14 @@ local types = omi.GetModule("types") local Trigger = types.CreateClass("Trigger") types.Trigger = Trigger + +---@param unit UnitFrame +---@param config table +---@return Trigger +function Trigger.CreateFromConfig(unit, config) + error("Must override") +end + --- Initialize a new Trigger object ---@param invert boolean|nil function Trigger:Init(invert) diff --git a/src/types/unitframe.lua b/src/types/unitframe.lua index 9fc58bf..3cda7c6 100644 --- a/src/types/unitframe.lua +++ b/src/types/unitframe.lua @@ -29,12 +29,6 @@ local SquareIndicator = types.SquareIndicator ---@class SquareIndicator local BorderIndicator = types.BorderIndicator ----@class AuraTrigger -local AuraTrigger = types.AuraTrigger - ----@class StatusTrigger -local StatusTrigger = types.StatusTrigger - ---@class UnitFrame local UnitFrame = types.CreateClass("UnitFrame") types.UnitFrame = UnitFrame @@ -97,27 +91,15 @@ function UnitFrame:Init(unit, config) end function UnitFrame:CreateTriggers(triggers) - for _, trigger in ipairs(triggers) do - local kind = trigger.kind - local indicator = self:CreateIndicator(trigger.indicator) - if kind == "AuraTrigger" then - local at = AuraTrigger:new( - trigger.spellId, - trigger.own, - trigger.requiredCount, - trigger.invert - ) - at:SetTarget(indicator) - self.auras:AddTrigger(at) - elseif kind == "StatusTrigger" then - local st = StatusTrigger:new(trigger.status, trigger.requiredCount, trigger.invert) - st:SetTarget(indicator) - self.auras:AddTrigger(st) - end + for _, config in ipairs(triggers) do + local indicator = self:CreateIndicator(config.indicator) + ---@type Trigger + local triggerType = types[config.kind] + local trigger = triggerType.CreateFromConfig(self, config) + trigger:SetTarget(indicator) end end - function UnitFrame:CreateIndicator(indicator) local kind = indicator.kind if kind == "SquareIndicator" then