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.

136 lines
5.0 KiB

  1. namespace VRTK
  2. {
  3. using UnityEngine;
  4. [RequireComponent(typeof(VRTK_RadialMenu))]
  5. public class VRTK_RadialMenuController : MonoBehaviour
  6. {
  7. [Tooltip("The controller to listen to the controller events on.")]
  8. public VRTK_ControllerEvents events;
  9. protected VRTK_RadialMenu menu;
  10. protected TouchAngleDeflection currentTad; //Keep track of angle and deflection for when we click
  11. protected bool touchpadTouched;
  12. protected virtual void Awake()
  13. {
  14. menu = GetComponent<VRTK_RadialMenu>();
  15. Initialize();
  16. }
  17. protected virtual void Initialize()
  18. {
  19. if (events == null)
  20. {
  21. events = GetComponentInParent<VRTK_ControllerEvents>();
  22. }
  23. }
  24. protected virtual void OnEnable()
  25. {
  26. if (events == null)
  27. {
  28. VRTK_Logger.Error(VRTK_Logger.GetCommonMessage(VRTK_Logger.CommonMessageKeys.REQUIRED_COMPONENT_MISSING_NOT_INJECTED, "RadialMenuController", "VRTK_ControllerEvents", "events", "the parent"));
  29. return;
  30. }
  31. else
  32. {
  33. events.TouchpadPressed += new ControllerInteractionEventHandler(DoTouchpadClicked);
  34. events.TouchpadReleased += new ControllerInteractionEventHandler(DoTouchpadUnclicked);
  35. events.TouchpadTouchStart += new ControllerInteractionEventHandler(DoTouchpadTouched);
  36. events.TouchpadTouchEnd += new ControllerInteractionEventHandler(DoTouchpadUntouched);
  37. events.TouchpadAxisChanged += new ControllerInteractionEventHandler(DoTouchpadAxisChanged);
  38. menu.FireHapticPulse += new HapticPulseEventHandler(AttemptHapticPulse);
  39. }
  40. }
  41. protected virtual void OnDisable()
  42. {
  43. events.TouchpadPressed -= new ControllerInteractionEventHandler(DoTouchpadClicked);
  44. events.TouchpadReleased -= new ControllerInteractionEventHandler(DoTouchpadUnclicked);
  45. events.TouchpadTouchStart -= new ControllerInteractionEventHandler(DoTouchpadTouched);
  46. events.TouchpadTouchEnd -= new ControllerInteractionEventHandler(DoTouchpadUntouched);
  47. events.TouchpadAxisChanged -= new ControllerInteractionEventHandler(DoTouchpadAxisChanged);
  48. menu.FireHapticPulse -= new HapticPulseEventHandler(AttemptHapticPulse);
  49. }
  50. protected virtual void DoClickButton(object sender = null) // The optional argument reduces the need for middleman functions in subclasses whose events likely pass object sender
  51. {
  52. menu.ClickButton(currentTad);
  53. }
  54. protected virtual void DoUnClickButton(object sender = null)
  55. {
  56. menu.UnClickButton(currentTad);
  57. }
  58. protected virtual void DoShowMenu(TouchAngleDeflection initialTad, object sender = null)
  59. {
  60. menu.ShowMenu();
  61. DoChangeAngle(initialTad); // Needed to register initial touch position before the touchpad axis actually changes
  62. }
  63. protected virtual void DoHideMenu(bool force, object sender = null)
  64. {
  65. menu.StopTouching();
  66. menu.HideMenu(force);
  67. }
  68. protected virtual void DoChangeAngle(TouchAngleDeflection givenTouchAngleDeflection, object sender = null)
  69. {
  70. currentTad = givenTouchAngleDeflection;
  71. menu.HoverButton(currentTad);
  72. }
  73. protected virtual void AttemptHapticPulse(float strength)
  74. {
  75. if (events)
  76. {
  77. VRTK_ControllerHaptics.TriggerHapticPulse(VRTK_ControllerReference.GetControllerReference(events.gameObject), strength);
  78. }
  79. }
  80. protected virtual void DoTouchpadClicked(object sender, ControllerInteractionEventArgs e)
  81. {
  82. DoClickButton();
  83. }
  84. protected virtual void DoTouchpadUnclicked(object sender, ControllerInteractionEventArgs e)
  85. {
  86. DoUnClickButton();
  87. }
  88. protected virtual void DoTouchpadTouched(object sender, ControllerInteractionEventArgs e)
  89. {
  90. touchpadTouched = true;
  91. DoShowMenu(CalculateAngle(e));
  92. }
  93. protected virtual void DoTouchpadUntouched(object sender, ControllerInteractionEventArgs e)
  94. {
  95. touchpadTouched = false;
  96. DoHideMenu(false);
  97. }
  98. //Touchpad finger moved position
  99. protected virtual void DoTouchpadAxisChanged(object sender, ControllerInteractionEventArgs e)
  100. {
  101. if (touchpadTouched)
  102. {
  103. DoChangeAngle(CalculateAngle(e));
  104. }
  105. }
  106. protected virtual TouchAngleDeflection CalculateAngle(ControllerInteractionEventArgs e)
  107. {
  108. TouchAngleDeflection touchAngleDeflection = new TouchAngleDeflection();
  109. touchAngleDeflection.angle = 360 - e.touchpadAngle;
  110. touchAngleDeflection.deflection = e.touchpadAxis.magnitude;
  111. return touchAngleDeflection;
  112. }
  113. }
  114. }