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.

297 lines
13 KiB

  1. // Interact Controller Appearance|Interactables|30040
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. using System;
  6. using System.Collections;
  7. /// <summary>
  8. /// Event Payload
  9. /// </summary>
  10. /// <param name="interactingObject">The object that is interacting.</param>
  11. /// <param name="ignoredObject">The object that is being ignored.</param>
  12. [Obsolete("`InteractControllerAppearanceEventArgs` will be removed in a future version of VRTK.")]
  13. public struct InteractControllerAppearanceEventArgs
  14. {
  15. public GameObject interactingObject;
  16. public GameObject ignoredObject;
  17. }
  18. /// <summary>
  19. /// Event Payload
  20. /// </summary>
  21. /// <param name="sender">this object</param>
  22. /// <param name="e"><see cref="InteractControllerAppearanceEventArgs"/></param>
  23. [Obsolete("`InteractControllerAppearanceEventHandler` will be removed in a future version of VRTK.")]
  24. public delegate void InteractControllerAppearanceEventHandler(object sender, InteractControllerAppearanceEventArgs e);
  25. /// <summary>
  26. /// The Interact Controller Appearance script is attached on the same GameObject as an Interactable Object script and is used to determine whether the controller model should be visible or hidden on touch, grab or use.
  27. /// </summary>
  28. /// <example>
  29. /// `VRTK/Examples/008_Controller_UsingAGrabbedObject` shows that the controller can be hidden when touching, grabbing and using an object.
  30. /// </example>
  31. [AddComponentMenu("VRTK/Scripts/Interactions/Interactables/VRTK_InteractControllerAppearance")]
  32. [Obsolete("`VRTK_InteractControllerAppearance` has been replaced with `VRTK_InteractObjectAppearance`. This script will be removed in a future version of VRTK.")]
  33. public class VRTK_InteractControllerAppearance : MonoBehaviour
  34. {
  35. [Header("Touch Visibility")]
  36. [Tooltip("Hides the controller model when a valid touch occurs.")]
  37. public bool hideControllerOnTouch = false;
  38. [Tooltip("The amount of seconds to wait before hiding the controller on touch.")]
  39. public float hideDelayOnTouch = 0f;
  40. [Header("Grab Visibility")]
  41. [Tooltip("Hides the controller model when a valid grab occurs.")]
  42. public bool hideControllerOnGrab = false;
  43. [Tooltip("The amount of seconds to wait before hiding the controller on grab.")]
  44. public float hideDelayOnGrab = 0f;
  45. [Header("Use Visibility")]
  46. [Tooltip("Hides the controller model when a valid use occurs.")]
  47. public bool hideControllerOnUse = false;
  48. [Tooltip("The amount of seconds to wait before hiding the controller on use.")]
  49. public float hideDelayOnUse = 0f;
  50. /// <summary>
  51. /// Emitted when the interacting object is hidden.
  52. /// </summary>
  53. public event InteractControllerAppearanceEventHandler ControllerHidden;
  54. /// <summary>
  55. /// Emitted when the interacting object is shown.
  56. /// </summary>
  57. public event InteractControllerAppearanceEventHandler ControllerVisible;
  58. /// <summary>
  59. /// Emitted when the interacting object is hidden on touch.
  60. /// </summary>
  61. public event InteractControllerAppearanceEventHandler HiddenOnTouch;
  62. /// <summary>
  63. /// Emitted when the interacting object is shown on untouch.
  64. /// </summary>
  65. public event InteractControllerAppearanceEventHandler VisibleOnTouch;
  66. /// <summary>
  67. /// Emitted when the interacting object is hidden on grab.
  68. /// </summary>
  69. public event InteractControllerAppearanceEventHandler HiddenOnGrab;
  70. /// <summary>
  71. /// Emitted when the interacting object is shown on ungrab.
  72. /// </summary>
  73. public event InteractControllerAppearanceEventHandler VisibleOnGrab;
  74. /// <summary>
  75. /// Emitted when the interacting object is hidden on use.
  76. /// </summary>
  77. public event InteractControllerAppearanceEventHandler HiddenOnUse;
  78. /// <summary>
  79. /// Emitted when the interacting object is shown on unuse.
  80. /// </summary>
  81. public event InteractControllerAppearanceEventHandler VisibleOnUse;
  82. protected bool touchControllerShow = true;
  83. protected bool grabControllerShow = true;
  84. protected Coroutine hideControllerRoutine;
  85. public virtual void OnControllerHidden(InteractControllerAppearanceEventArgs e)
  86. {
  87. if (ControllerHidden != null)
  88. {
  89. ControllerHidden(this, e);
  90. }
  91. }
  92. public virtual void OnControllerVisible(InteractControllerAppearanceEventArgs e)
  93. {
  94. if (ControllerVisible != null)
  95. {
  96. ControllerVisible(this, e);
  97. }
  98. }
  99. public virtual void OnHiddenOnTouch(InteractControllerAppearanceEventArgs e)
  100. {
  101. if (HiddenOnTouch != null)
  102. {
  103. HiddenOnTouch(this, e);
  104. }
  105. }
  106. public virtual void OnVisibleOnTouch(InteractControllerAppearanceEventArgs e)
  107. {
  108. if (VisibleOnTouch != null)
  109. {
  110. VisibleOnTouch(this, e);
  111. }
  112. }
  113. public virtual void OnHiddenOnGrab(InteractControllerAppearanceEventArgs e)
  114. {
  115. if (HiddenOnGrab != null)
  116. {
  117. HiddenOnGrab(this, e);
  118. }
  119. }
  120. public virtual void OnVisibleOnGrab(InteractControllerAppearanceEventArgs e)
  121. {
  122. if (VisibleOnGrab != null)
  123. {
  124. VisibleOnGrab(this, e);
  125. }
  126. }
  127. public virtual void OnHiddenOnUse(InteractControllerAppearanceEventArgs e)
  128. {
  129. if (HiddenOnUse != null)
  130. {
  131. HiddenOnUse(this, e);
  132. }
  133. }
  134. public virtual void OnVisibleOnUse(InteractControllerAppearanceEventArgs e)
  135. {
  136. if (VisibleOnUse != null)
  137. {
  138. VisibleOnUse(this, e);
  139. }
  140. }
  141. /// <summary>
  142. /// The ToggleControllerOnTouch method determines whether the controller should be shown or hidden when touching an interactable object.
  143. /// </summary>
  144. /// <param name="showController">If true then the controller will attempt to be made visible when no longer touching, if false then the controller will be hidden on touch.</param>
  145. /// <param name="touchingObject">The touching object to apply the visibility state to.</param>
  146. /// <param name="ignoredObject">The object that is currently being interacted with by the touching object which is passed through to the visibility to prevent the object from being hidden as well.</param>
  147. public virtual void ToggleControllerOnTouch(bool showController, GameObject touchingObject, GameObject ignoredObject)
  148. {
  149. if (hideControllerOnTouch)
  150. {
  151. touchControllerShow = showController;
  152. ToggleController(showController, touchingObject, ignoredObject, hideDelayOnTouch);
  153. if (showController)
  154. {
  155. OnVisibleOnTouch(SetEventPayload(touchingObject, ignoredObject));
  156. }
  157. else
  158. {
  159. OnHiddenOnTouch(SetEventPayload(touchingObject, ignoredObject));
  160. }
  161. }
  162. }
  163. /// <summary>
  164. /// The ToggleControllerOnGrab method determines whether the controller should be shown or hidden when grabbing an interactable object.
  165. /// </summary>
  166. /// <param name="showController">If true then the controller will attempt to be made visible when no longer grabbing, if false then the controller will be hidden on grab.</param>
  167. /// <param name="grabbingObject">The grabbing object to apply the visibility state to.</param>
  168. /// <param name="ignoredObject">The object that is currently being interacted with by the grabbing object which is passed through to the visibility to prevent the object from being hidden as well.</param>
  169. public virtual void ToggleControllerOnGrab(bool showController, GameObject grabbingObject, GameObject ignoredObject)
  170. {
  171. if (hideControllerOnGrab)
  172. {
  173. VRTK_InteractableObject objScript = (ignoredObject != null ? ignoredObject.GetComponentInParent<VRTK_InteractableObject>() : null);
  174. //if attempting to show the controller but it's touched and the touch should hide the controller
  175. if (showController && !touchControllerShow && objScript && objScript.IsTouched())
  176. {
  177. return;
  178. }
  179. grabControllerShow = showController;
  180. ToggleController(showController, grabbingObject, ignoredObject, hideDelayOnGrab);
  181. if (showController)
  182. {
  183. OnVisibleOnGrab(SetEventPayload(grabbingObject, ignoredObject));
  184. }
  185. else
  186. {
  187. OnHiddenOnGrab(SetEventPayload(grabbingObject, ignoredObject));
  188. }
  189. }
  190. }
  191. /// <summary>
  192. /// The ToggleControllerOnUse method determines whether the controller should be shown or hidden when using an interactable object.
  193. /// </summary>
  194. /// <param name="showController">If true then the controller will attempt to be made visible when no longer using, if false then the controller will be hidden on use.</param>
  195. /// <param name="usingObject">The using object to apply the visibility state to.</param>
  196. /// <param name="ignoredObject">The object that is currently being interacted with by the using object which is passed through to the visibility to prevent the object from being hidden as well.</param>
  197. public virtual void ToggleControllerOnUse(bool showController, GameObject usingObject, GameObject ignoredObject)
  198. {
  199. if (hideControllerOnUse)
  200. {
  201. VRTK_InteractableObject objScript = (ignoredObject != null ? ignoredObject.GetComponentInParent<VRTK_InteractableObject>() : null);
  202. //if attempting to show the controller but it's grabbed and the grab should hide the controller
  203. if (showController && ((!grabControllerShow && objScript && objScript.IsGrabbed()) || (!touchControllerShow && objScript && objScript.IsTouched())))
  204. {
  205. return;
  206. }
  207. ToggleController(showController, usingObject, ignoredObject, hideDelayOnUse);
  208. if (showController)
  209. {
  210. OnVisibleOnUse(SetEventPayload(usingObject, ignoredObject));
  211. }
  212. else
  213. {
  214. OnHiddenOnUse(SetEventPayload(usingObject, ignoredObject));
  215. }
  216. }
  217. }
  218. protected virtual void OnEnable()
  219. {
  220. if (!GetComponent<VRTK_InteractableObject>())
  221. {
  222. VRTK_Logger.Error(VRTK_Logger.GetCommonMessage(VRTK_Logger.CommonMessageKeys.REQUIRED_COMPONENT_MISSING_FROM_GAMEOBJECT, "VRTK_InteractControllerAppearance", "VRTK_InteractableObject", "the same"));
  223. }
  224. }
  225. protected virtual void OnDisable()
  226. {
  227. if (hideControllerRoutine != null)
  228. {
  229. StopCoroutine(hideControllerRoutine);
  230. }
  231. }
  232. protected virtual void ToggleController(bool showController, GameObject interactingObject, GameObject ignoredObject, float delayTime)
  233. {
  234. if (showController)
  235. {
  236. ShowController(interactingObject, ignoredObject);
  237. }
  238. else
  239. {
  240. hideControllerRoutine = StartCoroutine(HideController(interactingObject, ignoredObject, delayTime));
  241. }
  242. }
  243. protected virtual void ShowController(GameObject interactingObject, GameObject ignoredObject)
  244. {
  245. if (hideControllerRoutine != null)
  246. {
  247. StopCoroutine(hideControllerRoutine);
  248. }
  249. VRTK_ObjectAppearance.SetRendererVisible(interactingObject, ignoredObject);
  250. OnControllerVisible(SetEventPayload(interactingObject, ignoredObject));
  251. }
  252. protected virtual IEnumerator HideController(GameObject interactingObject, GameObject ignoredObject, float delayTime)
  253. {
  254. yield return new WaitForSeconds(delayTime);
  255. VRTK_ObjectAppearance.SetRendererHidden(interactingObject, ignoredObject);
  256. OnControllerHidden(SetEventPayload(interactingObject, ignoredObject));
  257. }
  258. protected virtual InteractControllerAppearanceEventArgs SetEventPayload(GameObject interactingObject, GameObject ignroedObject)
  259. {
  260. InteractControllerAppearanceEventArgs e;
  261. e.interactingObject = interactingObject;
  262. e.ignoredObject = ignroedObject;
  263. return e;
  264. }
  265. }
  266. }