RotationGesture.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace FairyGUI
  5. {
  6. /// <summary>
  7. /// 手指反向操作的手势。
  8. /// </summary>
  9. public class RotationGesture : 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 rotation;
  31. /// <summary>
  32. /// 从上次通知后的改变量。
  33. /// </summary>
  34. public float delta;
  35. /// <summary>
  36. /// 是否把变化量强制为整数。默认true。
  37. /// </summary>
  38. public bool snapping;
  39. Vector2 _startVector;
  40. float _lastRotation;
  41. int[] _touches;
  42. bool _started;
  43. bool _touchBegan;
  44. public RotationGesture(GObject host)
  45. {
  46. this.host = host;
  47. Enable(true);
  48. _touches = new int[2];
  49. snapping = true;
  50. onBegin = new EventListener(this, "onRotationBegin");
  51. onEnd = new EventListener(this, "onRotationEnd");
  52. onAction = new EventListener(this, "onRotationAction");
  53. }
  54. public void Dispose()
  55. {
  56. Enable(false);
  57. host = null;
  58. }
  59. public void Enable(bool value)
  60. {
  61. if (value)
  62. {
  63. if (host == GRoot.inst)
  64. {
  65. Stage.inst.onTouchBegin.Add(__touchBegin);
  66. Stage.inst.onTouchMove.Add(__touchMove);
  67. Stage.inst.onTouchEnd.Add(__touchEnd);
  68. }
  69. else
  70. {
  71. host.onTouchBegin.Add(__touchBegin);
  72. host.onTouchMove.Add(__touchMove);
  73. host.onTouchEnd.Add(__touchEnd);
  74. }
  75. }
  76. else
  77. {
  78. _started = false;
  79. _touchBegan = false;
  80. if (host == GRoot.inst)
  81. {
  82. Stage.inst.onTouchBegin.Remove(__touchBegin);
  83. Stage.inst.onTouchMove.Remove(__touchMove);
  84. Stage.inst.onTouchEnd.Remove(__touchEnd);
  85. }
  86. else
  87. {
  88. host.onTouchBegin.Remove(__touchBegin);
  89. host.onTouchMove.Remove(__touchMove);
  90. host.onTouchEnd.Remove(__touchEnd);
  91. }
  92. }
  93. }
  94. void __touchBegin(EventContext context)
  95. {
  96. if (Stage.inst.touchCount == 2)
  97. {
  98. if (!_started && !_touchBegan)
  99. {
  100. _touchBegan = true;
  101. Stage.inst.GetAllTouch(_touches);
  102. Vector2 pt1 = host.GlobalToLocal(Stage.inst.GetTouchPosition(_touches[0]));
  103. Vector2 pt2 = host.GlobalToLocal(Stage.inst.GetTouchPosition(_touches[1]));
  104. _startVector = pt1 - pt2;
  105. context.CaptureTouch();
  106. }
  107. }
  108. }
  109. void __touchMove(EventContext context)
  110. {
  111. if (!_touchBegan || Stage.inst.touchCount != 2)
  112. return;
  113. InputEvent evt = context.inputEvent;
  114. Vector2 pt1 = host.GlobalToLocal(Stage.inst.GetTouchPosition(_touches[0]));
  115. Vector2 pt2 = host.GlobalToLocal(Stage.inst.GetTouchPosition(_touches[1]));
  116. Vector2 vec = pt1 - pt2;
  117. float rot = Mathf.Rad2Deg * ((Mathf.Atan2(vec.y, vec.x) - Mathf.Atan2(_startVector.y, _startVector.x)));
  118. if (snapping)
  119. {
  120. rot = Mathf.Round(rot);
  121. if (rot == 0)
  122. return;
  123. }
  124. if (!_started && rot > 5)
  125. {
  126. _started = true;
  127. rotation = 0;
  128. _lastRotation = 0;
  129. onBegin.Call(evt);
  130. }
  131. if (_started)
  132. {
  133. delta = rot - _lastRotation;
  134. _lastRotation = rot;
  135. this.rotation += delta;
  136. onAction.Call(evt);
  137. }
  138. }
  139. void __touchEnd(EventContext context)
  140. {
  141. _touchBegan = false;
  142. if (_started)
  143. {
  144. _started = false;
  145. onEnd.Call(context.inputEvent);
  146. }
  147. }
  148. }
  149. }