ObservableMouseTrigger.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_METRO)
  2. using System; // require keep for Windows Universal App
  3. using UnityEngine;
  4. namespace UniRx.Triggers
  5. {
  6. [DisallowMultipleComponent]
  7. public class ObservableMouseTrigger : ObservableTriggerBase
  8. {
  9. Subject<Unit> onMouseDown;
  10. /// <summary>OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider.</summary>
  11. void OnMouseDown()
  12. {
  13. if (onMouseDown != null) onMouseDown.OnNext(Unit.Default);
  14. }
  15. /// <summary>OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider.</summary>
  16. public IObservable<Unit> OnMouseDownAsObservable()
  17. {
  18. return onMouseDown ?? (onMouseDown = new Subject<Unit>());
  19. }
  20. Subject<Unit> onMouseDrag;
  21. /// <summary>OnMouseDrag is called when the user has clicked on a GUIElement or Collider and is still holding down the mouse.</summary>
  22. void OnMouseDrag()
  23. {
  24. if (onMouseDrag != null) onMouseDrag.OnNext(Unit.Default);
  25. }
  26. /// <summary>OnMouseDrag is called when the user has clicked on a GUIElement or Collider and is still holding down the mouse.</summary>
  27. public IObservable<Unit> OnMouseDragAsObservable()
  28. {
  29. return onMouseDrag ?? (onMouseDrag = new Subject<Unit>());
  30. }
  31. Subject<Unit> onMouseEnter;
  32. /// <summary>OnMouseEnter is called when the mouse entered the GUIElement or Collider.</summary>
  33. void OnMouseEnter()
  34. {
  35. if (onMouseEnter != null) onMouseEnter.OnNext(Unit.Default);
  36. }
  37. /// <summary>OnMouseEnter is called when the mouse entered the GUIElement or Collider.</summary>
  38. public IObservable<Unit> OnMouseEnterAsObservable()
  39. {
  40. return onMouseEnter ?? (onMouseEnter = new Subject<Unit>());
  41. }
  42. Subject<Unit> onMouseExit;
  43. /// <summary>OnMouseExit is called when the mouse is not any longer over the GUIElement or Collider.</summary>
  44. void OnMouseExit()
  45. {
  46. if (onMouseExit != null) onMouseExit.OnNext(Unit.Default);
  47. }
  48. /// <summary>OnMouseExit is called when the mouse is not any longer over the GUIElement or Collider.</summary>
  49. public IObservable<Unit> OnMouseExitAsObservable()
  50. {
  51. return onMouseExit ?? (onMouseExit = new Subject<Unit>());
  52. }
  53. Subject<Unit> onMouseOver;
  54. /// <summary>OnMouseOver is called every frame while the mouse is over the GUIElement or Collider.</summary>
  55. void OnMouseOver()
  56. {
  57. if (onMouseOver != null) onMouseOver.OnNext(Unit.Default);
  58. }
  59. /// <summary>OnMouseOver is called every frame while the mouse is over the GUIElement or Collider.</summary>
  60. public IObservable<Unit> OnMouseOverAsObservable()
  61. {
  62. return onMouseOver ?? (onMouseOver = new Subject<Unit>());
  63. }
  64. Subject<Unit> onMouseUp;
  65. /// <summary>OnMouseUp is called when the user has released the mouse button.</summary>
  66. void OnMouseUp()
  67. {
  68. if (onMouseUp != null) onMouseUp.OnNext(Unit.Default);
  69. }
  70. /// <summary>OnMouseUp is called when the user has released the mouse button.</summary>
  71. public IObservable<Unit> OnMouseUpAsObservable()
  72. {
  73. return onMouseUp ?? (onMouseUp = new Subject<Unit>());
  74. }
  75. Subject<Unit> onMouseUpAsButton;
  76. /// <summary>OnMouseUpAsButton is only called when the mouse is released over the same GUIElement or Collider as it was pressed.</summary>
  77. void OnMouseUpAsButton()
  78. {
  79. if (onMouseUpAsButton != null) onMouseUpAsButton.OnNext(Unit.Default);
  80. }
  81. /// <summary>OnMouseUpAsButton is only called when the mouse is released over the same GUIElement or Collider as it was pressed.</summary>
  82. public IObservable<Unit> OnMouseUpAsButtonAsObservable()
  83. {
  84. return onMouseUpAsButton ?? (onMouseUpAsButton = new Subject<Unit>());
  85. }
  86. protected override void RaiseOnCompletedOnDestroy()
  87. {
  88. if (onMouseDown != null)
  89. {
  90. onMouseDown.OnCompleted();
  91. }
  92. if (onMouseDrag != null)
  93. {
  94. onMouseDrag.OnCompleted();
  95. }
  96. if (onMouseEnter != null)
  97. {
  98. onMouseEnter.OnCompleted();
  99. }
  100. if (onMouseExit != null)
  101. {
  102. onMouseExit.OnCompleted();
  103. }
  104. if (onMouseOver != null)
  105. {
  106. onMouseOver.OnCompleted();
  107. }
  108. if (onMouseUp != null)
  109. {
  110. onMouseUp.OnCompleted();
  111. }
  112. if (onMouseUpAsButton != null)
  113. {
  114. onMouseUpAsButton.OnCompleted();
  115. }
  116. }
  117. }
  118. }
  119. #endif