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.

280 lines
7.5 KiB

  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Enables/disables objects based on connectivity and assigned roles.
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using Valve.VR;
  8. public class SteamVR_ControllerManager : MonoBehaviour
  9. {
  10. public GameObject left, right;
  11. [Tooltip("Populate with objects you want to assign to additional controllers")]
  12. public GameObject[] objects;
  13. [Tooltip("Set to true if you want objects arbitrarily assigned to controllers before their role (left vs right) is identified")]
  14. public bool assignAllBeforeIdentified;
  15. uint[] indices; // assigned
  16. bool[] connected = new bool[OpenVR.k_unMaxTrackedDeviceCount]; // controllers only
  17. // cached roles - may or may not be connected
  18. uint leftIndex = OpenVR.k_unTrackedDeviceIndexInvalid;
  19. uint rightIndex = OpenVR.k_unTrackedDeviceIndexInvalid;
  20. // Helper function to avoid adding duplicates to object array.
  21. void SetUniqueObject(GameObject o, int index)
  22. {
  23. for (int i = 0; i < index; i++)
  24. if (objects[i] == o)
  25. return;
  26. objects[index] = o;
  27. }
  28. // This needs to be called if you update left, right or objects at runtime (e.g. when dyanmically spawned).
  29. public void UpdateTargets()
  30. {
  31. // Add left and right entries to the head of the list so we only have to operate on the list itself.
  32. var objects = this.objects;
  33. var additional = (objects != null) ? objects.Length : 0;
  34. this.objects = new GameObject[2 + additional];
  35. SetUniqueObject(right, 0);
  36. SetUniqueObject(left, 1);
  37. for (int i = 0; i < additional; i++)
  38. SetUniqueObject(objects[i], 2 + i);
  39. // Reset assignments.
  40. indices = new uint[2 + additional];
  41. for (int i = 0; i < indices.Length; i++)
  42. indices[i] = OpenVR.k_unTrackedDeviceIndexInvalid;
  43. }
  44. SteamVR_Events.Action inputFocusAction, deviceConnectedAction, trackedDeviceRoleChangedAction;
  45. void Awake()
  46. {
  47. UpdateTargets();
  48. }
  49. SteamVR_ControllerManager()
  50. {
  51. inputFocusAction = SteamVR_Events.InputFocusAction(OnInputFocus);
  52. deviceConnectedAction = SteamVR_Events.DeviceConnectedAction(OnDeviceConnected);
  53. trackedDeviceRoleChangedAction = SteamVR_Events.SystemAction(EVREventType.VREvent_TrackedDeviceRoleChanged, OnTrackedDeviceRoleChanged);
  54. }
  55. void OnEnable()
  56. {
  57. for (int i = 0; i < objects.Length; i++)
  58. {
  59. var obj = objects[i];
  60. if (obj != null)
  61. obj.SetActive(false);
  62. indices[i] = OpenVR.k_unTrackedDeviceIndexInvalid;
  63. }
  64. Refresh();
  65. for (int i = 0; i < SteamVR.connected.Length; i++)
  66. if (SteamVR.connected[i])
  67. OnDeviceConnected(i, true);
  68. inputFocusAction.enabled = true;
  69. deviceConnectedAction.enabled = true;
  70. trackedDeviceRoleChangedAction.enabled = true;
  71. }
  72. void OnDisable()
  73. {
  74. inputFocusAction.enabled = false;
  75. deviceConnectedAction.enabled = false;
  76. trackedDeviceRoleChangedAction.enabled = false;
  77. }
  78. static string hiddenPrefix = "hidden (", hiddenPostfix = ")";
  79. static string[] labels = { "left", "right" };
  80. // Hide controllers when the dashboard is up.
  81. private void OnInputFocus(bool hasFocus)
  82. {
  83. if (hasFocus)
  84. {
  85. for (int i = 0; i < objects.Length; i++)
  86. {
  87. var obj = objects[i];
  88. if (obj != null)
  89. {
  90. var label = (i < 2) ? labels[i] : (i - 1).ToString();
  91. ShowObject(obj.transform, hiddenPrefix + label + hiddenPostfix);
  92. }
  93. }
  94. }
  95. else
  96. {
  97. for (int i = 0; i < objects.Length; i++)
  98. {
  99. var obj = objects[i];
  100. if (obj != null)
  101. {
  102. var label = (i < 2) ? labels[i] : (i - 1).ToString();
  103. HideObject(obj.transform, hiddenPrefix + label + hiddenPostfix);
  104. }
  105. }
  106. }
  107. }
  108. // Reparents to a new object and deactivates that object (this allows
  109. // us to call SetActive in OnDeviceConnected independently.
  110. private void HideObject(Transform t, string name)
  111. {
  112. if (t.gameObject.name.StartsWith(hiddenPrefix))
  113. {
  114. Debug.Log("Ignoring double-hide.");
  115. return;
  116. }
  117. var hidden = new GameObject(name).transform;
  118. hidden.parent = t.parent;
  119. t.parent = hidden;
  120. hidden.gameObject.SetActive(false);
  121. }
  122. private void ShowObject(Transform t, string name)
  123. {
  124. var hidden = t.parent;
  125. if (hidden.gameObject.name != name)
  126. return;
  127. t.parent = hidden.parent;
  128. Destroy(hidden.gameObject);
  129. }
  130. private void SetTrackedDeviceIndex(int objectIndex, uint trackedDeviceIndex)
  131. {
  132. // First make sure no one else is already using this index.
  133. if (trackedDeviceIndex != OpenVR.k_unTrackedDeviceIndexInvalid)
  134. {
  135. for (int i = 0; i < objects.Length; i++)
  136. {
  137. if (i != objectIndex && indices[i] == trackedDeviceIndex)
  138. {
  139. var obj = objects[i];
  140. if (obj != null)
  141. obj.SetActive(false);
  142. indices[i] = OpenVR.k_unTrackedDeviceIndexInvalid;
  143. }
  144. }
  145. }
  146. // Only set when changed.
  147. if (trackedDeviceIndex != indices[objectIndex])
  148. {
  149. indices[objectIndex] = trackedDeviceIndex;
  150. var obj = objects[objectIndex];
  151. if (obj != null)
  152. {
  153. if (trackedDeviceIndex == OpenVR.k_unTrackedDeviceIndexInvalid)
  154. obj.SetActive(false);
  155. else
  156. {
  157. obj.SetActive(true);
  158. obj.BroadcastMessage("SetDeviceIndex", (int)trackedDeviceIndex, SendMessageOptions.DontRequireReceiver);
  159. }
  160. }
  161. }
  162. }
  163. // Keep track of assigned roles.
  164. private void OnTrackedDeviceRoleChanged(VREvent_t vrEvent)
  165. {
  166. Refresh();
  167. }
  168. // Keep track of connected controller indices.
  169. private void OnDeviceConnected(int index, bool connected)
  170. {
  171. bool changed = this.connected[index];
  172. this.connected[index] = false;
  173. if (connected)
  174. {
  175. var system = OpenVR.System;
  176. if (system != null)
  177. {
  178. var deviceClass = system.GetTrackedDeviceClass((uint)index);
  179. if (deviceClass == ETrackedDeviceClass.Controller ||
  180. deviceClass == ETrackedDeviceClass.GenericTracker)
  181. {
  182. this.connected[index] = true;
  183. changed = !changed; // if we clear and set the same index, nothing has changed
  184. }
  185. }
  186. }
  187. if (changed)
  188. Refresh();
  189. }
  190. public void Refresh()
  191. {
  192. int objectIndex = 0;
  193. var system = OpenVR.System;
  194. if (system != null)
  195. {
  196. leftIndex = system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand);
  197. rightIndex = system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand);
  198. }
  199. // If neither role has been assigned yet, try hooking up at least the right controller.
  200. if (leftIndex == OpenVR.k_unTrackedDeviceIndexInvalid && rightIndex == OpenVR.k_unTrackedDeviceIndexInvalid)
  201. {
  202. for (uint deviceIndex = 0; deviceIndex < connected.Length; deviceIndex++)
  203. {
  204. if (objectIndex >= objects.Length)
  205. break;
  206. if (!connected[deviceIndex])
  207. continue;
  208. SetTrackedDeviceIndex(objectIndex++, deviceIndex);
  209. if (!assignAllBeforeIdentified)
  210. break;
  211. }
  212. }
  213. else
  214. {
  215. SetTrackedDeviceIndex(objectIndex++, (rightIndex < connected.Length && connected[rightIndex]) ? rightIndex : OpenVR.k_unTrackedDeviceIndexInvalid);
  216. SetTrackedDeviceIndex(objectIndex++, (leftIndex < connected.Length && connected[leftIndex]) ? leftIndex : OpenVR.k_unTrackedDeviceIndexInvalid);
  217. // Assign out any additional controllers only after both left and right have been assigned.
  218. if (leftIndex != OpenVR.k_unTrackedDeviceIndexInvalid && rightIndex != OpenVR.k_unTrackedDeviceIndexInvalid)
  219. {
  220. for (uint deviceIndex = 0; deviceIndex < connected.Length; deviceIndex++)
  221. {
  222. if (objectIndex >= objects.Length)
  223. break;
  224. if (!connected[deviceIndex])
  225. continue;
  226. if (deviceIndex != leftIndex && deviceIndex != rightIndex)
  227. {
  228. SetTrackedDeviceIndex(objectIndex++, deviceIndex);
  229. }
  230. }
  231. }
  232. }
  233. // Reset the rest.
  234. while (objectIndex < objects.Length)
  235. {
  236. SetTrackedDeviceIndex(objectIndex++, OpenVR.k_unTrackedDeviceIndexInvalid);
  237. }
  238. }
  239. }