InputDeviceGGT26Dof.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using AOT;
  2. using SC.XR.Unity.Module_Device;
  3. using SC.XR.Unity.Module_InputSystem.InputDeviceGC;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using UnityEngine;
  12. namespace SC.XR.Unity.Module_InputSystem.InputDeviceHand.GGT26Dof
  13. {
  14. public class InputDeviceGGT26Dof : InputDeviceHand
  15. {
  16. public override InputDeviceType inputDeviceType
  17. {
  18. get
  19. {
  20. return InputDeviceType.GGT26Dof;
  21. }
  22. }
  23. [HideInInspector]
  24. public float LowPowerPercent = 15;
  25. private Coroutine lowPowerCoroutine = null;
  26. bool isShowHandLowPower = false;
  27. bool isHandTrackStart = false;
  28. Coroutine startHand;
  29. [MonoPInvokeCallback(typeof(Action))]
  30. public static void GestureModelDataChangeCallback()
  31. {
  32. //Debug.Log("[04]GestureModelDataChangeCallback start");
  33. //InputDeviceGGT26DofPart inputDeviceGGT26DofPart = (inputDevicePartList[0] as InputDeviceGGT26DofPart);
  34. //if (inputDeviceGGT26DofPart != null) {
  35. // inputDeviceGGT26DofPart.inputDataGetGGT26Dof.inputDataGetHandsData.OnUpdateInputDataAndStore();
  36. //}
  37. }
  38. public override void OnSCAwake() {
  39. base.OnSCAwake();
  40. //PermissionRequest.getInstance.GetPerssion(UnityEngine.Android.Permission.ExternalStorageRead);
  41. //PermissionRequest.getInstance.GetPerssion(UnityEngine.Android.Permission.ExternalStorageWrite);
  42. }
  43. public override void OnSCStart()
  44. {
  45. base.OnSCStart();
  46. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "ShowHandLowPower"))
  47. {
  48. isShowHandLowPower = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "ShowHandLowPower", 0);
  49. DebugMy.Log("ShowHandLowPower:" + isShowHandLowPower, this, true);
  50. }
  51. }
  52. protected override void InputDeviceStart(){
  53. StartHandTrack();
  54. }
  55. public override void OnSCUpdate() {
  56. base.OnSCUpdate();
  57. if (isHandTrackStart) {
  58. InputDataGGT26Dof.handsInfo.originDataPose[0] = 1024 + 512 + 788 * 3 * 4;
  59. API_GSXR_Slam.plugin.GSXR_Get_HandTrackingData(InputDataGGT26Dof.handsInfo.originDataMode, InputDataGGT26Dof.handsInfo.originDataPose);
  60. }
  61. }
  62. public override void OnSCLateUpdate()
  63. {
  64. base.OnSCLateUpdate();
  65. if (lowPowerTrigger )
  66. {
  67. DebugMy.Log("ScHANDTRACK_lowPowerTrigger", this, true);
  68. lowPowerTrigger = false;
  69. if (lowPowerCoroutine == null)
  70. {
  71. lowPowerCoroutine = StartCoroutine(LowPowerFunction());
  72. }
  73. }
  74. }
  75. IEnumerator LowPowerFunction(float loopTime = 3)
  76. {
  77. StopHandTrack();
  78. SetActiveInputDevicePart(InputDevicePartType.HandLeft, false);
  79. SetActiveInputDevicePart(InputDevicePartType.HandRight, false);
  80. while (loopTime-- > 0 && isShowHandLowPower)
  81. {
  82. if (inputDeviceUI as InputDeviceHandUI)
  83. {
  84. (inputDeviceUI as InputDeviceHandUI).SetActiveUI(HandUIType.LOWPOWER, true);
  85. }
  86. yield return null;
  87. yield return new WaitForSeconds(12);
  88. }
  89. lowPowerCoroutine = null;
  90. }
  91. IEnumerator StartGreyHand()
  92. {
  93. //yield return new WaitUntil(() => API_GSXR_Slam.SlamManager != null);
  94. //yield return new WaitUntil(() => API_GSXR_Slam.SlamManager.IsRunning == true);
  95. //if (API_GSXR_Slam.plugin.GSXR_Is_SupportHandTracking() == false) {
  96. // DebugMy.Log("Not Support HandTracking",this,true);
  97. // yield break;
  98. //}
  99. if (API_Module_Device.Current.BatteryLevel < LowPowerPercent ) {
  100. if (lowPowerCoroutine == null) {
  101. lowPowerCoroutine = StartCoroutine(LowPowerFunction());
  102. }
  103. DebugMy.Log("BatteryLevel < " + LowPowerPercent + " StopHandTrack", this, true);
  104. yield break;
  105. }
  106. if (isHandTrackStart == false) {
  107. isHandTrackStart = true;
  108. DebugMy.Log("GSXR_StartHandTracking", this, true);
  109. if (Application.platform == RuntimePlatform.Android) {
  110. try {
  111. API_GSXR_Slam.plugin.GSXR_StartHandTracking(LowPowerWarningCallback);
  112. API_GSXR_Slam.plugin.GSXR_Set_HandTrackingCallBack(GestureChangeCallback);
  113. API_GSXR_Slam.plugin.GSXR_Set_HandTrackingModelDataCallBack(GestureModelDataChangeCallback);
  114. } catch (Exception e) {
  115. Debug.Log(e);
  116. }
  117. }
  118. }
  119. startHand = null;
  120. base.InputDeviceStart();
  121. }
  122. public override void OnSCDisable()
  123. {
  124. base.OnSCDisable();
  125. StopHandTrack();
  126. }
  127. private void StartHandTrack() {
  128. if (startHand == null) {
  129. startHand = StartCoroutine(StartGreyHand());
  130. }
  131. }
  132. private void StopHandTrack() {
  133. if (startHand != null) {
  134. StopCoroutine(startHand);
  135. startHand = null;
  136. }
  137. if (isHandTrackStart) {
  138. isHandTrackStart = false;
  139. DebugMy.Log("GSXR_StopHandTracking", this, true);
  140. if (Application.platform == RuntimePlatform.Android) {
  141. try {
  142. API_GSXR_Slam.plugin.GSXR_StopHandTracking();
  143. } catch (Exception e) {
  144. Debug.Log(e);
  145. }
  146. }
  147. }
  148. }
  149. //{1, "THUMB"},
  150. //{2, "ONE"},
  151. //{3, "TWO"},
  152. //{4, "THREE"},
  153. //{5, "FOUR"},
  154. //{6, "FIVE"},
  155. //{7, "OK"},
  156. //{8, "DIRECTION"},
  157. [MonoPInvokeCallback(typeof(Action<int>))]
  158. public static void GestureChangeCallback(int gesture)
  159. {
  160. Debug.Log("GestureChangeCallback, gesture id:" + gesture);
  161. }
  162. static bool lowPowerTrigger = false;
  163. [MonoPInvokeCallback(typeof(Action<int>))]
  164. public static void LowPowerWarningCallback(int power)
  165. {
  166. Debug.Log("HandGesture Cannot work in low power state:" + power);
  167. lowPowerTrigger = true;
  168. }
  169. }
  170. }