UIInteractionState.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace Ximmerse.XR.InputSystems.GazeAndGestureInteraction
  6. {
  7. /// <summary>
  8. /// Interface : interaction with objects
  9. /// </summary>
  10. public interface I_InteractionState
  11. {
  12. bool IsEnabled
  13. {
  14. get;
  15. }
  16. void OnEnable();
  17. void OnDisable();
  18. void OnReticleEnter();
  19. void Tick();
  20. void OnReticleExit();
  21. }
  22. class LockedGameObjectInfo
  23. {
  24. public Object lockedReference;
  25. public enum LockType
  26. {
  27. None = 0,
  28. /// <summary>
  29. /// Lock interaction target for slider UI
  30. /// </summary>
  31. SliderUI,
  32. /// <summary>
  33. /// Lock interaction target for 3d GameObject
  34. /// </summary>
  35. GameObject,
  36. }
  37. public LockType lockType = LockType.None;
  38. public float lockTime = 0;
  39. }
  40. /// <summary>
  41. /// Gaze and hand interaction system manages how XR user interacts world objects withe eye reticle and hand gesture.
  42. /// </summary>
  43. public partial class GazeAndHandInteractionSystem
  44. {
  45. /// <summary>
  46. /// UI objects interaction state.
  47. /// </summary>
  48. internal class UIObjectsInteractionState : I_InteractionState
  49. {
  50. public bool IsEnabled
  51. {
  52. get; private set;
  53. }
  54. Transform mainCam;
  55. LockedGameObjectInfo lockInfo = new LockedGameObjectInfo();
  56. public void OnEnable()
  57. {
  58. }
  59. public void OnDisable()
  60. {
  61. }
  62. public void OnReticleEnter()
  63. {
  64. }
  65. public void Tick()
  66. {
  67. if (!mainCam)
  68. {
  69. mainCam = Camera.main.transform;
  70. }
  71. if (HandTracking.HandTrackingInfo.IsTracking == false)
  72. {
  73. //Clear lock info:
  74. if (lockInfo.lockType != LockedGameObjectInfo.LockType.None && (Time.realtimeSinceStartup - lockInfo.lockTime) >= 0.333f)
  75. {
  76. lockInfo.lockType = LockedGameObjectInfo.LockType.None;
  77. lockInfo.lockedReference = null;
  78. Debug.Log("Slider UI : Clear 1 : " + HandTracking.HandTrackingInfo.IsTracking);
  79. }
  80. return;
  81. }
  82. var isInteractingUI = GazeAndHandInteractionSystem.instance.eyeReticle.CurrentInteractingTarget.isUI;
  83. bool isclosepinch = HandTracking.HandTrackingInfo.NativeGestureType == (int)TouchlessA3D.GestureType.CLOSED_PINCH;
  84. bool isclosehand = HandTracking.HandTrackingInfo.gestureFistOpenHand == GestureType_Fist_OpenHand.Fist;
  85. bool isPinchGesture = false;
  86. if (isclosepinch || isclosehand)
  87. {
  88. isPinchGesture = true;
  89. }
  90. else
  91. {
  92. isPinchGesture = false;
  93. }
  94. //var isPinchGesture = HandTracking.HandTrackingInfo.NativeGestureType == (byte)(TouchlessA3D.GestureType.CLOSED_PINCH);
  95. //设置 lock target : slider UI:
  96. if (isInteractingUI && GazeAndHandInteractionSystem.instance.eyeReticle.CurrentInteractingTarget.target)
  97. {
  98. Debug.Log("miao0:" + GazeAndHandInteractionSystem.instance.eyeReticle.CurrentInteractingTarget.target.name+ GazeAndHandInteractionSystem.instance.eyeReticle.CurrentHoveringTarget.target.GetComponentInParent<Slider>());
  99. Slider sliderUI = GazeAndHandInteractionSystem.instance.eyeReticle.CurrentInteractingTarget.target.GetComponentInParent<Slider>();
  100. if (sliderUI && isPinchGesture)
  101. {
  102. lockInfo.lockType = LockedGameObjectInfo.LockType.SliderUI;
  103. lockInfo.lockedReference = sliderUI;
  104. lockInfo.lockTime = Time.realtimeSinceStartup;//记录lock time
  105. Debug.Log("Slider UI : Set");
  106. }
  107. }
  108. //在 lock slider UI的情况下, 拖动进度条:
  109. if (this.lockInfo.lockType == LockedGameObjectInfo.LockType.SliderUI)
  110. {
  111. //放手或者丢失引用的时候, 解锁:
  112. if ((!lockInfo.lockedReference || !isPinchGesture) && (Time.realtimeSinceStartup - lockInfo.lockTime) >= 0.1f)
  113. {
  114. lockInfo.lockType = LockedGameObjectInfo.LockType.None;
  115. lockInfo.lockedReference = default(Object);
  116. Debug.Log("Slider UI : Clear 2");
  117. }
  118. else
  119. {
  120. //捏合姿态下,移动slider:
  121. if (isPinchGesture)
  122. {
  123. MoveSliderUI(lockInfo.lockedReference as Slider);
  124. lockInfo.lockTime = Time.realtimeSinceStartup;//更新 lock time
  125. Debug.Log("Slider UI : Move !");
  126. }
  127. }
  128. }
  129. }
  130. private void MoveSliderUI(Slider sliderUI)
  131. {
  132. //Debug.LogFormat("On UI state hover: {0}, {1}", sliderUI, isPinchGesture);
  133. {
  134. var delta = HandTracking.HandTrackingInfo.PalmDeltaPosition;
  135. var deltaVectorToHead = mainCam.InverseTransformVector(delta);
  136. float velocityX = Mathf.Abs(delta.x / Time.deltaTime);
  137. float velocityY = Mathf.Abs(delta.y / Time.deltaTime);
  138. if (sliderUI.direction== Slider.Direction.LeftToRight|| sliderUI.direction == Slider.Direction.RightToLeft)
  139. {
  140. if (velocityX >= 0.5f)
  141. {
  142. bool isRight = deltaVectorToHead.x > 0;
  143. float progressDelta = Mathf.Abs(delta.x) * (isRight ? 1 : -1) * 8;//加速8倍
  144. sliderUI.normalizedValue = Mathf.Clamp01(sliderUI.normalizedValue + progressDelta);
  145. Debug.LogFormat("Change slider UI with delta: {0}, velX : {1}", progressDelta, velocityX);
  146. }
  147. }
  148. if (sliderUI.direction == Slider.Direction.BottomToTop || sliderUI.direction == Slider.Direction.TopToBottom)
  149. {
  150. if (velocityY >= 0.5f)
  151. {
  152. bool istop = deltaVectorToHead.y > 0;
  153. float progressDelta = Mathf.Abs(delta.y) * (istop ? 1 : -1) * 8;//加速8倍
  154. sliderUI.normalizedValue = Mathf.Clamp01(sliderUI.normalizedValue + progressDelta);
  155. Debug.LogFormat("Change slider UI with delta: {0}, velX : {1}", progressDelta, velocityY);
  156. }
  157. }
  158. }
  159. }
  160. public void OnReticleExit()
  161. {
  162. }
  163. }
  164. }
  165. }