NRInput.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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> Enumeration of handedness. </summary>
  14. public enum ControllerHandEnum
  15. {
  16. /// <summary> An enum constant representing the right option. </summary>
  17. Right = 0,
  18. /// <summary> An enum constant representing the left option. </summary>
  19. Left = 1
  20. }
  21. /// <summary> Enumeration of raycast mode. Normally, suggest using "Laser" mode. </summary>
  22. public enum RaycastModeEnum
  23. {
  24. /// <summary> An enum constant representing the gaze option. </summary>
  25. Gaze,
  26. /// <summary> An enum constant representing the laser option. </summary>
  27. Laser
  28. }
  29. /// <summary> Enumeration of input source type. </summary>
  30. public enum InputSourceEnum
  31. {
  32. /// <summary> An enum constant representing the hands option. </summary>
  33. Hands,
  34. /// <summary> An enum constant representing the controller option. </summary>
  35. Controller
  36. }
  37. /// <summary> Enumeration of controller visual types. </summary>
  38. public enum ControllerVisualType
  39. {
  40. /// <summary> An enum constant representing the none option. </summary>
  41. None = 0,
  42. /// <summary> An enum constant representing the nreal light option. </summary>
  43. NrealLight = 1,
  44. /// <summary> An enum constant representing the phone option. </summary>
  45. Phone = 2
  46. }
  47. /// <summary>
  48. /// The main class to handle controller related things, such as to get controller states, update
  49. /// controller states Through this class, application would create a controllerProvider which
  50. /// could be custom, then the controllerProvider iteself would define how to update the
  51. /// controller states, so that every frame NRInput could get the right states.There are max two
  52. /// states for one controllerProvider. </summary>
  53. [HelpURL("https://developer.nreal.ai/develop/unity/controller")]
  54. [ScriptOrder(NativeConstants.NRINPUT_ORDER)]
  55. public partial class NRInput : SingletonBehaviour<NRInput>
  56. {
  57. /// <summary> True to emulate virtual display in editor. </summary>
  58. [Tooltip("If enable this, phone virtual controller would be shown in Unity Editor")]
  59. [SerializeField]
  60. private bool m_EmulateVirtualDisplayInEditor;
  61. /// <summary> The override camera center. </summary>
  62. [SerializeField]
  63. private Transform m_OverrideCameraCenter;
  64. /// <summary> The anchor helper. </summary>
  65. [SerializeField]
  66. private ControllerAnchorsHelper m_AnchorHelper;
  67. /// <summary> The raycast mode. </summary>
  68. [SerializeField]
  69. private RaycastModeEnum m_RaycastMode = RaycastModeEnum.Laser;
  70. /// <summary> The current input source type. </summary>
  71. [SerializeField]
  72. private InputSourceEnum m_InputSourceType = InputSourceEnum.Controller;
  73. /// <summary> The click interval. </summary>
  74. [SerializeField]
  75. private float m_ClickInterval = 0.3f;
  76. /// <summary> The drag threshold. </summary>
  77. [SerializeField]
  78. private float m_DragThreshold = 0.02f;
  79. /// <summary> Manager for visual. </summary>
  80. private ControllerVisualManager m_VisualManager;
  81. /// <summary> Number of last controllers. </summary>
  82. private int m_LastControllerCount;
  83. /// <summary> True to reticle visual active. </summary>
  84. private bool m_ReticleVisualActive = true;
  85. /// <summary> True to laser visual active. </summary>
  86. private bool m_LaserVisualActive = true;
  87. /// <summary> True to controller visual active. </summary>
  88. private bool m_ControllerVisualActive = true;
  89. /// <summary> True to enable, false to disable the haptic vibration. </summary>
  90. private bool m_HapticVibrationEnabled = true;
  91. /// <summary> True to active, false to disactive the gameobjects of raycasters. </summary>
  92. private bool m_RaycastersActive = true;
  93. /// <summary> Whether has checked the camera center. </summary>
  94. private bool m_HasCheckedCameraCenter;
  95. /// <summary> True means will do something OnValidate. </summary>
  96. private bool m_IsListeningToEditorValidateEvents = false;
  97. /// <summary> The cached input source type in Editor. </summary>
  98. private InputSourceEnum m_EditorCachedInputSourceType = InputSourceEnum.Controller;
  99. /// <summary> True to ignore recenter callback. </summary>
  100. private static bool m_IgnoreRecenterCallback = false;
  101. /// <summary> The domain hand. </summary>
  102. private static ControllerHandEnum m_DomainHand = ControllerHandEnum.Right;
  103. /// <summary> The controller provider. </summary>
  104. private static ControllerProviderBase m_ControllerProvider;
  105. /// <summary> The states. </summary>
  106. private static ControllerState[] m_States = new ControllerState[MAX_CONTROLLER_STATE_COUNT]
  107. {
  108. new ControllerState(),
  109. new ControllerState()
  110. };
  111. /// <summary> Max count of controllerstates supported per frame. </summary>
  112. public const int MAX_CONTROLLER_STATE_COUNT = 2;
  113. /// <summary> Event invoked whenever the domain hand has changed. </summary>
  114. public static Action<ControllerHandEnum> OnDomainHandChanged;
  115. /// <summary> Event invoked whenever a controller device is connected. </summary>
  116. public static Action OnControllerConnected;
  117. /// <summary> Event invoked whenever a controller device is disconnected. </summary>
  118. public static Action OnControllerDisconnected;
  119. /// <summary> Event invoked before controller devices are going to recenter. </summary>
  120. public static Action OnBeforeControllerRecenter;
  121. /// <summary> Event invoked whenever controller devices are recentering. </summary>
  122. internal static Action OnControllerRecentering;
  123. /// <summary> Event invoked whenever controller devices are recentered. </summary>
  124. public static Action OnControllerRecentered;
  125. /// <summary> Event invoked whenever controller devices states are updated. </summary>
  126. public static Action OnControllerStatesUpdated;
  127. /// <summary>
  128. /// Determine whether to show reticle visuals, could be get and set at runtime. </summary>
  129. /// <value> True if reticle visual active, false if not. </value>
  130. public static bool ReticleVisualActive { get { return Instance.m_ReticleVisualActive; } set { Instance.m_ReticleVisualActive = value; } }
  131. /// <summary> Determine whether to show laser visuals, could be get and set at runtime. </summary>
  132. /// <value> True if laser visual active, false if not. </value>
  133. public static bool LaserVisualActive { get { return Instance.m_LaserVisualActive; } set { Instance.m_LaserVisualActive = value; } }
  134. /// <summary>
  135. /// Determine whether to show controller visuals, could be get and set at runtime. </summary>
  136. /// <value> True if controller visual active, false if not. </value>
  137. public static bool ControllerVisualActive { get { return Instance.m_ControllerVisualActive; } set { Instance.m_ControllerVisualActive = value; } }
  138. /// <summary> Determine whether enable haptic vibration. </summary>
  139. /// <value> True if haptic vibration enabled, false if not. </value>
  140. public static bool HapticVibrationEnabled { get { return Instance.m_HapticVibrationEnabled; } set { Instance.m_HapticVibrationEnabled = value; } }
  141. /// <summary>
  142. /// Determine whether to active raycaster gameobjects, could be get and set at runtime. </summary>
  143. /// <value> True if active raycaster gameobjects, false if not. </value>
  144. public static bool RaycastersActive { get { return Instance.m_RaycastersActive; } set { Instance.m_RaycastersActive = value; } }
  145. /// <summary> Determine whether emulate phone virtual display in Unity Editor. </summary>
  146. /// <value> True if emulate virtual display in editor, false if not. </value>
  147. public static bool EmulateVirtualDisplayInEditor { get { return Instance ? Instance.m_EmulateVirtualDisplayInEditor : false; } }
  148. /// <summary> It's a helper to get controller anchors which are frequently used. </summary>
  149. /// <value> The anchors helper. </value>
  150. public static ControllerAnchorsHelper AnchorsHelper { get { return Instance.m_AnchorHelper; } }
  151. /// <summary> Get the current enumeration of handedness. </summary>
  152. /// <value> The domain hand. </value>
  153. public static ControllerHandEnum DomainHand { get { return m_DomainHand; } }
  154. /// <summary> Determine which raycast mode to use. </summary>
  155. /// <value> The raycast mode. </value>
  156. public static RaycastModeEnum RaycastMode { get { return Instance.m_RaycastMode; } set { Instance.m_RaycastMode = value; } }
  157. /// <summary> Get the current input source type. </summary>
  158. /// <value> The input source type. </value>
  159. public static InputSourceEnum CurrentInputSourceType { get { return Instance.m_InputSourceType; } }
  160. /// <summary> Get and set button click interval. </summary>
  161. /// <value> The click interval. </value>
  162. public static float ClickInterval { get { return Instance.m_ClickInterval; } set { Instance.m_ClickInterval = value; } }
  163. /// <summary> Get and set pointer drag threshold. </summary>
  164. /// <value> The drag threshold. </value>
  165. public static float DragThreshold { get { return Instance.m_DragThreshold; } set { Instance.m_DragThreshold = value; } }
  166. /// <summary> Get the transform of the camera which controllers are following. </summary>
  167. /// <value> The camera center. </value>
  168. public static Transform CameraCenter { get { return Instance.GetCameraCenter(); } }
  169. /// <summary> The HandsManager which controls the hand-tracking. </summary>
  170. public static NRHandsManager Hands = new NRHandsManager();
  171. /// <summary> Starts this object. </summary>
  172. private void Start()
  173. {
  174. if (isDirty)
  175. {
  176. return;
  177. }
  178. Init();
  179. }
  180. /// <summary> Executes the 'update' action. </summary>
  181. private void OnUpdate()
  182. {
  183. if (m_ControllerProvider == null)
  184. return;
  185. UpdateControllerProvider();
  186. }
  187. /// <summary> Updates the controller provider. </summary>
  188. private void UpdateControllerProvider()
  189. {
  190. if (m_ControllerProvider.Inited)
  191. {
  192. m_ControllerProvider.Update();
  193. if (OnControllerStatesUpdated != null)
  194. {
  195. OnControllerStatesUpdated();
  196. }
  197. CheckControllerConnection();
  198. CheckControllerRecentered();
  199. CheckControllerButtonEvents();
  200. }
  201. else
  202. {
  203. m_ControllerProvider.Update();
  204. #if !UNITY_EDITOR
  205. if (m_ControllerProvider is NRControllerProvider)
  206. {
  207. m_DomainHand = ((NRControllerProvider)m_ControllerProvider).GetHandednessType();
  208. NRDebugger.Info("[NRInput] Set default domain hand:" + m_DomainHand);
  209. }
  210. #endif
  211. }
  212. }
  213. /// <summary> Executes the 'enable' action. </summary>
  214. private void OnEnable()
  215. {
  216. if (isDirty)
  217. {
  218. return;
  219. }
  220. NRKernalUpdater.OnPostUpdate += OnUpdate;
  221. m_ControllerProvider?.OnResume();
  222. }
  223. /// <summary> Executes the 'disable' action. </summary>
  224. private void OnDisable()
  225. {
  226. if (isDirty)
  227. {
  228. return;
  229. }
  230. NRKernalUpdater.OnPostUpdate -= OnUpdate;
  231. m_ControllerProvider?.OnPause();
  232. }
  233. #if UNITY_EDITOR
  234. /// <summary> Executes the 'validate' action. </summary>
  235. private void OnValidate()
  236. {
  237. if (!m_IsListeningToEditorValidateEvents)
  238. return;
  239. if (m_EditorCachedInputSourceType != m_InputSourceType)
  240. {
  241. SetInputSource(m_InputSourceType);
  242. }
  243. }
  244. #endif
  245. /// <summary> Gets a version. </summary>
  246. /// <param name="index"> Zero-based index of the.</param>
  247. /// <returns> The version. </returns>
  248. public string GetVersion(int index)
  249. {
  250. if (m_ControllerProvider is NRControllerProvider)
  251. {
  252. return ((NRControllerProvider)m_ControllerProvider).GetVersion(index);
  253. }
  254. else
  255. {
  256. return "0.0.0";
  257. }
  258. }
  259. /// <summary> Destroys this object. </summary>
  260. internal static void Destroy()
  261. {
  262. if (m_ControllerProvider != null)
  263. {
  264. m_ControllerProvider.OnDestroy();
  265. m_ControllerProvider = null;
  266. }
  267. }
  268. /// <summary>
  269. /// Base OnDestroy method that destroys the Singleton's unique instance. Called by Unity when
  270. /// destroying a MonoBehaviour. Scripts that extend Singleton should be sure to call
  271. /// base.OnDestroy() to ensure the underlying static Instance reference is properly cleaned up. </summary>
  272. new void OnDestroy()
  273. {
  274. if (isDirty)
  275. {
  276. return;
  277. }
  278. base.OnDestroy();
  279. Destroy();
  280. }
  281. /// <summary> Check controller connection. </summary>
  282. private void CheckControllerConnection()
  283. {
  284. int currentControllerCount = GetAvailableControllersCount();
  285. if (m_LastControllerCount < currentControllerCount)
  286. {
  287. if (OnControllerConnected != null)
  288. {
  289. OnControllerConnected();
  290. }
  291. }
  292. else if (m_LastControllerCount > currentControllerCount)
  293. {
  294. if (OnControllerDisconnected != null)
  295. {
  296. OnControllerDisconnected();
  297. }
  298. }
  299. m_LastControllerCount = currentControllerCount;
  300. }
  301. /// <summary> Check controller recentered. </summary>
  302. private void CheckControllerRecentered()
  303. {
  304. if (GetControllerState(DomainHand).recentered)
  305. {
  306. if (m_IgnoreRecenterCallback == false && OnBeforeControllerRecenter != null)
  307. {
  308. OnBeforeControllerRecenter();
  309. }
  310. if (OnControllerRecentering != null)
  311. {
  312. OnControllerRecentering();
  313. }
  314. if (m_IgnoreRecenterCallback == false && OnControllerRecentered != null)
  315. {
  316. OnControllerRecentered();
  317. }
  318. m_IgnoreRecenterCallback = false;
  319. }
  320. }
  321. /// <summary> Check controller button events. </summary>
  322. private void CheckControllerButtonEvents()
  323. {
  324. int currentControllerCount = GetAvailableControllersCount();
  325. for (int i = 0; i < currentControllerCount; i++)
  326. {
  327. m_States[i].CheckButtonEvents();
  328. }
  329. }
  330. /// <summary> Executes the 'application pause' action. </summary>
  331. /// <param name="paused"> True if paused.</param>
  332. private void OnApplicationPause(bool paused)
  333. {
  334. if (m_ControllerProvider == null || !m_ControllerProvider.Inited)
  335. return;
  336. if (paused)
  337. {
  338. m_ControllerProvider.OnPause();
  339. }
  340. else
  341. {
  342. m_ControllerProvider.OnResume();
  343. m_IgnoreRecenterCallback = true;
  344. m_ControllerProvider.Recenter();
  345. }
  346. }
  347. /// <summary> Initializes this object. </summary>
  348. private void Init()
  349. {
  350. NRDebugger.Info("[NRInput] Init");
  351. NRDevice.Instance.Init();
  352. m_VisualManager = gameObject.AddComponent<ControllerVisualManager>();
  353. m_VisualManager.Init(m_States);
  354. SwitchControllerProvider(ControllerProviderFactory.controllerProviderType);
  355. #if UNITY_EDITOR
  356. InitEmulator();
  357. m_IsListeningToEditorValidateEvents = true;
  358. #endif
  359. SetInputSourceSafely(m_InputSourceType);
  360. }
  361. #if UNITY_EDITOR
  362. private void InitEmulator()
  363. {
  364. if (!NREmulatorManager.Inited && !GameObject.Find("NREmulatorManager"))
  365. {
  366. NREmulatorManager.Inited = true;
  367. GameObject.Instantiate(Resources.Load("Prefabs/NREmulatorManager"));
  368. }
  369. if (!GameObject.Find("NREmulatorController"))
  370. {
  371. Instantiate(Resources.Load<GameObject>("Prefabs/NREmulatorController"));
  372. }
  373. }
  374. #endif
  375. /// <summary> Gets camera center. </summary>
  376. /// <returns> The camera center. </returns>
  377. private Transform GetCameraCenter()
  378. {
  379. if (m_OverrideCameraCenter == null)
  380. {
  381. m_HasCheckedCameraCenter = true;
  382. return NRSessionManager.Instance.CenterCameraAnchor;
  383. }
  384. else
  385. {
  386. if (!m_HasCheckedCameraCenter)
  387. {
  388. CheckCameraCenter();
  389. }
  390. return m_OverrideCameraCenter;
  391. }
  392. }
  393. /// <summary> To guarantee the camera center was right. </summary>
  394. private void CheckCameraCenter()
  395. {
  396. if (m_OverrideCameraCenter != null
  397. && NRSessionManager.Instance != null
  398. && NRSessionManager.Instance.NRSessionBehaviour != null)
  399. {
  400. var cameraRigTransform = NRSessionManager.Instance.NRSessionBehaviour.transform;
  401. if (m_OverrideCameraCenter.parent == cameraRigTransform)
  402. {
  403. m_OverrideCameraCenter = NRSessionManager.Instance.CenterCameraAnchor;
  404. }
  405. }
  406. m_HasCheckedCameraCenter = true;
  407. }
  408. /// <summary> Convert hand to index. </summary>
  409. /// <param name="handEnum"> .</param>
  410. /// <returns> The hand converted to index. </returns>
  411. private static int ConvertHandToIndex(ControllerHandEnum handEnum)
  412. {
  413. if (GetAvailableControllersCount() < 2)
  414. {
  415. return DomainHand == handEnum ? 0 : 1;
  416. }
  417. else
  418. {
  419. return (int)handEnum;
  420. }
  421. }
  422. /// <summary> Gets controller state. </summary>
  423. /// <param name="hand"> The hand.</param>
  424. /// <returns> The controller state. </returns>
  425. private static ControllerState GetControllerState(ControllerHandEnum hand)
  426. {
  427. return m_States[ConvertHandToIndex(hand)];
  428. }
  429. /// <summary>
  430. /// Set the current input source with fallback
  431. /// </summary>
  432. /// <param name="inputSourceType"></param>
  433. private static void SetInputSourceSafely(InputSourceEnum inputSourceType)
  434. {
  435. var adaptInputSourceType = AdaptInputSource(inputSourceType);
  436. if (adaptInputSourceType != inputSourceType)
  437. {
  438. NRDebugger.Warning("[NRInput] AutoAdaptInputSource : {0} => {1}", inputSourceType, adaptInputSourceType);
  439. inputSourceType = adaptInputSourceType;
  440. }
  441. if (SetInputSource(inputSourceType))
  442. {
  443. return;
  444. }
  445. var fallbackInputSourceType = InputSourceEnum.Controller;
  446. NRDebugger.Info("[NRInput] Set Input Source To {0} Failed. Now Set Input Source To Fallback: {1}", inputSourceType, fallbackInputSourceType);
  447. SetInputSource(fallbackInputSourceType);
  448. }
  449. /// <summary> Auto adaption for inputSource based on supported feature on current device. </summary>
  450. /// <returns> Fallback inputSource. </returns>
  451. private static InputSourceEnum AdaptInputSource(InputSourceEnum inputSourceType)
  452. {
  453. if (inputSourceType == InputSourceEnum.Hands && !NRDevice.Subsystem.IsFeatureSupported(NRSupportedFeature.NR_FEATURE_HANDTRACKING))
  454. return InputSourceEnum.Controller;
  455. return inputSourceType;
  456. }
  457. /// <summary>
  458. /// To swith the controller provider
  459. /// </summary>
  460. /// <param name="providerType"></param>
  461. internal static void SwitchControllerProvider(Type providerType)
  462. {
  463. if (m_ControllerProvider != null && m_ControllerProvider.GetType() == providerType)
  464. return;
  465. var nextControllerProvider = ControllerProviderFactory.GetOrCreateControllerProvider(providerType, m_States);
  466. if (nextControllerProvider == null)
  467. return;
  468. if (m_ControllerProvider != null)
  469. {
  470. m_ControllerProvider.OnPause();
  471. }
  472. m_ControllerProvider = nextControllerProvider;
  473. if (m_ControllerProvider != null)
  474. {
  475. m_ControllerProvider.OnResume();
  476. }
  477. }
  478. /// <summary> Set the current enumeration of handedness. </summary>
  479. /// <param name="handEnum"> .</param>
  480. public static void SetDomainHandMode(ControllerHandEnum handEnum)
  481. {
  482. if (m_DomainHand == handEnum)
  483. return;
  484. m_DomainHand = handEnum;
  485. if (OnDomainHandChanged != null)
  486. {
  487. OnDomainHandChanged(m_DomainHand);
  488. }
  489. }
  490. /// <summary> Set the current input source. </summary>
  491. /// <param name="inputSourceType"></param>
  492. /// <returns> The result of setting input source. </returns>
  493. public static bool SetInputSource(InputSourceEnum inputSourceType)
  494. {
  495. NRDebugger.Info("[NRInput] Set Input Source: " + inputSourceType);
  496. if (Instance == null)
  497. {
  498. return false;
  499. }
  500. bool success = true;
  501. switch (inputSourceType)
  502. {
  503. case InputSourceEnum.Hands:
  504. success = Hands.StartHandTracking();
  505. break;
  506. case InputSourceEnum.Controller:
  507. success = Hands.StopHandTracking();
  508. break;
  509. default:
  510. break;
  511. }
  512. if (success)
  513. {
  514. Instance.m_InputSourceType = inputSourceType;
  515. #if UNITY_EDITOR
  516. Instance.m_EditorCachedInputSourceType = inputSourceType;
  517. #endif
  518. }
  519. NRDebugger.Info("[NRInput] Input Source Set. Current Input Source = " + CurrentInputSourceType);
  520. return success;
  521. }
  522. /// <summary> Get the current count of controllers which are connected and available. </summary>
  523. /// <returns> The available controllers count. </returns>
  524. public static int GetAvailableControllersCount()
  525. {
  526. if (m_ControllerProvider == null)
  527. {
  528. return 0;
  529. }
  530. return m_ControllerProvider.ControllerCount;
  531. }
  532. /// <summary> Get the ControllerType of current controller. </summary>
  533. /// <returns> The controller type. </returns>
  534. public static ControllerType GetControllerType()
  535. {
  536. return GetControllerState(DomainHand).controllerType;
  537. }
  538. /// <summary> Get the ConnectionState of current controller. </summary>
  539. /// <returns> The controller connection state. </returns>
  540. public static ControllerConnectionState GetControllerConnectionState()
  541. {
  542. return GetControllerState(DomainHand).connectionState;
  543. }
  544. /// <summary> Returns true if the controller is available. </summary>
  545. /// <param name="handEnum"> .</param>
  546. /// <returns> True if it succeeds, false if it fails. </returns>
  547. public static bool CheckControllerAvailable(ControllerHandEnum handEnum)
  548. {
  549. if (m_ControllerProvider is NRHandControllerProvider)
  550. {
  551. return Hands.GetHandState(handEnum == ControllerHandEnum.Right ? HandEnum.RightHand : HandEnum.LeftHand).pointerPoseValid;
  552. }
  553. int availableCount = GetAvailableControllersCount();
  554. if (availableCount == 2)
  555. {
  556. return true;
  557. }
  558. if (availableCount == 1)
  559. {
  560. return handEnum == DomainHand;
  561. }
  562. return false;
  563. }
  564. /// <summary> Returns true if the current controller supports the certain feature. </summary>
  565. /// <param name="feature"> The feature.</param>
  566. /// <returns> True if it succeeds, false if it fails. </returns>
  567. public static bool GetControllerAvailableFeature(ControllerAvailableFeature feature)
  568. {
  569. if (GetAvailableControllersCount() == 0)
  570. return false;
  571. return GetControllerState(m_DomainHand).IsFeatureAvailable(feature);
  572. }
  573. /// <summary> Returns true if the button is currently pressed this frame. </summary>
  574. /// <param name="button"> The button.</param>
  575. /// <returns> True if it succeeds, false if it fails. </returns>
  576. public static bool GetButton(ControllerButton button)
  577. {
  578. return GetButton(m_DomainHand, button);
  579. }
  580. /// <summary> Returns true if the button was pressed down this frame. </summary>
  581. /// <param name="button"> The button.</param>
  582. /// <returns> True if it succeeds, false if it fails. </returns>
  583. public static bool GetButtonDown(ControllerButton button)
  584. {
  585. return GetButtonDown(m_DomainHand, button);
  586. }
  587. /// <summary> Returns true if the button was released this frame. </summary>
  588. /// <param name="button"> The button.</param>
  589. /// <returns> True if it succeeds, false if it fails. </returns>
  590. public static bool GetButtonUp(ControllerButton button)
  591. {
  592. return GetButtonUp(m_DomainHand, button);
  593. }
  594. /// <summary> Returns true if the touchpad is being touched. </summary>
  595. /// <returns> True if touching, false if not. </returns>
  596. public static bool IsTouching()
  597. {
  598. return IsTouching(m_DomainHand);
  599. }
  600. /// <summary>
  601. /// Returns a Vector2 touch position on touchpad of the domain controller, range: x(-1f ~ 1f), y(-
  602. /// 1f ~ 1f) </summary>
  603. /// <returns> The touch. </returns>
  604. public static Vector2 GetTouch()
  605. {
  606. return GetTouch(m_DomainHand);
  607. }
  608. /// <summary> Returns a Vector2 delta touch value on touchpad of the domain controller. </summary>
  609. /// <returns> The delta touch. </returns>
  610. public static Vector2 GetDeltaTouch()
  611. {
  612. return GetDeltaTouch(m_DomainHand);
  613. }
  614. /// <summary>
  615. /// Returns the current position of the domain controller if 6dof, otherwise returns Vector3.zero. </summary>
  616. /// <returns> The position. </returns>
  617. public static Vector3 GetPosition()
  618. {
  619. return GetPosition(m_DomainHand);
  620. }
  621. /// <summary> Returns the current rotation of the domain controller. </summary>
  622. /// <returns> The rotation. </returns>
  623. public static Quaternion GetRotation()
  624. {
  625. return GetRotation(m_DomainHand);
  626. }
  627. /// <summary> Returns the gyro sensor value of the domain controller. </summary>
  628. /// <returns> The gyro. </returns>
  629. public static Vector3 GetGyro()
  630. {
  631. return GetGyro(m_DomainHand);
  632. }
  633. /// <summary> Returns the accel sensor value of the domain controller. </summary>
  634. /// <returns> The accel. </returns>
  635. public static Vector3 GetAccel()
  636. {
  637. return GetAccel(m_DomainHand);
  638. }
  639. /// <summary> Returns the magnetic sensor value of the domain controller. </summary>
  640. /// <returns> The magnitude. </returns>
  641. public static Vector3 GetMag()
  642. {
  643. return GetMag(m_DomainHand);
  644. }
  645. /// <summary> Returns the battery level of the domain controller. </summary>
  646. /// <returns> The controller battery. </returns>
  647. public static int GetControllerBattery()
  648. {
  649. return GetControllerBattery(DomainHand);
  650. }
  651. /// <summary> Trigger vibration of the domain controller. </summary>
  652. /// <param name="durationSeconds"> (Optional) The duration in seconds.</param>
  653. /// <param name="frequency"> (Optional) The frequency.</param>
  654. /// <param name="amplitude"> (Optional) The amplitude.</param>
  655. public static void TriggerHapticVibration(float durationSeconds = 0.1f, float frequency = 200f, float amplitude = 0.8f)
  656. {
  657. TriggerHapticVibration(m_DomainHand, durationSeconds, frequency, amplitude);
  658. }
  659. /// <summary>
  660. /// Returns true if the button is currently pressed this frame on a certain handedness
  661. /// controller. </summary>
  662. /// <param name="hand"> The hand.</param>
  663. /// <param name="button"> The button.</param>
  664. /// <returns> True if it succeeds, false if it fails. </returns>
  665. public static bool GetButton(ControllerHandEnum hand, ControllerButton button)
  666. {
  667. return GetControllerState(hand).GetButton(button);
  668. }
  669. /// <summary>
  670. /// Returns true if the button was pressed down this frame on a certain handedness controller. </summary>
  671. /// <param name="hand"> The hand.</param>
  672. /// <param name="button"> The button.</param>
  673. /// <returns> True if it succeeds, false if it fails. </returns>
  674. public static bool GetButtonDown(ControllerHandEnum hand, ControllerButton button)
  675. {
  676. return GetControllerState(hand).GetButtonDown(button);
  677. }
  678. /// <summary>
  679. /// Returns true if the button was released this frame on a certain handedness controller. </summary>
  680. /// <param name="hand"> The hand.</param>
  681. /// <param name="button"> The button.</param>
  682. /// <returns> True if it succeeds, false if it fails. </returns>
  683. public static bool GetButtonUp(ControllerHandEnum hand, ControllerButton button)
  684. {
  685. return GetControllerState(hand).GetButtonUp(button);
  686. }
  687. /// <summary>
  688. /// Returns true if the touchpad is being touched this frame on a certain handedness controller. </summary>
  689. /// <param name="hand"> The hand.</param>
  690. /// <returns> True if touching, false if not. </returns>
  691. public static bool IsTouching(ControllerHandEnum hand)
  692. {
  693. return GetControllerState(hand).isTouching;
  694. }
  695. /// <summary>
  696. /// Returns a Vector2 touch position on touchpad of a certain handedness controller, range: x(-1f
  697. /// ~ 1f), y(-1f ~ 1f) </summary>
  698. /// <param name="hand"> The hand.</param>
  699. /// <returns> The touch. </returns>
  700. public static Vector2 GetTouch(ControllerHandEnum hand)
  701. {
  702. return GetControllerState(hand).touchPos;
  703. }
  704. /// <summary>
  705. /// Returns a Vector2 delta touch value on touchpad of a certain handedness controller. </summary>
  706. /// <param name="hand"> The hand.</param>
  707. /// <returns> The delta touch. </returns>
  708. public static Vector2 GetDeltaTouch(ControllerHandEnum hand)
  709. {
  710. return GetControllerState(hand).deltaTouch;
  711. }
  712. /// <summary>
  713. /// Returns the current position of a certain handedness controller if 6dof, otherwise returns
  714. /// Vector3.zero. </summary>
  715. /// <param name="hand"> The hand.</param>
  716. /// <returns> The position. </returns>
  717. public static Vector3 GetPosition(ControllerHandEnum hand)
  718. {
  719. return GetControllerState(hand).position;
  720. }
  721. /// <summary> Returns the current rotation of a certain handedness controller. </summary>
  722. /// <param name="hand"> The hand.</param>
  723. /// <returns> The rotation. </returns>
  724. public static Quaternion GetRotation(ControllerHandEnum hand)
  725. {
  726. return GetControllerState(hand).rotation;
  727. }
  728. /// <summary> Returns the gyro sensor value of a certain handedness controller. </summary>
  729. /// <param name="hand"> The hand.</param>
  730. /// <returns> The gyro. </returns>
  731. public static Vector3 GetGyro(ControllerHandEnum hand)
  732. {
  733. return GetControllerState(hand).gyro;
  734. }
  735. /// <summary> Returns the accel sensor value of a certain handedness controller. </summary>
  736. /// <param name="hand"> The hand.</param>
  737. /// <returns> The accel. </returns>
  738. public static Vector3 GetAccel(ControllerHandEnum hand)
  739. {
  740. return GetControllerState(hand).accel;
  741. }
  742. /// <summary> Returns the magnetic sensor value of a certain handedness controller. </summary>
  743. /// <param name="hand"> The hand.</param>
  744. /// <returns> The magnitude. </returns>
  745. public static Vector3 GetMag(ControllerHandEnum hand)
  746. {
  747. return GetControllerState(hand).mag;
  748. }
  749. /// <summary>
  750. /// Returns the battery level of a certain handedness controller, range from 0 to 100. </summary>
  751. /// <param name="hand"> The hand.</param>
  752. /// <returns> The controller battery. </returns>
  753. public static int GetControllerBattery(ControllerHandEnum hand)
  754. {
  755. return GetControllerState(hand).batteryLevel;
  756. }
  757. /// <summary> Trigger vibration of a certain handedness controller. </summary>
  758. /// <param name="hand"> The hand.</param>
  759. /// <param name="durationSeconds"> (Optional) The duration in seconds.</param>
  760. /// <param name="frequency"> (Optional) The frequency.</param>
  761. /// <param name="amplitude"> (Optional) The amplitude.</param>
  762. public static void TriggerHapticVibration(ControllerHandEnum hand, float durationSeconds = 0.1f, float frequency = 200f, float amplitude = 0.8f)
  763. {
  764. if (!HapticVibrationEnabled)
  765. return;
  766. if (GetAvailableControllersCount() == 0)
  767. return;
  768. m_ControllerProvider.TriggerHapticVibration(ConvertHandToIndex(hand), durationSeconds, frequency, amplitude);
  769. }
  770. /// <summary> Recenter controller. </summary>
  771. public static void RecenterController()
  772. {
  773. if (GetAvailableControllersCount() == 0)
  774. return;
  775. m_IgnoreRecenterCallback = false;
  776. m_ControllerProvider.Recenter();
  777. }
  778. /// <summary> Add button down event listerner. </summary>
  779. /// <param name="hand"> The hand.</param>
  780. /// <param name="button"> The button.</param>
  781. /// <param name="action"> The action.</param>
  782. public static void AddDownListener(ControllerHandEnum hand, ControllerButton button, Action action)
  783. {
  784. GetControllerState(hand).AddButtonListener(ButtonEventType.Down, button, action);
  785. }
  786. /// <summary> Remove button down event listerner. </summary>
  787. /// <param name="hand"> The hand.</param>
  788. /// <param name="button"> The button.</param>
  789. /// <param name="action"> The action.</param>
  790. public static void RemoveDownListener(ControllerHandEnum hand, ControllerButton button, Action action)
  791. {
  792. GetControllerState(hand).RemoveButtonListener(ButtonEventType.Down, button, action);
  793. }
  794. /// <summary> Add button pressing event listerner. </summary>
  795. /// <param name="hand"> The hand.</param>
  796. /// <param name="button"> The button.</param>
  797. /// <param name="action"> The action.</param>
  798. public static void AddPressingListener(ControllerHandEnum hand, ControllerButton button, Action action)
  799. {
  800. GetControllerState(hand).AddButtonListener(ButtonEventType.Pressing, button, action);
  801. }
  802. /// <summary> Remove button pressing event listerner. </summary>
  803. /// <param name="hand"> The hand.</param>
  804. /// <param name="button"> The button.</param>
  805. /// <param name="action"> The action.</param>
  806. public static void RemovePressingListener(ControllerHandEnum hand, ControllerButton button, Action action)
  807. {
  808. GetControllerState(hand).RemoveButtonListener(ButtonEventType.Pressing, button, action);
  809. }
  810. /// <summary> Add button up event listerner. </summary>
  811. /// <param name="hand"> The hand.</param>
  812. /// <param name="button"> The button.</param>
  813. /// <param name="action"> The action.</param>
  814. public static void AddUpListener(ControllerHandEnum hand, ControllerButton button, Action action)
  815. {
  816. GetControllerState(hand).AddButtonListener(ButtonEventType.Up, button, action);
  817. }
  818. /// <summary> Remove button up event listerner. </summary>
  819. /// <param name="hand"> The hand.</param>
  820. /// <param name="button"> The button.</param>
  821. /// <param name="action"> The action.</param>
  822. public static void RemoveUpListener(ControllerHandEnum hand, ControllerButton button, Action action)
  823. {
  824. GetControllerState(hand).RemoveButtonListener(ButtonEventType.Up, button, action);
  825. }
  826. /// <summary> Add button click event listerner. </summary>
  827. /// <param name="hand"> The hand.</param>
  828. /// <param name="button"> The button.</param>
  829. /// <param name="action"> The action.</param>
  830. public static void AddClickListener(ControllerHandEnum hand, ControllerButton button, Action action)
  831. {
  832. GetControllerState(hand).AddButtonListener(ButtonEventType.Click, button, action);
  833. }
  834. /// <summary> Remove button click event listerner. </summary>
  835. /// <param name="hand"> The hand.</param>
  836. /// <param name="button"> The button.</param>
  837. /// <param name="action"> The action.</param>
  838. public static void RemoveClickListener(ControllerHandEnum hand, ControllerButton button, Action action)
  839. {
  840. GetControllerState(hand).RemoveButtonListener(ButtonEventType.Click, button, action);
  841. }
  842. }
  843. }