LongPressGesture.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace FairyGUI
  5. {
  6. /// <summary>
  7. /// 长按手势。当按下一定时间后(duration),派发onAction,如果once为false,则间隔duration时间持续派发onAction,直到手指释放。
  8. /// </summary>
  9. public class LongPressGesture : EventDispatcher
  10. {
  11. /// <summary>
  12. ///
  13. /// </summary>
  14. public GObject host { get; private set; }
  15. /// <summary>
  16. /// 当手指按下时派发该事件。
  17. /// </summary>
  18. public EventListener onBegin { get; private set; }
  19. /// <summary>
  20. /// 手指离开屏幕时派发该事件。
  21. /// </summary>
  22. public EventListener onEnd { get; private set; }
  23. /// <summary>
  24. /// 当手指按下后一段时间后派发该事件。并且在手指离开前按一定周期派发该事件。
  25. /// </summary>
  26. public EventListener onAction { get; private set; }
  27. /// <summary>
  28. /// 第一次派发事件的触发时间。单位秒
  29. /// </summary>
  30. public float trigger;
  31. /// <summary>
  32. /// 派发onAction事件的时间间隔。单位秒。
  33. /// </summary>
  34. public float interval;
  35. /// <summary>
  36. /// 如果为真,则onAction再一次按下释放过程只派发一次。如果为假,则每隔duration时间派发一次。
  37. /// </summary>
  38. public bool once;
  39. /// <summary>
  40. /// 手指按住后,移动超出此半径范围则手势停止。
  41. /// </summary>
  42. public int holdRangeRadius;
  43. Vector2 _startPoint;
  44. bool _started;
  45. int _touchId;
  46. public static float TRIGGER = 1.5f;
  47. public static float INTERVAL = 1f;
  48. public LongPressGesture(GObject host)
  49. {
  50. this.host = host;
  51. trigger = TRIGGER;
  52. interval = INTERVAL;
  53. holdRangeRadius = 50;
  54. Enable(true);
  55. onBegin = new EventListener(this, "onLongPressBegin");
  56. onEnd = new EventListener(this, "onLongPressEnd");
  57. onAction = new EventListener(this, "onLongPressAction");
  58. }
  59. public void Dispose()
  60. {
  61. Enable(false);
  62. host = null;
  63. }
  64. public void Enable(bool value)
  65. {
  66. if (value)
  67. {
  68. if (host == GRoot.inst)
  69. {
  70. Stage.inst.onTouchBegin.Add(__touchBegin);
  71. Stage.inst.onTouchEnd.Add(__touchEnd);
  72. }
  73. else
  74. {
  75. host.onTouchBegin.Add(__touchBegin);
  76. host.onTouchEnd.Add(__touchEnd);
  77. }
  78. }
  79. else
  80. {
  81. if (host == GRoot.inst)
  82. {
  83. Stage.inst.onTouchBegin.Remove(__touchBegin);
  84. Stage.inst.onTouchEnd.Remove(__touchEnd);
  85. }
  86. else
  87. {
  88. host.onTouchBegin.Remove(__touchBegin);
  89. host.onTouchEnd.Remove(__touchEnd);
  90. }
  91. Timers.inst.Remove(__timer);
  92. }
  93. }
  94. public void Cancel()
  95. {
  96. Timers.inst.Remove(__timer);
  97. _started = false;
  98. }
  99. void __touchBegin(EventContext context)
  100. {
  101. InputEvent evt = context.inputEvent;
  102. _startPoint = host.GlobalToLocal(new Vector2(evt.x, evt.y));
  103. _started = false;
  104. _touchId = evt.touchId;
  105. Timers.inst.Add(trigger, 1, __timer);
  106. context.CaptureTouch();
  107. }
  108. void __timer(object param)
  109. {
  110. Vector2 pt = host.GlobalToLocal(Stage.inst.GetTouchPosition(_touchId));
  111. if (Mathf.Pow(pt.x - _startPoint.x, 2) + Mathf.Pow(pt.y - _startPoint.y, 2) > Mathf.Pow(holdRangeRadius, 2))
  112. {
  113. Timers.inst.Remove(__timer);
  114. return;
  115. }
  116. if (!_started)
  117. {
  118. _started = true;
  119. onBegin.Call();
  120. if (!once)
  121. Timers.inst.Add(interval, 0, __timer);
  122. }
  123. onAction.Call();
  124. }
  125. void __touchEnd(EventContext context)
  126. {
  127. Timers.inst.Remove(__timer);
  128. if (_started)
  129. {
  130. _started = false;
  131. onEnd.Call();
  132. }
  133. }
  134. }
  135. }