NRInput.cs 39 KB

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