InteractionManagement.cs 6.3 KB

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