InteractionManagement.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. }
  65. // Update is called once per frame
  66. void Update()
  67. {
  68. }
  69. // Open the menu if open = true
  70. public void SetMenu(bool open){
  71. Menu_Opened = open;
  72. Menu_Opened_Listener = Menu_Opened;
  73. }
  74. // Closes menu if already open. Opens and updates menu if menu was closed.
  75. public void SetMenu(){
  76. Menu_Opened = !Menu_Opened;
  77. Menu_Opened_Listener = Menu_Opened;
  78. }
  79. // Lock Robot, if lockRobot = true
  80. public void LockRobot(bool lockRobot)
  81. {
  82. Robot_Locked = lockRobot;
  83. Robot_Locked_Listener = Robot_Locked;
  84. }
  85. // Locks robot if robot was not locked. Unlocks robot if robot was locked before.
  86. public void LockRobot()
  87. {
  88. Robot_Locked = !Robot_Locked;
  89. Robot_Locked_Listener = Robot_Locked;
  90. }
  91. // Show messages to palyer
  92. // time: the duraton(second) that msg will remain
  93. public void SetPlayerText(string msg, float time = 0, bool showOldText = true){
  94. if(time == 0 ){
  95. playerText.text = msg;
  96. playerTextPanel.SetActive(true);
  97. }
  98. else{
  99. StartCoroutine(PrintInfoForXSeconds(msg,time,showOldText));
  100. }
  101. }
  102. // close the message box
  103. public void SetPlayerText(){
  104. playerText.text = "";
  105. playerTextPanel.SetActive(false);
  106. }
  107. IEnumerator PrintInfoForXSeconds(string text, float seconds, bool showOldText)
  108. {
  109. string oldPlayerText = playerText.text;
  110. SetPlayerText(text);
  111. yield return new WaitForSecondsRealtime(seconds);
  112. if(showOldText)
  113. SetPlayerText(oldPlayerText);
  114. else
  115. {
  116. SetPlayerText();
  117. }
  118. }
  119. // Updates camera images and loads new image data on buttons.
  120. // Need CameraAutoLoader.cs
  121. // has not been tested. This function is copied directly from the origin project -Jingyi
  122. public void SetCamera(MenuMaker CameraContent){
  123. cc.updateCams();
  124. StartCoroutine(Load(CameraContent));
  125. //CameraGUI.SetActive(true);
  126. }
  127. IEnumerator Load(MenuMaker CameraContent)
  128. {
  129. yield return new WaitWhile(() => !cc.finishedUpdating);
  130. Debug.Log("VRInput.cs: Starting to render updated camera Images");
  131. foreach(string camera in cc.getDictionary().Keys)
  132. {
  133. Debug.Log("VRInput.cs: Load : " + camera);
  134. GameObject button = GameObject.Find("/camera360/right/image_raw/compressedButton");
  135. CameraContent.loadTexture(camera);
  136. }
  137. SetPlayerText("Cameras Updated.");
  138. yield return new WaitForSeconds(2);
  139. SetPlayerText();
  140. }
  141. // Depending on input scrolls menu up or down.
  142. public void Scroll(string direction, ScrollRect CameraScrollView){
  143. if(direction == "down")
  144. {
  145. if(CameraScrollView.verticalNormalizedPosition >=0.03)
  146. {
  147. CameraScrollView.verticalNormalizedPosition-=0.05f;
  148. Debug.Log("VRInput.cs: Scroll down ");
  149. }
  150. }
  151. else if(direction == "up")
  152. {
  153. if(CameraScrollView.verticalNormalizedPosition <=0.98)
  154. {
  155. CameraScrollView.verticalNormalizedPosition+=0.05f;
  156. Debug.Log("VRInput.cs: Scroll up ");
  157. }
  158. }
  159. }
  160. //SteamVR.Interactionsystem.Samples SelektonUIOptions
  161. public void SetHandSelekton(RenderModelChangerUI prefabsLeft,RenderModelChangerUI prefabsRight)
  162. {
  163. for (int handIndex = 0; handIndex < Player.instance.hands.Length; handIndex++)
  164. {
  165. Hand hand = Player.instance.hands[handIndex];
  166. if (hand != null)
  167. {
  168. if (hand.handType == SteamVR_Input_Sources.RightHand)
  169. hand.SetRenderModel(prefabsRight.rightPrefab);
  170. if (hand.handType == SteamVR_Input_Sources.LeftHand)
  171. hand.SetRenderModel(prefabsLeft.leftPrefab);
  172. }
  173. }
  174. }
  175. public void ShowController()
  176. {
  177. for (int handIndex = 0; handIndex < Player.instance.hands.Length; handIndex++)
  178. {
  179. Hand hand = Player.instance.hands[handIndex];
  180. if (hand != null)
  181. {
  182. hand.ShowController(true);
  183. }
  184. }
  185. }
  186. public void HideController()
  187. {
  188. for (int handIndex = 0; handIndex < Player.instance.hands.Length; handIndex++)
  189. {
  190. Hand hand = Player.instance.hands[handIndex];
  191. if (hand != null)
  192. {
  193. hand.HideController(true);
  194. }
  195. }
  196. }
  197. }