GameEvent

GameEvent
This commit is contained in:
ALEXTANG
2023-03-31 22:50:05 +08:00
parent aff27b7e5d
commit 90a2398105
13 changed files with 712 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6400c6e6b97944a1b45a942c00c18236
timeCreated: 1680273952

View File

@@ -0,0 +1,265 @@
using System;
using System.Collections.Generic;
namespace TEngine
{
class EventDelegateData
{
private int m_eventType = 0;
public List<Delegate> m_listExist = new List<Delegate>();
private List<Delegate> m_addList = new List<Delegate>();
private List<Delegate> m_deleteList = new List<Delegate>();
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>(T arg1)
{
m_isExcute = true;
for (var i = 0; i < m_listExist.Count; i++)
{
var d = m_listExist[i];
var action = d as Action<T>;
if (action != null)
{
action(arg1);
}
}
CheckModify();
}
public void Callback<T, U>(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<T, U>;
if (action != null)
{
action(arg1, arg2);
}
}
CheckModify();
}
public void Callback<T, U, V>(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<T, U, V>;
if (action != null)
{
action(arg1, arg2, arg3);
}
}
CheckModify();
}
public void Callback<T, U, V, W>(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<T, U, V, W>;
if (action != null)
{
action(arg1, arg2, arg3, arg4);
}
}
CheckModify();
}
public void Callback<T, U, V, W, X>(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<T, U, V, W, X>;
if (action != null)
{
action(arg1, arg2, arg3, arg4, arg5);
}
}
CheckModify();
}
}
/// <summary>
/// 封装消息的底层分发和注册
/// </summary>
class EventDispatcher
{
static Dictionary<int, EventDelegateData> m_eventTable = new Dictionary<int, EventDelegateData>();
#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<T>(int eventType, T arg1)
{
EventDelegateData d;
if (m_eventTable.TryGetValue(eventType, out d))
{
d.Callback(arg1);
}
}
public void Send<T, U>(int eventType, T arg1, U arg2)
{
EventDelegateData d;
if (m_eventTable.TryGetValue(eventType, out d))
{
d.Callback(arg1, arg2);
}
}
public void Send<T, U, V>(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<T, U, V, W>(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<T, U, V, W, X>(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
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ccd33d1eef8948bdba477efbcffd7c08
timeCreated: 1680273952

View File

@@ -0,0 +1,27 @@
using System;
namespace TEngine
{
public enum EEventGroup
{
/// <summary>
/// UI相关的交互
/// </summary>
GroupUI,
/// <summary>
/// 逻辑层内部相关的交互
/// </summary>
GroupLogic,
}
[System.AttributeUsage(System.AttributeTargets.Interface)]
public class EventInterface : Attribute
{
public EEventGroup mGroup;
public EventInterface(EEventGroup group)
{
mGroup = group;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: cf6ccd1a939b4a219ceeb1e3053a4a93
timeCreated: 1680273952

View File

@@ -0,0 +1,50 @@
using System.Collections.Generic;
namespace TEngine
{
internal class EventEntryData
{
public object InterfaceWrap;
};
class EventMgr
{
private EventDispatcher m_dispatcher = new EventDispatcher();
/// <summary>
/// 封装了调用的代理函数
/// </summary>
private Dictionary<string, EventEntryData> m_entry = new Dictionary<string, EventEntryData>();
public T GetInterface<T>()
{
string typeName = typeof(T).FullName;
EventEntryData entry;
if (m_entry.TryGetValue(typeName, out entry))
{
return (T)entry.InterfaceWrap;
}
return default(T);
}
/// <summary>
/// 注册wrap的函数
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="callerWrap"></param>
public void RegWrapInterface<T>(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;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1fa2a6a1b01543238f031ac70df4d0ee
timeCreated: 1680273952

View File

@@ -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<T>(int eventType, Action<T> handler)
{
return m_mgr.GetDispatcher().AddEventListener(eventType, handler);
}
public static bool AddEventListener<T, U>(int eventType, Action<T, U> handler)
{
return m_mgr.GetDispatcher().AddEventListener(eventType, handler);
}
public static bool AddEventListener<T, U, V>(int eventType, Action<T, U, V> handler)
{
return m_mgr.GetDispatcher().AddEventListener(eventType, handler);
}
public static bool AddEventListener<T, U, V, W>(int eventType, Action<T, U, V, W> handler)
{
return m_mgr.GetDispatcher().AddEventListener(eventType, handler);
}
public static bool AddEventListener<T, U, V, W, X>(int eventType, Action<T, U, V, W, X> 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<T>(int eventType, Action<T> handler)
{
m_mgr.GetDispatcher().RemoveEventListener(eventType, handler);
}
public static void RemoveEventListener<T, U>(int eventType, Action<T, U> handler)
{
m_mgr.GetDispatcher().RemoveEventListener(eventType, handler);
}
public static void RemoveEventListener<T, U, V>(int eventType, Action<T, U, V> handler)
{
m_mgr.GetDispatcher().RemoveEventListener(eventType, handler);
}
public static void RemoveEventListener<T, U, V, W>(int eventType, Action<T, U, V, W> handler)
{
m_mgr.GetDispatcher().RemoveEventListener(eventType, handler);
}
public static void RemoveEventListener<T, U, V, W, X>(int eventType, Action<T, U, V, W, X> 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<T>(string eventType, Action<T> handler)
{
return m_mgr.GetDispatcher().AddEventListener(StringId.StringToHash(eventType), handler);
}
public static bool AddEventListener<T, U>(string eventType, Action<T, U> handler)
{
return m_mgr.GetDispatcher().AddEventListener(StringId.StringToHash(eventType), handler);
}
public static bool AddEventListener<T, U, V>(string eventType, Action<T, U, V> handler)
{
return m_mgr.GetDispatcher().AddEventListener(StringId.StringToHash(eventType), handler);
}
public static bool AddEventListener<T, U, V, W>(string eventType, Action<T, U, V, W> handler)
{
return m_mgr.GetDispatcher().AddEventListener(StringId.StringToHash(eventType), handler);
}
public static bool AddEventListener<T, U, V, W, X>(string eventType, Action<T, U, V, W, X> 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<T>(string eventType, Action<T> handler)
{
m_mgr.GetDispatcher().RemoveEventListener(StringId.StringToHash(eventType), handler);
}
public static void RemoveEventListener<T, U>(string eventType, Action<T, U> handler)
{
m_mgr.GetDispatcher().RemoveEventListener(StringId.StringToHash(eventType), handler);
}
public static void RemoveEventListener<T, U, V>(string eventType, Action<T, U, V> handler)
{
m_mgr.GetDispatcher().RemoveEventListener(StringId.StringToHash(eventType), handler);
}
public static void RemoveEventListener<T, U, V, W>(string eventType, Action<T, U, V, W> handler)
{
m_mgr.GetDispatcher().RemoveEventListener(StringId.StringToHash(eventType), handler);
}
public static void RemoveEventListener<T, U, V, W, X>(string eventType, Action<T, U, V, W, X> 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<T>()
{
return m_mgr.GetInterface<T>();
}
public static void Send(int eventType)
{
m_mgr.GetDispatcher().Send(eventType);
}
public static void Send<T>(int eventType, T arg1)
{
m_mgr.GetDispatcher().Send(eventType, arg1);
}
public static void Send<T, U>(int eventType, T arg1, U arg2)
{
m_mgr.GetDispatcher().Send(eventType, arg1, arg2);
}
public static void Send<T, U, V>(int eventType, T arg1, U arg2, V arg3)
{
m_mgr.GetDispatcher().Send(eventType, arg1, arg2, arg3);
}
public static void Send<T, U, V, W>(int eventType, T arg1, U arg2, V arg3, W arg4)
{
m_mgr.GetDispatcher().Send(eventType, arg1, arg2, arg3);
}
public static void Send<T, U, V, W, X>(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<T>(string eventType, T arg1)
{
m_mgr.GetDispatcher().Send(StringId.StringToHash(eventType), arg1);
}
public static void Send<T, U>(string eventType, T arg1, U arg2)
{
m_mgr.GetDispatcher().Send(StringId.StringToHash(eventType), arg1, arg2);
}
public static void Send<T, U, V>(string eventType, T arg1, U arg2, V arg3)
{
m_mgr.GetDispatcher().Send(StringId.StringToHash(eventType), arg1, arg2, arg3);
}
public static void Send<T, U, V, W>(string eventType, T arg1, U arg2, V arg3, W arg4)
{
m_mgr.GetDispatcher().Send(StringId.StringToHash(eventType), arg1, arg2, arg3);
}
public static void Send<T, U, V, W, X>(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
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d6b9be7cbb8a4a7780502d317c3b3372
timeCreated: 1680273952

View File

@@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
namespace TEngine
{
public class GameEventMgr : IMemory
{
private List<int> m_listEventTypes;
private List<Delegate> m_listHandles;
private bool m_isInit = false;
public GameEventMgr()
{
if (m_isInit)
{
return;
}
m_isInit = true;
m_listEventTypes = new List<int>();
m_listHandles = new List<Delegate>();
}
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<T>(int eventType, Action<T> handler)
{
if (GameEvent.AddEventListener(eventType, handler))
{
AddEvent(eventType, handler);
}
}
public void AddUIEvent<T, U>(int eventType, Action<T, U> handler)
{
if (GameEvent.AddEventListener(eventType, handler))
{
AddEvent(eventType, handler);
}
}
public void AddUIEvent<T, U, V>(int eventType, Action<T, U, V> handler)
{
if (GameEvent.AddEventListener(eventType, handler))
{
AddEvent(eventType, handler);
}
}
public void AddUIEvent<T, U, V, W>(int eventType, Action<T, U, V, W> handler)
{
if (GameEvent.AddEventListener(eventType, handler))
{
AddEvent(eventType, handler);
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 09c516fcdf8d4b0196a2039ab847a094
timeCreated: 1680273952

View File

@@ -0,0 +1,34 @@
using System.Collections.Generic;
namespace TEngine
{
public class StringId
{
private static readonly Dictionary<string, int> eventTypeHashMap = new Dictionary<string, int>();
private static readonly Dictionary<int, string> eventHashToStringMap = new Dictionary<int, string>();
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;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: af219e27c2e1424ea3823c0bc589301f
timeCreated: 1680273952