diff --git a/Assets/TEngine/Runtime/GameEvent.meta b/Assets/TEngine/Runtime/GameEvent.meta new file mode 100644 index 00000000..96601af2 --- /dev/null +++ b/Assets/TEngine/Runtime/GameEvent.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6400c6e6b97944a1b45a942c00c18236 +timeCreated: 1680273952 \ No newline at end of file diff --git a/Assets/TEngine/Runtime/GameEvent/EventDispatcher.cs b/Assets/TEngine/Runtime/GameEvent/EventDispatcher.cs new file mode 100644 index 00000000..f4db72d8 --- /dev/null +++ b/Assets/TEngine/Runtime/GameEvent/EventDispatcher.cs @@ -0,0 +1,265 @@ +using System; +using System.Collections.Generic; + +namespace TEngine +{ + class EventDelegateData + { + private int m_eventType = 0; + public List m_listExist = new List(); + private List m_addList = new List(); + private List m_deleteList = new List(); + private bool m_isExcute = false; + private bool m_dirty = false; + + public EventDelegateData(int evnetType) + { + m_eventType = evnetType; + } + + public bool AddHandler(Delegate handler) + { + if (m_listExist.Contains(handler)) + { + Log.Fatal("Repeated Add Handler"); + return false; + } + + if (m_isExcute) + { + m_dirty = true; + m_addList.Add(handler); + } + else + { + m_listExist.Add(handler); + } + + return true; + } + + public void RmvHandler(Delegate hander) + { + if (m_isExcute) + { + m_dirty = true; + m_deleteList.Add(hander); + } + else + { + if (!m_listExist.Remove(hander)) + { + Log.Fatal("Delete handle failed, not exist, EventId: {0}", StringId.HashToString(m_eventType)); + } + } + } + + private void CheckModify() + { + m_isExcute = false; + if (m_dirty) + { + for (int i = 0; i < m_addList.Count; i++) + { + m_listExist.Add(m_addList[i]); + } + + m_addList.Clear(); + + for (int i = 0; i < m_deleteList.Count; i++) + { + m_listExist.Remove(m_deleteList[i]); + } + + m_deleteList.Clear(); + } + } + + public void Callback() + { + m_isExcute = true; + for (var i = 0; i < m_listExist.Count; i++) + { + var d = m_listExist[i]; + Action action = d as Action; + if (action != null) + { + action(); + } + } + + CheckModify(); + } + + public void Callback(T arg1) + { + m_isExcute = true; + for (var i = 0; i < m_listExist.Count; i++) + { + var d = m_listExist[i]; + var action = d as Action; + if (action != null) + { + action(arg1); + } + } + + CheckModify(); + } + + public void Callback(T arg1, U arg2) + { + m_isExcute = true; + for (var i = 0; i < m_listExist.Count; i++) + { + var d = m_listExist[i]; + var action = d as Action; + if (action != null) + { + action(arg1, arg2); + } + } + + CheckModify(); + } + + public void Callback(T arg1, U arg2, V arg3) + { + m_isExcute = true; + for (var i = 0; i < m_listExist.Count; i++) + { + var d = m_listExist[i]; + var action = d as Action; + if (action != null) + { + action(arg1, arg2, arg3); + } + } + + CheckModify(); + } + + public void Callback(T arg1, U arg2, V arg3, W arg4) + { + m_isExcute = true; + for (var i = 0; i < m_listExist.Count; i++) + { + var d = m_listExist[i]; + var action = d as Action; + if (action != null) + { + action(arg1, arg2, arg3, arg4); + } + } + + CheckModify(); + } + + public void Callback(T arg1, U arg2, V arg3, W arg4, X arg5) + { + m_isExcute = true; + for (var i = 0; i < m_listExist.Count; i++) + { + var d = m_listExist[i]; + var action = d as Action; + if (action != null) + { + action(arg1, arg2, arg3, arg4, arg5); + } + } + + CheckModify(); + } + } + + /// + /// 封装消息的底层分发和注册 + /// + class EventDispatcher + { + static Dictionary m_eventTable = new Dictionary(); + + #region 事件管理接口 + + public bool AddEventListener(int eventType, Delegate handler) + { + EventDelegateData data; + if (!m_eventTable.TryGetValue(eventType, out data)) + { + data = new EventDelegateData(eventType); + m_eventTable.Add(eventType, data); + } + + return data.AddHandler(handler); + } + + public void RemoveEventListener(int eventType, Delegate handler) + { + EventDelegateData data; + if (m_eventTable.TryGetValue(eventType, out data)) + { + data.RmvHandler(handler); + } + } + + #endregion + + #region 事件分发接口 + + public void Send(int eventType) + { + EventDelegateData d; + if (m_eventTable.TryGetValue(eventType, out d)) + { + d.Callback(); + } + } + + public void Send(int eventType, T arg1) + { + EventDelegateData d; + if (m_eventTable.TryGetValue(eventType, out d)) + { + d.Callback(arg1); + } + } + + public void Send(int eventType, T arg1, U arg2) + { + EventDelegateData d; + if (m_eventTable.TryGetValue(eventType, out d)) + { + d.Callback(arg1, arg2); + } + } + + public void Send(int eventType, T arg1, U arg2, V arg3) + { + EventDelegateData d; + if (m_eventTable.TryGetValue(eventType, out d)) + { + d.Callback(arg1, arg2, arg3); + } + } + + public void Send(int eventType, T arg1, U arg2, V arg3, W arg4) + { + EventDelegateData d; + if (m_eventTable.TryGetValue(eventType, out d)) + { + d.Callback(arg1, arg2, arg3, arg4); + } + } + + public void Send(int eventType, T arg1, U arg2, V arg3, W arg4, X arg5) + { + EventDelegateData d; + if (m_eventTable.TryGetValue(eventType, out d)) + { + d.Callback(arg1, arg2, arg3, arg4, arg5); + } + } + + #endregion + } +} \ No newline at end of file diff --git a/Assets/TEngine/Runtime/GameEvent/EventDispatcher.cs.meta b/Assets/TEngine/Runtime/GameEvent/EventDispatcher.cs.meta new file mode 100644 index 00000000..c2282474 --- /dev/null +++ b/Assets/TEngine/Runtime/GameEvent/EventDispatcher.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ccd33d1eef8948bdba477efbcffd7c08 +timeCreated: 1680273952 \ No newline at end of file diff --git a/Assets/TEngine/Runtime/GameEvent/EventInterfaceAttribute.cs b/Assets/TEngine/Runtime/GameEvent/EventInterfaceAttribute.cs new file mode 100644 index 00000000..96de17c1 --- /dev/null +++ b/Assets/TEngine/Runtime/GameEvent/EventInterfaceAttribute.cs @@ -0,0 +1,27 @@ +using System; + +namespace TEngine +{ + public enum EEventGroup + { + /// + /// UI相关的交互 + /// + GroupUI, + + /// + /// 逻辑层内部相关的交互 + /// + GroupLogic, + } + + [System.AttributeUsage(System.AttributeTargets.Interface)] + public class EventInterface : Attribute + { + public EEventGroup mGroup; + public EventInterface(EEventGroup group) + { + mGroup = group; + } + } +} diff --git a/Assets/TEngine/Runtime/GameEvent/EventInterfaceAttribute.cs.meta b/Assets/TEngine/Runtime/GameEvent/EventInterfaceAttribute.cs.meta new file mode 100644 index 00000000..c18daec7 --- /dev/null +++ b/Assets/TEngine/Runtime/GameEvent/EventInterfaceAttribute.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: cf6ccd1a939b4a219ceeb1e3053a4a93 +timeCreated: 1680273952 \ No newline at end of file diff --git a/Assets/TEngine/Runtime/GameEvent/EventMgr.cs b/Assets/TEngine/Runtime/GameEvent/EventMgr.cs new file mode 100644 index 00000000..9b8108e6 --- /dev/null +++ b/Assets/TEngine/Runtime/GameEvent/EventMgr.cs @@ -0,0 +1,50 @@ +using System.Collections.Generic; + +namespace TEngine +{ + internal class EventEntryData + { + public object InterfaceWrap; + }; + + class EventMgr + { + private EventDispatcher m_dispatcher = new EventDispatcher(); + + /// + /// 封装了调用的代理函数 + /// + private Dictionary m_entry = new Dictionary(); + + public T GetInterface() + { + string typeName = typeof(T).FullName; + EventEntryData entry; + if (m_entry.TryGetValue(typeName, out entry)) + { + return (T)entry.InterfaceWrap; + } + + return default(T); + } + + /// + /// 注册wrap的函数 + /// + /// + /// + public void RegWrapInterface(T callerWrap) + { + string typeName = typeof(T).FullName; + + var entry = new EventEntryData(); + entry.InterfaceWrap = callerWrap; + m_entry.Add(typeName, entry); + } + + public EventDispatcher GetDispatcher() + { + return m_dispatcher; + } + } +} \ No newline at end of file diff --git a/Assets/TEngine/Runtime/GameEvent/EventMgr.cs.meta b/Assets/TEngine/Runtime/GameEvent/EventMgr.cs.meta new file mode 100644 index 00000000..4e4571f2 --- /dev/null +++ b/Assets/TEngine/Runtime/GameEvent/EventMgr.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 1fa2a6a1b01543238f031ac70df4d0ee +timeCreated: 1680273952 \ No newline at end of file diff --git a/Assets/TEngine/Runtime/GameEvent/GameEvent.cs b/Assets/TEngine/Runtime/GameEvent/GameEvent.cs new file mode 100644 index 00000000..ddeaa453 --- /dev/null +++ b/Assets/TEngine/Runtime/GameEvent/GameEvent.cs @@ -0,0 +1,227 @@ +using System; + +namespace TEngine +{ + public class GameEvent + { + private static EventMgr m_mgr = new EventMgr(); + + public static void Init() + { + + } + + #region 细分的注册接口 + + public static bool AddEventListener(int eventType, Action handler) + { + return m_mgr.GetDispatcher().AddEventListener(eventType, handler); + } + + public static bool AddEventListener(int eventType, Action handler) + { + return m_mgr.GetDispatcher().AddEventListener(eventType, handler); + } + + public static bool AddEventListener(int eventType, Action handler) + { + return m_mgr.GetDispatcher().AddEventListener(eventType, handler); + } + + public static bool AddEventListener(int eventType, Action handler) + { + return m_mgr.GetDispatcher().AddEventListener(eventType, handler); + } + + public static bool AddEventListener(int eventType, Action handler) + { + return m_mgr.GetDispatcher().AddEventListener(eventType, handler); + } + + public static bool AddEventListener(int eventType, Action handler) + { + return m_mgr.GetDispatcher().AddEventListener(eventType, handler); + } + + public static void RemoveEventListener(int eventType, Action handler) + { + m_mgr.GetDispatcher().RemoveEventListener(eventType, handler); + } + + public static void RemoveEventListener(int eventType, Action handler) + { + m_mgr.GetDispatcher().RemoveEventListener(eventType, handler); + } + + public static void RemoveEventListener(int eventType, Action handler) + { + m_mgr.GetDispatcher().RemoveEventListener(eventType, handler); + } + + public static void RemoveEventListener(int eventType, Action handler) + { + m_mgr.GetDispatcher().RemoveEventListener(eventType, handler); + } + + public static void RemoveEventListener(int eventType, Action handler) + { + m_mgr.GetDispatcher().RemoveEventListener(eventType, handler); + } + + public static void RemoveEventListener(int eventType, Action handler) + { + m_mgr.GetDispatcher().RemoveEventListener(eventType, handler); + } + + public static void RemoveEventListener(int eventType, Delegate handler) + { + m_mgr.GetDispatcher().RemoveEventListener(eventType, handler); + } + + //----------------------------string Event----------------------------// + public static bool AddEventListener(string eventType, Action handler) + { + return m_mgr.GetDispatcher().AddEventListener(StringId.StringToHash(eventType), handler); + } + + public static bool AddEventListener(string eventType, Action handler) + { + return m_mgr.GetDispatcher().AddEventListener(StringId.StringToHash(eventType), handler); + } + + public static bool AddEventListener(string eventType, Action handler) + { + return m_mgr.GetDispatcher().AddEventListener(StringId.StringToHash(eventType), handler); + } + + public static bool AddEventListener(string eventType, Action handler) + { + return m_mgr.GetDispatcher().AddEventListener(StringId.StringToHash(eventType), handler); + } + + public static bool AddEventListener(string eventType, Action handler) + { + return m_mgr.GetDispatcher().AddEventListener(StringId.StringToHash(eventType), handler); + } + + public static bool AddEventListener(string eventType, Action handler) + { + return m_mgr.GetDispatcher().AddEventListener(StringId.StringToHash(eventType), handler); + } + + public static void RemoveEventListener(string eventType, Action handler) + { + m_mgr.GetDispatcher().RemoveEventListener(StringId.StringToHash(eventType), handler); + } + + public static void RemoveEventListener(string eventType, Action handler) + { + m_mgr.GetDispatcher().RemoveEventListener(StringId.StringToHash(eventType), handler); + } + + public static void RemoveEventListener(string eventType, Action handler) + { + m_mgr.GetDispatcher().RemoveEventListener(StringId.StringToHash(eventType), handler); + } + + public static void RemoveEventListener(string eventType, Action handler) + { + m_mgr.GetDispatcher().RemoveEventListener(StringId.StringToHash(eventType), handler); + } + + public static void RemoveEventListener(string eventType, Action handler) + { + m_mgr.GetDispatcher().RemoveEventListener(StringId.StringToHash(eventType), handler); + } + + public static void RemoveEventListener(string eventType, Action handler) + { + m_mgr.GetDispatcher().RemoveEventListener(StringId.StringToHash(eventType), handler); + } + + public static void RemoveEventListener(string eventType, Delegate handler) + { + m_mgr.GetDispatcher().RemoveEventListener(StringId.StringToHash(eventType), handler); + } + #endregion + + #region 分发消息接口 + + public static T Get() + { + return m_mgr.GetInterface(); + } + + public static void Send(int eventType) + { + m_mgr.GetDispatcher().Send(eventType); + } + + public static void Send(int eventType, T arg1) + { + m_mgr.GetDispatcher().Send(eventType, arg1); + } + + public static void Send(int eventType, T arg1, U arg2) + { + m_mgr.GetDispatcher().Send(eventType, arg1, arg2); + } + + public static void Send(int eventType, T arg1, U arg2, V arg3) + { + m_mgr.GetDispatcher().Send(eventType, arg1, arg2, arg3); + } + + public static void Send(int eventType, T arg1, U arg2, V arg3, W arg4) + { + m_mgr.GetDispatcher().Send(eventType, arg1, arg2, arg3); + } + + public static void Send(int eventType, T arg1, U arg2, V arg3, W arg4, X arg5) + { + m_mgr.GetDispatcher().Send(eventType, arg1, arg2, arg3, arg4, arg5); + } + + public static void Send(int eventType, Delegate handler) + { + m_mgr.GetDispatcher().Send(eventType, handler); + } + + //-------------------------------string Send-------------------------------// + public static void Send(string eventType) + { + m_mgr.GetDispatcher().Send(StringId.StringToHash(eventType)); + } + + public static void Send(string eventType, T arg1) + { + m_mgr.GetDispatcher().Send(StringId.StringToHash(eventType), arg1); + } + + public static void Send(string eventType, T arg1, U arg2) + { + m_mgr.GetDispatcher().Send(StringId.StringToHash(eventType), arg1, arg2); + } + + public static void Send(string eventType, T arg1, U arg2, V arg3) + { + m_mgr.GetDispatcher().Send(StringId.StringToHash(eventType), arg1, arg2, arg3); + } + + public static void Send(string eventType, T arg1, U arg2, V arg3, W arg4) + { + m_mgr.GetDispatcher().Send(StringId.StringToHash(eventType), arg1, arg2, arg3); + } + + public static void Send(string eventType, T arg1, U arg2, V arg3, W arg4, X arg5) + { + m_mgr.GetDispatcher().Send(StringId.StringToHash(eventType), arg1, arg2, arg3, arg4, arg5); + } + + public static void Send(string eventType, Delegate handler) + { + m_mgr.GetDispatcher().Send(StringId.StringToHash(eventType), handler); + } + #endregion + } +} \ No newline at end of file diff --git a/Assets/TEngine/Runtime/GameEvent/GameEvent.cs.meta b/Assets/TEngine/Runtime/GameEvent/GameEvent.cs.meta new file mode 100644 index 00000000..908df5d8 --- /dev/null +++ b/Assets/TEngine/Runtime/GameEvent/GameEvent.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d6b9be7cbb8a4a7780502d317c3b3372 +timeCreated: 1680273952 \ No newline at end of file diff --git a/Assets/TEngine/Runtime/GameEvent/GameEventMgr.cs b/Assets/TEngine/Runtime/GameEvent/GameEventMgr.cs new file mode 100644 index 00000000..3a1afa3f --- /dev/null +++ b/Assets/TEngine/Runtime/GameEvent/GameEventMgr.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; + +namespace TEngine +{ + public class GameEventMgr : IMemory + { + private List m_listEventTypes; + private List m_listHandles; + private bool m_isInit = false; + + public GameEventMgr() + { + if (m_isInit) + { + return; + } + + m_isInit = true; + m_listEventTypes = new List(); + m_listHandles = new List(); + } + + public void Clear() + { + if (!m_isInit) + { + return; + } + + for (int i = 0; i < m_listEventTypes.Count; ++i) + { + var eventType = m_listEventTypes[i]; + var handle = m_listHandles[i]; + GameEvent.RemoveEventListener(eventType, handle); + } + + m_listEventTypes.Clear(); + m_listHandles.Clear(); + } + + private void AddEvent(int eventType, Delegate handler) + { + m_listEventTypes.Add(eventType); + m_listHandles.Add(handler); + } + + public void AddUIEvent(int eventType, Action handler) + { + if (GameEvent.AddEventListener(eventType, handler)) + { + AddEvent(eventType, handler); + } + } + + public void AddUIEvent(int eventType, Action handler) + { + if (GameEvent.AddEventListener(eventType, handler)) + { + AddEvent(eventType, handler); + } + } + + public void AddUIEvent(int eventType, Action handler) + { + if (GameEvent.AddEventListener(eventType, handler)) + { + AddEvent(eventType, handler); + } + } + + public void AddUIEvent(int eventType, Action handler) + { + if (GameEvent.AddEventListener(eventType, handler)) + { + AddEvent(eventType, handler); + } + } + + public void AddUIEvent(int eventType, Action handler) + { + if (GameEvent.AddEventListener(eventType, handler)) + { + AddEvent(eventType, handler); + } + } + } +} \ No newline at end of file diff --git a/Assets/TEngine/Runtime/GameEvent/GameEventMgr.cs.meta b/Assets/TEngine/Runtime/GameEvent/GameEventMgr.cs.meta new file mode 100644 index 00000000..3704cd00 --- /dev/null +++ b/Assets/TEngine/Runtime/GameEvent/GameEventMgr.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 09c516fcdf8d4b0196a2039ab847a094 +timeCreated: 1680273952 \ No newline at end of file diff --git a/Assets/TEngine/Runtime/GameEvent/StringId.cs b/Assets/TEngine/Runtime/GameEvent/StringId.cs new file mode 100644 index 00000000..f6a59644 --- /dev/null +++ b/Assets/TEngine/Runtime/GameEvent/StringId.cs @@ -0,0 +1,34 @@ +using System.Collections.Generic; + +namespace TEngine +{ + public class StringId + { + private static readonly Dictionary eventTypeHashMap = new Dictionary(); + private static readonly Dictionary eventHashToStringMap = new Dictionary(); + private static int _currentId = 0; + + public static int StringToHash(string val) + { + if (eventTypeHashMap.TryGetValue(val, out var hashId)) + { + return hashId; + } + + hashId = ++_currentId; + eventTypeHashMap[val] = hashId; + eventHashToStringMap[hashId] = val; + + return hashId; + } + + public static string HashToString(int hash) + { + if (eventHashToStringMap.TryGetValue(hash, out var value)) + { + return value; + } + return string.Empty; + } + } +} \ No newline at end of file diff --git a/Assets/TEngine/Runtime/GameEvent/StringId.cs.meta b/Assets/TEngine/Runtime/GameEvent/StringId.cs.meta new file mode 100644 index 00000000..f78fa3a9 --- /dev/null +++ b/Assets/TEngine/Runtime/GameEvent/StringId.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: af219e27c2e1424ea3823c0bc589301f +timeCreated: 1680273952 \ No newline at end of file