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.

434 lines
22 KiB

  1. // SDK Input Override|Utilities|90170
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. [Serializable]
  9. public class VRTK_SDKInputOverrideType
  10. {
  11. [Header("SDK settings")]
  12. [Tooltip("An optional SDK Setup to use to determine when to modify the transform.")]
  13. public VRTK_SDKSetup loadedSDKSetup;
  14. [Tooltip("An optional SDK controller type to use to determine when to modify the transform.")]
  15. public SDK_BaseController.ControllerType controllerType;
  16. }
  17. [Serializable]
  18. public class VRTK_SDKButtonInputOverrideType : VRTK_SDKInputOverrideType
  19. {
  20. [Header("Button Override")]
  21. [Tooltip("The button to override to.")]
  22. public VRTK_ControllerEvents.ButtonAlias overrideButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
  23. }
  24. [Serializable]
  25. public class VRTK_SDKVector2AxisInputOverrideType : VRTK_SDKInputOverrideType
  26. {
  27. [Header("Vector2 Axis Override")]
  28. [Tooltip("The Vector2 axis to override to.")]
  29. public VRTK_ControllerEvents.Vector2AxisAlias overrideAxis = VRTK_ControllerEvents.Vector2AxisAlias.Undefined;
  30. }
  31. /// <summary>
  32. /// Provides the ability to switch button mappings based on the current SDK or controller type
  33. /// </summary>
  34. /// <remarks>
  35. /// **Script Usage:**
  36. /// * Place the `VRTK_SDKInputOverride` script on any active scene GameObject.
  37. /// * Customise the input button for each script type for each SDK controller type.
  38. /// </remarks>
  39. public class VRTK_SDKInputOverride : VRTK_SDKControllerReady
  40. {
  41. [Header("Interact Grab")]
  42. [Tooltip("The Interact Grab script to override the controls on.")]
  43. public VRTK_InteractGrab interactGrabScript;
  44. [Tooltip("The list of overrides.")]
  45. public List<VRTK_SDKButtonInputOverrideType> interactGrabOverrides = new List<VRTK_SDKButtonInputOverrideType>();
  46. [Header("Interact Use")]
  47. [Tooltip("The Interact Use script to override the controls on.")]
  48. public VRTK_InteractUse interactUseScript;
  49. [Tooltip("The list of overrides.")]
  50. public List<VRTK_SDKButtonInputOverrideType> interactUseOverrides = new List<VRTK_SDKButtonInputOverrideType>();
  51. [Header("Pointer")]
  52. [Tooltip("The Pointer script to override the controls on.")]
  53. public VRTK_Pointer pointerScript;
  54. [Tooltip("The list of overrides for the activation button.")]
  55. public List<VRTK_SDKButtonInputOverrideType> pointerActivationOverrides = new List<VRTK_SDKButtonInputOverrideType>();
  56. [Tooltip("The list of overrides for the selection button.")]
  57. public List<VRTK_SDKButtonInputOverrideType> pointerSelectionOverrides = new List<VRTK_SDKButtonInputOverrideType>();
  58. [Header("UI Pointer")]
  59. [Tooltip("The UI Pointer script to override the controls on.")]
  60. public VRTK_UIPointer uiPointerScript;
  61. [Tooltip("The list of overrides for the activation button.")]
  62. public List<VRTK_SDKButtonInputOverrideType> uiPointerActivationOverrides = new List<VRTK_SDKButtonInputOverrideType>();
  63. [Tooltip("The list of overrides for the selection button.")]
  64. public List<VRTK_SDKButtonInputOverrideType> uiPointerSelectionOverrides = new List<VRTK_SDKButtonInputOverrideType>();
  65. [Header("Pointer Direction Indicator")]
  66. [Tooltip("The Pointer Direction Indicator script to override the controls on.")]
  67. public VRTK_PointerDirectionIndicator pointerDirectionIndicatorScript;
  68. [Tooltip("The list of overrides for the coordinate axis.")]
  69. public List<VRTK_SDKVector2AxisInputOverrideType> directionIndicatorCoordinateOverrides = new List<VRTK_SDKVector2AxisInputOverrideType>();
  70. [Header("Touchpad Control")]
  71. [Tooltip("The Touchpad Control script to override the controls on.")]
  72. public VRTK_TouchpadControl touchpadControlScript;
  73. [Tooltip("The list of overrides for the Touchpad Control coordinate axis.")]
  74. public List<VRTK_SDKVector2AxisInputOverrideType> touchpadControlCoordinateOverrides = new List<VRTK_SDKVector2AxisInputOverrideType>();
  75. [Tooltip("The list of overrides for the activation button.")]
  76. public List<VRTK_SDKButtonInputOverrideType> touchpadControlActivationOverrides = new List<VRTK_SDKButtonInputOverrideType>();
  77. [Tooltip("The list of overrides for the modifier button.")]
  78. public List<VRTK_SDKButtonInputOverrideType> touchpadControlModifierOverrides = new List<VRTK_SDKButtonInputOverrideType>();
  79. [Header("Button Control")]
  80. [Tooltip("The ButtonControl script to override the controls on.")]
  81. public VRTK_ButtonControl buttonControlScript;
  82. [Tooltip("The list of overrides for the forward button.")]
  83. public List<VRTK_SDKButtonInputOverrideType> buttonControlForwardOverrides = new List<VRTK_SDKButtonInputOverrideType>();
  84. [Tooltip("The list of overrides for the backward button.")]
  85. public List<VRTK_SDKButtonInputOverrideType> buttonControlBackwardOverrides = new List<VRTK_SDKButtonInputOverrideType>();
  86. [Tooltip("The list of overrides for the left button.")]
  87. public List<VRTK_SDKButtonInputOverrideType> buttonControlLeftOverrides = new List<VRTK_SDKButtonInputOverrideType>();
  88. [Tooltip("The list of overrides for the right button.")]
  89. public List<VRTK_SDKButtonInputOverrideType> buttonControlRightOverrides = new List<VRTK_SDKButtonInputOverrideType>();
  90. [Header("Slingshot Jump")]
  91. [Tooltip("The SlingshotJump script to override the controls on.")]
  92. public VRTK_SlingshotJump slingshotJumpScript;
  93. [Tooltip("The list of overrides for the activation button.")]
  94. public List<VRTK_SDKButtonInputOverrideType> slingshotJumpActivationOverrides = new List<VRTK_SDKButtonInputOverrideType>();
  95. [Tooltip("The list of overrides for the cancel button.")]
  96. public List<VRTK_SDKButtonInputOverrideType> slingshotJumpCancelOverrides = new List<VRTK_SDKButtonInputOverrideType>();
  97. [Header("Move In Place")]
  98. [Tooltip("The MoveInPlace script to override the controls on.")]
  99. public VRTK_MoveInPlace moveInPlaceScript;
  100. [Tooltip("The list of overrides for the engage button.")]
  101. public List<VRTK_SDKButtonInputOverrideType> moveInPlaceEngageOverrides = new List<VRTK_SDKButtonInputOverrideType>();
  102. [Header("Step Multiplier")]
  103. [Tooltip("The Step Multiplier script to override the controls on.")]
  104. public VRTK_StepMultiplier stepMultiplierScript;
  105. [Tooltip("The list of overrides for the activation button.")]
  106. public List<VRTK_SDKButtonInputOverrideType> stepMultiplierActivationOverrides = new List<VRTK_SDKButtonInputOverrideType>();
  107. /// <summary>
  108. /// The ForceManage method forces the inputs to be updated even without an SDK change event occuring.
  109. /// </summary>
  110. public virtual void ForceManage()
  111. {
  112. ManageInputs();
  113. }
  114. protected override void OnEnable()
  115. {
  116. base.OnEnable();
  117. ManageInputs();
  118. }
  119. protected override void OnDisable()
  120. {
  121. if (!gameObject.activeSelf)
  122. {
  123. base.OnDisable();
  124. }
  125. }
  126. protected override void ControllerReady(VRTK_ControllerReference controllerReference)
  127. {
  128. if (VRTK_SDKManager.GetLoadedSDKSetup() != null && gameObject.activeInHierarchy)
  129. {
  130. ManageInputs();
  131. }
  132. }
  133. protected virtual VRTK_SDKButtonInputOverrideType GetSelectedModifier(List<VRTK_SDKButtonInputOverrideType> overrideTypes, VRTK_ControllerReference controllerReference)
  134. {
  135. VRTK_SDKButtonInputOverrideType selectedModifier = null;
  136. //attempt to find by the overall SDK set up to start with
  137. if (VRTK_SDKManager.GetLoadedSDKSetup() != null)
  138. {
  139. selectedModifier = overrideTypes.FirstOrDefault(item => item.loadedSDKSetup == VRTK_SDKManager.GetLoadedSDKSetup());
  140. }
  141. //If no sdk set up is found or it is null then try and find by the SDK controller
  142. if (selectedModifier == null)
  143. {
  144. SDK_BaseController.ControllerType currentControllerType = VRTK_DeviceFinder.GetCurrentControllerType(controllerReference);
  145. selectedModifier = overrideTypes.FirstOrDefault(item => item.controllerType == currentControllerType);
  146. }
  147. return selectedModifier;
  148. }
  149. protected virtual VRTK_SDKVector2AxisInputOverrideType GetSelectedModifier(List<VRTK_SDKVector2AxisInputOverrideType> overrideTypes, VRTK_ControllerReference controllerReference)
  150. {
  151. //attempt to find by the overall SDK set up to start with
  152. VRTK_SDKVector2AxisInputOverrideType selectedModifier = overrideTypes.FirstOrDefault(item => item.loadedSDKSetup == VRTK_SDKManager.GetLoadedSDKSetup());
  153. //If no sdk set up is found or it is null then try and find by the SDK controller
  154. if (selectedModifier == null)
  155. {
  156. SDK_BaseController.ControllerType currentControllerType = VRTK_DeviceFinder.GetCurrentControllerType(controllerReference);
  157. selectedModifier = overrideTypes.FirstOrDefault(item => item.controllerType == currentControllerType);
  158. }
  159. return selectedModifier;
  160. }
  161. protected virtual void ManageInputs()
  162. {
  163. ManageInteractGrab();
  164. ManageInteractUse();
  165. ManagePointer();
  166. ManageUIPointer();
  167. ManagePointerDirectionIndicator();
  168. ManageTouchpadControl();
  169. ManageButtonControl();
  170. ManageSlingshotJump();
  171. ManageMoveInPlace();
  172. ManageStepMultiplier();
  173. }
  174. protected virtual VRTK_ControllerReference GetReferenceFromEvents(VRTK_ControllerEvents controllerEvents)
  175. {
  176. return VRTK_ControllerReference.GetControllerReference((controllerEvents != null ? controllerEvents.gameObject : null));
  177. }
  178. protected virtual VRTK_ControllerReference GetRightThenLeftReference()
  179. {
  180. VRTK_ControllerReference controllerReference = VRTK_ControllerReference.GetControllerReference(SDK_BaseController.ControllerHand.Right);
  181. return (VRTK_ControllerReference.IsValid(controllerReference) ? controllerReference : VRTK_ControllerReference.GetControllerReference(SDK_BaseController.ControllerHand.Left));
  182. }
  183. protected virtual void ManageInteractGrab()
  184. {
  185. if (interactGrabScript != null)
  186. {
  187. VRTK_ControllerReference controllerReference = GetReferenceFromEvents(interactGrabScript.controllerEvents);
  188. VRTK_SDKButtonInputOverrideType selectedModifier = GetSelectedModifier(interactGrabOverrides, controllerReference);
  189. if (selectedModifier != null && selectedModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
  190. {
  191. interactGrabScript.enabled = false;
  192. interactGrabScript.grabButton = selectedModifier.overrideButton;
  193. interactGrabScript.enabled = true;
  194. }
  195. }
  196. }
  197. protected virtual void ManageInteractUse()
  198. {
  199. if (interactUseScript != null)
  200. {
  201. VRTK_ControllerReference controllerReference = GetReferenceFromEvents(interactUseScript.controllerEvents);
  202. VRTK_SDKButtonInputOverrideType selectedModifier = GetSelectedModifier(interactUseOverrides, controllerReference);
  203. if (selectedModifier != null && selectedModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
  204. {
  205. interactUseScript.enabled = false;
  206. interactUseScript.useButton = selectedModifier.overrideButton;
  207. interactUseScript.enabled = true;
  208. }
  209. }
  210. }
  211. protected virtual void ManagePointer()
  212. {
  213. if (pointerScript != null)
  214. {
  215. VRTK_ControllerReference controllerReference = GetReferenceFromEvents(pointerScript.controllerEvents);
  216. VRTK_SDKButtonInputOverrideType selectedActivationModifier = GetSelectedModifier(pointerActivationOverrides, controllerReference);
  217. if (selectedActivationModifier != null && selectedActivationModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
  218. {
  219. pointerScript.enabled = false;
  220. pointerScript.activationButton = selectedActivationModifier.overrideButton;
  221. pointerScript.enabled = true;
  222. }
  223. VRTK_SDKButtonInputOverrideType selectedSelectionModifier = GetSelectedModifier(pointerSelectionOverrides, controllerReference);
  224. if (selectedSelectionModifier != null && selectedSelectionModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
  225. {
  226. pointerScript.enabled = false;
  227. pointerScript.selectionButton = selectedSelectionModifier.overrideButton;
  228. pointerScript.enabled = true;
  229. }
  230. }
  231. }
  232. protected virtual void ManageUIPointer()
  233. {
  234. if (uiPointerScript != null)
  235. {
  236. VRTK_ControllerReference controllerReference = GetReferenceFromEvents(uiPointerScript.controllerEvents);
  237. VRTK_SDKButtonInputOverrideType selectedActivationModifier = GetSelectedModifier(uiPointerActivationOverrides, controllerReference);
  238. if (selectedActivationModifier != null && selectedActivationModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
  239. {
  240. uiPointerScript.enabled = false;
  241. uiPointerScript.activationButton = selectedActivationModifier.overrideButton;
  242. uiPointerScript.enabled = true;
  243. }
  244. VRTK_SDKButtonInputOverrideType selectedSelectionModifier = GetSelectedModifier(uiPointerSelectionOverrides, controllerReference);
  245. if (selectedSelectionModifier != null && selectedSelectionModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
  246. {
  247. uiPointerScript.enabled = false;
  248. uiPointerScript.selectionButton = selectedSelectionModifier.overrideButton;
  249. uiPointerScript.enabled = true;
  250. }
  251. }
  252. }
  253. protected virtual void ManagePointerDirectionIndicator()
  254. {
  255. if (pointerDirectionIndicatorScript != null)
  256. {
  257. VRTK_ControllerReference controllerReference = GetReferenceFromEvents(pointerDirectionIndicatorScript.GetControllerEvents());
  258. VRTK_SDKVector2AxisInputOverrideType selectedCoorinateModifier = GetSelectedModifier(directionIndicatorCoordinateOverrides, controllerReference);
  259. if (selectedCoorinateModifier != null && selectedCoorinateModifier.overrideAxis != VRTK_ControllerEvents.Vector2AxisAlias.Undefined)
  260. {
  261. pointerDirectionIndicatorScript.enabled = false;
  262. pointerDirectionIndicatorScript.coordinateAxis = selectedCoorinateModifier.overrideAxis;
  263. pointerDirectionIndicatorScript.enabled = true;
  264. }
  265. }
  266. }
  267. protected virtual void ManageTouchpadControl()
  268. {
  269. if (touchpadControlScript != null)
  270. {
  271. VRTK_ControllerReference controllerReference = GetReferenceFromEvents(touchpadControlScript.controller);
  272. VRTK_SDKVector2AxisInputOverrideType selectedCoorinateModifier = GetSelectedModifier(touchpadControlCoordinateOverrides, controllerReference);
  273. if (selectedCoorinateModifier != null && selectedCoorinateModifier.overrideAxis != VRTK_ControllerEvents.Vector2AxisAlias.Undefined)
  274. {
  275. touchpadControlScript.enabled = false;
  276. touchpadControlScript.coordinateAxis = selectedCoorinateModifier.overrideAxis;
  277. touchpadControlScript.enabled = true;
  278. }
  279. VRTK_SDKButtonInputOverrideType selectedActivationModifier = GetSelectedModifier(touchpadControlActivationOverrides, controllerReference);
  280. if (selectedActivationModifier != null && selectedActivationModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
  281. {
  282. touchpadControlScript.enabled = false;
  283. touchpadControlScript.primaryActivationButton = selectedActivationModifier.overrideButton;
  284. touchpadControlScript.enabled = true;
  285. }
  286. VRTK_SDKButtonInputOverrideType selectedActionModifier = GetSelectedModifier(touchpadControlModifierOverrides, controllerReference);
  287. if (selectedActionModifier != null && selectedActionModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
  288. {
  289. touchpadControlScript.enabled = false;
  290. touchpadControlScript.actionModifierButton = selectedActionModifier.overrideButton;
  291. touchpadControlScript.enabled = true;
  292. }
  293. }
  294. }
  295. protected virtual void ManageButtonControl()
  296. {
  297. if (buttonControlScript != null)
  298. {
  299. VRTK_ControllerReference controllerReference = GetReferenceFromEvents(buttonControlScript.controller);
  300. VRTK_SDKButtonInputOverrideType selectedForwardModifier = GetSelectedModifier(buttonControlForwardOverrides, controllerReference);
  301. if (selectedForwardModifier != null && selectedForwardModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
  302. {
  303. buttonControlScript.enabled = false;
  304. buttonControlScript.forwardButton = selectedForwardModifier.overrideButton;
  305. buttonControlScript.enabled = true;
  306. }
  307. VRTK_SDKButtonInputOverrideType selectedBackwardModifier = GetSelectedModifier(buttonControlBackwardOverrides, controllerReference);
  308. if (selectedBackwardModifier != null && selectedBackwardModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
  309. {
  310. buttonControlScript.enabled = false;
  311. buttonControlScript.backwardButton = selectedBackwardModifier.overrideButton;
  312. buttonControlScript.enabled = true;
  313. }
  314. VRTK_SDKButtonInputOverrideType selectedLeftModifier = GetSelectedModifier(buttonControlLeftOverrides, controllerReference);
  315. if (selectedLeftModifier != null && selectedLeftModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
  316. {
  317. buttonControlScript.enabled = false;
  318. buttonControlScript.leftButton = selectedLeftModifier.overrideButton;
  319. buttonControlScript.enabled = true;
  320. }
  321. VRTK_SDKButtonInputOverrideType selectedRightModifier = GetSelectedModifier(buttonControlRightOverrides, controllerReference);
  322. if (selectedRightModifier != null && selectedRightModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
  323. {
  324. buttonControlScript.enabled = false;
  325. buttonControlScript.rightButton = selectedRightModifier.overrideButton;
  326. buttonControlScript.enabled = true;
  327. }
  328. }
  329. }
  330. protected virtual void ManageSlingshotJump()
  331. {
  332. if (slingshotJumpScript != null)
  333. {
  334. VRTK_ControllerReference controllerReference = GetRightThenLeftReference();
  335. VRTK_SDKButtonInputOverrideType selectedActivationModifier = GetSelectedModifier(slingshotJumpActivationOverrides, controllerReference);
  336. if (selectedActivationModifier != null && selectedActivationModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
  337. {
  338. slingshotJumpScript.enabled = false;
  339. slingshotJumpScript.SetActivationButton(selectedActivationModifier.overrideButton);
  340. slingshotJumpScript.enabled = true;
  341. }
  342. VRTK_SDKButtonInputOverrideType selectedCancelModifier = GetSelectedModifier(slingshotJumpCancelOverrides, controllerReference);
  343. if (selectedCancelModifier != null && selectedCancelModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
  344. {
  345. slingshotJumpScript.enabled = false;
  346. slingshotJumpScript.SetCancelButton(selectedCancelModifier.overrideButton);
  347. slingshotJumpScript.enabled = true;
  348. }
  349. }
  350. }
  351. protected virtual void ManageMoveInPlace()
  352. {
  353. if (moveInPlaceScript != null)
  354. {
  355. VRTK_ControllerReference controllerReference = GetRightThenLeftReference();
  356. VRTK_SDKButtonInputOverrideType selectedEngageModifier = GetSelectedModifier(moveInPlaceEngageOverrides, controllerReference);
  357. if (selectedEngageModifier != null && selectedEngageModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
  358. {
  359. moveInPlaceScript.enabled = false;
  360. moveInPlaceScript.engageButton = selectedEngageModifier.overrideButton;
  361. moveInPlaceScript.enabled = true;
  362. }
  363. }
  364. }
  365. protected virtual void ManageStepMultiplier()
  366. {
  367. if (stepMultiplierScript != null)
  368. {
  369. VRTK_ControllerReference controllerReference = GetReferenceFromEvents(stepMultiplierScript.controllerEvents);
  370. VRTK_SDKButtonInputOverrideType selectedModifier = GetSelectedModifier(stepMultiplierActivationOverrides, controllerReference);
  371. if (selectedModifier != null && selectedModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
  372. {
  373. stepMultiplierScript.enabled = false;
  374. stepMultiplierScript.activationButton = selectedModifier.overrideButton;
  375. stepMultiplierScript.enabled = true;
  376. }
  377. }
  378. }
  379. }
  380. }