2025-01-04 23:00:19 +08:00

109 lines
4.0 KiB
Lua

UGCEventSystem = UGCEventSystem or {}
UGCEventSystem.Events = {}
-- 添加监听
function UGCEventSystem.AddListener(EventType, Func, Object)
if EventType == nil or Func == nil or type(Func) ~= "function" then
UGCLogSystem.LogError("[UGCEventSystem_AddListener] EventType or Func is nil!")
return
end
local FuncData = {}
FuncData.Object = Object
FuncData.Func = Func
if UGCEventSystem.Events[EventType]==nil then
local NewEventFuncs={}
table.insert(NewEventFuncs ,FuncData)
UGCEventSystem.Events[EventType] = NewEventFuncs
UGCLogSystem.Log("[UGCEventSystem:AddListener] EventType[%s], Func[%s]", tostring(EventType), tostring(Func))
else
table.insert(UGCEventSystem.Events[EventType], FuncData)
UGCLogSystem.Log("[UGCEventSystem:AddListener] EventType[%s], Func[%s]", tostring(EventType), tostring(Func))
end
end
-- 移除监听
function UGCEventSystem.RemoveListener(EventType, Func, Object)
if EventType == nil or Func == nil then
UGCLogSystem.LogError("[UGCEventSystem_RemoveListener] EventType or Func is nil!")
return
end
if UGCEventSystem.Events[EventType] ~= nil then
for i = #UGCEventSystem.Events[EventType], 1, -1 do
local FuncData = UGCEventSystem.Events[EventType][i]
if FuncData.Func == Func and FuncData.Object == Object then
local result = table.removeKey(UGCEventSystem.Events[EventType], i)
UGCLogSystem.Log("[UGCEventSystem_RemoveListener] EventType[%s], Func[%s]", tostring(EventType), tostring(result))
break
end
end
else
UGCLogSystem.LogError("[UGCEventSystem_RemoveListener] EventFuncs[%s] is nil!", tostring(EventType))
end
end
-- 派发事件
function UGCEventSystem.SendEvent(EventType, ...)
print(string.format("[UGCEventSystem_SendEvent] EventType: %s", tostring(EventType)))
if EventType ~= nil then
local EventFuncs = UGCEventSystem.Events[EventType];
if EventFuncs ~= nil then
for i, FuncData in pairs(EventFuncs) do
if FuncData.Object ~= nil then
FuncData.Func(FuncData.Object, ...)
else
FuncData.Func(...)
end
end
else
UGCLogSystem.LogError("[UGCEventSystem_SendEvent] EventFuncs[%s] is nil!", tostring(EventType))
end
end
end
--- SetTimer
---@param Obj UObject
---@param Fun fun()
---@param IntevalSeconds float
---@return TimerHandle
function UGCEventSystem.SetTimer(Obj, Fun, IntevalSeconds)
UGCLogSystem.Log("[UGCEventSystem_SetTimer] 设置定时器 %s:%s", UE.GetPathName(Obj), tostring(Fun))
local TimerDelegate = ObjectExtend.CreateDelegate(Obj, Fun, Obj)
local Handle = KismetSystemLibrary.K2_SetTimerDelegateForLua(TimerDelegate, Obj, IntevalSeconds, false)
local R = {}
R.Object = Obj
R.Handle = Handle
return R
end
--- SetTimerLoop
---@param Obj UObject
---@param Fun fun()
---@param IntevalSeconds float
---@return TimerHandle
function UGCEventSystem.SetTimerLoop(Obj, Fun, IntevalSeconds)
local TimerDelegate = ObjectExtend.CreateDelegate(Obj, Fun, Obj);
local Handle = KismetSystemLibrary.K2_SetTimerDelegateForLua(TimerDelegate, Obj, IntevalSeconds, true);
local R = {};
R.Object = Obj;
R.Handle = Handle;
UGCLogSystem.Log("[UGCEventSystem_SetTimerLoop] 设置循环定时器 %s:%s H:%s", UE.GetPathName(Obj), tostring(Fun), tostring(R));
return R;
end
--- StopTimer
---@param HandlePair TimerHandle
function UGCEventSystem.StopTimer(HandlePair)
if HandlePair == nil then
UGCLogSystem.LogError("[UGCEventSystem_StopTimer] HandlePair is nil")
else
UGCLogSystem.Log("[UGCEventSystem_StopTimer] 停止定时器 Object:%s Handle:%s", UE.GetPathName(HandlePair.Object), tostring(HandlePair.Handle));
KismetSystemLibrary.K2_ClearTimerHandle(HandlePair.Object, HandlePair.Handle);
HandlePair = nil
end
end
return UGCEventSystem