ControllerInput.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //***********************************************************
  2. // Filename: ControllerInput.cs
  3. // Author: Marco Fendrich, Moritz Kolvenbach
  4. // Last changes: Thursday, 9th of August 2018
  5. // Content: Central controllerInput management
  6. //***********************************************************
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. /// <summary>
  11. /// A class managing the central controller input
  12. /// </summary>
  13. public class ControllerInput : MonoBehaviour
  14. {
  15. // scripts to be set for given button
  16. public MonoBehaviour triggerButtonCall;
  17. public MonoBehaviour touchpadButtonCall;
  18. public MonoBehaviour gripButtonCall;
  19. public MonoBehaviour menuButtonCall;
  20. // private entities for the scripts to be casted into
  21. private IButton triggerButton;
  22. private IButton touchpadButton;
  23. private ITouchpadAxis touchpadAxis;
  24. private IButton gripButton;
  25. private IButton menuButton;
  26. // trackedObj reference used to get full controller and its input
  27. private SteamVR_Controller.Device Controller
  28. { get { return SteamVR_Controller.Input((int)trackedObj.index); } }
  29. // object this script is attached to as a general tracked object
  30. private SteamVR_TrackedObject trackedObj;
  31. void Start()
  32. {
  33. // cast the single calls to see if they implement the necessary interface
  34. triggerButton = triggerButtonCall as IButton;
  35. touchpadButton = touchpadButtonCall as IButton;
  36. touchpadAxis = touchpadButtonCall as ITouchpadAxis;
  37. gripButton = gripButtonCall as IButton;
  38. menuButton = menuButtonCall as IButton;
  39. }
  40. void Awake()
  41. {
  42. trackedObj = GetComponent<SteamVR_TrackedObject>();
  43. }
  44. void Update()
  45. {
  46. /* call all casted buttons if they're not null with the methods implemented by the interfaces */
  47. // Trigger calls
  48. if (triggerButton != null)
  49. {
  50. if (Controller.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
  51. triggerButton.OnButtonDown(gameObject, (int)trackedObj.index);
  52. if (Controller.GetPressUp(SteamVR_Controller.ButtonMask.Trigger))
  53. triggerButton.OnButtonUp();
  54. }
  55. // Touchpad calls
  56. if (touchpadButton != null)
  57. {
  58. if (Controller.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad))
  59. touchpadButton.OnButtonDown(gameObject, (int)trackedObj.index);
  60. if (Controller.GetPressUp(SteamVR_Controller.ButtonMask.Touchpad))
  61. touchpadButton.OnButtonUp();
  62. } // Additional call if added script supports axis coordinate input
  63. if (touchpadAxis != null && Controller.GetPress(SteamVR_Controller.ButtonMask.Touchpad))
  64. touchpadAxis.OnButtonPressed(Controller.GetAxis());
  65. // Grip calls
  66. if (gripButton != null)
  67. {
  68. if (Controller.GetPressDown(SteamVR_Controller.ButtonMask.Grip))
  69. gripButton.OnButtonDown(gameObject, (int)trackedObj.index);
  70. if (Controller.GetPressUp(SteamVR_Controller.ButtonMask.Grip))
  71. gripButton.OnButtonUp();
  72. }
  73. // Menu calls
  74. if (menuButton != null)
  75. {
  76. if (Controller.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
  77. menuButton.OnButtonDown(gameObject, (int)trackedObj.index);
  78. if (Controller.GetPressUp(SteamVR_Controller.ButtonMask.ApplicationMenu))
  79. menuButton.OnButtonUp();
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// An interface used by scripts which need acces to controllerButtonInputs; for press and release of a button
  85. /// </summary>
  86. public interface IButton
  87. {
  88. void OnButtonDown(GameObject controllerObject, int controllerIdentificator);
  89. void OnButtonUp();
  90. }
  91. /// <summary>
  92. /// An interface used by scripts that need acces to the location input from the trackpad
  93. /// </summary>
  94. public interface ITouchpadAxis
  95. {
  96. void OnButtonPressed(Vector2 axis);
  97. }