InputUIEventProxy.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using UnityEngine.Events;
  5. using UnityEngine.EventSystems;
  6. using System;
  7. public class InputUIEventProxy : IInputProxyBase
  8. {
  9. #region 添加监听
  10. public static InputButtonClickRegisterInfo GetOnClickListener(Button button, string UIName, string ComponentName, string parm, InputEventHandle<InputUIOnClickEvent> callback)
  11. {
  12. InputButtonClickRegisterInfo info = HeapObjectPool<InputButtonClickRegisterInfo>.GetObject();
  13. info.eventKey = InputUIOnClickEvent.GetEventKey(UIName, ComponentName, parm);
  14. info.callBack = callback;
  15. info.m_button = button;
  16. info.m_OnClick = () =>
  17. {
  18. DispatchOnClickEvent(UIName, ComponentName, parm);
  19. };
  20. return info;
  21. }
  22. public static InputEventRegisterInfo<InputUILongPressEvent> GetLongPressListener(LongPressAcceptor acceptor, string UIName, string ComponentName, string parm, InputEventHandle<InputUILongPressEvent> callback)
  23. {
  24. InputlongPressRegisterInfo info = HeapObjectPool<InputlongPressRegisterInfo>.GetObject();
  25. info.eventKey = InputUILongPressEvent.GetEventKey(UIName, ComponentName, parm);
  26. info.callBack = callback;
  27. info.m_acceptor = acceptor;
  28. info.m_OnLongPress = (type) =>
  29. {
  30. DispatchLongPressEvent(UIName, ComponentName, parm, type);
  31. };
  32. return info;
  33. }
  34. internal static InputEventRegisterInfo<InputUIOnEndDragEvent> GetOnEndDragListener(string m_UIEventKey, string UIName, string ComponentName, InputEventHandle<InputUIOnEndDragEvent> callback)
  35. {
  36. InputEventRegisterInfo<InputUIOnEndDragEvent> info = HeapObjectPool<InputEventRegisterInfo<InputUIOnEndDragEvent>>.GetObject();
  37. info.eventKey = InputUIOnEndDragEvent.GetEventKey(UIName, ComponentName);
  38. info.callBack = callback;
  39. InputManager.AddListener(
  40. InputUIOnEndDragEvent.GetEventKey(UIName, ComponentName), callback);
  41. return info;
  42. }
  43. //internal static InputEventRegisterInfo<InputUIOnEndDragEvent> GetOnEndDragListener(string m_UIEventKey, string name, Action<PointerEventData> onEndDrag)
  44. //{
  45. // throw new NotImplementedException();
  46. //}
  47. internal static InputEventRegisterInfo<InputUIOnDragEvent> GetOnDragListener( string m_UIEventKey, string UIName, string ComponentName, InputEventHandle<InputUIOnDragEvent> callback)
  48. {
  49. InputEventRegisterInfo<InputUIOnDragEvent> info = HeapObjectPool<InputEventRegisterInfo<InputUIOnDragEvent>>.GetObject();
  50. info.eventKey = InputUIOnDragEvent.GetEventKey(UIName, ComponentName);
  51. info.callBack = callback;
  52. InputManager.AddListener(
  53. InputUIOnDragEvent.GetEventKey(UIName, ComponentName), callback);
  54. return info;
  55. }
  56. internal static InputEventRegisterInfo<InputUIOnBeginDragEvent> GetOnBeginDragListener(string m_UIEventKey, string UIName, string ComponentName, InputEventHandle<InputUIOnBeginDragEvent> callback)
  57. {
  58. InputEventRegisterInfo<InputUIOnBeginDragEvent> info = HeapObjectPool<InputEventRegisterInfo<InputUIOnBeginDragEvent>>.GetObject();
  59. info.eventKey = InputUIOnBeginDragEvent.GetEventKey(UIName, ComponentName);
  60. info.callBack = callback;
  61. InputManager.AddListener(
  62. InputUIOnBeginDragEvent.GetEventKey(UIName, ComponentName), callback);
  63. return info;
  64. }
  65. internal static InputEventRegisterInfo<InputUIOnMouseEvent> GetOnMouseListener(string m_UIEventKey, string UIName, string ComponentName,bool isDown, InputEventHandle<InputUIOnMouseEvent> callback)
  66. {
  67. InputEventRegisterInfo<InputUIOnMouseEvent> info = HeapObjectPool<InputEventRegisterInfo<InputUIOnMouseEvent>>.GetObject();
  68. info.eventKey = InputUIOnMouseEvent.GetEventKey(UIName, ComponentName, isDown);
  69. info.callBack = callback;
  70. InputManager.AddListener(
  71. InputUIOnMouseEvent.GetEventKey(UIName, ComponentName, isDown), callback);
  72. return info;
  73. }
  74. public static InputEventRegisterInfo<InputUIOnScrollEvent> GetOnScrollListener(string UIName, string ComponentName, InputEventHandle<InputUIOnScrollEvent> callback)
  75. {
  76. InputEventRegisterInfo<InputUIOnScrollEvent> info = HeapObjectPool<InputEventRegisterInfo<InputUIOnScrollEvent>>.GetObject();
  77. info.eventKey = InputUIOnScrollEvent.GetEventKey(UIName, ComponentName);
  78. info.callBack = callback;
  79. InputManager.AddListener(
  80. InputUIOnScrollEvent.GetEventKey(UIName, ComponentName), callback);
  81. return info;
  82. }
  83. public static InputEventRegisterInfo<InputUIOnDragEvent> GetOnDragListener(DragAcceptor acceptor, string UIName, string ComponentName, string parm, InputEventHandle<InputUIOnDragEvent> callback)
  84. {
  85. InputDragRegisterInfo info = HeapObjectPool<InputDragRegisterInfo>.GetObject();
  86. info.eventKey = InputUIOnDragEvent.GetEventKey(UIName, ComponentName);
  87. info.callBack = callback;
  88. info.m_acceptor = acceptor;
  89. info.m_OnDrag = (data) =>
  90. {
  91. DispatchDragEvent(UIName, ComponentName, parm, data);
  92. };
  93. return info;
  94. }
  95. public static InputBeginDragRegisterInfo GetOnBeginDragListener(DragAcceptor acceptor, string UIName, string ComponentName, string parm, InputEventHandle<InputUIOnBeginDragEvent> callback)
  96. {
  97. InputBeginDragRegisterInfo info = HeapObjectPool<InputBeginDragRegisterInfo>.GetObject();
  98. info.eventKey = InputUIOnBeginDragEvent.GetEventKey(UIName, ComponentName);
  99. info.callBack = callback;
  100. info.m_acceptor = acceptor;
  101. info.m_OnBeginDrag = (data) =>
  102. {
  103. DispatchBeginDragEvent(UIName, ComponentName, parm, data);
  104. };
  105. return info;
  106. }
  107. public static InputEndDragRegisterInfo GetOnEndDragListener(DragAcceptor acceptor, string UIName, string ComponentName, string parm, InputEventHandle<InputUIOnEndDragEvent> callback)
  108. {
  109. InputEndDragRegisterInfo info = HeapObjectPool<InputEndDragRegisterInfo>.GetObject();
  110. info.eventKey = InputUIOnEndDragEvent.GetEventKey(UIName, ComponentName);
  111. info.callBack = callback;
  112. info.m_acceptor = acceptor;
  113. info.m_OnEndDrag = (data) =>
  114. {
  115. DispatchEndDragEvent(UIName, ComponentName, parm, data);
  116. };
  117. return info;
  118. }
  119. #endregion
  120. #region 事件派发
  121. //public static void DispatchUIEvent<T>(string UIName, string ComponentName, string parm) where T : InputUIEventBase, new()
  122. //{
  123. // //只有允许输入时才派发事件
  124. // if (IsActive)
  125. // {
  126. // T eventTmp = GetUIEvent<T>(UIName, ComponentName, parm);
  127. // InputManager.Dispatch<T>(eventTmp);
  128. // }
  129. //}
  130. public static void DispatchOnClickEvent(string UIName, string ComponentName, string parm)
  131. {
  132. //只有允许输入时才派发事件
  133. if (IsActive)
  134. {
  135. InputUIOnClickEvent e = GetUIEvent<InputUIOnClickEvent>(UIName, ComponentName, parm);
  136. InputManager.Dispatch("InputUIOnClickEvent", e);
  137. }
  138. }
  139. public static void DispatchLongPressEvent(string UIName, string ComponentName, string parm, InputUIEventType type)
  140. {
  141. //只有允许输入时才派发事件
  142. if (IsActive)
  143. {
  144. InputUILongPressEvent e = GetUIEvent<InputUILongPressEvent>(UIName, ComponentName, parm);
  145. e.m_type = type;
  146. //e.m_LongPressType = type;
  147. e.EventKey = InputUILongPressEvent.GetEventKey(UIName, ComponentName, parm);
  148. InputManager.Dispatch("InputUILongPressEvent",e);
  149. }
  150. }
  151. public static void DispatchScrollEvent(string UIName, string ComponentName, string parm, Vector2 position)
  152. {
  153. //只有允许输入时才派发事件
  154. if (IsActive)
  155. {
  156. InputUIOnScrollEvent e = GetOnScrollEvent(UIName, ComponentName, parm, position);
  157. InputManager.Dispatch("InputUIOnScrollEvent",e);
  158. }
  159. }
  160. public static void DispatchDragEvent(string UIName, string ComponentName,string parm, PointerEventData data)
  161. {
  162. //只有允许输入时才派发事件
  163. if (IsActive)
  164. {
  165. InputUIOnDragEvent e = GetUIEvent<InputUIOnDragEvent>(UIName, ComponentName, parm);
  166. e.m_dragPosition = data.position;
  167. e.m_delta = data.delta;
  168. InputManager.Dispatch("InputUIOnDragEvent",e);
  169. }
  170. }
  171. public static void DispatchBeginDragEvent(string UIName, string ComponentName, string parm, PointerEventData data)
  172. {
  173. //只有允许输入时才派发事件
  174. if (IsActive)
  175. {
  176. InputUIOnBeginDragEvent e = GetUIEvent<InputUIOnBeginDragEvent>(UIName, ComponentName, parm);
  177. e.m_dragPosition = data.position;
  178. e.m_delta = data.delta;
  179. InputManager.Dispatch("InputUIOnBeginDragEvent",e);
  180. }
  181. }
  182. public static void DispatchMouseEvent(string UIName, string ComponentName, bool isDown, string parm)
  183. {
  184. //只有允许输入时才派发事件
  185. if (IsActive)
  186. {
  187. InputUIOnMouseEvent e = GetUIEvent<InputUIOnMouseEvent>(UIName, ComponentName, parm);
  188. e.m_isDown = isDown;
  189. e.m_type = isDown? InputUIEventType.PressDown:InputUIEventType.PressUp;
  190. InputManager.Dispatch("InputUIOnMouseEvent", e);
  191. }
  192. }
  193. public static void DispatchEndDragEvent(string UIName, string ComponentName, string parm , PointerEventData data)
  194. {
  195. //只有允许输入时才派发事件
  196. if (IsActive)
  197. {
  198. InputUIOnEndDragEvent e = GetUIEvent<InputUIOnEndDragEvent>(UIName, ComponentName, parm);
  199. e.m_dragPosition = data.position;
  200. e.m_delta = data.delta;
  201. InputManager.Dispatch("InputUIOnEndDragEvent",e);
  202. }
  203. }
  204. #endregion
  205. #region 事件池
  206. static T GetUIEvent<T>(string UIName, string ComponentName, string parm) where T:InputUIEventBase,new()
  207. {
  208. T msg = HeapObjectPool<T>.GetObject();
  209. msg.Reset();
  210. msg.m_name = UIName;
  211. msg.m_compName = ComponentName;
  212. msg.m_pram = parm;
  213. return msg;
  214. }
  215. static InputUIOnScrollEvent GetOnScrollEvent(string UIName, string ComponentName, string parm, Vector2 position)
  216. {
  217. InputUIOnScrollEvent msg = GetUIEvent<InputUIOnScrollEvent>(UIName, ComponentName,parm);
  218. msg.m_pos = position;
  219. return msg;
  220. }
  221. #endregion
  222. }
  223. public class InputButtonClickRegisterInfo : InputEventRegisterInfo<InputUIOnClickEvent>
  224. {
  225. public Button m_button;
  226. public UnityAction m_OnClick;
  227. public override void RemoveListener()
  228. {
  229. base.RemoveListener();
  230. m_button.onClick.RemoveListener(m_OnClick);
  231. }
  232. public override void AddListener()
  233. {
  234. base.AddListener();
  235. m_button.onClick.AddListener(m_OnClick);
  236. }
  237. }
  238. public class InputlongPressRegisterInfo : InputEventRegisterInfo<InputUILongPressEvent>
  239. {
  240. public LongPressAcceptor m_acceptor;
  241. public InputUIEventLongPressCallBack m_OnLongPress;
  242. public override void RemoveListener()
  243. {
  244. base.RemoveListener();
  245. m_acceptor.OnLongPress -= m_OnLongPress;
  246. }
  247. public override void AddListener()
  248. {
  249. base.AddListener();
  250. m_acceptor.OnLongPress += m_OnLongPress;
  251. }
  252. }
  253. public class InputDragRegisterInfo : InputEventRegisterInfo<InputUIOnDragEvent>
  254. {
  255. public DragAcceptor m_acceptor;
  256. public InputUIEventDragCallBack m_OnDrag;
  257. public override void AddListener()
  258. {
  259. base.AddListener();
  260. m_acceptor.m_OnDrag += m_OnDrag;
  261. }
  262. public override void RemoveListener()
  263. {
  264. base.RemoveListener();
  265. m_acceptor.m_OnDrag -= m_OnDrag;
  266. }
  267. }
  268. public class InputBeginDragRegisterInfo : InputEventRegisterInfo<InputUIOnBeginDragEvent>
  269. {
  270. public DragAcceptor m_acceptor;
  271. public InputUIEventDragCallBack m_OnBeginDrag;
  272. public override void AddListener()
  273. {
  274. base.AddListener();
  275. m_acceptor.m_OnBeginDrag += m_OnBeginDrag;
  276. }
  277. public override void RemoveListener()
  278. {
  279. base.RemoveListener();
  280. m_acceptor.m_OnBeginDrag -= m_OnBeginDrag;
  281. }
  282. }
  283. public class InputEndDragRegisterInfo : InputEventRegisterInfo<InputUIOnEndDragEvent>
  284. {
  285. public DragAcceptor m_acceptor;
  286. public InputUIEventDragCallBack m_OnEndDrag;
  287. public override void AddListener()
  288. {
  289. base.AddListener();
  290. m_acceptor.m_OnEndDrag += m_OnEndDrag;
  291. }
  292. public override void RemoveListener()
  293. {
  294. base.RemoveListener();
  295. m_acceptor.m_OnEndDrag -= m_OnEndDrag;
  296. }
  297. }