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.

423 lines
21 KiB

  1. // Daydream Controller|SDK_Daydream|004
  2. namespace VRTK
  3. {
  4. #if VRTK_DEFINE_SDK_DAYDREAM
  5. using UnityEngine;
  6. using System.Collections.Generic;
  7. #endif
  8. /// <summary>
  9. /// The Daydream Controller SDK script provides a bridge to SDK methods that deal with the input devices.
  10. /// </summary>
  11. [SDK_Description(typeof(SDK_DaydreamSystem))]
  12. public class SDK_DaydreamController
  13. #if VRTK_DEFINE_SDK_DAYDREAM
  14. : SDK_BaseController
  15. #else
  16. : SDK_FallbackController
  17. #endif
  18. {
  19. #if VRTK_DEFINE_SDK_DAYDREAM
  20. protected GameObject controller;
  21. protected Vector3 prevPosition;
  22. protected Vector3 prevRotation;
  23. protected Vector3 velocity = Vector3.zero;
  24. protected Vector3 angularVelocity = Vector3.zero;
  25. /// <summary>
  26. /// The ProcessUpdate method enables an SDK to run logic for every Unity Update
  27. /// </summary>
  28. /// <param name="controllerReference">The reference for the controller.</param>
  29. /// <param name="options">A dictionary of generic options that can be used to within the update.</param>
  30. public override void ProcessUpdate(VRTK_ControllerReference controllerReference, Dictionary<string, object> options)
  31. {
  32. if (controller == null)
  33. {
  34. return;
  35. }
  36. velocity = (controller.transform.position - prevPosition) / Time.deltaTime;
  37. angularVelocity = Quaternion.FromToRotation(prevRotation, controller.transform.eulerAngles).eulerAngles / Time.deltaTime;
  38. prevPosition = controller.transform.position;
  39. prevRotation = controller.transform.rotation.eulerAngles;
  40. }
  41. /// <summary>
  42. /// The ProcessFixedUpdate method enables an SDK to run logic for every Unity FixedUpdate
  43. /// </summary>
  44. /// <param name="controllerReference">The reference for the controller.</param>
  45. /// <param name="options">A dictionary of generic options that can be used to within the fixed update.</param>
  46. public override void ProcessFixedUpdate(VRTK_ControllerReference controllerReference, Dictionary<string, object> options)
  47. {
  48. }
  49. /// <summary>
  50. /// The GetCurrentControllerType method returns the current used ControllerType based on the SDK and headset being used.
  51. /// </summary>
  52. /// <param name="controllerReference">The reference to the controller to get type of.</param>
  53. /// <returns>The ControllerType based on the SDK and headset being used.</returns>
  54. public override ControllerType GetCurrentControllerType(VRTK_ControllerReference controllerReference = null)
  55. {
  56. return ControllerType.Daydream_Controller;
  57. }
  58. /// <summary>
  59. /// The GetControllerDefaultColliderPath returns the path to the prefab that contains the collider objects for the default controller of this SDK.
  60. /// </summary>
  61. /// <param name="hand">The controller hand to check for</param>
  62. /// <returns>A path to the resource that contains the collider GameObject.</returns>
  63. public override string GetControllerDefaultColliderPath(ControllerHand hand)
  64. {
  65. return "ControllerColliders/Fallback";
  66. }
  67. /// <summary>
  68. /// The GetControllerElementPath returns the path to the game object that the given controller element for the given hand resides in.
  69. /// </summary>
  70. /// <param name="element">The controller element to look up.</param>
  71. /// <param name="hand">The controller hand to look up.</param>
  72. /// <param name="fullPath">Whether to get the initial path or the full path to the element.</param>
  73. /// <returns>A string containing the path to the game object that the controller element resides in.</returns>
  74. public override string GetControllerElementPath(ControllerElements element, ControllerHand hand, bool fullPath = false)
  75. {
  76. //TODO: Use Gvr's tooltips or add an attach object ourselves
  77. string dd = "Controller/ddcontroller/";
  78. string pad = dd + "Tooltips/TouchPadOutside";
  79. string app = dd + "Tooltips/AppButtonOutside";
  80. switch (element)
  81. {
  82. case ControllerElements.AttachPoint:
  83. return pad; //TODO: attach point at tip of controller?
  84. case ControllerElements.Touchpad:
  85. return pad;
  86. case ControllerElements.ButtonOne:
  87. return app;
  88. default:
  89. return dd;
  90. }
  91. }
  92. /// <summary>
  93. /// The GetControllerIndex method returns the index of the given controller.
  94. /// </summary>
  95. /// <param name="controller">The GameObject containing the controller.</param>
  96. /// <returns>The index of the given controller.</returns>
  97. public override uint GetControllerIndex(GameObject controller)
  98. {
  99. return (CheckActualOrScriptAliasControllerIsRightHand(controller) ? 1 : uint.MaxValue);
  100. }
  101. /// <summary>
  102. /// The GetControllerByIndex method returns the GameObject of a controller with a specific index.
  103. /// </summary>
  104. /// <param name="index">The index of the controller to find.</param>
  105. /// <param name="actual">If true it will return the actual controller, if false it will return the script alias controller GameObject.</param>
  106. /// <returns></returns>
  107. public override GameObject GetControllerByIndex(uint index, bool actual = false)
  108. {
  109. return (index == 1 ? controller : null);
  110. }
  111. /// <summary>
  112. /// The GetControllerOrigin method returns the origin of the given controller.
  113. /// </summary>
  114. /// <param name="controllerReference">The reference to the controller to retrieve the origin from.</param>
  115. /// <returns>A Transform containing the origin of the controller.</returns>
  116. public override Transform GetControllerOrigin(VRTK_ControllerReference controllerReference)
  117. {
  118. return controllerReference.actual.transform;
  119. }
  120. /// <summary>
  121. /// The GenerateControllerPointerOrigin method can create a custom pointer origin Transform to represent the pointer position and forward.
  122. /// </summary>
  123. /// <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>
  124. /// <returns>A generated Transform that contains the custom pointer origin.</returns>
  125. [System.Obsolete("GenerateControllerPointerOrigin has been deprecated and will be removed in a future version of VRTK.")]
  126. public override Transform GenerateControllerPointerOrigin(GameObject parent)
  127. {
  128. return null;
  129. }
  130. /// <summary>
  131. /// The GetControllerLeftHand method returns the GameObject containing the representation of the left hand controller.
  132. /// </summary>
  133. /// <param name="actual">If true it will return the actual controller, if false it will return the script alias controller GameObject.</param>
  134. /// <returns>The GameObject containing the left hand controller.</returns>
  135. public override GameObject GetControllerLeftHand(bool actual = false)
  136. {
  137. return null;
  138. }
  139. /// <summary>
  140. /// The GetControllerRightHand method returns the GameObject containing the representation of the right hand controller.
  141. /// </summary>
  142. /// <param name="actual">If true it will return the actual controller, if false it will return the script alias controller GameObject.</param>
  143. /// <returns>The GameObject containing the right hand controller.</returns>
  144. public override GameObject GetControllerRightHand(bool actual = false)
  145. {
  146. controller = GetSDKManagerControllerRightHand(actual);
  147. if ((controller == null) && actual)
  148. {
  149. controller = VRTK_SharedMethods.FindEvenInactiveGameObject<GvrControllerVisualManager>("Controller", true);
  150. }
  151. if (controller != null)
  152. {
  153. prevPosition = controller.transform.position;
  154. prevRotation = controller.transform.rotation.eulerAngles;
  155. }
  156. return controller;
  157. }
  158. /// <summary>
  159. /// The IsControllerLeftHand/1 method is used to check if the given controller is the the left hand controller.
  160. /// </summary>
  161. /// <param name="controller">The GameObject to check.</param>
  162. /// <returns>Returns true if the given controller is the left hand controller.</returns>
  163. public override bool IsControllerLeftHand(GameObject controller)
  164. {
  165. return false;
  166. }
  167. /// <summary>
  168. /// The IsControllerRightHand/1 method is used to check if the given controller is the the right hand controller.
  169. /// </summary>
  170. /// <param name="controller">The GameObject to check.</param>
  171. /// <returns>Returns true if the given controller is the right hand controller.</returns>
  172. public override bool IsControllerRightHand(GameObject controller)
  173. {
  174. return true;
  175. }
  176. /// <summary>
  177. /// The IsControllerLeftHand/2 method is used to check if the given controller is the the left hand controller.
  178. /// </summary>
  179. /// <param name="controller">The GameObject to check.</param>
  180. /// <param name="actual">If true it will check the actual controller, if false it will check the script alias controller.</param>
  181. /// <returns>Returns true if the given controller is the left hand controller.</returns>
  182. public override bool IsControllerLeftHand(GameObject controller, bool actual)
  183. {
  184. return false;
  185. }
  186. /// <summary>
  187. /// The IsControllerRightHand/2 method is used to check if the given controller is the the right hand controller.
  188. /// </summary>
  189. /// <param name="controller">The GameObject to check.</param>
  190. /// <param name="actual">If true it will check the actual controller, if false it will check the script alias controller.</param>
  191. /// <returns>Returns true if the given controller is the right hand controller.</returns>
  192. public override bool IsControllerRightHand(GameObject controller, bool actual)
  193. {
  194. return true;
  195. }
  196. /// <summary>
  197. /// The WaitForControllerModel method determines whether the controller model for the given hand requires waiting to load in on scene start.
  198. /// </summary>
  199. /// <param name="hand">The hand to determine if the controller model will be ready for.</param>
  200. /// <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>
  201. public override bool WaitForControllerModel(ControllerHand hand)
  202. {
  203. return false;
  204. }
  205. /// <summary>
  206. /// The GetControllerModel method returns the model alias for the given GameObject.
  207. /// </summary>
  208. /// <param name="controller">The GameObject to get the model alias for.</param>
  209. /// <returns>The GameObject that has the model alias within it.</returns>
  210. public override GameObject GetControllerModel(GameObject controller)
  211. {
  212. return GetControllerModelFromController(controller);
  213. }
  214. /// <summary>
  215. /// The GetControllerModel method returns the model alias for the given controller hand.
  216. /// </summary>
  217. /// <param name="hand">The hand enum of which controller model to retrieve.</param>
  218. /// <returns>The GameObject that has the model alias within it.</returns>
  219. public override GameObject GetControllerModel(ControllerHand hand)
  220. {
  221. GameObject model = GetSDKManagerControllerModelForHand(hand);
  222. if (model == null)
  223. {
  224. GameObject controller = null;
  225. switch (hand)
  226. {
  227. case ControllerHand.Left:
  228. controller = GetControllerLeftHand(true);
  229. break;
  230. case ControllerHand.Right:
  231. controller = GetControllerRightHand(true);
  232. break;
  233. }
  234. if (controller != null)
  235. {
  236. model = controller;
  237. }
  238. }
  239. return model;
  240. }
  241. /// <summary>
  242. /// The GetControllerRenderModel method gets the game object that contains the given controller's render model.
  243. /// </summary>
  244. /// <param name="controllerReference">The reference to the controller to check.</param>
  245. /// <returns>A GameObject containing the object that has a render model for the controller.</returns>
  246. public override GameObject GetControllerRenderModel(VRTK_ControllerReference controllerReference)
  247. {
  248. return controllerReference.model;
  249. }
  250. /// <summary>
  251. /// The SetControllerRenderModelWheel method sets the state of the scroll wheel on the controller render model.
  252. /// </summary>
  253. /// <param name="renderModel">The GameObject containing the controller render model.</param>
  254. /// <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>
  255. public override void SetControllerRenderModelWheel(GameObject renderModel, bool state)
  256. {
  257. }
  258. /// <summary>
  259. /// The HapticPulse/2 method is used to initiate a simple haptic pulse on the tracked object of the given controller reference.
  260. /// </summary>
  261. /// <param name="controllerReference">The reference to the tracked object to initiate the haptic pulse on.</param>
  262. /// <param name="strength">The intensity of the rumble of the controller motor. `0` to `1`.</param>
  263. public override void HapticPulse(VRTK_ControllerReference controllerReference, float strength = 0.5f)
  264. {
  265. }
  266. /// <summary>
  267. /// 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.
  268. /// </summary>
  269. /// <param name="controllerReference">The reference to the tracked object to initiate the haptic pulse on.</param>
  270. /// <param name="clip">The audio clip to use for the haptic pattern.</param>
  271. public override bool HapticPulse(VRTK_ControllerReference controllerReference, AudioClip clip)
  272. {
  273. //Return true so it just always prevents doing a fallback routine.
  274. return true;
  275. }
  276. /// <summary>
  277. /// The GetHapticModifiers method is used to return modifiers for the duration and interval if the SDK handles it slightly differently.
  278. /// </summary>
  279. /// <returns>An SDK_ControllerHapticModifiers object with a given `durationModifier` and an `intervalModifier`.</returns>
  280. public override SDK_ControllerHapticModifiers GetHapticModifiers()
  281. {
  282. return new SDK_ControllerHapticModifiers();
  283. }
  284. /// <summary>
  285. /// The GetVelocity method is used to determine the current velocity of the tracked object on the given controller reference.
  286. /// </summary>
  287. /// <param name="controllerReference">The reference to the tracked object to check for.</param>
  288. /// <returns>A Vector3 containing the current velocity of the tracked object.</returns>
  289. public override Vector3 GetVelocity(VRTK_ControllerReference controllerReference)
  290. {
  291. return velocity;
  292. }
  293. /// <summary>
  294. /// The GetAngularVelocity method is used to determine the current angular velocity of the tracked object on the given controller reference.
  295. /// </summary>
  296. /// <param name="controllerReference">The reference to the tracked object to check for.</param>
  297. /// <returns>A Vector3 containing the current angular velocity of the tracked object.</returns>
  298. public override Vector3 GetAngularVelocity(VRTK_ControllerReference controllerReference)
  299. {
  300. return angularVelocity;
  301. }
  302. /// <summary>
  303. /// The IsTouchpadStatic method is used to determine if the touchpad is currently not being moved.
  304. /// </summary>
  305. /// <param name="currentAxisValues"></param>
  306. /// <param name="previousAxisValues"></param>
  307. /// <param name="compareFidelity"></param>
  308. /// <returns>Returns true if the touchpad is not currently being touched or moved.</returns>
  309. public override bool IsTouchpadStatic(bool isTouched, Vector2 currentAxisValues, Vector2 previousAxisValues, int compareFidelity)
  310. {
  311. return (!isTouched || VRTK_SharedMethods.Vector2ShallowCompare(currentAxisValues, previousAxisValues, compareFidelity));
  312. }
  313. /// <summary>
  314. /// The GetButtonAxis method retrieves the current X/Y axis values for the given button type on the given controller reference.
  315. /// </summary>
  316. /// <param name="buttonType">The type of button to check for the axis on.</param>
  317. /// <param name="controllerReference">The reference to the controller to check the button axis on.</param>
  318. /// <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>
  319. public override Vector2 GetButtonAxis(ButtonTypes buttonType, VRTK_ControllerReference controllerReference)
  320. {
  321. return (buttonType == ButtonTypes.Touchpad ? GvrController.TouchPos : Vector2.zero);
  322. }
  323. /// <summary>
  324. /// The GetButtonSenseAxis method retrieves the current sense axis value for the given button type on the given controller reference.
  325. /// </summary>
  326. /// <param name="buttonType">The type of button to check for the sense axis on.</param>
  327. /// <param name="controllerReference">The reference to the controller to check the sense axis on.</param>
  328. /// <returns>The current sense axis value.</returns>
  329. public override float GetButtonSenseAxis(ButtonTypes buttonType, VRTK_ControllerReference controllerReference)
  330. {
  331. return 0f;
  332. }
  333. /// <summary>
  334. /// The GetButtonHairlineDelta method is used to get the difference between the current button press and the previous frame button press.
  335. /// </summary>
  336. /// <param name="buttonType">The type of button to get the hairline delta for.</param>
  337. /// <param name="controllerReference">The reference to the controller to get the hairline delta for.</param>
  338. /// <returns>The delta between the button presses.</returns>
  339. public override float GetButtonHairlineDelta(ButtonTypes buttonType, VRTK_ControllerReference controllerReference)
  340. {
  341. return 0f;
  342. }
  343. /// <summary>
  344. /// 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.
  345. /// </summary>
  346. /// <param name="buttonType">The type of button to check for the state of.</param>
  347. /// <param name="pressType">The button state to check for.</param>
  348. /// <param name="controllerReference">The reference to the controller to check the button state on.</param>
  349. /// <returns>Returns true if the given button is in the state of the given press type on the given controller reference.</returns>
  350. public override bool GetControllerButtonState(ButtonTypes buttonType, ButtonPressTypes pressType, VRTK_ControllerReference controllerReference)
  351. {
  352. switch (buttonType)
  353. {
  354. case ButtonTypes.Touchpad:
  355. switch (pressType)
  356. {
  357. case ButtonPressTypes.Press:
  358. return GvrController.ClickButton;
  359. case ButtonPressTypes.PressDown:
  360. return GvrController.ClickButtonDown;
  361. case ButtonPressTypes.PressUp:
  362. return GvrController.ClickButtonUp;
  363. case ButtonPressTypes.Touch:
  364. return GvrController.IsTouching;
  365. case ButtonPressTypes.TouchDown:
  366. return GvrController.TouchDown;
  367. case ButtonPressTypes.TouchUp:
  368. return GvrController.TouchUp;
  369. }
  370. break;
  371. case ButtonTypes.ButtonOne:
  372. switch (pressType)
  373. {
  374. case ButtonPressTypes.Press:
  375. return GvrController.AppButton;
  376. case ButtonPressTypes.PressDown:
  377. return GvrController.AppButtonDown;
  378. case ButtonPressTypes.PressUp:
  379. return GvrController.AppButtonUp;
  380. }
  381. break;
  382. }
  383. return false;
  384. }
  385. #endif
  386. }
  387. }