VRInputCopy.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * author: Lydia Ebbinghaus, Ayumi Bischoff, Yannic Seidler
  3. */
  4. using UnityEngine;
  5. using System.Collections;
  6. using Valve.VR;
  7. using UnityEngine.UI;
  8. using Valve.VR.InteractionSystem;
  9. // Class that handles controller input.
  10. // Attached to GameObject Input in the hierachy.
  11. public class VRInputCopy : MonoBehaviour
  12. {
  13. public ScrollRect scrollRect;
  14. public SteamVR_LaserPointerMod laserPointer;
  15. public GameObject cameraMenu;
  16. public GameObject playerTextPanel;
  17. public TextMesh playerText;
  18. public CameraCreator cc;
  19. public MenuMaker menuMaker;
  20. public Hand leftHand;
  21. public Hand rightHand;
  22. public bool inMenu, isLocked;
  23. // If more functions are added: a possibility to differentiate between a game mode and menu mode.
  24. public SteamVR_ActionSet menuSet;
  25. //public SteamVR_ActionSet gameSet;
  26. SteamVR_Action_Vibration vibration = SteamVR_Input.GetVibrationAction("Haptic");
  27. SteamVR_Action_Vector2 touchpad = SteamVR_Input.GetVector2Action("read_touchpad");
  28. SteamVR_Action_Boolean touchPressed = SteamVR_Input.GetBooleanAction("is_klicked");
  29. SteamVR_Action_Boolean scrollUp = SteamVR_Input.GetBooleanAction("TouchUp");
  30. SteamVR_Action_Boolean scrollDown = SteamVR_Input.GetBooleanAction("TouchDown");
  31. SteamVR_Action_Boolean locked = SteamVR_Input.GetBooleanAction("Lock");
  32. SteamVR_Action_Boolean menu = SteamVR_Input.GetBooleanAction("Menu");
  33. Vector2 touchValue;
  34. bool down, up, controlPressed, interactUI, menuPressed;
  35. private void Awake()
  36. {
  37. locked.onStateDown += Locked_onStateDown;
  38. }
  39. // Start is called before the first frame update. At the beginning user is in menu and robot is locked.
  40. void Start()
  41. {
  42. playerTextPanel.SetActive(false);
  43. inMenu = true;
  44. isLocked = true;
  45. menuSet.Activate();
  46. }
  47. // Haptic feedback for lock button.
  48. private void Locked_onStateDown(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  49. {
  50. if(!inMenu)
  51. {
  52. // Experimental: Writes Information and deletes text after specifc time period.
  53. // PrintInfoForXSeconds("Push Trackpad to drive.",5,true);
  54. vibration.Execute(0f, 0.5f, 100f, 1f, SteamVR_Input_Sources.RightHand);
  55. }
  56. }
  57. private void OnDestroy()
  58. {
  59. locked.onStateDown -= Locked_onStateDown;
  60. }
  61. // Update is called once per frame
  62. // Gets input of controllers and handles it.
  63. // Same Controller Input handled depending on if user is in menu or not.
  64. void Update()
  65. { Debug.Log( "touchValue" + touchpad.GetAxis(SteamVR_Input_Sources.RightHand));
  66. menuPressed = menu.GetStateDown(SteamVR_Input_Sources.LeftHand);
  67. if(menuPressed) OpenCloseMenu();
  68. if(inMenu)
  69. {
  70. down = scrollDown.GetState(SteamVR_Input_Sources.LeftHand);
  71. up = scrollUp.GetState(SteamVR_Input_Sources.LeftHand);
  72. if(down || up) scroll();
  73. }
  74. else
  75. {
  76. touchValue = touchpad.GetAxis(SteamVR_Input_Sources.RightHand);
  77. controlPressed = touchPressed.GetState(SteamVR_Input_Sources.RightHand);
  78. interactUI = locked.GetStateDown(SteamVR_Input_Sources.RightHand);
  79. if(interactUI) lockUnlock();
  80. }
  81. }
  82. // Closes menu if already open. Opens and updates menu if menu was closed.
  83. private void OpenCloseMenu()
  84. {
  85. Debug.Log("VRInput.cs: Menu pressed");
  86. if(inMenu)
  87. {
  88. //menuSet.Deactivate();
  89. //gameSet.Activate();
  90. cameraMenu.SetActive(false);
  91. isLocked = false;
  92. laserPointer.active = false;
  93. lockUnlock();
  94. playerText.text = "Robot is locked";
  95. playerTextPanel.SetActive(true);
  96. }
  97. else
  98. {
  99. //gameSet.Deactivate();
  100. //menuSet.Activate();
  101. // To update Camera Pictures when opening menu.
  102. playerText.text = "Please wait for Cameras to update";
  103. playerTextPanel.SetActive(true);
  104. cc.updateCams();
  105. StartCoroutine(Load());
  106. cameraMenu.SetActive(true);
  107. isLocked = true;
  108. laserPointer.active = true;
  109. }
  110. inMenu = !inMenu;
  111. }
  112. // Locks robot if robot was not locked. Unlocks robot if robot was locked before.
  113. private void lockUnlock()
  114. {
  115. if(isLocked)
  116. {
  117. playerText.text = "";
  118. playerTextPanel.SetActive(false);
  119. }
  120. else
  121. {
  122. playerText.text = ("Robot is locked");
  123. playerTextPanel.SetActive(true);
  124. }
  125. isLocked = !isLocked;
  126. }
  127. // Depending on input scrolls menu up or down.
  128. private void scroll()
  129. {
  130. if(down)
  131. {
  132. if(scrollRect.verticalNormalizedPosition >=0.03)
  133. {
  134. scrollRect.verticalNormalizedPosition-=0.05f;
  135. Debug.Log("VRInput.cs: Scroll down ");
  136. }
  137. }
  138. else if(up)
  139. {
  140. if(scrollRect.verticalNormalizedPosition <=0.98)
  141. {
  142. scrollRect.verticalNormalizedPosition+=0.05f;
  143. Debug.Log("VRInput.cs: Scroll up ");
  144. }
  145. }
  146. }
  147. // Experimental: Prints a text for "seconds" seconds in player text. If showOldText is true, shows text that was shown before after time is over.
  148. IEnumerator PrintInfoForXSeconds(string text, float seconds, bool showOldText)
  149. {
  150. string oldPlayerText = playerText.text;
  151. playerText.text = text;
  152. Debug.Log(text);
  153. yield return new WaitForSecondsRealtime(seconds);
  154. if(showOldText)
  155. playerText.text = oldPlayerText;
  156. else
  157. {
  158. playerText.text = "";
  159. playerTextPanel.SetActive(false);
  160. }
  161. }
  162. // Updates camera images and loads new image data on buttons.
  163. IEnumerator Load()
  164. {
  165. yield return new WaitWhile(() => !cc.finishedUpdating);
  166. Debug.Log("VRInput.cs: Starting to render updated camera Images");
  167. foreach(string camera in cc.getDictionary().Keys)
  168. {
  169. Debug.Log("VRInput.cs: Load : " + camera);
  170. GameObject button = GameObject.Find("/camera360/right/image_raw/compressedButton");
  171. menuMaker.loadTexture(camera);
  172. }
  173. if(inMenu)
  174. {
  175. playerText.text = "Cameras Updated.";
  176. yield return new WaitForSeconds(2);
  177. if(inMenu)
  178. {
  179. playerText.text = "";
  180. playerTextPanel.SetActive(false);
  181. }
  182. }
  183. }
  184. public Vector2 getSignal()
  185. {
  186. return touchValue;
  187. }
  188. // true, if driving is enabled (e.g trackpad pressed).
  189. public bool getControlPressed()
  190. {
  191. return controlPressed;
  192. }
  193. }