Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
6.7 KiB

  1. // SDK Setup Switcher|Prefabs|0010
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. /// <summary>
  11. /// Provides a GUI overlay to allow switching the loaded VRTK_SDKSetup of the the current VRTK_SDKManager.
  12. /// </summary>
  13. /// <remarks>
  14. /// **Prefab Usage:**
  15. /// * Place the `VRTK/Prefabs/SDKSetupSwitcher/SDKSetupSwitcher` prefab into the scene hierarchy.
  16. /// </remarks>
  17. public class VRTK_SDKSetupSwitcher : MonoBehaviour
  18. {
  19. [Header("Fallback Objects")]
  20. [SerializeField]
  21. protected Camera fallbackCamera;
  22. [SerializeField]
  23. protected EventSystem eventSystem;
  24. [Header("Object References")]
  25. [SerializeField]
  26. protected Text currentText;
  27. [SerializeField]
  28. protected RectTransform statusPanel;
  29. [SerializeField]
  30. protected RectTransform selectionPanel;
  31. [SerializeField]
  32. protected Button switchButton;
  33. [SerializeField]
  34. protected Button cancelButton;
  35. [SerializeField]
  36. protected Button chooseButton;
  37. [SerializeField]
  38. protected bool playareaSync = true;
  39. protected enum ViewingState
  40. {
  41. Status,
  42. Selection
  43. }
  44. protected readonly List<GameObject> chooseButtonGameObjects = new List<GameObject>();
  45. protected Transform currentPlayarea;
  46. protected virtual void Awake()
  47. {
  48. fallbackCamera.gameObject.SetActive(false);
  49. eventSystem.gameObject.SetActive(false);
  50. chooseButton.gameObject.SetActive(false);
  51. }
  52. protected virtual void OnEnable()
  53. {
  54. VRTK_SDKManager.SubscribeLoadedSetupChanged(OnLoadedSetupChanged);
  55. switchButton.onClick.AddListener(OnSwitchButtonClick);
  56. cancelButton.onClick.AddListener(OnCancelButtonClick);
  57. Show(ViewingState.Status);
  58. }
  59. protected virtual void OnDisable()
  60. {
  61. VRTK_SDKManager.UnsubscribeLoadedSetupChanged(OnLoadedSetupChanged);
  62. switchButton.onClick.RemoveListener(OnSwitchButtonClick);
  63. cancelButton.onClick.RemoveListener(OnCancelButtonClick);
  64. Show(ViewingState.Status);
  65. }
  66. protected virtual void OnLoadedSetupChanged(VRTK_SDKManager sender, VRTK_SDKManager.LoadedSetupChangeEventArgs e)
  67. {
  68. Show(ViewingState.Status);
  69. if (playareaSync && currentPlayarea != null)
  70. {
  71. Transform newPlayarea = VRTK_DeviceFinder.PlayAreaTransform();
  72. newPlayarea.transform.position = currentPlayarea.transform.position;
  73. newPlayarea.transform.rotation = currentPlayarea.transform.rotation;
  74. VRTK_SharedMethods.SetGlobalScale(newPlayarea, currentPlayarea.transform.lossyScale);
  75. }
  76. currentPlayarea = VRTK_DeviceFinder.PlayAreaTransform();
  77. }
  78. protected virtual void OnSwitchButtonClick()
  79. {
  80. Show(ViewingState.Selection);
  81. }
  82. protected virtual void OnCancelButtonClick()
  83. {
  84. Show(ViewingState.Status);
  85. }
  86. protected virtual void Show(ViewingState viewingState)
  87. {
  88. switch (viewingState)
  89. {
  90. case ViewingState.Status:
  91. RemoveCreatedChooseButtons();
  92. UpdateCurrentText();
  93. selectionPanel.gameObject.SetActive(false);
  94. statusPanel.gameObject.SetActive(true);
  95. break;
  96. case ViewingState.Selection:
  97. AddSelectionButtons();
  98. selectionPanel.gameObject.SetActive(true);
  99. statusPanel.gameObject.SetActive(false);
  100. break;
  101. default:
  102. VRTK_Logger.Fatal(new ArgumentOutOfRangeException("viewingState", viewingState, null));
  103. return;
  104. }
  105. bool isAnyOtherCameraUsed = VRTK_SDKManager.GetAllSDKSetups().Any(setup => setup != null && setup.gameObject.activeSelf)
  106. || VRTK_DeviceFinder.HeadsetCamera() != null;
  107. fallbackCamera.gameObject.SetActive(!isAnyOtherCameraUsed);
  108. eventSystem.gameObject.SetActive(EventSystem.current == null || EventSystem.current == eventSystem);
  109. }
  110. protected virtual void UpdateCurrentText()
  111. {
  112. VRTK_SDKSetup loadedSetup = VRTK_SDKManager.GetLoadedSDKSetup();
  113. currentText.text = (loadedSetup == null ? "None" : loadedSetup.name);
  114. }
  115. protected virtual void AddSelectionButtons()
  116. {
  117. if (VRTK_SDKManager.GetLoadedSDKSetup() != null)
  118. {
  119. GameObject chooseNoneButton = Instantiate(chooseButton.gameObject, chooseButton.transform.parent);
  120. chooseNoneButton.GetComponentInChildren<Text>().text = "None";
  121. chooseNoneButton.name = "ChooseNoneButton";
  122. chooseNoneButton.SetActive(true);
  123. chooseNoneButton.GetComponent<Button>().onClick.AddListener(
  124. () => VRTK_SDKManager.AttemptUnloadSDKSetup(true)
  125. );
  126. chooseButtonGameObjects.Add(chooseNoneButton);
  127. }
  128. VRTK_SDKSetup[] setups = VRTK_SDKManager.GetAllSDKSetups();
  129. for (int index = 0; index < setups.Length; index++)
  130. {
  131. VRTK_SDKSetup setup = setups[index];
  132. if (setup == null || setup == VRTK_SDKManager.GetLoadedSDKSetup())
  133. {
  134. continue;
  135. }
  136. GameObject chooseButtonCopy = Instantiate(chooseButton.gameObject, chooseButton.transform.parent);
  137. chooseButtonCopy.GetComponentInChildren<Text>().text = setup.name;
  138. chooseButtonCopy.name = string.Format("Choose{0}Button", setup.name);
  139. chooseButtonCopy.SetActive(true);
  140. int indexCopy = index;
  141. Button button = chooseButtonCopy.GetComponent<Button>();
  142. button.onClick.AddListener(
  143. () => VRTK_SDKManager.AttemptTryLoadSDKSetup(indexCopy, true, setups)
  144. );
  145. ColorBlock buttonColors = button.colors;
  146. buttonColors.colorMultiplier = setup.isValid ? 1.0f : 0.8f;
  147. button.colors = buttonColors;
  148. chooseButtonGameObjects.Add(chooseButtonCopy);
  149. }
  150. }
  151. protected virtual void RemoveCreatedChooseButtons()
  152. {
  153. chooseButtonGameObjects.ForEach(Destroy);
  154. chooseButtonGameObjects.Clear();
  155. }
  156. }
  157. }