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.

207 lines
7.2 KiB

  1. namespace VRTK
  2. {
  3. using UnityEngine;
  4. using System;
  5. using System.Collections.Generic;
  6. public class VRTK_ControllerReference : IEquatable<VRTK_ControllerReference>
  7. {
  8. public static Dictionary<uint, VRTK_ControllerReference> controllerReferences = new Dictionary<uint, VRTK_ControllerReference>();
  9. public static VRTK_ControllerReference GetControllerReference(uint controllerIndex)
  10. {
  11. if (controllerIndex < uint.MaxValue)
  12. {
  13. VRTK_ControllerReference foundReference = VRTK_SharedMethods.GetDictionaryValue(controllerReferences, controllerIndex);
  14. if (foundReference != null)
  15. {
  16. return foundReference;
  17. }
  18. return new VRTK_ControllerReference(controllerIndex);
  19. }
  20. return null;
  21. }
  22. public static VRTK_ControllerReference GetControllerReference(GameObject controllerObject)
  23. {
  24. //Try and get the index from either the actual or alias
  25. uint controllerIndex = VRTK_SDK_Bridge.GetControllerIndex(controllerObject);
  26. //If not found then try and get index from the model object
  27. if (controllerIndex >= uint.MaxValue)
  28. {
  29. controllerIndex = VRTK_SDK_Bridge.GetControllerIndex(GetValidObjectFromHand(VRTK_SDK_Bridge.GetControllerModelHand(controllerObject)));
  30. }
  31. VRTK_ControllerReference foundReference = VRTK_SharedMethods.GetDictionaryValue(controllerReferences, controllerIndex);
  32. if (foundReference != null)
  33. {
  34. return foundReference;
  35. }
  36. return new VRTK_ControllerReference(controllerIndex);
  37. }
  38. public static VRTK_ControllerReference GetControllerReference(SDK_BaseController.ControllerHand controllerHand)
  39. {
  40. GameObject scriptAlias = GetValidObjectFromHand(controllerHand);
  41. uint controllerIndex = VRTK_SDK_Bridge.GetControllerIndex(scriptAlias);
  42. VRTK_ControllerReference foundReference = VRTK_SharedMethods.GetDictionaryValue(controllerReferences, controllerIndex);
  43. if (foundReference != null)
  44. {
  45. return foundReference;
  46. }
  47. return new VRTK_ControllerReference(scriptAlias);
  48. }
  49. public static bool IsValid(VRTK_ControllerReference controllerReference)
  50. {
  51. return (controllerReference != null ? controllerReference.IsValid() : false);
  52. }
  53. public static uint GetRealIndex(VRTK_ControllerReference controllerReference)
  54. {
  55. return (IsValid(controllerReference) ? controllerReference.index : uint.MaxValue);
  56. }
  57. protected uint storedControllerIndex = uint.MaxValue;
  58. public VRTK_ControllerReference(uint controllerIndex)
  59. {
  60. //Only set up if the given index matches an actual controller
  61. if (VRTK_SDK_Bridge.GetControllerByIndex(controllerIndex, true) != null)
  62. {
  63. storedControllerIndex = controllerIndex;
  64. AddToCache();
  65. }
  66. }
  67. public VRTK_ControllerReference(GameObject controllerObject) : this(GetControllerHand(controllerObject))
  68. {
  69. }
  70. public VRTK_ControllerReference(SDK_BaseController.ControllerHand controllerHand)
  71. {
  72. storedControllerIndex = VRTK_SDK_Bridge.GetControllerIndex(GetValidObjectFromHand(controllerHand));
  73. AddToCache();
  74. }
  75. public uint index
  76. {
  77. get
  78. {
  79. return storedControllerIndex;
  80. }
  81. }
  82. public GameObject scriptAlias
  83. {
  84. get
  85. {
  86. return VRTK_SDK_Bridge.GetControllerByIndex(storedControllerIndex, false);
  87. }
  88. }
  89. public GameObject actual
  90. {
  91. get
  92. {
  93. return VRTK_SDK_Bridge.GetControllerByIndex(storedControllerIndex, true);
  94. }
  95. }
  96. public GameObject model
  97. {
  98. get
  99. {
  100. return VRTK_SDK_Bridge.GetControllerModel(GetValidObjectFromIndex());
  101. }
  102. }
  103. public SDK_BaseController.ControllerHand hand
  104. {
  105. get
  106. {
  107. return GetControllerHand(GetValidObjectFromIndex());
  108. }
  109. }
  110. public bool IsValid()
  111. {
  112. return (index < uint.MaxValue);
  113. }
  114. public override string ToString()
  115. {
  116. return base.ToString() + " --> INDEX[" + index + "] - ACTUAL[" + actual + "] - SCRIPT_ALIAS[" + scriptAlias + "] - MODEL[" + model + "] - HAND[" + hand + "]";
  117. }
  118. public override int GetHashCode()
  119. {
  120. return (int)index;
  121. }
  122. public override bool Equals(object obj)
  123. {
  124. if (obj == null)
  125. {
  126. return false;
  127. }
  128. VRTK_ControllerReference objAsPart = obj as VRTK_ControllerReference;
  129. if (objAsPart == null)
  130. {
  131. return false;
  132. }
  133. else
  134. {
  135. return Equals(objAsPart);
  136. }
  137. }
  138. public bool Equals(VRTK_ControllerReference other)
  139. {
  140. if (other == null)
  141. {
  142. return false;
  143. }
  144. return (index == other.index);
  145. }
  146. protected virtual GameObject GetValidObjectFromIndex()
  147. {
  148. GameObject checkObject = VRTK_SDK_Bridge.GetControllerByIndex(storedControllerIndex, false);
  149. return (checkObject == null ? VRTK_SDK_Bridge.GetControllerByIndex(storedControllerIndex, true) : checkObject);
  150. }
  151. protected virtual void AddToCache()
  152. {
  153. if (IsValid())
  154. {
  155. VRTK_SharedMethods.AddDictionaryValue(controllerReferences, storedControllerIndex, this, true);
  156. }
  157. }
  158. private static GameObject GetValidObjectFromHand(SDK_BaseController.ControllerHand controllerHand)
  159. {
  160. switch (controllerHand)
  161. {
  162. case SDK_BaseController.ControllerHand.Left:
  163. return (VRTK_SDK_Bridge.GetControllerLeftHand(false) ? VRTK_SDK_Bridge.GetControllerLeftHand(false) : VRTK_SDK_Bridge.GetControllerLeftHand(true));
  164. case SDK_BaseController.ControllerHand.Right:
  165. return (VRTK_SDK_Bridge.GetControllerRightHand(false) ? VRTK_SDK_Bridge.GetControllerRightHand(false) : VRTK_SDK_Bridge.GetControllerRightHand(true));
  166. }
  167. return null;
  168. }
  169. private static SDK_BaseController.ControllerHand GetControllerHand(GameObject controllerObject)
  170. {
  171. if (VRTK_SDK_Bridge.IsControllerLeftHand(controllerObject, false) || VRTK_SDK_Bridge.IsControllerLeftHand(controllerObject, true))
  172. {
  173. return SDK_BaseController.ControllerHand.Left;
  174. }
  175. else if (VRTK_SDK_Bridge.IsControllerRightHand(controllerObject, false) || VRTK_SDK_Bridge.IsControllerRightHand(controllerObject, true))
  176. {
  177. return SDK_BaseController.ControllerHand.Right;
  178. }
  179. return VRTK_SDK_Bridge.GetControllerModelHand(controllerObject);
  180. }
  181. }
  182. }