HandPool.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace Leap.Unity
  6. {
  7. public class HandPool : HandFactory
  8. {
  9. public void ReturnToPool(IHandModel model)
  10. {
  11. HandPool.ModelGroup modelGroup;
  12. bool flag = this.modelGroupMapping.TryGetValue(model, out modelGroup);
  13. for (int i = 0; i < this.activeHandReps.Count; i++)
  14. {
  15. HandProxy handProxy = this.activeHandReps[i];
  16. if (handProxy.RepChirality == model.Handedness && handProxy.RepType == model.HandModelType)
  17. {
  18. bool flag2 = false;
  19. if (handProxy.handModels != null)
  20. {
  21. for (int j = 0; j < modelGroup.modelsCheckedOut.Count; j++)
  22. {
  23. IHandModel y = modelGroup.modelsCheckedOut[j];
  24. for (int k = 0; k < handProxy.handModels.Count; k++)
  25. {
  26. if (handProxy.handModels[k] == y)
  27. {
  28. flag2 = true;
  29. }
  30. }
  31. }
  32. }
  33. if (!flag2)
  34. {
  35. handProxy.AddModel(model);
  36. this.modelToHandRepMapping[model] = handProxy;
  37. return;
  38. }
  39. }
  40. }
  41. modelGroup.ReturnToGroup(model);
  42. }
  43. public void RemoveHandRepresentation(HandProxy handRep)
  44. {
  45. this.activeHandReps.Remove(handRep);
  46. }
  47. private void Start()
  48. {
  49. if (this.ModelsParent == null)
  50. {
  51. Debug.LogWarning("HandPool.ModelsParent needs to reference the parent transform of the hand models. This transform should be a child of the LMHeadMountedRig transform.");
  52. }
  53. foreach (HandPool.ModelGroup modelGroup in this.ModelPool)
  54. {
  55. modelGroup._handPool = this;
  56. IHandModel handModel;
  57. if (modelGroup.IsLeftToBeSpawned)
  58. {
  59. IHandModel leftModel = modelGroup.LeftModel;
  60. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(leftModel.gameObject);
  61. handModel = gameObject.GetComponent<IHandModel>();
  62. handModel.transform.parent = this.ModelsParent;
  63. }
  64. else
  65. {
  66. handModel = modelGroup.LeftModel;
  67. }
  68. if (handModel != null)
  69. {
  70. modelGroup.modelList.Add(handModel);
  71. this.modelGroupMapping.Add(handModel, modelGroup);
  72. }
  73. IHandModel handModel2;
  74. if (modelGroup.IsRightToBeSpawned)
  75. {
  76. IHandModel rightModel = modelGroup.RightModel;
  77. GameObject gameObject2 = UnityEngine.Object.Instantiate<GameObject>(rightModel.gameObject);
  78. handModel2 = gameObject2.GetComponent<IHandModel>();
  79. handModel2.transform.parent = this.ModelsParent;
  80. }
  81. else
  82. {
  83. handModel2 = modelGroup.RightModel;
  84. }
  85. if (handModel2 != null)
  86. {
  87. modelGroup.modelList.Add(handModel2);
  88. this.modelGroupMapping.Add(handModel2, modelGroup);
  89. }
  90. }
  91. }
  92. public override HandRepresentation MakeHandRepresentation(Hand hand, ModelType modelType)
  93. {
  94. Chirality chirality = (!hand.IsRight) ? Chirality.Left : Chirality.Right;
  95. HandProxy handProxy = new HandProxy(this, hand, chirality, modelType);
  96. for (int i = 0; i < this.ModelPool.Count; i++)
  97. {
  98. HandPool.ModelGroup modelGroup = this.ModelPool[i];
  99. if (modelGroup.IsEnabled)
  100. {
  101. IHandModel handModel = modelGroup.TryGetModel(chirality, modelType);
  102. if (handModel != null)
  103. {
  104. handProxy.AddModel(handModel);
  105. if (!this.modelToHandRepMapping.ContainsKey(handModel))
  106. {
  107. this.modelToHandRepMapping.Add(handModel, handProxy);
  108. }
  109. }
  110. }
  111. }
  112. this.activeHandReps.Add(handProxy);
  113. return handProxy;
  114. }
  115. public void EnableGroup(string groupName)
  116. {
  117. base.StartCoroutine(this.enableGroup(groupName));
  118. }
  119. private IEnumerator enableGroup(string groupName)
  120. {
  121. yield return new WaitForEndOfFrame();
  122. HandPool.ModelGroup group = null;
  123. for (int i = 0; i < this.ModelPool.Count; i++)
  124. {
  125. if (this.ModelPool[i].GroupName == groupName)
  126. {
  127. group = this.ModelPool[i];
  128. for (int j = 0; j < this.activeHandReps.Count; j++)
  129. {
  130. HandProxy handProxy = this.activeHandReps[j];
  131. IHandModel handModel = group.TryGetModel(handProxy.RepChirality, handProxy.RepType);
  132. if (handModel != null)
  133. {
  134. handProxy.AddModel(handModel);
  135. this.modelToHandRepMapping.Add(handModel, handProxy);
  136. }
  137. }
  138. group.IsEnabled = true;
  139. }
  140. }
  141. if (group == null)
  142. {
  143. Debug.LogWarning("A group matching that name does not exisit in the modelPool");
  144. }
  145. yield break;
  146. }
  147. public void DisableGroup(string groupName)
  148. {
  149. base.StartCoroutine(this.disableGroup(groupName));
  150. }
  151. private IEnumerator disableGroup(string groupName)
  152. {
  153. yield return new WaitForEndOfFrame();
  154. HandPool.ModelGroup group = null;
  155. for (int i = 0; i < this.ModelPool.Count; i++)
  156. {
  157. if (this.ModelPool[i].GroupName == groupName)
  158. {
  159. group = this.ModelPool[i];
  160. for (int j = 0; j < group.modelsCheckedOut.Count; j++)
  161. {
  162. IHandModel handModel = group.modelsCheckedOut[j];
  163. HandProxy handProxy;
  164. if (this.modelToHandRepMapping.TryGetValue(handModel, out handProxy))
  165. {
  166. handProxy.RemoveModel(handModel);
  167. group.ReturnToGroup(handModel);
  168. j--;
  169. }
  170. }
  171. group.IsEnabled = false;
  172. }
  173. }
  174. if (group == null)
  175. {
  176. Debug.LogWarning("A group matching that name does not exisit in the modelPool");
  177. }
  178. yield break;
  179. }
  180. public void ToggleGroup(string groupName)
  181. {
  182. base.StartCoroutine(this.toggleGroup(groupName));
  183. }
  184. private IEnumerator toggleGroup(string groupName)
  185. {
  186. yield return new WaitForEndOfFrame();
  187. HandPool.ModelGroup modelGroup = this.ModelPool.Find((HandPool.ModelGroup i) => i.GroupName == groupName);
  188. if (modelGroup != null)
  189. {
  190. if (modelGroup.IsEnabled)
  191. {
  192. this.DisableGroup(groupName);
  193. modelGroup.IsEnabled = false;
  194. }
  195. else
  196. {
  197. this.EnableGroup(groupName);
  198. modelGroup.IsEnabled = true;
  199. }
  200. }
  201. else
  202. {
  203. Debug.LogWarning("A group matching that name does not exisit in the modelPool");
  204. }
  205. yield break;
  206. }
  207. public void AddNewGroup(string groupName, IHandModel leftModel, IHandModel rightModel)
  208. {
  209. HandPool.ModelGroup modelGroup = new HandPool.ModelGroup();
  210. modelGroup.LeftModel = leftModel;
  211. modelGroup.RightModel = rightModel;
  212. modelGroup.GroupName = groupName;
  213. modelGroup.CanDuplicate = false;
  214. modelGroup.IsEnabled = true;
  215. this.ModelPool.Add(modelGroup);
  216. }
  217. public void RemoveGroup(string groupName)
  218. {
  219. while (this.ModelPool.Find((HandPool.ModelGroup i) => i.GroupName == groupName) != null)
  220. {
  221. HandPool.ModelGroup modelGroup = this.ModelPool.Find((HandPool.ModelGroup i) => i.GroupName == groupName);
  222. if (modelGroup != null)
  223. {
  224. this.ModelPool.Remove(modelGroup);
  225. }
  226. }
  227. }
  228. [SerializeField]
  229. [Tooltip("Reference for the transform that is a child of the camera rig's root and is a parent to all hand models")]
  230. private Transform ModelsParent;
  231. [SerializeField]
  232. private List<HandPool.ModelGroup> ModelPool;
  233. private List<HandProxy> activeHandReps = new List<HandProxy>();
  234. private Dictionary<IHandModel, HandPool.ModelGroup> modelGroupMapping = new Dictionary<IHandModel, HandPool.ModelGroup>();
  235. private Dictionary<IHandModel, HandProxy> modelToHandRepMapping = new Dictionary<IHandModel, HandProxy>();
  236. [Serializable]
  237. public class ModelGroup
  238. {
  239. public IHandModel TryGetModel(Chirality chirality, ModelType modelType)
  240. {
  241. for (int i = 0; i < this.modelList.Count; i++)
  242. {
  243. if (this.modelList[i].HandModelType == modelType && this.modelList[i].Handedness == chirality)
  244. {
  245. IHandModel handModel = this.modelList[i];
  246. this.modelList.RemoveAt(i);
  247. this.modelsCheckedOut.Add(handModel);
  248. return handModel;
  249. }
  250. }
  251. if (this.CanDuplicate)
  252. {
  253. for (int j = 0; j < this.modelsCheckedOut.Count; j++)
  254. {
  255. if (this.modelsCheckedOut[j].HandModelType == modelType && this.modelsCheckedOut[j].Handedness == chirality)
  256. {
  257. IHandModel original = this.modelsCheckedOut[j];
  258. IHandModel handModel2 = UnityEngine.Object.Instantiate<IHandModel>(original);
  259. handModel2.transform.parent = this._handPool.ModelsParent;
  260. this._handPool.modelGroupMapping.Add(handModel2, this);
  261. this.modelsCheckedOut.Add(handModel2);
  262. return handModel2;
  263. }
  264. }
  265. }
  266. return null;
  267. }
  268. public void ReturnToGroup(IHandModel model)
  269. {
  270. this.modelsCheckedOut.Remove(model);
  271. this.modelList.Add(model);
  272. this._handPool.modelToHandRepMapping.Remove(model);
  273. }
  274. public string GroupName;
  275. [HideInInspector]
  276. public HandPool _handPool;
  277. public IHandModel LeftModel;
  278. [HideInInspector]
  279. public bool IsLeftToBeSpawned;
  280. public IHandModel RightModel;
  281. [HideInInspector]
  282. public bool IsRightToBeSpawned;
  283. [HideInInspector]
  284. public List<IHandModel> modelList;
  285. [HideInInspector]
  286. public List<IHandModel> modelsCheckedOut;
  287. public bool IsEnabled = true;
  288. public bool CanDuplicate;
  289. }
  290. }
  291. }