118 lines
4.5 KiB
Lua
118 lines
4.5 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.remove(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, ...)
|
||
|
-- UGCLogSystem.Log("[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.Log("[UGCEventSystem_SendEvent] EventFuncs[%s] is nil!", tostring(EventType))
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
--- SetTimer
|
||
|
---@param Obj UObject
|
||
|
---@param Fun fun()
|
||
|
---@param IntevalSeconds float
|
||
|
---@param Tips string|nil 提示文本:表示当前是哪个定时器
|
||
|
---@return TimerHandle
|
||
|
function UGCEventSystem.SetTimer(Obj, Fun, IntevalSeconds, Tips)
|
||
|
if Tips == nil then
|
||
|
UGCLogSystem.Log("[UGCEventSystem_SetTimer] 设置定时器 %s:%s", UE.GetPathName(Obj), tostring(Fun))
|
||
|
else
|
||
|
UGCLogSystem.Log("[UGCEventSystem_SetTimer] 设置定时器 %s:%s 提示:%s", UE.GetPathName(Obj), tostring(Fun), tostring(Tips));
|
||
|
end
|
||
|
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
|
||
|
---@param Tips string|nil 提示文本
|
||
|
---@return TimerHandle
|
||
|
function UGCEventSystem.SetTimerLoop(Obj, Fun, IntevalSeconds, Tips)
|
||
|
local TimerDelegate = ObjectExtend.CreateDelegate(Obj, Fun, Obj);
|
||
|
local Handle = KismetSystemLibrary.K2_SetTimerDelegateForLua(TimerDelegate, Obj, IntevalSeconds, true);
|
||
|
local R = {};
|
||
|
R.Object = Obj;
|
||
|
R.Handle = Handle;
|
||
|
|
||
|
if Tips == nil then
|
||
|
UGCLogSystem.Log("[UGCEventSystem_SetTimerLoop] 设置循环定时器 %s:%s H:%s", UE.GetPathName(Obj), tostring(Fun), tostring(R));
|
||
|
else
|
||
|
UGCLogSystem.Log("[UGCEventSystem_SetTimerLoop] 设置循环定时器 %s:%s H:%s, 提示:%s", UE.GetPathName(Obj), tostring(Fun), tostring(R), tostring(Tips));
|
||
|
end
|
||
|
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);
|
||
|
end
|
||
|
end
|
||
|
|
||
|
return UGCEventSystem
|