115 lines
4.4 KiB
Lua
115 lines
4.4 KiB
Lua
|
UGCEventSystem = UGCEventSystem or {}
|
||
|
UGCEventSystem.Events = {};
|
||
|
|
||
|
-- 添加监听
|
||
|
function UGCEventSystem.AddListener(EventType, Func, Object)
|
||
|
if UE_BUILD_DEBUG or WITH_ENGINE then
|
||
|
assert(EventType ~= nil, string.format("[UGCEventSystem.AddListener] EventType 为空,这是不被允许的"));
|
||
|
end
|
||
|
if 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 UE_BUILD_DEBUG or WITH_ENGINE then
|
||
|
assert(EventType ~= nil, string.format("[UGCEventSystem.RemoveListener] EventType 为空,这是不被允许的"));
|
||
|
end
|
||
|
if 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 UE_BUILD_DEBUG or WITH_ENGINE then
|
||
|
assert(EventType ~= nil, string.format("[UGCEventSystem.SendEvent] EventType 为空,这是不被允许的"));
|
||
|
end
|
||
|
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
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
--- SetTimer
|
||
|
---@param Obj UObject
|
||
|
---@param Fun fun()
|
||
|
---@param IntevalSeconds float
|
||
|
---@return FTimerHandle
|
||
|
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)
|
||
|
return {
|
||
|
Object = Obj,
|
||
|
Handle = Handle,
|
||
|
};
|
||
|
end
|
||
|
|
||
|
--- SetTimerLoop
|
||
|
---@param Obj UObject
|
||
|
---@param Fun fun()
|
||
|
---@param IntevalSeconds float
|
||
|
---@return FTimerHandle
|
||
|
function UGCEventSystem.SetTimerLoop(Obj, Fun, IntevalSeconds)
|
||
|
local TimerDelegate = ObjectExtend.CreateDelegate(Obj, Fun, Obj);
|
||
|
local Handle = KismetSystemLibrary.K2_SetTimerDelegateForLua(TimerDelegate, Obj, IntevalSeconds, true);
|
||
|
local R = {
|
||
|
Object = Obj,
|
||
|
Handle = Handle,
|
||
|
};
|
||
|
UGCLogSystem.Log("[UGCEventSystem:SetTimerLoop] 设置循环定时器 %s:%s H:%s", UE.GetPathName(Obj), tostring(Fun), tostring(R));
|
||
|
return R;
|
||
|
end
|
||
|
|
||
|
--- StopTimer
|
||
|
---@param HandlePair FTimerHandle
|
||
|
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
|