Handle.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Handle the Event in Handle Mode
  3. *
  4. *
  5. */
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. using Valve.VR;
  11. using Valve.VR.InteractionSystem.Sample;
  12. using UnityEngine.UI;
  13. public class Handle : MonoBehaviour,IMode
  14. {
  15. InteractionManagement interactionManagement;
  16. public Player_Control Player_Control;
  17. public GameObject[] ModeMenu;
  18. public Vector2 signal;
  19. public Vector2 Signal => signal;
  20. SteamVR_Action_Vibration vibration = SteamVR_Input.GetVibrationAction("Haptic");
  21. private void OnEnable() {
  22. GameObject.Find("Input").GetComponent<VRInput>().SetMode(this.gameObject);
  23. // The player can be raised or lowered (the aerial perspective is enabled)
  24. Player_Control.upDownEnabled = true;
  25. interactionManagement = InteractionManagement.Instance;
  26. interactionManagement.SetPlayerText( "Control Mode : " + gameObject.name, 7, false);
  27. interactionManagement.SetMenu(false);
  28. // Add SteamVR Actions Listener
  29. SteamVR_Actions.default_Menu.AddOnStateDownListener(MenuActionHandler,SteamVR_Input_Sources.Any);
  30. SteamVR_Actions.default_Lock.AddOnStateDownListener(Locked_onStateDown,SteamVR_Input_Sources.Any);
  31. SteamVR_Actions.default_ModeMenu.AddOnStateDownListener(ModeMenuHandler,SteamVR_Input_Sources.Any);
  32. }
  33. // Start is called before the first frame update
  34. void Start()
  35. {
  36. }
  37. // Update is called once per frame
  38. void Update()
  39. {
  40. if(interactionManagement.Robot_Locked){
  41. signal = new Vector2(0,0);
  42. }
  43. else{
  44. UpdateSignal();
  45. }
  46. }
  47. public void UpdateSignal()
  48. {
  49. SteamVR_Action_Vector2 touchpad = SteamVR_Input.GetVector2Action("read_touchpad");
  50. Vector2 touchValue = touchpad.GetAxis(SteamVR_Input_Sources.RightHand);
  51. SteamVR_Action_Single squezz = SteamVR_Input.GetSingleAction("Squeeze");
  52. float angle = touchValue.x;
  53. float velocity = squezz.GetAxis(SteamVR_Input_Sources.RightHand);
  54. //vibration.Execute(0f, 0.1f, velocity*50f, velocity, SteamVR_Input_Sources.RightHand);
  55. signal = new Vector2(angle,velocity);
  56. }
  57. // open or close the menu
  58. private void MenuActionHandler(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  59. {
  60. interactionManagement.SetMenu();
  61. vibration.Execute(0f, 0.1f, 50f, 1f, SteamVR_Input_Sources.Any);
  62. }
  63. // Handle Robot Locking
  64. private void Locked_onStateDown(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  65. {
  66. if (!interactionManagement.Menu_Opened)
  67. {
  68. interactionManagement.LockRobot();
  69. vibration.Execute(0f, 0.1f, 50f, 1f, SteamVR_Input_Sources.Any);
  70. }
  71. else {
  72. interactionManagement.SetPlayerText("Please close the menu first",5,false);
  73. }
  74. }
  75. // open or close the modemenu: monitor
  76. private void ModeMenuHandler(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  77. {
  78. bool active = false;
  79. foreach(GameObject child in ModeMenu){
  80. active = !child.activeSelf;
  81. child.SetActive(!child.activeSelf);
  82. }
  83. vibration.Execute(0f, 0.1f, 50f, 1f, SteamVR_Input_Sources.Any);
  84. }
  85. private void OnDisable() {
  86. // close the modemenu and the UILaser effect, which parent was changed during runnning
  87. foreach(GameObject child in ModeMenu){
  88. if(child != null)
  89. child.SetActive(false);
  90. }
  91. // Remove SteamVR Actions Listener
  92. SteamVR_Actions.default_Menu.RemoveOnStateDownListener(MenuActionHandler,SteamVR_Input_Sources.Any);
  93. SteamVR_Actions.default_Lock.RemoveOnStateDownListener(Locked_onStateDown,SteamVR_Input_Sources.Any);
  94. SteamVR_Actions.default_ModeMenu.RemoveOnStateDownListener(ModeMenuHandler,SteamVR_Input_Sources.Any);
  95. }
  96. }