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.

162 lines
7.6 KiB

  1. // Button Control|Locomotion|20080
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. /// <summary>
  6. /// Provides the ability to control a GameObject's position based the press of a controller button linked to a specific axis direction.
  7. /// </summary>
  8. /// <remarks>
  9. /// > This script forms the stub of emitting the axis X and Y changes that are then digested by the corresponding Object Control Actions that are listening for the relevant event.
  10. ///
  11. /// **Required Components:**
  12. /// * `VRTK_ControllerEvents` - The Controller Events script to listen for button presses events on.
  13. ///
  14. /// **Optional Components:**
  15. /// * `VRTK_BodyPhysics` - The Body Physics script to utilise to determine if falling is occuring.
  16. ///
  17. /// **Script Usage:**
  18. /// * Place the `VRTK_ButtonControl` script on either:
  19. /// * The GameObject with the Controller Events script.
  20. /// * Any other scene GameObject and provide a valid `VRTK_ControllerEvents` component to the `Controller` parameter of this script.
  21. /// * Place a corresponding Object Control Action for the Button Control script to notify of axis changes. Without a corresponding Object Control Action, the Button Control script will do nothing.
  22. /// </remarks>
  23. [AddComponentMenu("VRTK/Scripts/Locomotion/VRTK_ButtonControl")]
  24. public class VRTK_ButtonControl : VRTK_ObjectControl
  25. {
  26. [Header("Button Control Settings")]
  27. [Tooltip("The button to set the y axis to +1.")]
  28. public VRTK_ControllerEvents.ButtonAlias forwardButton = VRTK_ControllerEvents.ButtonAlias.TriggerPress;
  29. [Tooltip("The button to set the y axis to -1.")]
  30. public VRTK_ControllerEvents.ButtonAlias backwardButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
  31. [Tooltip("The button to set the x axis to -1.")]
  32. public VRTK_ControllerEvents.ButtonAlias leftButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
  33. [Tooltip("The button to set the x axis to +1.")]
  34. public VRTK_ControllerEvents.ButtonAlias rightButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
  35. protected bool forwardPressed;
  36. protected bool backwardPressed;
  37. protected bool leftPressed;
  38. protected bool rightPressed;
  39. protected VRTK_ControllerEvents.ButtonAlias subscribedForwardButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
  40. protected VRTK_ControllerEvents.ButtonAlias subscribedBackwardButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
  41. protected VRTK_ControllerEvents.ButtonAlias subscribedLeftButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
  42. protected VRTK_ControllerEvents.ButtonAlias subscribedRightButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
  43. protected Vector2 axisDeadzone = Vector2.zero;
  44. protected override void Update()
  45. {
  46. base.Update();
  47. if (forwardButton != subscribedForwardButton || backwardButton != subscribedBackwardButton || leftButton != subscribedLeftButton || rightButton != subscribedRightButton)
  48. {
  49. SetListeners(true);
  50. }
  51. }
  52. protected override void ControlFixedUpdate()
  53. {
  54. float xAxis = (leftPressed ? -1f : (rightPressed ? 1f : 0f));
  55. float yAxis = (forwardPressed ? 1f : (backwardPressed ? -1f : 0f));
  56. currentAxis = new Vector2(xAxis, yAxis);
  57. if (currentAxis.x != 0f)
  58. {
  59. OnXAxisChanged(SetEventArguements(directionDevice.right, currentAxis.x, axisDeadzone.x));
  60. }
  61. if (currentAxis.y != 0f)
  62. {
  63. OnYAxisChanged(SetEventArguements(directionDevice.forward, currentAxis.y, axisDeadzone.y));
  64. }
  65. }
  66. protected override VRTK_ObjectControl GetOtherControl()
  67. {
  68. GameObject foundController = (VRTK_DeviceFinder.IsControllerLeftHand(gameObject) ? VRTK_DeviceFinder.GetControllerRightHand(false) : VRTK_DeviceFinder.GetControllerLeftHand(false));
  69. if (foundController)
  70. {
  71. return foundController.GetComponentInChildren<VRTK_ButtonControl>();
  72. }
  73. return null;
  74. }
  75. protected override void SetListeners(bool state)
  76. {
  77. SetDirectionListener(state, forwardButton, ref subscribedForwardButton, ForwardButtonPressed, ForwardButtonReleased);
  78. SetDirectionListener(state, backwardButton, ref subscribedBackwardButton, BackwardButtonPressed, BackwardButtonReleased);
  79. SetDirectionListener(state, leftButton, ref subscribedLeftButton, LeftButtonPressed, LeftButtonReleased);
  80. SetDirectionListener(state, rightButton, ref subscribedRightButton, RightButtonPressed, RightButtonReleased);
  81. }
  82. protected override bool IsInAction()
  83. {
  84. return (forwardPressed || backwardPressed || leftPressed || rightPressed);
  85. }
  86. protected virtual void SetDirectionListener(bool state, VRTK_ControllerEvents.ButtonAlias directionButton, ref VRTK_ControllerEvents.ButtonAlias subscribedDirectionButton, ControllerInteractionEventHandler pressCallback, ControllerInteractionEventHandler releaseCallback)
  87. {
  88. if (controllerEvents)
  89. {
  90. if (subscribedDirectionButton != VRTK_ControllerEvents.ButtonAlias.Undefined && (!state || directionButton == VRTK_ControllerEvents.ButtonAlias.Undefined || directionButton != subscribedDirectionButton))
  91. {
  92. controllerEvents.UnsubscribeToButtonAliasEvent(subscribedDirectionButton, true, pressCallback);
  93. controllerEvents.UnsubscribeToButtonAliasEvent(subscribedDirectionButton, false, releaseCallback);
  94. subscribedDirectionButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
  95. }
  96. if (state && directionButton != VRTK_ControllerEvents.ButtonAlias.Undefined && directionButton != subscribedDirectionButton)
  97. {
  98. controllerEvents.SubscribeToButtonAliasEvent(directionButton, true, pressCallback);
  99. controllerEvents.SubscribeToButtonAliasEvent(directionButton, false, releaseCallback);
  100. subscribedDirectionButton = directionButton;
  101. }
  102. }
  103. }
  104. protected virtual void ForwardButtonPressed(object sender, ControllerInteractionEventArgs e)
  105. {
  106. forwardPressed = true;
  107. backwardPressed = false;
  108. }
  109. protected virtual void ForwardButtonReleased(object sender, ControllerInteractionEventArgs e)
  110. {
  111. forwardPressed = false;
  112. }
  113. protected virtual void BackwardButtonPressed(object sender, ControllerInteractionEventArgs e)
  114. {
  115. backwardPressed = true;
  116. forwardPressed = false;
  117. }
  118. protected virtual void BackwardButtonReleased(object sender, ControllerInteractionEventArgs e)
  119. {
  120. backwardPressed = false;
  121. }
  122. protected virtual void LeftButtonPressed(object sender, ControllerInteractionEventArgs e)
  123. {
  124. leftPressed = true;
  125. rightPressed = false;
  126. }
  127. protected virtual void LeftButtonReleased(object sender, ControllerInteractionEventArgs e)
  128. {
  129. leftPressed = false;
  130. }
  131. protected virtual void RightButtonPressed(object sender, ControllerInteractionEventArgs e)
  132. {
  133. rightPressed = true;
  134. leftPressed = false;
  135. }
  136. protected virtual void RightButtonReleased(object sender, ControllerInteractionEventArgs e)
  137. {
  138. rightPressed = false;
  139. }
  140. }
  141. }