EnumEventSystem.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /****************************************************************************
  2. * Copyright (c) 2017 snowcold
  3. * Copyright (c) 2021.1 liangxie
  4. *
  5. * https://qframework.cn
  6. * https://github.com/liangxiegame/QFramework
  7. * https://gitee.com/liangxiegame/QFramework
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. ****************************************************************************/
  27. using UnityEngine;
  28. namespace QFramework
  29. {
  30. using System;
  31. using System.Collections.Generic;
  32. #region 事件接口
  33. public delegate void OnEvent(int key, params object[] param);
  34. #endregion
  35. public class QEventSystem : IPoolable,ISingleton
  36. {
  37. public static QEventSystem Instance => SingletonProperty<QEventSystem>.Instance;
  38. //private bool mIsRecycled = false;
  39. private readonly Dictionary<int, ListenerWrap> mAllListenerMap = new Dictionary<int, ListenerWrap>(50);
  40. public bool IsRecycled { get; set; }
  41. private QEventSystem() {}
  42. #region 内部结构
  43. private class ListenerWrap
  44. {
  45. private LinkedList<OnEvent> mEventList;
  46. public bool Fire(int key, params object[] param)
  47. {
  48. if (mEventList == null)
  49. {
  50. return false;
  51. }
  52. var next = mEventList.First;
  53. OnEvent call = null;
  54. LinkedListNode<OnEvent> nextCache = null;
  55. while (next != null)
  56. {
  57. call = next.Value;
  58. nextCache = next.Next;
  59. call(key, param);
  60. next = next.Next ?? nextCache;
  61. }
  62. return true;
  63. }
  64. public bool Add(OnEvent listener)
  65. {
  66. if (mEventList == null)
  67. {
  68. mEventList = new LinkedList<OnEvent>();
  69. }
  70. if (mEventList.Contains(listener))
  71. {
  72. return false;
  73. }
  74. mEventList.AddLast(listener);
  75. return true;
  76. }
  77. public void Remove(OnEvent listener)
  78. {
  79. if (mEventList == null)
  80. {
  81. return;
  82. }
  83. mEventList.Remove(listener);
  84. }
  85. public void RemoveAll()
  86. {
  87. if (mEventList == null)
  88. {
  89. return;
  90. }
  91. mEventList.Clear();
  92. }
  93. }
  94. #endregion
  95. #region 功能函数
  96. public bool Register<T>(T key, OnEvent fun) where T : IConvertible
  97. {
  98. var kv = key.ToInt32(null);
  99. ListenerWrap wrap;
  100. if (!mAllListenerMap.TryGetValue(kv, out wrap))
  101. {
  102. wrap = new ListenerWrap();
  103. mAllListenerMap.Add(kv, wrap);
  104. }
  105. if (wrap.Add(fun))
  106. {
  107. return true;
  108. }
  109. Debug.LogWarning("Already Register Same Event:" + key);
  110. return false;
  111. }
  112. public void UnRegister<T>(T key, OnEvent fun) where T : IConvertible
  113. {
  114. ListenerWrap wrap;
  115. if (mAllListenerMap.TryGetValue(key.ToInt32(null), out wrap))
  116. {
  117. wrap.Remove(fun);
  118. }
  119. }
  120. public void UnRegister<T>(T key) where T : IConvertible
  121. {
  122. ListenerWrap wrap;
  123. if (mAllListenerMap.TryGetValue(key.ToInt32(null), out wrap))
  124. {
  125. wrap.RemoveAll();
  126. wrap = null;
  127. mAllListenerMap.Remove(key.ToInt32(null));
  128. }
  129. }
  130. public bool Send<T>(T key, params object[] param) where T : IConvertible
  131. {
  132. int kv = key.ToInt32(null);
  133. ListenerWrap wrap;
  134. if (mAllListenerMap.TryGetValue(kv, out wrap))
  135. {
  136. return wrap.Fire(kv, param);
  137. }
  138. return false;
  139. }
  140. public void OnRecycled()
  141. {
  142. mAllListenerMap.Clear();
  143. }
  144. #endregion
  145. #region 高频率使用的API
  146. public static bool SendEvent<T>(T key,params object[] param) where T : IConvertible
  147. {
  148. return Instance.Send(key, param);
  149. }
  150. public static bool RegisterEvent<T>(T key, OnEvent fun) where T : IConvertible
  151. {
  152. return Instance.Register(key, fun);
  153. }
  154. public static void UnRegisterEvent<T>(T key, OnEvent fun) where T : IConvertible
  155. {
  156. Instance.UnRegister(key, fun);
  157. }
  158. #endregion
  159. public void OnSingletonInit()
  160. {
  161. }
  162. }
  163. }