EventMgr.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public static class EventMgr
  5. {
  6. public delegate void CoreEventDelegate<T, T1>(CoreEvent<T, T1> e);
  7. public delegate void CoreEventDelegate<T>(CoreEvent<T> e);
  8. public delegate void LuaCoreEventDelegate<T>(LuaCoreEvent<T> e);
  9. public delegate void LuaCoreEventDelegate(LuaCoreEvent e);
  10. static readonly Dictionary<int, Delegate> m_Delegates = new Dictionary<int, Delegate>();
  11. static readonly Dictionary<string, Delegate> m_LuaDelegates = new Dictionary<string, Delegate>();
  12. public static void AddEventListener<T>(int type, CoreEventDelegate<T> listener)
  13. {
  14. Delegate d;
  15. if (m_Delegates.TryGetValue(type, out d))
  16. {
  17. m_Delegates[type] = Delegate.Combine(d, listener);
  18. }
  19. else
  20. {
  21. m_Delegates[type] = listener;
  22. }
  23. }
  24. public static void AddEventListener<T,T1>(int type, CoreEventDelegate<T,T1> listener)
  25. {
  26. Delegate d;
  27. if (m_Delegates.TryGetValue(type, out d))
  28. {
  29. m_Delegates[type] = Delegate.Combine(d, listener);
  30. }
  31. else
  32. {
  33. m_Delegates[type] = listener;
  34. }
  35. }
  36. public static void RemoveEventListener<T>(int type, CoreEventDelegate<T> listener)
  37. {
  38. Delegate d;
  39. if (m_Delegates.TryGetValue(type, out d))
  40. {
  41. Delegate currentDel = Delegate.Remove(d, listener);
  42. if (currentDel == null)
  43. {
  44. m_Delegates.Remove(type);
  45. }
  46. else
  47. {
  48. m_Delegates[type] = currentDel;
  49. }
  50. }
  51. }
  52. public static void RemoveEventListener<T,T1>(int type, CoreEventDelegate<T, T1> listener)
  53. {
  54. Delegate d;
  55. if (m_Delegates.TryGetValue(type, out d))
  56. {
  57. Delegate currentDel = Delegate.Remove(d, listener);
  58. if (currentDel == null)
  59. {
  60. m_Delegates.Remove(type);
  61. }
  62. else
  63. {
  64. m_Delegates[type] = currentDel;
  65. }
  66. }
  67. }
  68. public static void AddLuaEventListenerArgs<T>(string luaFunction, LuaCoreEventDelegate<T> listener)
  69. {
  70. Delegate d;
  71. if (m_LuaDelegates.TryGetValue(luaFunction, out d))
  72. {
  73. m_LuaDelegates[luaFunction] = Delegate.Combine(d, listener);
  74. }
  75. else
  76. {
  77. m_LuaDelegates[luaFunction] = listener;
  78. }
  79. }
  80. public static void RemoveLuaEventListenerArgs<T>(string luaFunction, LuaCoreEventDelegate<T> listener)
  81. {
  82. Delegate d;
  83. if (m_LuaDelegates.TryGetValue(luaFunction, out d))
  84. {
  85. Delegate currentDel = Delegate.Remove(d, listener);
  86. if (currentDel == null)
  87. {
  88. m_LuaDelegates.Remove(luaFunction);
  89. }
  90. else
  91. {
  92. m_LuaDelegates[luaFunction] = currentDel;
  93. }
  94. }
  95. }
  96. public static void AddLuaEventListener(string luaFunction, LuaCoreEventDelegate listener)
  97. {
  98. Delegate d;
  99. if (m_LuaDelegates.TryGetValue(luaFunction, out d))
  100. {
  101. m_LuaDelegates[luaFunction] = Delegate.Combine(d, listener);
  102. }
  103. else
  104. {
  105. m_LuaDelegates[luaFunction] = listener;
  106. }
  107. }
  108. public static void RemoveLuaEventListener(string luaFunction, LuaCoreEventDelegate listener)
  109. {
  110. Delegate d;
  111. if (m_LuaDelegates.TryGetValue(luaFunction, out d))
  112. {
  113. Delegate currentDel = Delegate.Remove(d, listener);
  114. if (currentDel == null)
  115. {
  116. m_LuaDelegates.Remove(luaFunction);
  117. }
  118. else
  119. {
  120. m_LuaDelegates[luaFunction] = currentDel;
  121. }
  122. }
  123. }
  124. public static void RemoveLuaEventListener<T>(int type)
  125. {
  126. if (m_Delegates.ContainsKey(type))
  127. {
  128. m_Delegates.Remove(type);
  129. }
  130. }
  131. public static void DispatchEvent<T>(CoreEvent<T> e)
  132. {
  133. if (e == null)
  134. {
  135. throw new ArgumentNullException("e");
  136. }
  137. Delegate d;
  138. if (m_Delegates.TryGetValue(e.EventType, out d))
  139. {
  140. CoreEventDelegate<T> callback = d as CoreEventDelegate<T>;
  141. if (callback != null)
  142. {
  143. callback(e);
  144. }
  145. }
  146. }
  147. public static void DispatchEvent<T,T1>(CoreEvent<T,T1> e)
  148. {
  149. if (e == null)
  150. {
  151. throw new ArgumentNullException("e");
  152. }
  153. Delegate d;
  154. if (m_Delegates.TryGetValue(e.EventType, out d))
  155. {
  156. CoreEventDelegate<T, T1> callback = d as CoreEventDelegate<T, T1>;
  157. if (callback != null)
  158. {
  159. callback(e);
  160. }
  161. }
  162. }
  163. public static void LuaDispatchEvent<T>(LuaCoreEvent<T> e)
  164. {
  165. if (e == null)
  166. {
  167. throw new ArgumentNullException("e");
  168. }
  169. Delegate d;
  170. if (m_LuaDelegates.TryGetValue(e.Func, out d))
  171. {
  172. LuaCoreEventDelegate<T> callback = d as LuaCoreEventDelegate<T>;
  173. if (callback != null)
  174. {
  175. callback(e);
  176. }
  177. }
  178. }
  179. public static void LuaDispatchEvent(LuaCoreEvent e)
  180. {
  181. if (e == null)
  182. {
  183. throw new ArgumentNullException("e");
  184. }
  185. Delegate d;
  186. if (m_LuaDelegates.TryGetValue(e.Func, out d))
  187. {
  188. LuaCoreEventDelegate callback = d as LuaCoreEventDelegate;
  189. if (callback != null)
  190. {
  191. callback(e);
  192. }
  193. }
  194. }
  195. }
  196. public class CoreEvent<T,T1>
  197. {
  198. int m_EventType;
  199. public int EventType
  200. {
  201. get { return m_EventType; }
  202. }
  203. T m_Data;
  204. public T Data
  205. {
  206. get { return m_Data; }
  207. }
  208. T1 m_Data1;
  209. public T1 Data1
  210. {
  211. get { return m_Data1; }
  212. }
  213. public CoreEvent(int type, T data,T1 data1)
  214. {
  215. m_EventType = type;
  216. m_Data = data;
  217. m_Data1 = data1;
  218. }
  219. }
  220. public class CoreEvent<T>
  221. {
  222. int m_EventType;
  223. public int EventType
  224. {
  225. get { return m_EventType; }
  226. }
  227. T m_Data;
  228. public T Data
  229. {
  230. get { return m_Data; }
  231. }
  232. object mParam = null;
  233. public object Param
  234. {
  235. get { return mParam; }
  236. set { mParam = value; }
  237. }
  238. object mParam2 = null;
  239. public object Param2
  240. {
  241. get { return mParam2; }
  242. set { mParam2 = value; }
  243. }
  244. public CoreEvent(int type, T data)
  245. {
  246. m_EventType = type;
  247. m_Data = data;
  248. }
  249. public void SetData(T data)
  250. {
  251. m_Data = data;
  252. }
  253. }
  254. public class LuaCoreEvent<T>
  255. {
  256. string m_Func;
  257. public string Func
  258. {
  259. get { return m_Func; }
  260. }
  261. T m_Data;
  262. public T Data
  263. {
  264. get { return m_Data; }
  265. }
  266. public LuaCoreEvent(string luaFunc, T data)
  267. {
  268. m_Func = luaFunc;
  269. m_Data = data;
  270. }
  271. }
  272. public class LuaCoreEvent
  273. {
  274. string m_Func;
  275. public string Func
  276. {
  277. get { return m_Func; }
  278. }
  279. public LuaCoreEvent(string luaFunc)
  280. {
  281. m_Func = luaFunc;
  282. }
  283. }