NRControllerProvider.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using System;
  12. using UnityEngine;
  13. /// <summary>
  14. /// This class obtains the runtime information of controller devices through a native controller
  15. /// plugin, and would use these info to update controller states. </summary>
  16. internal partial class NRControllerProvider : ControllerProviderBase
  17. {
  18. /// <summary> The native controller. </summary>
  19. private NativeController m_NativeController;
  20. /// <summary> The processed frame. </summary>
  21. private int m_ProcessedFrame;
  22. /// <summary> True to need initialize. </summary>
  23. private bool m_NeedInit = true;
  24. /// <summary> True to need recenter. </summary>
  25. private bool m_NeedRecenter;
  26. /// <summary> Array of home pressing timers. </summary>
  27. private float[] homePressingTimerArr = new float[NRInput.MAX_CONTROLLER_STATE_COUNT];
  28. /// <summary> The home long press time. </summary>
  29. private const float HOME_LONG_PRESS_TIME = 1.1f;
  30. /// <summary> Constructor. </summary>
  31. /// <param name="states"> The states.</param>
  32. public NRControllerProvider(ControllerState[] states) : base(states)
  33. {
  34. }
  35. /// <summary> Gets the number of controllers. </summary>
  36. /// <value> The number of controllers. </value>
  37. public override int ControllerCount
  38. {
  39. get
  40. {
  41. if (!Inited)
  42. return 0;
  43. return m_NativeController.GetControllerCount();
  44. }
  45. }
  46. /// <summary> Gets a version. </summary>
  47. /// <param name="index"> Zero-based index of the controller.</param>
  48. /// <returns> The version. </returns>
  49. public string GetVersion(int index)
  50. {
  51. if (m_NativeController != null)
  52. {
  53. return m_NativeController.GetVersion(index);
  54. }
  55. return string.Empty;
  56. }
  57. public ControllerHandEnum GetHandednessType()
  58. {
  59. if (m_NativeController != null)
  60. {
  61. return m_NativeController.GetHandednessType() == HandednessType.LEFT_HANDEDNESS ? ControllerHandEnum.Left : ControllerHandEnum.Right;
  62. }
  63. return ControllerHandEnum.Right;
  64. }
  65. /// <summary> Executes the 'pause' action. </summary>
  66. public override void OnPause()
  67. {
  68. if (m_NativeController != null)
  69. {
  70. m_NativeController.Pause();
  71. }
  72. }
  73. /// <summary> Executes the 'resume' action. </summary>
  74. public override void OnResume()
  75. {
  76. if (m_NativeController != null)
  77. {
  78. m_NativeController.Resume();
  79. }
  80. }
  81. /// <summary> Updates this object. </summary>
  82. public override void Update()
  83. {
  84. if (m_ProcessedFrame == Time.frameCount)
  85. return;
  86. m_ProcessedFrame = Time.frameCount;
  87. if (m_NeedInit)
  88. {
  89. InitNativeController();
  90. return;
  91. }
  92. if (!Inited)
  93. return;
  94. int availableCount = ControllerCount;
  95. if (availableCount > 0 && NRInput.GetControllerAvailableFeature(ControllerAvailableFeature.CONTROLLER_AVAILABLE_FEATURE_POSITION))
  96. {
  97. UpdateHeadPoseToController();
  98. }
  99. for (int i = 0; i < availableCount; i++)
  100. {
  101. UpdateControllerState(i);
  102. }
  103. }
  104. /// <summary> Executes the 'destroy' action. </summary>
  105. public override void OnDestroy()
  106. {
  107. if (m_NativeController != null)
  108. {
  109. m_NativeController.Destroy();
  110. m_NativeController = null;
  111. }
  112. }
  113. /// <summary> Trigger haptic vibration. </summary>
  114. /// <param name="index"> Zero-based index of the controller.</param>
  115. /// <param name="durationSeconds"> (Optional) The duration in seconds.</param>
  116. /// <param name="frequency"> (Optional) The frequency.</param>
  117. /// <param name="amplitude"> (Optional) The amplitude.</param>
  118. public override void TriggerHapticVibration(int index, float durationSeconds = 0.1f, float frequency = 1000f, float amplitude = 0.5f)
  119. {
  120. if (!Inited)
  121. return;
  122. if (states[index].controllerType == ControllerType.CONTROLLER_TYPE_PHONE)
  123. {
  124. PhoneVibrateTool.TriggerVibrate(durationSeconds);
  125. }
  126. else
  127. {
  128. if (m_NativeController != null && NRInput.GetControllerAvailableFeature(ControllerAvailableFeature.CONTROLLER_AVAILABLE_FEATURE_HAPTIC_VIBRATE))
  129. {
  130. Int64 durationNano = (Int64)(durationSeconds * 1000000000);
  131. m_NativeController.TriggerHapticVibrate(index, durationNano, frequency, amplitude);
  132. }
  133. }
  134. }
  135. /// <summary> Recenters this object. </summary>
  136. public override void Recenter()
  137. {
  138. base.Recenter();
  139. m_NeedRecenter = true;
  140. }
  141. /// <summary> Initializes the native controller. </summary>
  142. private void InitNativeController()
  143. {
  144. m_NativeController = new NativeController();
  145. if (m_NativeController.Init())
  146. {
  147. Inited = true;
  148. NRDebugger.Info("[NRControllerProvider] Init Succeed");
  149. }
  150. else
  151. {
  152. m_NativeController = null;
  153. NRDebugger.Error("[NRControllerProvider] Init Failed !!");
  154. }
  155. #if !UNITY_EDITOR
  156. NRDebugger.Info("[NRInput] version:" + GetVersion(0));
  157. #endif
  158. m_NeedInit = false;
  159. }
  160. /// <summary> Updates the controller state described by index. </summary>
  161. /// <param name="index"> Zero-based index of the.</param>
  162. private void UpdateControllerState(int index)
  163. {
  164. m_NativeController.UpdateState(index);
  165. states[index].controllerType = m_NativeController.GetControllerType(index);
  166. #if UNITY_EDITOR
  167. if (NRInput.EmulateVirtualDisplayInEditor)
  168. {
  169. states[index].controllerType = ControllerType.CONTROLLER_TYPE_PHONE;
  170. }
  171. #endif
  172. states[index].availableFeature = (ControllerAvailableFeature)m_NativeController.GetAvailableFeatures(index);
  173. states[index].connectionState = m_NativeController.GetConnectionState(index);
  174. states[index].rotation = m_NativeController.GetPose(index).rotation;
  175. states[index].position = m_NativeController.GetPose(index).position;
  176. states[index].gyro = m_NativeController.GetGyro(index);
  177. states[index].accel = m_NativeController.GetAccel(index);
  178. states[index].mag = m_NativeController.GetMag(index);
  179. states[index].touchPos = m_NativeController.GetTouch(index);
  180. states[index].isTouching = m_NativeController.IsTouching(index);
  181. states[index].recentered = false;
  182. states[index].isCharging = m_NativeController.IsCharging(index);
  183. states[index].batteryLevel = m_NativeController.GetBatteryLevel(index);
  184. states[index].buttonsState = (ControllerButton)m_NativeController.GetButtonState(index);
  185. states[index].buttonsDown = (ControllerButton)m_NativeController.GetButtonDown(index);
  186. states[index].buttonsUp = (ControllerButton)m_NativeController.GetButtonUp(index);
  187. IControllerStateParser stateParser = ControllerStateParseUtility.GetControllerStateParser(states[index].controllerType, index);
  188. if (stateParser != null)
  189. {
  190. stateParser.ParserControllerState(states[index]);
  191. }
  192. CheckRecenter(index);
  193. if (m_NeedRecenter)
  194. {
  195. for (int i = 0; i < ControllerCount; i++)
  196. {
  197. states[i].recentered = true;
  198. m_NativeController.RecenterController(i);
  199. }
  200. m_NeedRecenter = false;
  201. }
  202. }
  203. /// <summary> Check recenter. </summary>
  204. /// <param name="index"> Zero-based index of the.</param>
  205. private void CheckRecenter(int index)
  206. {
  207. if (states[index].GetButton(ControllerButton.HOME))
  208. {
  209. homePressingTimerArr[index] += Time.deltaTime;
  210. if (homePressingTimerArr[index] > HOME_LONG_PRESS_TIME)
  211. {
  212. homePressingTimerArr[index] = float.MinValue;
  213. Recenter();
  214. }
  215. }
  216. else
  217. {
  218. homePressingTimerArr[index] = 0f;
  219. }
  220. }
  221. /// <summary> Updates the head pose to controller. </summary>
  222. private void UpdateHeadPoseToController()
  223. {
  224. if (m_NativeController != null && NRInput.CameraCenter)
  225. {
  226. m_NativeController.UpdateHeadPose(new Pose(NRInput.CameraCenter.position, NRInput.CameraCenter.rotation));
  227. }
  228. }
  229. }
  230. }