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.

223 lines
10 KiB

  1. namespace VRTK.Examples
  2. {
  3. using UnityEngine;
  4. public class VRTK_ControllerAppearance_Example : MonoBehaviour
  5. {
  6. public bool highlightBodyOnlyOnCollision = false;
  7. public bool pulseTriggerHighlightColor = false;
  8. private VRTK_ControllerTooltips tooltips;
  9. private VRTK_ControllerHighlighter highligher;
  10. private VRTK_ControllerEvents events;
  11. private Color highlightColor = Color.yellow;
  12. private Color pulseColor = Color.black;
  13. private Color currentPulseColor;
  14. private float highlightTimer = 0.5f;
  15. private float pulseTimer = 0.75f;
  16. private float dimOpacity = 0.8f;
  17. private float defaultOpacity = 1f;
  18. private bool highlighted;
  19. private void OnEnable()
  20. {
  21. if (GetComponent<VRTK_ControllerEvents>() == null)
  22. {
  23. VRTK_Logger.Error(VRTK_Logger.GetCommonMessage(VRTK_Logger.CommonMessageKeys.REQUIRED_COMPONENT_MISSING_FROM_GAMEOBJECT, "VRTK_ControllerAppearance_Example", "VRTK_ControllerEvents", "the same"));
  24. return;
  25. }
  26. events = GetComponent<VRTK_ControllerEvents>();
  27. highligher = GetComponent<VRTK_ControllerHighlighter>();
  28. tooltips = GetComponentInChildren<VRTK_ControllerTooltips>();
  29. currentPulseColor = pulseColor;
  30. highlighted = false;
  31. //Setup controller event listeners
  32. events.TriggerPressed += DoTriggerPressed;
  33. events.TriggerReleased += DoTriggerReleased;
  34. events.ButtonOnePressed += DoButtonOnePressed;
  35. events.ButtonOneReleased += DoButtonOneReleased;
  36. events.ButtonTwoPressed += DoButtonTwoPressed;
  37. events.ButtonTwoReleased += DoButtonTwoReleased;
  38. events.StartMenuPressed += DoStartMenuPressed;
  39. events.StartMenuReleased += DoStartMenuReleased;
  40. events.GripPressed += DoGripPressed;
  41. events.GripReleased += DoGripReleased;
  42. events.TouchpadPressed += DoTouchpadPressed;
  43. events.TouchpadReleased += DoTouchpadReleased;
  44. tooltips.ToggleTips(false);
  45. }
  46. private void OnDisable()
  47. {
  48. events.TriggerPressed -= DoTriggerPressed;
  49. events.TriggerReleased -= DoTriggerReleased;
  50. events.ButtonOnePressed -= DoButtonOnePressed;
  51. events.ButtonOneReleased -= DoButtonOneReleased;
  52. events.ButtonTwoPressed -= DoButtonTwoPressed;
  53. events.ButtonTwoReleased -= DoButtonTwoReleased;
  54. events.StartMenuPressed -= DoStartMenuPressed;
  55. events.StartMenuReleased -= DoStartMenuReleased;
  56. events.GripPressed -= DoGripPressed;
  57. events.GripReleased -= DoGripReleased;
  58. events.TouchpadPressed -= DoTouchpadPressed;
  59. events.TouchpadReleased -= DoTouchpadReleased;
  60. }
  61. private void PulseTrigger()
  62. {
  63. highligher.HighlightElement(SDK_BaseController.ControllerElements.Trigger, currentPulseColor, pulseTimer);
  64. currentPulseColor = (currentPulseColor == pulseColor ? highlightColor : pulseColor);
  65. }
  66. private void DoTriggerPressed(object sender, ControllerInteractionEventArgs e)
  67. {
  68. tooltips.ToggleTips(true, VRTK_ControllerTooltips.TooltipButtons.TriggerTooltip);
  69. highligher.HighlightElement(SDK_BaseController.ControllerElements.Trigger, highlightColor, (pulseTriggerHighlightColor ? pulseTimer : highlightTimer));
  70. if (pulseTriggerHighlightColor)
  71. {
  72. InvokeRepeating("PulseTrigger", pulseTimer, pulseTimer);
  73. }
  74. VRTK_ObjectAppearance.SetOpacity(VRTK_DeviceFinder.GetModelAliasController(events.gameObject), dimOpacity);
  75. }
  76. private void DoTriggerReleased(object sender, ControllerInteractionEventArgs e)
  77. {
  78. tooltips.ToggleTips(false, VRTK_ControllerTooltips.TooltipButtons.TriggerTooltip);
  79. highligher.UnhighlightElement(SDK_BaseController.ControllerElements.Trigger);
  80. if (pulseTriggerHighlightColor)
  81. {
  82. CancelInvoke("PulseTrigger");
  83. }
  84. if (!events.AnyButtonPressed())
  85. {
  86. VRTK_ObjectAppearance.SetOpacity(VRTK_DeviceFinder.GetModelAliasController(events.gameObject), defaultOpacity);
  87. }
  88. }
  89. private void DoButtonOnePressed(object sender, ControllerInteractionEventArgs e)
  90. {
  91. tooltips.ToggleTips(true, VRTK_ControllerTooltips.TooltipButtons.ButtonOneTooltip);
  92. highligher.HighlightElement(SDK_BaseController.ControllerElements.ButtonOne, highlightColor, highlightTimer);
  93. VRTK_ObjectAppearance.SetOpacity(VRTK_DeviceFinder.GetModelAliasController(events.gameObject), dimOpacity);
  94. }
  95. private void DoButtonOneReleased(object sender, ControllerInteractionEventArgs e)
  96. {
  97. tooltips.ToggleTips(false, VRTK_ControllerTooltips.TooltipButtons.ButtonOneTooltip);
  98. highligher.UnhighlightElement(SDK_BaseController.ControllerElements.ButtonOne);
  99. if (!events.AnyButtonPressed())
  100. {
  101. VRTK_ObjectAppearance.SetOpacity(VRTK_DeviceFinder.GetModelAliasController(events.gameObject), defaultOpacity);
  102. }
  103. }
  104. private void DoButtonTwoPressed(object sender, ControllerInteractionEventArgs e)
  105. {
  106. tooltips.ToggleTips(true, VRTK_ControllerTooltips.TooltipButtons.ButtonTwoTooltip);
  107. highligher.HighlightElement(SDK_BaseController.ControllerElements.ButtonTwo, highlightColor, highlightTimer);
  108. VRTK_ObjectAppearance.SetOpacity(VRTK_DeviceFinder.GetModelAliasController(events.gameObject), dimOpacity);
  109. }
  110. private void DoButtonTwoReleased(object sender, ControllerInteractionEventArgs e)
  111. {
  112. tooltips.ToggleTips(false, VRTK_ControllerTooltips.TooltipButtons.ButtonTwoTooltip);
  113. highligher.UnhighlightElement(SDK_BaseController.ControllerElements.ButtonTwo);
  114. if (!events.AnyButtonPressed())
  115. {
  116. VRTK_ObjectAppearance.SetOpacity(VRTK_DeviceFinder.GetModelAliasController(events.gameObject), defaultOpacity);
  117. }
  118. }
  119. private void DoStartMenuPressed(object sender, ControllerInteractionEventArgs e)
  120. {
  121. tooltips.ToggleTips(true, VRTK_ControllerTooltips.TooltipButtons.StartMenuTooltip);
  122. highligher.HighlightElement(SDK_BaseController.ControllerElements.StartMenu, highlightColor, highlightTimer);
  123. VRTK_ObjectAppearance.SetOpacity(VRTK_DeviceFinder.GetModelAliasController(events.gameObject), dimOpacity);
  124. }
  125. private void DoStartMenuReleased(object sender, ControllerInteractionEventArgs e)
  126. {
  127. tooltips.ToggleTips(false, VRTK_ControllerTooltips.TooltipButtons.StartMenuTooltip);
  128. highligher.UnhighlightElement(SDK_BaseController.ControllerElements.StartMenu);
  129. if (!events.AnyButtonPressed())
  130. {
  131. VRTK_ObjectAppearance.SetOpacity(VRTK_DeviceFinder.GetModelAliasController(events.gameObject), defaultOpacity);
  132. }
  133. }
  134. private void DoGripPressed(object sender, ControllerInteractionEventArgs e)
  135. {
  136. tooltips.ToggleTips(true, VRTK_ControllerTooltips.TooltipButtons.GripTooltip);
  137. highligher.HighlightElement(SDK_BaseController.ControllerElements.GripLeft, highlightColor, highlightTimer);
  138. highligher.HighlightElement(SDK_BaseController.ControllerElements.GripRight, highlightColor, highlightTimer);
  139. VRTK_ObjectAppearance.SetOpacity(VRTK_DeviceFinder.GetModelAliasController(events.gameObject), dimOpacity);
  140. }
  141. private void DoGripReleased(object sender, ControllerInteractionEventArgs e)
  142. {
  143. tooltips.ToggleTips(false, VRTK_ControllerTooltips.TooltipButtons.GripTooltip);
  144. highligher.UnhighlightElement(SDK_BaseController.ControllerElements.GripLeft);
  145. highligher.UnhighlightElement(SDK_BaseController.ControllerElements.GripRight);
  146. if (!events.AnyButtonPressed())
  147. {
  148. VRTK_ObjectAppearance.SetOpacity(VRTK_DeviceFinder.GetModelAliasController(events.gameObject), defaultOpacity);
  149. }
  150. }
  151. private void DoTouchpadPressed(object sender, ControllerInteractionEventArgs e)
  152. {
  153. tooltips.ToggleTips(true, VRTK_ControllerTooltips.TooltipButtons.TouchpadTooltip);
  154. highligher.HighlightElement(SDK_BaseController.ControllerElements.Touchpad, highlightColor, highlightTimer);
  155. VRTK_ObjectAppearance.SetOpacity(VRTK_DeviceFinder.GetModelAliasController(events.gameObject), dimOpacity);
  156. }
  157. private void DoTouchpadReleased(object sender, ControllerInteractionEventArgs e)
  158. {
  159. tooltips.ToggleTips(false, VRTK_ControllerTooltips.TooltipButtons.TouchpadTooltip);
  160. highligher.UnhighlightElement(SDK_BaseController.ControllerElements.Touchpad);
  161. if (!events.AnyButtonPressed())
  162. {
  163. VRTK_ObjectAppearance.SetOpacity(VRTK_DeviceFinder.GetModelAliasController(events.gameObject), defaultOpacity);
  164. }
  165. }
  166. private void OnTriggerEnter(Collider collider)
  167. {
  168. OnTriggerStay(collider);
  169. }
  170. private void OnTriggerStay(Collider collider)
  171. {
  172. if (!VRTK_PlayerObject.IsPlayerObject(collider.gameObject) && !highlighted)
  173. {
  174. if (highlightBodyOnlyOnCollision)
  175. {
  176. highligher.HighlightElement(SDK_BaseController.ControllerElements.Body, highlightColor, highlightTimer);
  177. }
  178. else
  179. {
  180. highligher.HighlightController(highlightColor, highlightTimer);
  181. }
  182. highlighted = true;
  183. }
  184. }
  185. private void OnTriggerExit(Collider collider)
  186. {
  187. if (!VRTK_PlayerObject.IsPlayerObject(collider.gameObject))
  188. {
  189. if (highlightBodyOnlyOnCollision)
  190. {
  191. highligher.UnhighlightElement(SDK_BaseController.ControllerElements.Body);
  192. }
  193. else
  194. {
  195. highligher.UnhighlightController();
  196. }
  197. highlighted = false;
  198. }
  199. }
  200. }
  201. }