diff --git a/Assets/TEngine/Scripts/Runtime/Core/Event/GameEvent.cs b/Assets/TEngine/Scripts/Runtime/Core/Event/GameEvent.cs index 627479b2..7fcaa675 100644 --- a/Assets/TEngine/Scripts/Runtime/Core/Event/GameEvent.cs +++ b/Assets/TEngine/Scripts/Runtime/Core/Event/GameEvent.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; namespace TEngine.Runtime { #region EventInfo + internal interface IEventInfo { void Free(); @@ -15,11 +16,7 @@ namespace TEngine.Runtime public Action Actions => _actions; - public int EventCount - { - set; - get; - } + public int EventCount { set; get; } public void AddAction(Action action) { @@ -45,18 +42,13 @@ namespace TEngine.Runtime } } - public class EventInfo : IEventInfo, IMemory { private Action _actions; public Action Actions => _actions; - public int EventCount - { - set; - get; - } + public int EventCount { set; get; } public void AddAction(Action action) { @@ -88,11 +80,7 @@ namespace TEngine.Runtime public Action Actions => _actions; - public int EventCount - { - set; - get; - } + public int EventCount { set; get; } public void AddAction(Action action) { @@ -124,11 +112,7 @@ namespace TEngine.Runtime public Action Actions => _actions; - public int EventCount - { - set; - get; - } + public int EventCount { set; get; } public void AddAction(Action action) { @@ -153,6 +137,71 @@ namespace TEngine.Runtime MemoryPool.Release(this); } } + + public class EventInfo : IEventInfo, IMemory + { + private Action _actions; + + public Action Actions => _actions; + + public int EventCount { set; get; } + + public void AddAction(Action action) + { + _actions += action; + EventCount++; + } + + public void RmvAction(Action action) + { + _actions -= action; + EventCount--; + } + + public void Clear() + { + EventCount = 0; + _actions = null; + } + + public void Free() + { + MemoryPool.Release(this); + } + } + + public class EventInfo : IEventInfo, IMemory + { + private Action _actions; + + public Action Actions => _actions; + + public int EventCount { set; get; } + + public void AddAction(Action action) + { + _actions += action; + EventCount++; + } + + public void RmvAction(Action action) + { + _actions -= action; + EventCount--; + } + + public void Clear() + { + EventCount = 0; + _actions = null; + } + + public void Free() + { + MemoryPool.Release(this); + } + } + #endregion public class GameEvent : IMemory @@ -173,6 +222,7 @@ namespace TEngine.Runtime private Dictionary m_eventStrDic = new Dictionary(); #region AddEventListener + public void AddEventListener(int eventId, Action action) { if (_eventDic.ContainsKey(eventId)) @@ -239,6 +289,50 @@ namespace TEngine.Runtime } } + public void AddEventListener(int eventId, Action action) + { + if (_eventDic.ContainsKey(eventId)) + { + var eventInfo = _eventDic[eventId] as EventInfo; + if (eventInfo != null) + { + eventInfo.AddAction(action); + } + else + { + throw new Exception("The Same GameEventId AddEventListener Need Same Args"); + } + } + else + { + var eventInfo = MemoryPool.Acquire>(); + eventInfo.AddAction(action); + _eventDic.Add(eventId, eventInfo); + } + } + + public void AddEventListener(int eventId, Action action) + { + if (_eventDic.ContainsKey(eventId)) + { + var eventInfo = _eventDic[eventId] as EventInfo; + if (eventInfo != null) + { + eventInfo.AddAction(action); + } + else + { + throw new Exception("The Same GameEventId AddEventListener Need Same Args"); + } + } + else + { + var eventInfo = MemoryPool.Acquire>(); + eventInfo.AddAction(action); + _eventDic.Add(eventId, eventInfo); + } + } + public void AddEventListener(int eventId, Action action) { if (_eventDic.ContainsKey(eventId)) @@ -260,9 +354,11 @@ namespace TEngine.Runtime _eventDic.Add(eventId, eventInfo); } } + #endregion #region RemoveEventListener + public void RemoveEventListener(int eventId, Action action) { if (action == null) @@ -341,6 +437,58 @@ namespace TEngine.Runtime } } + public void RemoveEventListener(int eventId, Action action) + { + if (action == null) + { + return; + } + + if (_eventDic.ContainsKey(eventId)) + { + var eventInfo = _eventDic[eventId] as EventInfo; + if (eventInfo != null) + { + eventInfo.RmvAction(action); + if (eventInfo.EventCount <= 0) + { + _eventDic.Remove(eventId); + MemoryPool.Release(eventInfo); + } + } + else + { + throw new Exception("The Same GameEventId RemoveEventListener Need Same Args"); + } + } + } + + public void RemoveEventListener(int eventId, Action action) + { + if (action == null) + { + return; + } + + if (_eventDic.ContainsKey(eventId)) + { + var eventInfo = _eventDic[eventId] as EventInfo; + if (eventInfo != null) + { + eventInfo.RmvAction(action); + if (eventInfo.EventCount <= 0) + { + _eventDic.Remove(eventId); + MemoryPool.Release(eventInfo); + } + } + else + { + throw new Exception("The Same GameEventId RemoveEventListener Need Same Args"); + } + } + } + public void RemoveEventListener(int eventId, Action action) { if (action == null) @@ -366,9 +514,11 @@ namespace TEngine.Runtime } } } + #endregion #region Send + public void Send(int eventId, T info) { if (_eventDic.ContainsKey(eventId)) @@ -405,6 +555,30 @@ namespace TEngine.Runtime } } + public void Send(int eventId, T info, U info2, W info3, X info4) + { + if (_eventDic.ContainsKey(eventId)) + { + var eventInfo = _eventDic[eventId] as EventInfo; + if (eventInfo != null) + { + eventInfo.Actions?.Invoke(info, info2, info3, info4); + } + } + } + + public void Send(int eventId, T info, U info2, W info3, X info4, Y Info5) + { + if (_eventDic.ContainsKey(eventId)) + { + var eventInfo = _eventDic[eventId] as EventInfo; + if (eventInfo != null) + { + eventInfo.Actions?.Invoke(info, info2, info3, info4, Info5); + } + } + } + public void Send(int eventId) { if (_eventDic.ContainsKey(eventId)) @@ -416,10 +590,13 @@ namespace TEngine.Runtime } } } + #endregion #region StringEvent + #region AddEventListener + public void AddEventListener(string eventId, Action action) { if (m_eventStrDic.ContainsKey(eventId)) @@ -507,9 +684,11 @@ namespace TEngine.Runtime m_eventStrDic.Add(eventId, eventInfo); } } + #endregion #region RemoveEventListener + public void RemoveEventListener(string eventId, Action action) { if (action == null) @@ -613,9 +792,11 @@ namespace TEngine.Runtime } } } + #endregion #region Send + public void Send(string eventId, T info) { if (m_eventStrDic.ContainsKey(eventId)) @@ -663,10 +844,13 @@ namespace TEngine.Runtime } } } + #endregion + #endregion #region Clear + public void Clear() { var etr = _eventDic.GetEnumerator(); @@ -675,6 +859,7 @@ namespace TEngine.Runtime var eventInfo = etr.Current.Value; eventInfo.Free(); } + etr.Dispose(); var etrStr = m_eventStrDic.GetEnumerator(); @@ -683,11 +868,13 @@ namespace TEngine.Runtime var eventInfo = etrStr.Current.Value; eventInfo.Free(); } + etrStr.Dispose(); _eventDic.Clear(); m_eventStrDic.Clear(); } + #endregion } } \ No newline at end of file