Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

623 lines
30 KiB

  1. // Simulator Controller|SDK_Simulator|003
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. using System.Collections.Generic;
  6. /// <summary>
  7. /// The Sim Controller SDK script provides functions to help simulate VR controllers.
  8. /// </summary>
  9. [SDK_Description(typeof(SDK_SimSystem))]
  10. public class SDK_SimController : SDK_BaseController
  11. {
  12. protected SDK_ControllerSim rightController;
  13. protected SDK_ControllerSim leftController;
  14. protected Dictionary<string, KeyCode> keyMappings = new Dictionary<string, KeyCode>()
  15. {
  16. {"Trigger", KeyCode.Mouse1 },
  17. {"Grip", KeyCode.Mouse0 },
  18. {"TouchpadPress", KeyCode.Q },
  19. {"ButtonOne", KeyCode.E },
  20. {"ButtonTwo", KeyCode.R },
  21. {"StartMenu", KeyCode.F },
  22. {"TouchModifier", KeyCode.T},
  23. {"HairTouchModifier", KeyCode.H}
  24. };
  25. protected const string RIGHT_HAND_CONTROLLER_NAME = "RightHand";
  26. protected const string LEFT_HAND_CONTROLLER_NAME = "LeftHand";
  27. public virtual void SetKeyMappings(Dictionary<string, KeyCode> givenKeyMappings)
  28. {
  29. keyMappings = givenKeyMappings;
  30. }
  31. /// <summary>
  32. /// The ProcessUpdate method enables an SDK to run logic for every Unity Update
  33. /// </summary>
  34. /// <param name="controllerReference">The reference for the controller.</param>
  35. /// <param name="options">A dictionary of generic options that can be used to within the update.</param>
  36. public override void ProcessUpdate(VRTK_ControllerReference controllerReference, Dictionary<string, object> options)
  37. {
  38. }
  39. /// <summary>
  40. /// The ProcessFixedUpdate method enables an SDK to run logic for every Unity FixedUpdate
  41. /// </summary>
  42. /// <param name="controllerReference">The reference for the controller.</param>
  43. /// <param name="options">A dictionary of generic options that can be used to within the fixed update.</param>
  44. public override void ProcessFixedUpdate(VRTK_ControllerReference controllerReference, Dictionary<string, object> options)
  45. {
  46. }
  47. /// <summary>
  48. /// The GetCurrentControllerType method returns the current used ControllerType based on the SDK and headset being used.
  49. /// </summary>
  50. /// <param name="controllerReference">The reference to the controller to get type of.</param>
  51. /// <returns>The ControllerType based on the SDK and headset being used.</returns>
  52. public override ControllerType GetCurrentControllerType(VRTK_ControllerReference controllerReference = null)
  53. {
  54. return ControllerType.Simulator_Hand;
  55. }
  56. /// <summary>
  57. /// The GetControllerDefaultColliderPath returns the path to the prefab that contains the collider objects for the default controller of this SDK.
  58. /// </summary>
  59. /// <param name="hand">The controller hand to check for</param>
  60. /// <returns>A path to the resource that contains the collider GameObject.</returns>
  61. public override string GetControllerDefaultColliderPath(ControllerHand hand)
  62. {
  63. return "ControllerColliders/Simulator";
  64. }
  65. /// <summary>
  66. /// The GetControllerElementPath returns the path to the game object that the given controller element for the given hand resides in.
  67. /// </summary>
  68. /// <param name="element">The controller element to look up.</param>
  69. /// <param name="hand">The controller hand to look up.</param>
  70. /// <param name="fullPath">Whether to get the initial path or the full path to the element.</param>
  71. /// <returns>A string containing the path to the game object that the controller element resides in.</returns>
  72. public override string GetControllerElementPath(ControllerElements element, ControllerHand hand, bool fullPath = false)
  73. {
  74. string suffix = (fullPath ? "/attach" : "");
  75. switch (element)
  76. {
  77. case ControllerElements.AttachPoint:
  78. return "";
  79. case ControllerElements.Trigger:
  80. return "" + suffix;
  81. case ControllerElements.GripLeft:
  82. return "" + suffix;
  83. case ControllerElements.GripRight:
  84. return "" + suffix;
  85. case ControllerElements.Touchpad:
  86. return "" + suffix;
  87. case ControllerElements.ButtonOne:
  88. return "" + suffix;
  89. case ControllerElements.SystemMenu:
  90. return "" + suffix;
  91. case ControllerElements.Body:
  92. return "";
  93. }
  94. return "";
  95. }
  96. /// <summary>
  97. /// The GetControllerIndex method returns the index of the given controller.
  98. /// </summary>
  99. /// <param name="controller">The GameObject containing the controller.</param>
  100. /// <returns>The index of the given controller.</returns>
  101. public override uint GetControllerIndex(GameObject controller)
  102. {
  103. if (CheckActualOrScriptAliasControllerIsRightHand(controller))
  104. {
  105. return 1;
  106. }
  107. if (CheckActualOrScriptAliasControllerIsLeftHand(controller))
  108. {
  109. return 2;
  110. }
  111. return uint.MaxValue;
  112. }
  113. /// <summary>
  114. /// The GetControllerByIndex method returns the GameObject of a controller with a specific index.
  115. /// </summary>
  116. /// <param name="index">The index of the controller to find.</param>
  117. /// <param name="actual">If true it will return the actual controller, if false it will return the script alias controller GameObject.</param>
  118. /// <returns>The GameObject of the controller</returns>
  119. public override GameObject GetControllerByIndex(uint index, bool actual = false)
  120. {
  121. SetupPlayer();
  122. VRTK_SDKManager sdkManager = VRTK_SDKManager.instance;
  123. switch (index)
  124. {
  125. case 1:
  126. return (sdkManager != null && !actual ? sdkManager.scriptAliasRightController : (rightController != null ? rightController.gameObject : null));
  127. case 2:
  128. return (sdkManager != null && !actual ? sdkManager.scriptAliasLeftController : (leftController != null ? leftController.gameObject : null));
  129. default:
  130. return null;
  131. }
  132. }
  133. /// <summary>
  134. /// The GetControllerOrigin method returns the origin of the given controller.
  135. /// </summary>
  136. /// <param name="controllerReference">The reference to the controller to retrieve the origin from.</param>
  137. /// <returns>A Transform containing the origin of the controller.</returns>
  138. public override Transform GetControllerOrigin(VRTK_ControllerReference controllerReference)
  139. {
  140. return (controllerReference != null && controllerReference.actual != null ? controllerReference.actual.transform : null);
  141. }
  142. /// <summary>
  143. /// The GenerateControllerPointerOrigin method can create a custom pointer origin Transform to represent the pointer position and forward.
  144. /// </summary>
  145. /// <param name="parent">The GameObject that the origin will become parent of. If it is a controller then it will also be used to determine the hand if required.</param>
  146. /// <returns>A generated Transform that contains the custom pointer origin.</returns>
  147. [System.Obsolete("GenerateControllerPointerOrigin has been deprecated and will be removed in a future version of VRTK.")]
  148. public override Transform GenerateControllerPointerOrigin(GameObject parent)
  149. {
  150. return null;
  151. }
  152. /// <summary>
  153. /// The GetControllerLeftHand method returns the GameObject containing the representation of the left hand controller.
  154. /// </summary>
  155. /// <param name="actual">If true it will return the actual controller, if false it will return the script alias controller GameObject.</param>
  156. /// <returns>The GameObject containing the left hand controller.</returns>
  157. public override GameObject GetControllerLeftHand(bool actual = false)
  158. {
  159. // use the basic base functionality to find the left hand controller
  160. GameObject controller = GetSDKManagerControllerLeftHand(actual);
  161. // if the controller cannot be found with default settings, try finding it below the InputSimulator by name
  162. if (controller == null && actual)
  163. {
  164. controller = GetActualController(ControllerHand.Left);
  165. }
  166. return controller;
  167. }
  168. /// <summary>
  169. /// The GetControllerRightHand method returns the GameObject containing the representation of the right hand controller.
  170. /// </summary>
  171. /// <param name="actual">If true it will return the actual controller, if false it will return the script alias controller GameObject.</param>
  172. /// <returns>The GameObject containing the right hand controller.</returns>
  173. public override GameObject GetControllerRightHand(bool actual = false)
  174. {
  175. // use the basic base functionality to find the right hand controller
  176. GameObject controller = GetSDKManagerControllerRightHand(actual);
  177. // if the controller cannot be found with default settings, try finding it below the InputSimulator by name
  178. if (controller == null && actual)
  179. {
  180. controller = GetActualController(ControllerHand.Right);
  181. }
  182. return controller;
  183. }
  184. /// <summary>
  185. /// The IsControllerLeftHand/1 method is used to check if the given controller is the the left hand controller.
  186. /// </summary>
  187. /// <param name="controller">The GameObject to check.</param>
  188. /// <returns>Returns true if the given controller is the left hand controller.</returns>
  189. public override bool IsControllerLeftHand(GameObject controller)
  190. {
  191. return CheckActualOrScriptAliasControllerIsLeftHand(controller);
  192. }
  193. /// <summary>
  194. /// The IsControllerRightHand/1 method is used to check if the given controller is the the right hand controller.
  195. /// </summary>
  196. /// <param name="controller">The GameObject to check.</param>
  197. /// <returns>Returns true if the given controller is the right hand controller.</returns>
  198. public override bool IsControllerRightHand(GameObject controller)
  199. {
  200. return CheckActualOrScriptAliasControllerIsRightHand(controller);
  201. }
  202. /// <summary>
  203. /// The IsControllerLeftHand/2 method is used to check if the given controller is the the left hand controller.
  204. /// </summary>
  205. /// <param name="controller">The GameObject to check.</param>
  206. /// <param name="actual">If true it will check the actual controller, if false it will check the script alias controller.</param>
  207. /// <returns>Returns true if the given controller is the left hand controller.</returns>
  208. public override bool IsControllerLeftHand(GameObject controller, bool actual)
  209. {
  210. return CheckControllerLeftHand(controller, actual);
  211. }
  212. /// <summary>
  213. /// The IsControllerRightHand/2 method is used to check if the given controller is the the right hand controller.
  214. /// </summary>
  215. /// <param name="controller">The GameObject to check.</param>
  216. /// <param name="actual">If true it will check the actual controller, if false it will check the script alias controller.</param>
  217. /// <returns>Returns true if the given controller is the right hand controller.</returns>
  218. public override bool IsControllerRightHand(GameObject controller, bool actual)
  219. {
  220. return CheckControllerRightHand(controller, actual);
  221. }
  222. /// <summary>
  223. /// The WaitForControllerModel method determines whether the controller model for the given hand requires waiting to load in on scene start.
  224. /// </summary>
  225. /// <param name="hand">The hand to determine if the controller model will be ready for.</param>
  226. /// <returns>Returns true if the controller model requires loading in at runtime and therefore needs waiting for. Returns false if the controller model will be available at start.</returns>
  227. public override bool WaitForControllerModel(ControllerHand hand)
  228. {
  229. return false;
  230. }
  231. /// <summary>
  232. /// The GetControllerModel method returns the model alias for the given GameObject.
  233. /// </summary>
  234. /// <param name="controller">The GameObject to get the model alias for.</param>
  235. /// <returns>The GameObject that has the model alias within it.</returns>
  236. public override GameObject GetControllerModel(GameObject controller)
  237. {
  238. return GetControllerModelFromController(controller);
  239. }
  240. /// <summary>
  241. /// The GetControllerModel method returns the model alias for the given controller hand.
  242. /// </summary>
  243. /// <param name="hand">The hand enum of which controller model to retrieve.</param>
  244. /// <returns>The GameObject that has the model alias within it.</returns>
  245. public override GameObject GetControllerModel(ControllerHand hand)
  246. {
  247. GameObject model = null;
  248. GameObject simPlayer = SDK_InputSimulator.FindInScene();
  249. if (simPlayer != null)
  250. {
  251. switch (hand)
  252. {
  253. case ControllerHand.Left:
  254. model = simPlayer.transform.Find(string.Format("{0}/Hand", LEFT_HAND_CONTROLLER_NAME)).gameObject;
  255. break;
  256. case ControllerHand.Right:
  257. model = simPlayer.transform.Find(string.Format("{0}/Hand", RIGHT_HAND_CONTROLLER_NAME)).gameObject;
  258. break;
  259. }
  260. }
  261. return model;
  262. }
  263. /// <summary>
  264. /// The GetControllerRenderModel method gets the game object that contains the given controller's render model.
  265. /// </summary>
  266. /// <param name="controllerReference">The reference to the controller to check.</param>
  267. /// <returns>A GameObject containing the object that has a render model for the controller.</returns>
  268. public override GameObject GetControllerRenderModel(VRTK_ControllerReference controllerReference)
  269. {
  270. return controllerReference.scriptAlias.transform.parent.Find("Hand").gameObject;
  271. }
  272. /// <summary>
  273. /// The SetControllerRenderModelWheel method sets the state of the scroll wheel on the controller render model.
  274. /// </summary>
  275. /// <param name="renderModel">The GameObject containing the controller render model.</param>
  276. /// <param name="state">If true and the render model has a scroll wheen then it will be displayed, if false then the scroll wheel will be hidden.</param>
  277. public override void SetControllerRenderModelWheel(GameObject renderModel, bool state)
  278. {
  279. }
  280. /// <summary>
  281. /// The HapticPulse/2 method is used to initiate a simple haptic pulse on the tracked object of the given controller reference.
  282. /// </summary>
  283. /// <param name="controllerReference">The reference to the tracked object to initiate the haptic pulse on.</param>
  284. /// <param name="strength">The intensity of the rumble of the controller motor. `0` to `1`.</param>
  285. public override void HapticPulse(VRTK_ControllerReference controllerReference, float strength = 0.5f)
  286. {
  287. }
  288. /// <summary>
  289. /// The HapticPulse/2 method is used to initiate a haptic pulse based on an audio clip on the tracked object of the given controller reference.
  290. /// </summary>
  291. /// <param name="controllerReference">The reference to the tracked object to initiate the haptic pulse on.</param>
  292. /// <param name="clip">The audio clip to use for the haptic pattern.</param>
  293. public override bool HapticPulse(VRTK_ControllerReference controllerReference, AudioClip clip)
  294. {
  295. //Return true so it just always prevents doing a fallback routine.
  296. return true;
  297. }
  298. /// <summary>
  299. /// The GetHapticModifiers method is used to return modifiers for the duration and interval if the SDK handles it slightly differently.
  300. /// </summary>
  301. /// <returns>An SDK_ControllerHapticModifiers object with a given `durationModifier` and an `intervalModifier`.</returns>
  302. public override SDK_ControllerHapticModifiers GetHapticModifiers()
  303. {
  304. return new SDK_ControllerHapticModifiers();
  305. }
  306. /// <summary>
  307. /// The GetVelocity method is used to determine the current velocity of the tracked object on the given controller reference.
  308. /// </summary>
  309. /// <param name="controllerReference">The reference to the tracked object to check for.</param>
  310. /// <returns>A Vector3 containing the current velocity of the tracked object.</returns>
  311. public override Vector3 GetVelocity(VRTK_ControllerReference controllerReference)
  312. {
  313. SetupPlayer();
  314. uint index = VRTK_ControllerReference.GetRealIndex(controllerReference);
  315. switch (index)
  316. {
  317. case 1:
  318. return (rightController != null ? rightController.GetVelocity() : Vector3.zero);
  319. case 2:
  320. return (leftController != null ? leftController.GetVelocity() : Vector3.zero);
  321. default:
  322. return Vector3.zero;
  323. }
  324. }
  325. /// <summary>
  326. /// The GetAngularVelocity method is used to determine the current angular velocity of the tracked object on the given controller reference.
  327. /// </summary>
  328. /// <param name="controllerReference">The reference to the tracked object to check for.</param>
  329. /// <returns>A Vector3 containing the current angular velocity of the tracked object.</returns>
  330. public override Vector3 GetAngularVelocity(VRTK_ControllerReference controllerReference)
  331. {
  332. SetupPlayer();
  333. uint index = VRTK_ControllerReference.GetRealIndex(controllerReference);
  334. switch (index)
  335. {
  336. case 1:
  337. return (rightController != null ? rightController.GetAngularVelocity() : Vector3.zero);
  338. case 2:
  339. return (leftController != null ? leftController.GetAngularVelocity() : Vector3.zero);
  340. default:
  341. return Vector3.zero;
  342. }
  343. }
  344. /// <summary>
  345. /// The IsTouchpadStatic method is used to determine if the touchpad is currently not being moved.
  346. /// </summary>
  347. /// <param name="currentAxisValues"></param>
  348. /// <param name="previousAxisValues"></param>
  349. /// <param name="compareFidelity"></param>
  350. /// <returns>Returns true if the touchpad is not currently being touched or moved.</returns>
  351. public override bool IsTouchpadStatic(bool isTouched, Vector2 currentAxisValues, Vector2 previousAxisValues, int compareFidelity)
  352. {
  353. return (!isTouched || VRTK_SharedMethods.Vector2ShallowCompare(currentAxisValues, previousAxisValues, compareFidelity));
  354. }
  355. /// <summary>
  356. /// The GetButtonAxis method retrieves the current X/Y axis values for the given button type on the given controller reference.
  357. /// </summary>
  358. /// <param name="buttonType">The type of button to check for the axis on.</param>
  359. /// <param name="controllerReference">The reference to the controller to check the button axis on.</param>
  360. /// <returns>A Vector2 of the X/Y values of the button axis. If no axis values exist for the given button, then a Vector2.Zero is returned.</returns>
  361. public override Vector2 GetButtonAxis(ButtonTypes buttonType, VRTK_ControllerReference controllerReference)
  362. {
  363. return Vector2.zero;
  364. }
  365. /// <summary>
  366. /// The GetButtonSenseAxis method retrieves the current sense axis value for the given button type on the given controller reference.
  367. /// </summary>
  368. /// <param name="buttonType">The type of button to check for the sense axis on.</param>
  369. /// <param name="controllerReference">The reference to the controller to check the sense axis on.</param>
  370. /// <returns>The current sense axis value.</returns>
  371. public override float GetButtonSenseAxis(ButtonTypes buttonType, VRTK_ControllerReference controllerReference)
  372. {
  373. return 0f;
  374. }
  375. /// <summary>
  376. /// The GetButtonHairlineDelta method is used to get the difference between the current button press and the previous frame button press.
  377. /// </summary>
  378. /// <param name="buttonType">The type of button to get the hairline delta for.</param>
  379. /// <param name="controllerReference">The reference to the controller to get the hairline delta for.</param>
  380. /// <returns>The delta between the button presses.</returns>
  381. public override float GetButtonHairlineDelta(ButtonTypes buttonType, VRTK_ControllerReference controllerReference)
  382. {
  383. return 0f;
  384. }
  385. /// <summary>
  386. /// The GetControllerButtonState method is used to determine if the given controller button for the given press type on the given controller reference is currently taking place.
  387. /// </summary>
  388. /// <param name="buttonType">The type of button to check for the state of.</param>
  389. /// <param name="pressType">The button state to check for.</param>
  390. /// <param name="controllerReference">The reference to the controller to check the button state on.</param>
  391. /// <returns>Returns true if the given button is in the state of the given press type on the given controller reference.</returns>
  392. public override bool GetControllerButtonState(ButtonTypes buttonType, ButtonPressTypes pressType, VRTK_ControllerReference controllerReference)
  393. {
  394. uint index = VRTK_ControllerReference.GetRealIndex(controllerReference);
  395. switch (buttonType)
  396. {
  397. case ButtonTypes.Trigger:
  398. case ButtonTypes.TriggerHairline:
  399. return GetControllerButtonState(index, "Trigger", pressType);
  400. case ButtonTypes.Grip:
  401. case ButtonTypes.GripHairline:
  402. return GetControllerButtonState(index, "Grip", pressType);
  403. case ButtonTypes.Touchpad:
  404. return GetControllerButtonState(index, "TouchpadPress", pressType);
  405. case ButtonTypes.ButtonOne:
  406. return GetControllerButtonState(index, "ButtonOne", pressType);
  407. case ButtonTypes.ButtonTwo:
  408. return GetControllerButtonState(index, "ButtonTwo", pressType);
  409. case ButtonTypes.StartMenu:
  410. return GetControllerButtonState(index, "StartMenu", pressType);
  411. }
  412. return false;
  413. }
  414. protected virtual void OnEnable()
  415. {
  416. SetupPlayer();
  417. }
  418. protected virtual void SetupPlayer()
  419. {
  420. if (rightController == null || leftController == null)
  421. {
  422. GameObject simPlayer = SDK_InputSimulator.FindInScene();
  423. if (simPlayer != null)
  424. {
  425. rightController = (rightController == null ? simPlayer.transform.Find(RIGHT_HAND_CONTROLLER_NAME).GetComponent<SDK_ControllerSim>() : rightController);
  426. leftController = (leftController == null ? simPlayer.transform.Find(LEFT_HAND_CONTROLLER_NAME).GetComponent<SDK_ControllerSim>() : leftController);
  427. }
  428. }
  429. }
  430. /// <summary>
  431. /// whether or not the touch modifier is currently pressed
  432. /// if so, pressing a key on the keyboard will only emit touch events,
  433. /// but not a real press (or hair touch events).
  434. /// </summary>
  435. /// <returns>whether or not the TouchModifier is active</returns>
  436. protected virtual bool IsTouchModifierPressed()
  437. {
  438. return Input.GetKey(VRTK_SharedMethods.GetDictionaryValue(keyMappings, "TouchModifier", KeyCode.None));
  439. }
  440. /// <summary>
  441. /// whether or not the hair touch modifier is currently pressed
  442. /// if so, pressing a key on the keyboard will only emit touch and hair touch events,
  443. /// but not a real press.
  444. /// </summary>
  445. /// <returns>whether or not the HairTouchModifier is active</returns>
  446. protected virtual bool IsHairTouchModifierPressed()
  447. {
  448. return Input.GetKey(VRTK_SharedMethods.GetDictionaryValue(keyMappings, "HairTouchModifier", KeyCode.None));
  449. }
  450. /// <summary>
  451. /// whether or not a button press shall be ignored, e.g. because of the
  452. /// use of the touch or hair touch modifier
  453. /// </summary>
  454. /// <returns>Returns true if the button press is ignored.</returns>
  455. protected virtual bool IsButtonPressIgnored()
  456. {
  457. // button presses shall be ignored if the hair touch or touch modifiers are used
  458. return IsHairTouchModifierPressed() || IsTouchModifierPressed();
  459. }
  460. /// <summary>
  461. /// whether or not a button press shall be ignored, e.g. because of the
  462. /// use of the touch or hair touch modifier
  463. /// </summary>
  464. /// <returns>Returns true if the hair trigger touch should be ignored.</returns>
  465. protected virtual bool IsButtonHairTouchIgnored()
  466. {
  467. // button presses shall be ignored if the hair touch or touch modifiers are used
  468. return IsTouchModifierPressed() && !IsHairTouchModifierPressed();
  469. }
  470. /// <summary>
  471. /// Gets the state of the given button key mapping for the press type on the controller index.
  472. /// </summary>
  473. /// <param name="index">The index of the controller.</param>
  474. /// <param name="keyMapping">The key mapping key to check.</param>
  475. /// <param name="pressType">The type of button press to check.</param>
  476. /// <returns>Returns true if the button state matches the given data.</returns>
  477. protected virtual bool GetControllerButtonState(uint index, string keyMapping, ButtonPressTypes pressType)
  478. {
  479. KeyCode buttonCode = VRTK_SharedMethods.GetDictionaryValue(keyMappings, keyMapping, KeyCode.None);
  480. if (pressType == ButtonPressTypes.Touch)
  481. {
  482. return IsButtonPressed(index, ButtonPressTypes.Press, buttonCode);
  483. }
  484. else if (pressType == ButtonPressTypes.TouchDown)
  485. {
  486. return IsButtonPressed(index, ButtonPressTypes.PressDown, buttonCode);
  487. }
  488. else if (pressType == ButtonPressTypes.TouchUp)
  489. {
  490. return IsButtonPressed(index, ButtonPressTypes.PressUp, buttonCode);
  491. }
  492. else if (pressType == ButtonPressTypes.Press)
  493. {
  494. return !IsButtonPressIgnored() && IsButtonPressed(index, ButtonPressTypes.Press, buttonCode);
  495. }
  496. else if (pressType == ButtonPressTypes.PressDown)
  497. {
  498. return !IsButtonPressIgnored() && IsButtonPressed(index, ButtonPressTypes.PressDown, buttonCode);
  499. }
  500. else if (pressType == ButtonPressTypes.PressUp)
  501. {
  502. return !IsButtonPressIgnored() && IsButtonPressed(index, ButtonPressTypes.PressUp, buttonCode);
  503. }
  504. return false;
  505. }
  506. /// <summary>
  507. /// checks if the given button (KeyCode) is currently in a specific pressed state (ButtonPressTypes) on the keyboard
  508. /// also asserts that button presses are only handled for the currently active controller by comparing the controller indices
  509. /// </summary>
  510. /// <param name="index">unique index of the controller for which the button press is to be checked</param>
  511. /// <param name="type">the type of press (up, down, hold)</param>
  512. /// <param name="button">the button on the keyboard</param>
  513. /// <returns>Returns true if the button is being pressed.</returns>
  514. protected virtual bool IsButtonPressed(uint index, ButtonPressTypes type, KeyCode button)
  515. {
  516. SetupPlayer();
  517. if (index >= uint.MaxValue)
  518. {
  519. return false;
  520. }
  521. if (index == 1)
  522. {
  523. if (rightController == null || !rightController.selected)
  524. {
  525. return false;
  526. }
  527. }
  528. else if (index == 2)
  529. {
  530. if (leftController == null || !leftController.selected)
  531. {
  532. return false;
  533. }
  534. }
  535. else
  536. {
  537. return false;
  538. }
  539. switch (type)
  540. {
  541. case ButtonPressTypes.Press:
  542. return Input.GetKey(button);
  543. case ButtonPressTypes.PressDown:
  544. return Input.GetKeyDown(button);
  545. case ButtonPressTypes.PressUp:
  546. return Input.GetKeyUp(button);
  547. }
  548. return false;
  549. }
  550. /// <summary>
  551. /// finds the actual controller for the specified hand (identified by name) and returns it
  552. /// </summary>
  553. /// <param name="hand">the for which to find the respective controller gameobject</param>
  554. /// <returns>the gameobject of the actual controller corresponding to the specified hand</returns>
  555. protected virtual GameObject GetActualController(ControllerHand hand)
  556. {
  557. GameObject simPlayer = SDK_InputSimulator.FindInScene();
  558. GameObject controller = null;
  559. if (simPlayer != null)
  560. {
  561. switch (hand)
  562. {
  563. case ControllerHand.Right:
  564. controller = simPlayer.transform.Find(RIGHT_HAND_CONTROLLER_NAME).gameObject;
  565. break;
  566. case ControllerHand.Left:
  567. controller = simPlayer.transform.Find(LEFT_HAND_CONTROLLER_NAME).gameObject;
  568. break;
  569. default:
  570. break;
  571. }
  572. }
  573. return controller;
  574. }
  575. }
  576. }