ObservableEventTrigger.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // for uGUI(from 4.6)
  2. #if !(UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5)
  3. using System; // require keep for Windows Universal App
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. namespace UniRx.Triggers
  7. {
  8. [DisallowMultipleComponent]
  9. public class ObservableEventTrigger : ObservableTriggerBase, IEventSystemHandler, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler, IPointerClickHandler, IBeginDragHandler, IInitializePotentialDragHandler, IDragHandler, IEndDragHandler, IDropHandler, IScrollHandler, IUpdateSelectedHandler, ISelectHandler, IDeselectHandler, IMoveHandler, ISubmitHandler, ICancelHandler
  10. {
  11. #region IDeselectHandler
  12. Subject<BaseEventData> onDeselect;
  13. void IDeselectHandler.OnDeselect(BaseEventData eventData)
  14. {
  15. if (onDeselect != null) onDeselect.OnNext(eventData);
  16. }
  17. public IObservable<BaseEventData> OnDeselectAsObservable()
  18. {
  19. return onDeselect ?? (onDeselect = new Subject<BaseEventData>());
  20. }
  21. #endregion
  22. #region IMoveHandler
  23. Subject<AxisEventData> onMove;
  24. void IMoveHandler.OnMove(AxisEventData eventData)
  25. {
  26. if (onMove != null) onMove.OnNext(eventData);
  27. }
  28. public IObservable<AxisEventData> OnMoveAsObservable()
  29. {
  30. return onMove ?? (onMove = new Subject<AxisEventData>());
  31. }
  32. #endregion
  33. #region IPointerDownHandler
  34. Subject<PointerEventData> onPointerDown;
  35. void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
  36. {
  37. if (onPointerDown != null) onPointerDown.OnNext(eventData);
  38. }
  39. public IObservable<PointerEventData> OnPointerDownAsObservable()
  40. {
  41. return onPointerDown ?? (onPointerDown = new Subject<PointerEventData>());
  42. }
  43. #endregion
  44. #region IPointerEnterHandler
  45. Subject<PointerEventData> onPointerEnter;
  46. void IPointerEnterHandler.OnPointerEnter(PointerEventData eventData)
  47. {
  48. if (onPointerEnter != null) onPointerEnter.OnNext(eventData);
  49. }
  50. public IObservable<PointerEventData> OnPointerEnterAsObservable()
  51. {
  52. return onPointerEnter ?? (onPointerEnter = new Subject<PointerEventData>());
  53. }
  54. #endregion
  55. #region IPointerExitHandler
  56. Subject<PointerEventData> onPointerExit;
  57. void IPointerExitHandler.OnPointerExit(PointerEventData eventData)
  58. {
  59. if (onPointerExit != null) onPointerExit.OnNext(eventData);
  60. }
  61. public IObservable<PointerEventData> OnPointerExitAsObservable()
  62. {
  63. return onPointerExit ?? (onPointerExit = new Subject<PointerEventData>());
  64. }
  65. #endregion
  66. #region IPointerUpHandler
  67. Subject<PointerEventData> onPointerUp;
  68. void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
  69. {
  70. if (onPointerUp != null) onPointerUp.OnNext(eventData);
  71. }
  72. public IObservable<PointerEventData> OnPointerUpAsObservable()
  73. {
  74. return onPointerUp ?? (onPointerUp = new Subject<PointerEventData>());
  75. }
  76. #endregion
  77. #region ISelectHandler
  78. Subject<BaseEventData> onSelect;
  79. void ISelectHandler.OnSelect(BaseEventData eventData)
  80. {
  81. if (onSelect != null) onSelect.OnNext(eventData);
  82. }
  83. public IObservable<BaseEventData> OnSelectAsObservable()
  84. {
  85. return onSelect ?? (onSelect = new Subject<BaseEventData>());
  86. }
  87. #endregion
  88. #region IPointerClickHandler
  89. Subject<PointerEventData> onPointerClick;
  90. void IPointerClickHandler.OnPointerClick(PointerEventData eventData)
  91. {
  92. if (onPointerClick != null) onPointerClick.OnNext(eventData);
  93. }
  94. public IObservable<PointerEventData> OnPointerClickAsObservable()
  95. {
  96. return onPointerClick ?? (onPointerClick = new Subject<PointerEventData>());
  97. }
  98. #endregion
  99. #region ISubmitHandler
  100. Subject<BaseEventData> onSubmit;
  101. void ISubmitHandler.OnSubmit(BaseEventData eventData)
  102. {
  103. if (onSubmit != null) onSubmit.OnNext(eventData);
  104. }
  105. public IObservable<BaseEventData> OnSubmitAsObservable()
  106. {
  107. return onSubmit ?? (onSubmit = new Subject<BaseEventData>());
  108. }
  109. #endregion
  110. #region IDragHandler
  111. Subject<PointerEventData> onDrag;
  112. void IDragHandler.OnDrag(PointerEventData eventData)
  113. {
  114. if (onDrag != null) onDrag.OnNext(eventData);
  115. }
  116. public IObservable<PointerEventData> OnDragAsObservable()
  117. {
  118. return onDrag ?? (onDrag = new Subject<PointerEventData>());
  119. }
  120. #endregion
  121. #region IBeginDragHandler
  122. Subject<PointerEventData> onBeginDrag;
  123. void IBeginDragHandler.OnBeginDrag(PointerEventData eventData)
  124. {
  125. if (onBeginDrag != null) onBeginDrag.OnNext(eventData);
  126. }
  127. public IObservable<PointerEventData> OnBeginDragAsObservable()
  128. {
  129. return onBeginDrag ?? (onBeginDrag = new Subject<PointerEventData>());
  130. }
  131. #endregion
  132. #region IEndDragHandler
  133. Subject<PointerEventData> onEndDrag;
  134. void IEndDragHandler.OnEndDrag(PointerEventData eventData)
  135. {
  136. if (onEndDrag != null) onEndDrag.OnNext(eventData);
  137. }
  138. public IObservable<PointerEventData> OnEndDragAsObservable()
  139. {
  140. return onEndDrag ?? (onEndDrag = new Subject<PointerEventData>());
  141. }
  142. #endregion
  143. #region IDropHandler
  144. Subject<PointerEventData> onDrop;
  145. void IDropHandler.OnDrop(PointerEventData eventData)
  146. {
  147. if (onDrop != null) onDrop.OnNext(eventData);
  148. }
  149. public IObservable<PointerEventData> OnDropAsObservable()
  150. {
  151. return onDrop ?? (onDrop = new Subject<PointerEventData>());
  152. }
  153. #endregion
  154. #region IUpdateSelectedHandler
  155. Subject<BaseEventData> onUpdateSelected;
  156. void IUpdateSelectedHandler.OnUpdateSelected(BaseEventData eventData)
  157. {
  158. if (onUpdateSelected != null) onUpdateSelected.OnNext(eventData);
  159. }
  160. public IObservable<BaseEventData> OnUpdateSelectedAsObservable()
  161. {
  162. return onUpdateSelected ?? (onUpdateSelected = new Subject<BaseEventData>());
  163. }
  164. #endregion
  165. #region IInitializePotentialDragHandler
  166. Subject<PointerEventData> onInitializePotentialDrag;
  167. void IInitializePotentialDragHandler.OnInitializePotentialDrag(PointerEventData eventData)
  168. {
  169. if (onInitializePotentialDrag != null) onInitializePotentialDrag.OnNext(eventData);
  170. }
  171. public IObservable<PointerEventData> OnInitializePotentialDragAsObservable()
  172. {
  173. return onInitializePotentialDrag ?? (onInitializePotentialDrag = new Subject<PointerEventData>());
  174. }
  175. #endregion
  176. #region ICancelHandler
  177. Subject<BaseEventData> onCancel;
  178. void ICancelHandler.OnCancel(BaseEventData eventData)
  179. {
  180. if (onCancel != null) onCancel.OnNext(eventData);
  181. }
  182. public IObservable<BaseEventData> OnCancelAsObservable()
  183. {
  184. return onCancel ?? (onCancel = new Subject<BaseEventData>());
  185. }
  186. #endregion
  187. #region IScrollHandler
  188. Subject<PointerEventData> onScroll;
  189. void IScrollHandler.OnScroll(PointerEventData eventData)
  190. {
  191. if (onScroll != null) onScroll.OnNext(eventData);
  192. }
  193. public IObservable<PointerEventData> OnScrollAsObservable()
  194. {
  195. return onScroll ?? (onScroll = new Subject<PointerEventData>());
  196. }
  197. #endregion
  198. protected override void RaiseOnCompletedOnDestroy()
  199. {
  200. if (onDeselect != null)
  201. {
  202. onDeselect.OnCompleted();
  203. }
  204. if (onMove != null)
  205. {
  206. onMove.OnCompleted();
  207. }
  208. if (onPointerDown != null)
  209. {
  210. onPointerDown.OnCompleted();
  211. }
  212. if (onPointerEnter != null)
  213. {
  214. onPointerEnter.OnCompleted();
  215. }
  216. if (onPointerExit != null)
  217. {
  218. onPointerExit.OnCompleted();
  219. }
  220. if (onPointerUp != null)
  221. {
  222. onPointerUp.OnCompleted();
  223. }
  224. if (onSelect != null)
  225. {
  226. onSelect.OnCompleted();
  227. }
  228. if (onPointerClick != null)
  229. {
  230. onPointerClick.OnCompleted();
  231. }
  232. if (onSubmit != null)
  233. {
  234. onSubmit.OnCompleted();
  235. }
  236. if (onDrag != null)
  237. {
  238. onDrag.OnCompleted();
  239. }
  240. if (onBeginDrag != null)
  241. {
  242. onBeginDrag.OnCompleted();
  243. }
  244. if (onEndDrag != null)
  245. {
  246. onEndDrag.OnCompleted();
  247. }
  248. if (onDrop != null)
  249. {
  250. onDrop.OnCompleted();
  251. }
  252. if (onUpdateSelected != null)
  253. {
  254. onUpdateSelected.OnCompleted();
  255. }
  256. if (onInitializePotentialDrag != null)
  257. {
  258. onInitializePotentialDrag.OnCompleted();
  259. }
  260. if (onCancel != null)
  261. {
  262. onCancel.OnCompleted();
  263. }
  264. if (onScroll != null)
  265. {
  266. onScroll.OnCompleted();
  267. }
  268. }
  269. }
  270. }
  271. #endif