11using System.Collections;
12using System.Collections.Generic;
33 #region Formerly in LeapHandController
35 protected Dictionary<int, HandRepresentation>
graphicsHandReps =
new Dictionary<int, HandRepresentation>();
36 protected Dictionary<int, HandRepresentation>
physicsHandReps =
new Dictionary<int, HandRepresentation>();
84 for (
int i = 0; i < frame.
Hands.Count; i++) {
85 var curHand = frame.
Hands[i];
87 if (!all_hand_reps.TryGetValue(curHand.Id, out rep)) {
89 all_hand_reps.Add(curHand.Id, rep);
100 for (var it = all_hand_reps.GetEnumerator(); it.MoveNext();) {
102 if (r.Value !=
null) {
103 if (r.Value.IsMarked) {
109 toBeDeleted = r.Value;
115 if (toBeDeleted !=
null) {
116 all_hand_reps.Remove(toBeDeleted.
HandID);
123 #region HandPool Inspector
125 [Tooltip(
"The LeapProvider to use to drive hand representations in the defined "
126 +
"model pool groups.")]
128 [OnEditorChange(
"leapProvider")]
131 get {
return _leapProvider; }
133 if (_leapProvider !=
null) {
138 _leapProvider = value;
140 if (_leapProvider !=
null) {
148 [Tooltip(
"To add a new set of Hand Models, first add the Left and Right objects as "
149 +
"children of this object. Then create a new Model Pool entry referencing "
150 +
"the new Hand Models and configure it as desired. "
151 +
"Once configured, the Hand Model Manager object pipes Leap tracking data "
152 +
"to the Hand Models as hands are tracked, and spawns duplicates as needed "
153 +
"if \"Can Duplicate\" is enabled.")]
154 private List<ModelGroup> ModelPool =
new List<ModelGroup>();
155 private List<HandRepresentation> activeHandReps =
new List<HandRepresentation>();
157 private Dictionary<HandModelBase, ModelGroup> modelGroupMapping =
new Dictionary<HandModelBase, ModelGroup>();
158 private Dictionary<HandModelBase, HandRepresentation> modelToHandRepMapping =
new Dictionary<HandModelBase, HandRepresentation>();
162 #region ModelGroup Class
171 [System.Serializable]
184 public List<HandModelBase>
modelList =
new List<HandModelBase>();
193 for (
int i = 0; i <
modelList.Count; i++) {
194 if (
modelList[i].HandModelType == modelType &&
modelList[i].Handedness == chirality) {
205 HandModelBase spawnedModel = GameObject.Instantiate(modelToSpawn);
218 this._handModelManager.modelToHandRepMapping.Remove(model);
224 #region HandPool Methods
228 bool groupFound = modelGroupMapping.TryGetValue(model, out modelGroup);
229 Assert.IsTrue(groupFound);
231 for (
int i = 0; i < activeHandReps.Count; i++) {
234 bool modelFromGroupFound =
false;
239 for (
int k = 0; k < rep.
handModels.Count; k++) {
241 modelFromGroupFound =
true;
246 if (!modelFromGroupFound) {
248 modelToHandRepMapping[model] = rep;
270 #region Hand Representations
280 for (
int i = 0; i < ModelPool.Count; i++) {
286 if (!modelToHandRepMapping.ContainsKey(model)) {
288 modelToHandRepMapping.Add(model, handRep);
293 activeHandReps.Add(handRep);
298 activeHandReps.Remove(handRepresentation);
306 if (_leapProvider ==
null) {
307 _leapProvider = Hands.Provider;
324 for(
int i=0; i<ModelPool.Count; i++) {
325 InitializeModelGroup(ModelPool[i]);
331 #region Group Methods
333 private void InitializeModelGroup(ModelGroup collectionGroup) {
335 if (modelGroupMapping.ContainsValue(collectionGroup)) {
339 collectionGroup._handModelManager =
this;
340 HandModelBase leftModel;
341 HandModelBase rightModel;
342 if (collectionGroup.IsLeftToBeSpawned) {
343 HandModelBase modelToSpawn = collectionGroup.LeftModel;
344 GameObject spawnedGO = Instantiate(modelToSpawn.gameObject);
345 leftModel = spawnedGO.GetComponent<HandModelBase>();
346 leftModel.transform.parent = this.transform;
348 leftModel = collectionGroup.LeftModel;
350 if (leftModel !=
null) {
351 collectionGroup.modelList.Add(leftModel);
352 modelGroupMapping.Add(leftModel, collectionGroup);
355 if (collectionGroup.IsRightToBeSpawned) {
356 HandModelBase modelToSpawn = collectionGroup.RightModel;
357 GameObject spawnedGO = Instantiate(modelToSpawn.gameObject);
358 rightModel = spawnedGO.GetComponent<HandModelBase>();
359 rightModel.transform.parent = this.transform;
361 rightModel = collectionGroup.RightModel;
363 if (rightModel !=
null) {
364 collectionGroup.modelList.Add(rightModel);
365 modelGroupMapping.Add(rightModel, collectionGroup);
374 StartCoroutine(enableGroup(groupName));
376 private IEnumerator enableGroup(
string groupName) {
377 yield
return new WaitForEndOfFrame();
378 ModelGroup group =
null;
379 for (
int i = 0; i < ModelPool.Count; i++) {
380 if (ModelPool[i].GroupName == groupName) {
381 group = ModelPool[i];
382 for (
int hp = 0; hp < activeHandReps.Count; hp++) {
387 modelToHandRepMapping.Add(model, handRep);
390 group.IsEnabled =
true;
394 Debug.LogWarning(
"A group matching that name does not exisit in the modelPool");
403 StartCoroutine(disableGroup(groupName));
405 private IEnumerator disableGroup(
string groupName) {
406 yield
return new WaitForEndOfFrame();
407 ModelGroup group =
null;
408 for (
int i = 0; i < ModelPool.Count; i++) {
409 if (ModelPool[i].GroupName == groupName) {
410 group = ModelPool[i];
411 for (
int m = 0; m < group.modelsCheckedOut.Count; m++) {
414 if (modelToHandRepMapping.TryGetValue(model, out handRep)) {
416 group.ReturnToGroup(model);
420 Assert.AreEqual(0, group.modelsCheckedOut.Count, group.GroupName +
"'s modelsCheckedOut List has not been cleared");
421 group.IsEnabled =
false;
425 Debug.LogWarning(
"A group matching that name does not exisit in the modelPool");
430 StartCoroutine(toggleGroup(groupName));
432 private IEnumerator toggleGroup(
string groupName) {
433 yield
return new WaitForEndOfFrame();
434 ModelGroup modelGroup = ModelPool.Find(i => i.GroupName == groupName);
435 if (modelGroup !=
null) {
436 if (modelGroup.IsEnabled ==
true) {
438 modelGroup.IsEnabled =
false;
441 modelGroup.IsEnabled =
true;
443 }
else Debug.LogWarning(
"A group matching that name does not exisit in the modelPool");
453 ModelPool.Add(newGroup);
454 InitializeModelGroup(newGroup);
458 while (ModelPool.Find(i => i.GroupName == groupName) !=
null) {
459 ModelGroup modelGroup = ModelPool.Find(i => i.GroupName == groupName);
460 if (modelGroup !=
null) {
461 ModelPool.Remove(modelGroup);
469 if (handModel.
GetLeapHand().
Id == handId && handModel is T) {
470 return handModel as T;
479 #region Editor-only Methods
484 if (ModelPool !=
null) {
485 for (
int i = 0; i < ModelPool.Count; i++) {
486 if (ModelPool[i] !=
null) {
487 if (ModelPool[i].LeftModel) {
488 ModelPool[i].IsLeftToBeSpawned = Utils.IsObjectPartOfPrefabAsset(
489 ModelPool[i].LeftModel);
491 if (ModelPool[i].RightModel) {
492 ModelPool[i].IsRightToBeSpawned = Utils.IsObjectPartOfPrefabAsset(
493 ModelPool[i].RightModel);
The Frame class represents a set of hand and finger tracking data detected in a single frame.
long Timestamp
The frame capture time in microseconds elapsed since an arbitrary point in time in the past.
List< Hand > Hands
The list of Hand objects detected in this frame, given in arbitrary order. The list can be empty if n...
The Hand class reports the physical characteristics of a detected hand.
int Id
A unique ID assigned to this Hand object, whose value remains the same across consecutive frames whil...
bool IsRight
Identifies whether this Hand is a right hand.
abstract ModelType HandModelType
HandModelManager.ModelGroup group
abstract Chirality Handedness
abstract Hand GetLeapHand()
List< HandModelBase > modelsCheckedOut
List< HandModelBase > modelList
HandModelBase TryGetModel(Chirality chirality, ModelType modelType)
HandModelManager _handModelManager
void ReturnToGroup(HandModelBase model)
The HandModelManager manages a pool of HandModelBases and makes HandRepresentations when a it detects...
virtual void OnFixedFrame(Frame frame)
void RemoveHandRepresentation(HandRepresentation handRepresentation)
void DisableGroup(string groupName)
void ToggleGroup(string groupName)
void ReturnToPool(HandModelBase model)
T GetHandModel< T >(int handId)
Dictionary< int, HandRepresentation > graphicsHandReps
void EnableGroup(string groupName)
virtual void UpdateHandRepresentations(Dictionary< int, HandRepresentation > all_hand_reps, ModelType modelType, Frame frame)
HandRepresentation MakeHandRepresentation(Hand hand, ModelType modelType)
void RemoveGroup(string groupName)
Dictionary< int, HandRepresentation > physicsHandReps
void AddNewGroup(string groupName, HandModelBase leftModel, HandModelBase rightModel)
virtual void OnUpdateFrame(Frame frame)
LeapProvider leapProvider
void RemoveModel(HandModelBase model)
List< HandModelBase > handModels
void AddModel(HandModelBase model)
void UpdateRepresentation(Hand hand)
Provides Frame object data to the Unity application by firing events as soon as Frame data is availab...
Action< Frame > OnFixedFrame
Action< Frame > OnUpdateFrame