HandDetector.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7. namespace SC.XR.Unity.Module_InputSystem.InputDeviceHand {
  8. public class HandDetector : DetectorBase {
  9. public InputDeviceHandPart inputDeviceHandPart {
  10. get {
  11. return inputDevicePartBase as InputDeviceHandPart;
  12. }
  13. }
  14. public FarPointer farPointer {
  15. get {
  16. return Transition<FarPointer>(pointerBase);
  17. }
  18. }
  19. [SerializeField]
  20. TouchPointer mTouchPointer;
  21. public TouchPointer touchPointer {
  22. get {
  23. if(mTouchPointer == null) {
  24. mTouchPointer = GetComponentInChildren<TouchPointer>(true);
  25. }
  26. return mTouchPointer;
  27. }
  28. set {
  29. mTouchPointer = value;
  30. }
  31. }
  32. [SerializeField]
  33. GrabPointer mGrabPointer;
  34. public GrabPointer grabPointer {
  35. get {
  36. if(mGrabPointer == null) {
  37. mGrabPointer = GetComponentInChildren<GrabPointer>(true);
  38. }
  39. return mGrabPointer;
  40. }
  41. set {
  42. mGrabPointer = value;
  43. }
  44. }
  45. [HideInInspector]
  46. public bool EnableHandTouchInteraction = true;
  47. [HideInInspector]
  48. public bool EnableHandGrabInteraction = true;
  49. public override void OnSCAwake() {
  50. base.OnSCAwake();
  51. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "EnableHandTouchInteraction")) {
  52. EnableHandTouchInteraction = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "EnableHandTouchInteraction", 1);
  53. }
  54. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "EnableHandGrabInteraction")) {
  55. EnableHandGrabInteraction = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "EnableHandGrabInteraction", 1);
  56. }
  57. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "EnableHandFarInteraction")) {
  58. EnableFarInteraction = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "EnableHandFarInteraction", 1);
  59. }
  60. PointerDic.Add(PointerType.Touch, touchPointer);
  61. PointerDic.Add(PointerType.Grab, grabPointer);
  62. AddModule(touchPointer);
  63. AddModule(grabPointer);
  64. }
  65. public override void OnSCStart() {
  66. base.OnSCStart();
  67. currentPointer = MutualOpenPointer(PointerType.Null);
  68. }
  69. public override void OnSCDestroy() {
  70. base.OnSCDestroy();
  71. touchPointer = null;
  72. grabPointer = null;
  73. currentPointer = null;
  74. }
  75. protected override PointerBase PointerActionALG() {
  76. if (touchPointer.IsFocusLocked || grabPointer.IsFocusLocked || farPointer.IsFocusLocked) {
  77. return currentPointer;
  78. } else {
  79. if (inputDeviceHandPart.inputDevicePartDispatchEventHand.inputDeviceHandPartFaceHead.currentEvent == HandEventType.PalmFaceHead) {
  80. return MutualOpenPointer(PointerType.Null);
  81. } else {
  82. if (EnableHandTouchInteraction && touchPointer.FindClosestTouchableForLayerMask()) {
  83. if (touchPointer.IsNearObject) {
  84. return MutualOpenPointer(PointerType.Touch);
  85. } else if (EnableHandGrabInteraction && grabPointer.FindClosestGrabbableForLayerMask()) {
  86. return MutualOpenPointer(PointerType.Grab);
  87. }
  88. return MutualOpenPointer(PointerType.Null);
  89. } else if (EnableHandGrabInteraction && grabPointer.FindClosestGrabbableForLayerMask()) {
  90. return MutualOpenPointer(PointerType.Grab);
  91. } else if (EnableFarInteraction) {
  92. return MutualOpenPointer(PointerType.Far);
  93. } else {
  94. return MutualOpenPointer(PointerType.Null);
  95. }
  96. }
  97. }
  98. }
  99. PointerBase pointerTemp;
  100. PointerBase MutualOpenPointer(PointerType type) {
  101. pointerTemp = null;
  102. foreach (var pointerType in PointerDic.Keys) {
  103. if (pointerType == type) {
  104. if (PointerDic[pointerType].IsModuleStarted == false) {
  105. PointerDic[pointerType].ModuleStart();
  106. }
  107. pointerTemp = PointerDic[pointerType];
  108. } else if (PointerDic[pointerType].IsModuleStarted) {
  109. PointerDic[pointerType].ModuleStop();
  110. }
  111. }
  112. return pointerTemp;
  113. }
  114. }
  115. }