InteractionManagement.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Control center, processing interaction, menu display, robot lock etc.
  3. *
  4. */
  5. using UnityEngine;
  6. using System.Collections;
  7. using Valve.VR;
  8. using UnityEngine.UI;
  9. using Valve.VR.InteractionSystem;
  10. using Valve.VR.InteractionSystem.Sample;
  11. using TMPro;
  12. public class InteractionManagement : MonoBehaviour
  13. {
  14. public static InteractionManagement Instance { get; private set; }
  15. public bool Robot_Locked = false;
  16. public bool Menu_Opened = true;
  17. public GameObject MenuGUI;
  18. public TextMeshPro playerText;
  19. public GameObject playerTextPanel;
  20. public CameraCreator cc;
  21. public GameObject[] Laser;
  22. // Listener(Handle the Change of the attribute)
  23. private bool Robot_Locked_Listener{
  24. get{return Robot_Locked; }
  25. set{
  26. if(Robot_Locked){
  27. SetPlayerText("Robot is locked",7,false);
  28. }
  29. else{
  30. SetPlayerText ("Robot is unlocked",7,false);
  31. }
  32. }
  33. }
  34. private bool Menu_Opened_Listener{
  35. get{return Menu_Opened; }
  36. set{
  37. if(Menu_Opened)
  38. {
  39. // open the menu
  40. SetPlayerText("Open the Menu", 3, false);
  41. foreach(GameObject child in Laser){
  42. child.SetActive(true);
  43. }
  44. MenuGUI.SetActive(true);
  45. LockRobot(true);
  46. ShowController();
  47. }
  48. else
  49. {
  50. MenuGUI.SetActive(false);
  51. foreach(GameObject child in Laser){
  52. child.SetActive(false);
  53. }
  54. }
  55. }
  56. }
  57. private void Awake()
  58. {
  59. Instance = this;
  60. }
  61. // Start is called before the first frame update
  62. void Start()
  63. {
  64. //SetMenu(true);
  65. }
  66. // Update is called once per frame
  67. void Update()
  68. {
  69. }
  70. // Open the menu if open = true
  71. public void SetMenu(bool open){
  72. Menu_Opened = open;
  73. Menu_Opened_Listener = Menu_Opened;
  74. }
  75. // Closes menu if already open. Opens and updates menu if menu was closed.
  76. public void SetMenu(){
  77. Menu_Opened = !Menu_Opened;
  78. Menu_Opened_Listener = Menu_Opened;
  79. }
  80. // Lock Robot, if lockRobot = true
  81. public void LockRobot(bool lockRobot)
  82. {
  83. Robot_Locked = lockRobot;
  84. Robot_Locked_Listener = Robot_Locked;
  85. }
  86. // Locks robot if robot was not locked. Unlocks robot if robot was locked before.
  87. public void LockRobot()
  88. {
  89. Robot_Locked = !Robot_Locked;
  90. Robot_Locked_Listener = Robot_Locked;
  91. }
  92. public void SetPlayerText(string msg, float time = 0, bool showOldText = true){
  93. if(time == 0 ){
  94. playerText.text = msg;
  95. playerTextPanel.SetActive(true);
  96. }
  97. else{
  98. StartCoroutine(PrintInfoForXSeconds(msg,time,showOldText));
  99. }
  100. }
  101. public void SetPlayerText(){
  102. playerText.text = "";
  103. playerTextPanel.SetActive(false);
  104. }
  105. // Experimental: Prints a text for "seconds" seconds in player text. If showOldText is true, shows text that was shown before after time is over.
  106. IEnumerator PrintInfoForXSeconds(string text, float seconds, bool showOldText)
  107. {
  108. string oldPlayerText = playerText.text;
  109. SetPlayerText(text);
  110. yield return new WaitForSecondsRealtime(seconds);
  111. if(showOldText)
  112. SetPlayerText(oldPlayerText);
  113. else
  114. {
  115. SetPlayerText();
  116. }
  117. }
  118. // Updates camera images and loads new image data on buttons.
  119. public void SetCamera(MenuMaker CameraContent){
  120. cc.updateCams();
  121. StartCoroutine(Load(CameraContent));
  122. //CameraGUI.SetActive(true);
  123. }
  124. IEnumerator Load(MenuMaker CameraContent)
  125. {
  126. yield return new WaitWhile(() => !cc.finishedUpdating);
  127. Debug.Log("VRInput.cs: Starting to render updated camera Images");
  128. foreach(string camera in cc.getDictionary().Keys)
  129. {
  130. Debug.Log("VRInput.cs: Load : " + camera);
  131. GameObject button = GameObject.Find("/camera360/right/image_raw/compressedButton");
  132. CameraContent.loadTexture(camera);
  133. }
  134. SetPlayerText("Cameras Updated.");
  135. yield return new WaitForSeconds(2);
  136. SetPlayerText();
  137. }
  138. // Depending on input scrolls menu up or down.
  139. public void Scroll(string direction, ScrollRect CameraScrollView){
  140. if(direction == "down")
  141. {
  142. if(CameraScrollView.verticalNormalizedPosition >=0.03)
  143. {
  144. CameraScrollView.verticalNormalizedPosition-=0.05f;
  145. Debug.Log("VRInput.cs: Scroll down ");
  146. }
  147. }
  148. else if(direction == "up")
  149. {
  150. if(CameraScrollView.verticalNormalizedPosition <=0.98)
  151. {
  152. CameraScrollView.verticalNormalizedPosition+=0.05f;
  153. Debug.Log("VRInput.cs: Scroll up ");
  154. }
  155. }
  156. }
  157. //SteamVR.Interactionsystem.Samples SelektonUIOptions
  158. public void SetHandSelekton(RenderModelChangerUI prefabsLeft,RenderModelChangerUI prefabsRight)
  159. {
  160. for (int handIndex = 0; handIndex < Player.instance.hands.Length; handIndex++)
  161. {
  162. Hand hand = Player.instance.hands[handIndex];
  163. if (hand != null)
  164. {
  165. if (hand.handType == SteamVR_Input_Sources.RightHand)
  166. hand.SetRenderModel(prefabsRight.rightPrefab);
  167. if (hand.handType == SteamVR_Input_Sources.LeftHand)
  168. hand.SetRenderModel(prefabsLeft.leftPrefab);
  169. }
  170. }
  171. }
  172. public void ShowController()
  173. {
  174. for (int handIndex = 0; handIndex < Player.instance.hands.Length; handIndex++)
  175. {
  176. Hand hand = Player.instance.hands[handIndex];
  177. if (hand != null)
  178. {
  179. hand.ShowController(true);
  180. }
  181. }
  182. }
  183. public void HideController()
  184. {
  185. for (int handIndex = 0; handIndex < Player.instance.hands.Length; handIndex++)
  186. {
  187. Hand hand = Player.instance.hands[handIndex];
  188. if (hand != null)
  189. {
  190. hand.HideController(true);
  191. }
  192. }
  193. }
  194. }