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.

79 lines
3.2 KiB

  1. namespace VRTK.Examples
  2. {
  3. using UnityEngine;
  4. [System.Obsolete("`VRTK_RoomExtender_ControllerExample` will be removed in a future version of VRTK.")]
  5. public class VRTK_RoomExtender_ControllerExample : MonoBehaviour
  6. {
  7. protected VRTK_RoomExtender roomExtender;
  8. // Use this for initialization
  9. private void Start()
  10. {
  11. if (GetComponent<VRTK_ControllerEvents>() == null)
  12. {
  13. VRTK_Logger.Error(VRTK_Logger.GetCommonMessage(VRTK_Logger.CommonMessageKeys.REQUIRED_COMPONENT_MISSING_FROM_GAMEOBJECT, "VRTK_RoomExtender_ControllerExample", "VRTK_ControllerEvents", "the Controller Alias"));
  14. return;
  15. }
  16. if (FindObjectOfType<VRTK_RoomExtender>() == null)
  17. {
  18. VRTK_Logger.Error(VRTK_Logger.GetCommonMessage(VRTK_Logger.CommonMessageKeys.REQUIRED_COMPONENT_MISSING_FROM_SCENE, "VRTK_RoomExtender_ControllerExample", "VRTK_RoomExtender"));
  19. return;
  20. }
  21. roomExtender = FindObjectOfType<VRTK_RoomExtender>();
  22. //Setup controller event listeners
  23. GetComponent<VRTK_ControllerEvents>().TouchpadPressed += new ControllerInteractionEventHandler(DoTouchpadPressed);
  24. GetComponent<VRTK_ControllerEvents>().TouchpadReleased += new ControllerInteractionEventHandler(DoTouchpadReleased);
  25. GetComponent<VRTK_ControllerEvents>().ButtonTwoPressed += new ControllerInteractionEventHandler(DoSwitchMovementFunction);
  26. }
  27. private void DoTouchpadPressed(object sender, ControllerInteractionEventArgs e)
  28. {
  29. roomExtender.additionalMovementMultiplier = e.touchpadAxis.magnitude * 5 > 1 ? e.touchpadAxis.magnitude * 5 : 1;
  30. if (roomExtender.additionalMovementEnabledOnButtonPress)
  31. {
  32. EnableAdditionalMovement();
  33. }
  34. else
  35. {
  36. DisableAdditionalMovement();
  37. }
  38. }
  39. private void DoTouchpadReleased(object sender, ControllerInteractionEventArgs e)
  40. {
  41. if (roomExtender.additionalMovementEnabledOnButtonPress)
  42. {
  43. DisableAdditionalMovement();
  44. }
  45. else
  46. {
  47. EnableAdditionalMovement();
  48. }
  49. }
  50. private void DoSwitchMovementFunction(object sender, ControllerInteractionEventArgs e)
  51. {
  52. switch (roomExtender.movementFunction)
  53. {
  54. case VRTK_RoomExtender.MovementFunction.Nonlinear:
  55. roomExtender.movementFunction = VRTK_RoomExtender.MovementFunction.LinearDirect;
  56. break;
  57. case VRTK_RoomExtender.MovementFunction.LinearDirect:
  58. roomExtender.movementFunction = VRTK_RoomExtender.MovementFunction.Nonlinear;
  59. break;
  60. default:
  61. break;
  62. }
  63. }
  64. private void EnableAdditionalMovement()
  65. {
  66. roomExtender.additionalMovementEnabled = true;
  67. }
  68. private void DisableAdditionalMovement()
  69. {
  70. roomExtender.additionalMovementEnabled = false;
  71. }
  72. }
  73. }