ZEDControllerTracker_DemoInputs.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6. #if UNITY_2019_3_OR_NEWER
  7. using UnityEngine.XR;
  8. #endif
  9. #if ZED_STEAM_VR
  10. using Valve.VR;
  11. #endif
  12. /// <summary>
  13. /// Extended version of ZEDControllerTracker that also checks for several inputs in a generic way.
  14. /// You can check a state with
  15. /// Used because input methods vary a lot between controllers and between SteamVR (new and old) and Oculus.
  16. /// See base class ZEDControllerTracker for any code that don't directly relate to inputs.
  17. /// </summary>
  18. public class ZEDControllerTracker_DemoInputs : ZEDControllerTracker
  19. {
  20. //#if ZED_STEAM_VR
  21. #if ZED_SVR_2_0_INPUT
  22. /// !! On v2.0, Steam VR action bindings must be done in the inspector ro once steam.initialize(true) has been called !!
  23. /// <summary>
  24. /// SteamVR action to cause a Fire event when checked or subscribed to.
  25. /// </summary>
  26. [Header("SteamVR Plugin 2.0 Bindings")]
  27. [Tooltip("SteamVR action to cause a Fire event when checked or subscribed to.")]
  28. public SteamVR_Action_Boolean fireBinding;// = SteamVR_Input.GetAction<SteamVR_Action_Boolean>("Fire");
  29. /// <summary>
  30. /// SteamVR action to cause a Click event when checked or subscribed to.
  31. /// </summary>
  32. [Tooltip("SteamVR action to cause a Click event when checked or subscribed to.")]
  33. public SteamVR_Action_Boolean clickBinding;// = SteamVR_Input.GetAction<SteamVR_Action_Boolean>("Click");
  34. /// <summary>
  35. /// SteamVR action to cause a Back event when checked or subscribed to.
  36. /// </summary>
  37. [Tooltip("SteamVR action to cause a Back event when checked or subscribed to.")]
  38. public SteamVR_Action_Boolean backBinding;// = SteamVR_Input.GetAction<SteamVR_Action_Boolean>("Back");
  39. /// <summary>
  40. /// SteamVR action to cause a Grab event when checked or subscribed to.
  41. /// </summary>
  42. [Tooltip("SteamVR action to cause a Grab event when checked or subscribed to.")]
  43. public SteamVR_Action_Boolean grabBinding;// = SteamVR_Input.GetAction<SteamVR_Action_Boolean>("Grab");
  44. /// <summary>
  45. /// SteamVR action to cause a Vector2 UI navigation event when checked or subscribed to.
  46. /// </summary>
  47. [Tooltip("SteamVR action to cause a UI navigation event when checked or subscribed to.")]
  48. public SteamVR_Action_Vector2 navigateUIBinding;// = SteamVR_Input.GetAction<SteamVR_Action_Vector2>("NavigateUI");
  49. #elif ZED_STEAM_VR
  50. /// <summary>
  51. /// Legacy SteamVR button to cause a Fire event when checked or subscribed to.
  52. /// </summary>
  53. [Header("SteamVR Legacy Input Bindings")]
  54. [Tooltip("Legacy SteamVR button to cause a Fire event when checked or subscribed to.")]
  55. public EVRButtonId fireBinding_Legacy = EVRButtonId.k_EButton_SteamVR_Trigger;
  56. /// <summary>
  57. /// Legacy SteamVR button to cause a Click event when checked or subscribed to.
  58. /// </summary>
  59. [Tooltip("Legacy SteamVR button to cause a Click event when checked or subscribed to.")]
  60. public EVRButtonId clickBinding_Legacy = EVRButtonId.k_EButton_SteamVR_Trigger;
  61. /// <summary>
  62. /// Legacy SteamVR button to cause a Back event when checked or subscribed to.
  63. /// </summary>
  64. [Tooltip("Legacy SteamVR button to cause a Back event when checked or subscribed to.")]
  65. public EVRButtonId backBinding_Legacy = EVRButtonId.k_EButton_Grip;
  66. /// <summary>
  67. /// Legacy SteamVR button to cause a Grip event when checked or subscribed to.
  68. /// </summary>
  69. [Tooltip("Legacy SteamVR button to cause a Grip event when checked or subscribed to.")]
  70. public EVRButtonId grabBinding_Legacy = EVRButtonId.k_EButton_SteamVR_Trigger;
  71. /// <summary>
  72. /// Legacy SteamVR axis to cause a Vector2 Navigate UI event when checked or subscribed to.
  73. /// </summary>
  74. [Tooltip("Legacy SteamVR button to cause a Vector2 Navigate UI event when checked or subscribed to.")]
  75. public EVRButtonId navigateUIBinding_Legacy = EVRButtonId.k_EButton_Axis0;
  76. #endif
  77. #if ZED_OCULUS
  78. public static bool ovrUpdateCalledThisFrame = false;
  79. #if UNITY_2019_3_OR_NEWER
  80. /// <summary>
  81. /// Input Button checked to signal a Fire event when checked or subscribed to.
  82. /// </summary>
  83. [Header("Input Bindings")]
  84. [Tooltip("Input Button checked to signal a Fire event when checked or subscribed to")]
  85. public InputFeatureUsage<bool> fireButton = CommonUsages.triggerButton;
  86. /// <summary>
  87. /// Input Button checked to signal a Click event when checked or subscribed to.
  88. /// </summary>
  89. [Tooltip("Input Button checked to signal a Click event when checked or subscribed to")]
  90. public InputFeatureUsage<bool> clickButton = CommonUsages.triggerButton;
  91. /// <summary>
  92. /// Input Button checked to signal a Back event when checked or subscribed to.
  93. /// </summary>
  94. [Tooltip("Input Button checked to signal a Back event when checked or subscribed to")]
  95. public InputFeatureUsage<bool> backButton = CommonUsages.secondaryButton; //Y, or B if just right controller is connected.
  96. /// <summary>
  97. /// Input Button checked to signal a Grab event when checked or subscribed to.
  98. /// </summary>
  99. [Tooltip("Input Button checked to signal a Grab event when checked or subscribed to")]
  100. public InputFeatureUsage<bool> grabButton = CommonUsages.gripButton;
  101. /// <summary>
  102. /// Input Button checked to signal a Vector2 UI navigation event when checked or subscribed to.
  103. /// </summary>
  104. [Tooltip("Input Button checked to signal a Vector2 UI navigation event when checked or subscribed to")]
  105. public InputFeatureUsage<Vector2> navigateUIAxis = CommonUsages.primary2DAxis;
  106. private bool fireActive = false;
  107. private bool clickActive = false;
  108. private bool backActive = false;
  109. private bool grabActive = false;
  110. #else
  111. /// <summary>
  112. /// Oculus Button checked to signal a Fire event when checked or subscribed to.
  113. /// </summary>
  114. [Header("Oculus Input Bindings")]
  115. [Tooltip("Oculus Button checked to signal a Fire event when checked or subscribed to")]
  116. public OVRInput.Button fireButton = OVRInput.Button.PrimaryIndexTrigger;
  117. /// <summary>
  118. /// Oculus Button checked to signal a Click event when checked or subscribed to.
  119. /// </summary>
  120. [Tooltip("Oculus Button checked to signal a Click event when checked or subscribed to")]
  121. public OVRInput.Button clickButton = OVRInput.Button.PrimaryIndexTrigger;
  122. /// <summary>
  123. /// Oculus Button checked to signal a Back event when checked or subscribed to.
  124. /// </summary>
  125. [Tooltip("Oculus Button checked to signal a Back event when checked or subscribed to")]
  126. public OVRInput.Button backButton = OVRInput.Button.Two; //Y, or B if just right controller is connected.
  127. /// <summary>
  128. /// Oculus Button checked to signal a Grab event when checked or subscribed to.
  129. /// </summary>
  130. [Tooltip("Oculus Button checked to signal a Grab event when checked or subscribed to")]
  131. public OVRInput.Button grabButton = OVRInput.Button.PrimaryHandTrigger;
  132. /// <summary>
  133. /// Oculus Button checked to signal a Vector2 UI navigation event when checked or subscribed to.
  134. /// </summary>
  135. [Tooltip("Oculus Button checked to signal a Vector2 UI navigation event when checked or subscribed to")]
  136. public OVRInput.Axis2D navigateUIAxis = OVRInput.Axis2D.PrimaryThumbstick;
  137. #endif
  138. #endif
  139. /// <summary>
  140. /// Events called when the Fire button/action was just pressed.
  141. /// </summary>
  142. [Header("Events")]
  143. [Space(5)]
  144. [Tooltip("Events called when the Fire button/action was just pressed.")]
  145. public UnityEvent onFireDown;
  146. /// <summary>
  147. /// Events called when the Fire button/action was just released.
  148. /// </summary>
  149. [Tooltip("Events called when the Fire button/action was just released.")]
  150. public UnityEvent onFireUp;
  151. /// <summary>
  152. /// Events called when the Click button/action was just pressed.
  153. /// </summary>
  154. [Tooltip("Events called when the Click button/action was just pressed.")]
  155. public UnityEvent onClickDown;
  156. /// <summary>
  157. /// Events called when the Click button/action was just released.
  158. /// </summary>
  159. [Tooltip("Events called when the Click button/action was just released.")]
  160. public UnityEvent onClickUp;
  161. /// <summary>
  162. /// Events called when the Back button/action was just pressed.
  163. /// </summary>
  164. [Tooltip("Events called when the Back button/action was just pressed.")]
  165. public UnityEvent onBackDown;
  166. /// <summary>
  167. /// Events called when the Back button/action was just released.
  168. /// </summary>
  169. [Tooltip("Events called when the Back button/action was just released.")]
  170. public UnityEvent onBackUp;
  171. /// <summary>
  172. /// Events called when the Grab button/action was just pressed.
  173. /// </summary>
  174. [Tooltip("Events called when the Grab button/action was just pressed.")]
  175. public UnityEvent onGrabDown;
  176. /// <summary>
  177. /// Events called when the Grab button/action was just released.
  178. /// </summary>
  179. [Tooltip("Events called when the Grab button/action was just released.")]
  180. public UnityEvent onGrabUp;
  181. /// <summary>
  182. /// Returns if the Fire button/action matched the provided state.
  183. /// </summary>
  184. /// <param name="state">Whether to check if the button/action is just pressed, just released, or is being held down.</param>
  185. public bool CheckFireButton(ControllerButtonState state)
  186. {
  187. #if ZED_SVR_2_0_INPUT
  188. return CheckSteamVRBoolActionState(fireBinding, state);
  189. #elif ZED_STEAM_VR
  190. return CheckSteamVRButtonState_Legacy(fireBinding_Legacy, state);
  191. #endif
  192. #if ZED_OCULUS
  193. #if UNITY_2019_3_OR_NEWER
  194. return CheckButtonState(fireButton, state, fireActive);
  195. #else
  196. return CheckOculusButtonState(fireButton, state);
  197. #endif
  198. #endif
  199. return false;
  200. }
  201. /// <summary>
  202. /// Returns if the Click button/action matched the provided state.
  203. /// </summary>
  204. /// <param name="state">Whether to check if the button/action is just pressed, just released, or is being held down.</param>
  205. public bool CheckClickButton(ControllerButtonState state)
  206. {
  207. #if ZED_SVR_2_0_INPUT
  208. return CheckSteamVRBoolActionState(clickBinding, state);
  209. #elif ZED_STEAM_VR
  210. return CheckSteamVRButtonState_Legacy(clickBinding_Legacy, state);
  211. #endif
  212. #if ZED_OCULUS
  213. #if UNITY_2019_3_OR_NEWER
  214. return CheckButtonState(clickButton, state, clickActive);
  215. #else
  216. return CheckOculusButtonState(clickButton, state);
  217. #endif
  218. #endif
  219. return false;
  220. }
  221. /// <summary>
  222. /// Returns if the Back button/action matched the provided state.
  223. /// </summary>
  224. /// <param name="state">Whether to check if the button/action is just pressed, just released, or is being held down.</param>
  225. public bool CheckBackButton(ControllerButtonState state)
  226. {
  227. #if ZED_SVR_2_0_INPUT
  228. return CheckSteamVRBoolActionState(backBinding, state);
  229. #elif ZED_STEAM_VR
  230. return CheckSteamVRButtonState_Legacy(backBinding_Legacy, state);
  231. #endif
  232. #if ZED_OCULUS
  233. #if UNITY_2019_3_OR_NEWER
  234. return CheckButtonState(backButton, state, backActive);
  235. #else
  236. return CheckOculusButtonState(backButton, state);
  237. #endif
  238. #endif
  239. return false;
  240. }
  241. /// <summary>
  242. /// Returns if the Grab button/action matched the provided state.
  243. /// </summary>
  244. /// <param name="state">Whether to check if the button/action is just pressed, just released, or is being held down.</param>
  245. public bool CheckGrabButton(ControllerButtonState state)
  246. {
  247. #if ZED_SVR_2_0_INPUT
  248. return CheckSteamVRBoolActionState(grabBinding, state);
  249. #elif ZED_STEAM_VR
  250. return CheckSteamVRButtonState_Legacy(grabBinding_Legacy, state);
  251. #endif
  252. #if ZED_OCULUS
  253. #if UNITY_2019_3_OR_NEWER
  254. return CheckButtonState(grabButton, state, grabActive);
  255. #else
  256. return CheckOculusButtonState(grabButton, state);
  257. #endif
  258. #endif
  259. return false;
  260. }
  261. /// <summary>
  262. /// Returns the current 2D axis value of the NavigateUIAxis button/action.
  263. /// </summary>
  264. public Vector2 CheckNavigateUIAxis()
  265. {
  266. #if ZED_SVR_2_0_INPUT
  267. return CheckSteamVR2DAxis(navigateUIBinding);
  268. #elif ZED_STEAM_VR
  269. return CheckSteamVRAxis_Legacy(navigateUIBinding_Legacy);
  270. #endif
  271. #if ZED_OCULUS
  272. #if UNITY_2019_3_OR_NEWER
  273. return Check2DAxisState(navigateUIAxis);
  274. #else
  275. return CheckOculus2DAxisState(navigateUIAxis);
  276. #endif
  277. #endif
  278. return Vector3.zero;
  279. }
  280. protected override void Awake()
  281. {
  282. base.Awake();
  283. #if ZED_SVR_2_0_INPUT
  284. if (!useLegacySteamVRInput)
  285. {
  286. if(!SteamVR.active) SteamVR.Initialize(true); //Force SteamVR to activate, so we can use the input system.
  287. //script binding example
  288. //fireBinding = SteamVR_Input._default.inActions.GrabGrip; //...
  289. }
  290. #endif
  291. }
  292. protected override void Update()
  293. {
  294. base.Update();
  295. if (CheckClickButton(ControllerButtonState.Down)) onClickDown.Invoke();
  296. if (CheckClickButton(ControllerButtonState.Up)) onClickUp.Invoke();
  297. if (CheckFireButton(ControllerButtonState.Down)) onFireDown.Invoke();
  298. if (CheckFireButton(ControllerButtonState.Up)) onFireUp.Invoke();
  299. if (CheckBackButton(ControllerButtonState.Down)) onBackDown.Invoke();
  300. if (CheckBackButton(ControllerButtonState.Up)) onBackUp.Invoke();
  301. if (CheckGrabButton(ControllerButtonState.Down)) onGrabDown.Invoke();
  302. if (CheckGrabButton(ControllerButtonState.Up)) onGrabUp.Invoke();
  303. }
  304. protected void LateUpdate()
  305. {
  306. #if ZED_OCULUS
  307. ovrUpdateCalledThisFrame = false;
  308. #endif
  309. }
  310. #if ZED_STEAM_VR
  311. protected override void UpdateControllerState()
  312. {
  313. base.UpdateControllerState();
  314. //If using legacy SteamVR input, we check buttons directly from the OpenVR API.
  315. #if ZED_SVR_2_0_INPUT //If using SteamVR plugin 2.0 or higher, give the option to use legacy input.
  316. if (useLegacySteamVRInput)
  317. {
  318. openvrsystem.GetControllerState((uint)index, ref controllerstate, controllerstatesize);
  319. }
  320. #else //We're using an older SteamVR plugin, so we need to use the legacy input.
  321. openvrsystem.GetControllerState((uint)index, ref controllerstate, controllerstatesize);
  322. #endif
  323. }
  324. #endif
  325. #if ZED_OCULUS
  326. /// <summary>
  327. /// Checks the button state of a given Oculus button.
  328. /// </summary>
  329. /// <param name="state">Whether to check if the button/action is just pressed, just released, or is being held down.</param>
  330. public bool CheckOculusButtonState(OVRInput.Button button, ControllerButtonState state)
  331. {
  332. if (!ovrUpdateCalledThisFrame)
  333. {
  334. OVRInput.Update();
  335. ovrUpdateCalledThisFrame = true;
  336. }
  337. bool result = false;
  338. switch (state)
  339. {
  340. case ControllerButtonState.Down:
  341. result = OVRInput.GetDown(button, GetOculusController());
  342. break;
  343. case ControllerButtonState.Held:
  344. result = OVRInput.Get(button, GetOculusController());
  345. break;
  346. case ControllerButtonState.Up:
  347. result = OVRInput.GetUp(button, GetOculusController());
  348. break;
  349. }
  350. return result;
  351. }
  352. #if UNITY_2019_3_OR_NEWER
  353. public bool CheckButtonState(InputFeatureUsage<bool> button, ControllerButtonState state, bool isActive){
  354. bool down = false;
  355. bool up = false;
  356. InputDevice device = new InputDevice();
  357. if (deviceToTrack == Devices.LeftController)
  358. device = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand);
  359. else device = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
  360. ProcessInputDeviceButton(device, button, ref isActive,
  361. () => // On Button Down
  362. {
  363. down = true;
  364. },
  365. () => // On Button Up
  366. {
  367. up = true;
  368. });
  369. if (state == ControllerButtonState.Down) return down;
  370. if (state == ControllerButtonState.Up) return up;
  371. else return false;
  372. }
  373. public Vector2 Check2DAxisState(InputFeatureUsage<Vector2> navigateUIAxis){
  374. InputDevice device = new InputDevice();
  375. if (deviceToTrack == Devices.LeftController)
  376. device = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand);
  377. else device = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
  378. Vector2 result = Vector2.zero;
  379. if (device.TryGetFeatureValue(navigateUIAxis, out Vector2 value))
  380. result = value;
  381. return result;
  382. }
  383. private void ProcessInputDeviceButton(InputDevice inputDevice, InputFeatureUsage<bool> button, ref bool _wasPressedDownPreviousFrame, Action onButtonDown = null, Action onButtonUp = null, Action onButtonHeld = null)
  384. {
  385. if (inputDevice.TryGetFeatureValue(button, out bool isPressed) && isPressed)
  386. {
  387. if (!_wasPressedDownPreviousFrame) // // this is button down
  388. {
  389. onButtonDown?.Invoke();
  390. }
  391. _wasPressedDownPreviousFrame = true;
  392. onButtonHeld?.Invoke();
  393. }
  394. else
  395. {
  396. if (_wasPressedDownPreviousFrame) // this is button up
  397. {
  398. onButtonUp?.Invoke();
  399. }
  400. _wasPressedDownPreviousFrame = false;
  401. }
  402. }
  403. #endif
  404. /// <summary>
  405. /// Returns the axis of a given Oculus axis button/joystick.
  406. /// </summary>
  407. public Vector3 CheckOculus2DAxisState(OVRInput.Axis2D axis)
  408. {
  409. if (!ovrUpdateCalledThisFrame)
  410. {
  411. OVRInput.Update();
  412. ovrUpdateCalledThisFrame = true;
  413. }
  414. return OVRInput.Get(axis, GetOculusController());
  415. }
  416. /// <summary>
  417. /// Returns the Oculus controller script of the controller currently attached to this object.
  418. /// </summary>
  419. public OVRInput.Controller GetOculusController()
  420. {
  421. if (deviceToTrack == Devices.LeftController) return OVRInput.Controller.LTouch;
  422. else if (deviceToTrack == Devices.RightController) return OVRInput.Controller.RTouch;
  423. else return OVRInput.Controller.None;
  424. }
  425. #endif
  426. //#if ZED_STEAM_VR
  427. #if ZED_SVR_2_0_INPUT
  428. /// <summary>
  429. /// Checks the button state of a given SteamVR boolean action.
  430. /// </summary>
  431. /// <param name="state">Whether to check if the button/action is just pressed, just released, or is being held down.</param>
  432. protected bool CheckSteamVRBoolActionState(SteamVR_Action_Boolean action, ControllerButtonState buttonstate)
  433. {
  434. switch (buttonstate)
  435. {
  436. case ControllerButtonState.Down:
  437. return action.GetLastStateDown(GetSteamVRInputSource());
  438. case ControllerButtonState.Held:
  439. return action.GetLastState(GetSteamVRInputSource());
  440. case ControllerButtonState.Up:
  441. return action.GetLastStateUp(GetSteamVRInputSource());
  442. default:
  443. return false;
  444. }
  445. }
  446. /// <summary>
  447. /// Returns the axis of a given SteamVR 2D action.
  448. /// </summary>
  449. protected Vector2 CheckSteamVR2DAxis(SteamVR_Action_Vector2 action)
  450. {
  451. return action.GetAxis(GetSteamVRInputSource());
  452. }
  453. public SteamVR_Input_Sources GetSteamVRInputSource()
  454. {
  455. if (deviceToTrack == Devices.LeftController) return SteamVR_Input_Sources.LeftHand;
  456. else if (deviceToTrack == Devices.RightController) return SteamVR_Input_Sources.RightHand;
  457. else return SteamVR_Input_Sources.Any;
  458. }
  459. #elif ZED_STEAM_VR
  460. public bool CheckSteamVRButtonState_Legacy(EVRButtonId button, ControllerButtonState state)
  461. {
  462. switch(state)
  463. {
  464. case ControllerButtonState.Down:
  465. return GetVRButtonDown_Legacy(button);
  466. case ControllerButtonState.Held:
  467. default:
  468. return GetVRButtonHeld_Legacy(button);
  469. case ControllerButtonState.Up:
  470. return GetVRButtonReleased_Legacy(button);
  471. }
  472. }
  473. /// <summary>
  474. /// Returns if the VR controller button with the given ID was pressed for the first time this frame.
  475. /// </summary>
  476. /// <param name="buttonid">EVR ID of the button as listed in OpenVR.</param>
  477. public bool GetVRButtonDown_Legacy(EVRButtonId buttonid)
  478. {
  479. if (openvrsystem == null) return false; //If VR isn't running, we can't check.
  480. bool washeldlastupdate = (lastcontrollerstate.ulButtonPressed & (1UL << (int)buttonid)) > 0L;
  481. if (washeldlastupdate == true) return false; //If the key was held last check, it can't be pressed for the first time now.
  482. bool isheld = (controllerstate.ulButtonPressed & (1UL << (int)buttonid)) > 0L;
  483. return isheld; //If we got here, we know it was not down last frame.
  484. }
  485. /// <summary>
  486. /// Returns if the VR controller button with the given ID is currently held.
  487. /// </summary>
  488. /// <param name="buttonid">EVR ID of the button as listed in OpenVR.</param>
  489. public bool GetVRButtonHeld_Legacy(EVRButtonId buttonid)
  490. {
  491. if (openvrsystem == null) return false; //If VR isn't running, we can't check.
  492. bool isheld = (controllerstate.ulButtonPressed & (1UL << (int)buttonid)) > 0L;
  493. return isheld;
  494. }
  495. /// <summary>
  496. /// Returns if the VR controller button with the given ID was held last frame, but released this frame.
  497. /// </summary>
  498. /// <param name="buttonid">EVR ID of the button as listed in OpenVR.</param>
  499. public bool GetVRButtonReleased_Legacy(EVRButtonId buttonid)
  500. {
  501. if (openvrsystem == null) return false; //If VR isn't running, we can't check.
  502. bool washeldlastupdate = (lastcontrollerstate.ulButtonPressed & (1UL << (int)buttonid)) > 0L;
  503. if (washeldlastupdate == false) return false; //If the key was held last check, it can't be released now.
  504. bool isheld = (controllerstate.ulButtonPressed & (1UL << (int)buttonid)) > 0L;
  505. return !isheld; //If we got here, we know it was not up last frame.
  506. }
  507. /// <summary>
  508. /// Returns the value of an axis with the provided ID.
  509. /// Note that for single-value axes, the relevant value will be the X in the returned Vector2 (the Y is unused).
  510. /// </summary>
  511. /// <param name="buttonid"></param>
  512. public Vector2 CheckSteamVRAxis_Legacy(EVRButtonId buttonid)
  513. {
  514. //Convert the EVRButtonID enum to the axis number and check if it's not an axis.
  515. uint axis = (uint)buttonid - (uint)EVRButtonId.k_EButton_Axis0;
  516. if (axis < 0 || axis > 4)
  517. {
  518. Debug.LogError("Called GetAxis with " + buttonid + ", which is not an axis.");
  519. return Vector2.zero;
  520. }
  521. switch (axis)
  522. {
  523. case 0: return new Vector2(controllerstate.rAxis0.x, controllerstate.rAxis0.y);
  524. case 1: return new Vector2(controllerstate.rAxis1.x, controllerstate.rAxis1.y);
  525. case 2: return new Vector2(controllerstate.rAxis2.x, controllerstate.rAxis2.y);
  526. case 3: return new Vector2(controllerstate.rAxis3.x, controllerstate.rAxis3.y);
  527. case 4: return new Vector2(controllerstate.rAxis4.x, controllerstate.rAxis4.y);
  528. default: return Vector2.zero;
  529. }
  530. }
  531. #endif
  532. }
  533. /// <summary>
  534. /// List of possible button states, used to check inputs.
  535. /// </summary>
  536. public enum ControllerButtonState
  537. {
  538. /// <summary>
  539. /// The button was pressed this frame.
  540. /// </summary>
  541. Down,
  542. /// <summary>
  543. /// The button is being held down - it doesn't matter which frame it started being held.
  544. /// </summary>
  545. Held,
  546. /// <summary>
  547. /// The button was released this frame.
  548. /// </summary>
  549. Up
  550. }