PGRData/Script/matrix/xredpoint/XRedPointEvent.lua

180 lines
4.4 KiB
Lua
Raw Normal View History

--[[--红点事件个体类
RedPointEvent.id Id
RedPointEvent.conditionGroup XRedPointConditionGroup
RedPointEvent.listener XRedPointListener
RedPointEvent.node
]]
--
2024-09-01 20:49:41 +00:00
---@class XRedPointEvent 红点事件个体类
---@field
local XRedPointEvent = XClass(nil, "XRedPointEvent")
2024-09-01 20:49:41 +00:00
---@type XObjectPool
local RedPointConditionGroupPool
---@type XObjectPool
local RedPointListenerPool
local EventHandler = function(method, eventId)
return function(obj, ...)
return method(obj, eventId, ...)
end
end
--构造
2024-09-01 20:49:41 +00:00
function XRedPointEvent:Ctor()
end
function XRedPointEvent:Init(id, node, conditionGroup, listener, func, args)
self.EventListener = {}
self.id = id
2024-09-01 20:49:41 +00:00
self.condition = RedPointConditionGroupPool:Create(conditionGroup)
self.listener = RedPointListenerPool:Create(listener, func)
self.node = node
self.args = args
2024-09-01 20:49:41 +00:00
self:AddConditionsChangeEvent()
self.checkExist = nil
if node.Exist then
self.checkExist = function() return node:Exist() end
else
local gameObject = node.GameObject or node.gameObject or node.Transform or node.transform
if gameObject and gameObject.Exist then
self.checkExist = function() return gameObject:Exist() end
end
end
end
--检测红点条件
function XRedPointEvent:Check(args)
if not self:CheckNode() then
self:Release()
return
end
if self.condition then
--如果条件参数改变,则替换
if args then
self.args = args
end
--条件检测
local result = self.condition:Check(self.args)
--回调
if self.listener then
if self.listener.func then
self.listener:Call(result, self.args)
else
self.node.gameObject:SetActive(result >= 0)
end
end
end
end
--添加事件監聽
2024-09-01 20:49:41 +00:00
function XRedPointEvent:AddConditionsChangeEvent()
if not self.condition then
return
end
local events = self.condition.Events
if not events then
return
end
2024-09-01 20:49:41 +00:00
for _, var in pairs(events) do
2024-09-01 20:49:41 +00:00
local func = EventHandler(self.OnConditionChange, var.EventId)
XEventManager.AddEventListener(var.EventId, func, self)
self.EventListener[var.EventId] = func
end
end
--删除事件監聽
2024-09-01 20:49:41 +00:00
function XRedPointEvent:RemoveConditionsChangeEvent()
if not self.condition then
return
end
local events = self.condition.Events
if not events then
return
end
for _, var in pairs(events) do
2024-09-01 20:49:41 +00:00
local func = self.EventListener[var.EventId]
if func then
XEventManager.RemoveEventListener(var.EventId, func, self)
end
self.EventListener[var.EventId] = nil
end
end
--条件改变事件回调
2024-09-01 20:49:41 +00:00
function XRedPointEvent:OnConditionChange(eventId, args)
-- 分析参数
if self.condition and self.condition.Events and self.condition.Events[eventId] then
local element = self.condition.Events[eventId]
if element:Equal(eventId, args) then
self:Check()
return
end
end
if self.args == nil or args == nil then
self:Check(args)
elseif self.args == args and args ~= nil then
self:Check(args)
end
end
--检测是否已经被释放
function XRedPointEvent:CheckNode()
if self.checkExist == nil then
return false
end
if not self.checkExist() then
return false
end
return true
end
--释放
function XRedPointEvent:Release()
2024-09-01 20:49:41 +00:00
self:RemoveConditionsChangeEvent()
self.checkExist = nil
2024-09-01 20:49:41 +00:00
if self.listener then
self.listener:Release()
RedPointListenerPool:Recycle(self.listener)
self.listener = nil
end
self.node = nil
2024-09-01 20:49:41 +00:00
if self.condition then
self.condition:Release()
RedPointConditionGroupPool:Recycle(self.condition)
self.condition = nil
end
XRedPointManager.RemoveRedPointEventOnly(self.id)
2024-09-01 20:49:41 +00:00
for eventId, func in pairs(self.EventListener or {}) do
XEventManager.RemoveEventListener(eventId, func, self)
end
end
--导入时调用一次
local function OnRequire()
if not RedPointConditionGroupPool then
RedPointConditionGroupPool = XObjectPool.New(XRedPointConditionGroup.New)
end
if not RedPointListenerPool then
RedPointListenerPool = XObjectPool.New(XRedPointListener.New)
end
end
2024-09-01 20:49:41 +00:00
OnRequire()
return XRedPointEvent