using System; using System.Collections.Generic; using UnityEngine; public static class EventMgr { public delegate void CoreEventDelegate(CoreEvent e); public delegate void CoreEventDelegate(CoreEvent e); public delegate void LuaCoreEventDelegate(LuaCoreEvent e); public delegate void LuaCoreEventDelegate(LuaCoreEvent e); static readonly Dictionary m_Delegates = new Dictionary(); static readonly Dictionary m_LuaDelegates = new Dictionary(); public static void AddEventListener(int type, CoreEventDelegate listener) { Delegate d; if (m_Delegates.TryGetValue(type, out d)) { m_Delegates[type] = Delegate.Combine(d, listener); } else { m_Delegates[type] = listener; } } public static void AddEventListener(int type, CoreEventDelegate listener) { Delegate d; if (m_Delegates.TryGetValue(type, out d)) { m_Delegates[type] = Delegate.Combine(d, listener); } else { m_Delegates[type] = listener; } } public static void RemoveEventListener(int type, CoreEventDelegate listener) { Delegate d; if (m_Delegates.TryGetValue(type, out d)) { Delegate currentDel = Delegate.Remove(d, listener); if (currentDel == null) { m_Delegates.Remove(type); } else { m_Delegates[type] = currentDel; } } } public static void RemoveEventListener(int type, CoreEventDelegate listener) { Delegate d; if (m_Delegates.TryGetValue(type, out d)) { Delegate currentDel = Delegate.Remove(d, listener); if (currentDel == null) { m_Delegates.Remove(type); } else { m_Delegates[type] = currentDel; } } } public static void AddLuaEventListenerArgs(string luaFunction, LuaCoreEventDelegate listener) { Delegate d; if (m_LuaDelegates.TryGetValue(luaFunction, out d)) { m_LuaDelegates[luaFunction] = Delegate.Combine(d, listener); } else { m_LuaDelegates[luaFunction] = listener; } } public static void RemoveLuaEventListenerArgs(string luaFunction, LuaCoreEventDelegate listener) { Delegate d; if (m_LuaDelegates.TryGetValue(luaFunction, out d)) { Delegate currentDel = Delegate.Remove(d, listener); if (currentDel == null) { m_LuaDelegates.Remove(luaFunction); } else { m_LuaDelegates[luaFunction] = currentDel; } } } public static void AddLuaEventListener(string luaFunction, LuaCoreEventDelegate listener) { Delegate d; if (m_LuaDelegates.TryGetValue(luaFunction, out d)) { m_LuaDelegates[luaFunction] = Delegate.Combine(d, listener); } else { m_LuaDelegates[luaFunction] = listener; } } public static void RemoveLuaEventListener(string luaFunction, LuaCoreEventDelegate listener) { Delegate d; if (m_LuaDelegates.TryGetValue(luaFunction, out d)) { Delegate currentDel = Delegate.Remove(d, listener); if (currentDel == null) { m_LuaDelegates.Remove(luaFunction); } else { m_LuaDelegates[luaFunction] = currentDel; } } } public static void RemoveLuaEventListener(int type) { if (m_Delegates.ContainsKey(type)) { m_Delegates.Remove(type); } } public static void DispatchEvent(CoreEvent e) { if (e == null) { throw new ArgumentNullException("e"); } Delegate d; if (m_Delegates.TryGetValue(e.EventType, out d)) { CoreEventDelegate callback = d as CoreEventDelegate; if (callback != null) { callback(e); } } } public static void DispatchEvent(CoreEvent e) { if (e == null) { throw new ArgumentNullException("e"); } Delegate d; if (m_Delegates.TryGetValue(e.EventType, out d)) { CoreEventDelegate callback = d as CoreEventDelegate; if (callback != null) { callback(e); } } } public static void LuaDispatchEvent(LuaCoreEvent e) { if (e == null) { throw new ArgumentNullException("e"); } Delegate d; if (m_LuaDelegates.TryGetValue(e.Func, out d)) { LuaCoreEventDelegate callback = d as LuaCoreEventDelegate; if (callback != null) { callback(e); } } } public static void LuaDispatchEvent(LuaCoreEvent e) { if (e == null) { throw new ArgumentNullException("e"); } Delegate d; if (m_LuaDelegates.TryGetValue(e.Func, out d)) { LuaCoreEventDelegate callback = d as LuaCoreEventDelegate; if (callback != null) { callback(e); } } } } public class CoreEvent { int m_EventType; public int EventType { get { return m_EventType; } } T m_Data; public T Data { get { return m_Data; } } T1 m_Data1; public T1 Data1 { get { return m_Data1; } } public CoreEvent(int type, T data,T1 data1) { m_EventType = type; m_Data = data; m_Data1 = data1; } } public class CoreEvent { int m_EventType; public int EventType { get { return m_EventType; } } T m_Data; public T Data { get { return m_Data; } } object mParam = null; public object Param { get { return mParam; } set { mParam = value; } } object mParam2 = null; public object Param2 { get { return mParam2; } set { mParam2 = value; } } public CoreEvent(int type, T data) { m_EventType = type; m_Data = data; } public void SetData(T data) { m_Data = data; } } public class LuaCoreEvent { string m_Func; public string Func { get { return m_Func; } } T m_Data; public T Data { get { return m_Data; } } public LuaCoreEvent(string luaFunc, T data) { m_Func = luaFunc; m_Data = data; } } public class LuaCoreEvent { string m_Func; public string Func { get { return m_Func; } } public LuaCoreEvent(string luaFunc) { m_Func = luaFunc; } }