RotationGesture.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. Debug.Log("zoya_000:" + _touchBegan + " " + Stage.inst.touchCount);
  112. if (!_touchBegan || Stage.inst.touchCount != 2)
  113. return;
  114. InputEvent evt = context.inputEvent;
  115. Vector2 pt1 = host.GlobalToLocal(Stage.inst.GetTouchPosition(_touches[0]));
  116. Vector2 pt2 = host.GlobalToLocal(Stage.inst.GetTouchPosition(_touches[1]));
  117. Vector2 vec = pt1 - pt2;
  118. float rot = Mathf.Rad2Deg * ((Mathf.Atan2(vec.y, vec.x) - Mathf.Atan2(_startVector.y, _startVector.x)));
  119. Debug.Log("zoya_111:" + rot + " " + vec + " " + _startVector);
  120. if (snapping)
  121. {
  122. rot = Mathf.Round(rot);
  123. if (rot == 0)
  124. return;
  125. }
  126. Debug.Log("zoya_222:" + _started + " " + rot);
  127. if (!_started && rot > 5)
  128. {
  129. _started = true;
  130. rotation = 0;
  131. _lastRotation = 0;
  132. onBegin.Call(evt);
  133. }
  134. if (_started)
  135. {
  136. delta = rot - _lastRotation;
  137. _lastRotation = rot;
  138. this.rotation += delta;
  139. onAction.Call(evt);
  140. }
  141. }
  142. void __touchEnd(EventContext context)
  143. {
  144. _touchBegan = false;
  145. if (_started)
  146. {
  147. _started = false;
  148. onEnd.Call(context.inputEvent);
  149. }
  150. }
  151. }
  152. }