10
10
11
11
namespace Leap . Unity {
12
12
/**
13
- * HandPool holds a pool of IHandModels and makes HandRepresentations
13
+ * HandPool holds a pool of IHandModels and makes HandProxys
14
14
* when given a Leap Hand and a model type of graphics or physics.
15
- * When a HandRepresentation is created, an IHandModel is removed from the pool.
16
- * When a HandRepresentation is finished, its IHandModel is returned to the pool.
15
+ * When a HandProxy is created, an IHandModel is removed from the pool.
16
+ * When a HandProxy is finished, its IHandModel is returned to the pool.
17
17
*/
18
18
public class HandPool :
19
19
HandFactory {
20
20
[ SerializeField ]
21
21
private List < ModelGroup > ModelPool ;
22
- private List < HandRepresentation > activeHandReps = new List < HandRepresentation > ( ) ;
22
+ private List < HandProxy > activeHandReps = new List < HandProxy > ( ) ;
23
23
24
24
private Dictionary < IHandModel , ModelGroup > modelGroupMapping = new Dictionary < IHandModel , ModelGroup > ( ) ;
25
- private Dictionary < IHandModel , HandRepresentation > modelToHandRepMapping = new Dictionary < IHandModel , HandRepresentation > ( ) ;
25
+ private Dictionary < IHandModel , HandProxy > modelToHandRepMapping = new Dictionary < IHandModel , HandProxy > ( ) ;
26
26
27
27
/**
28
28
* ModelGroup contains a left/right pair of IHandModel's
29
- * @param modelList The IHandModels available for use by HandRepresentations
30
- * @param modelsCheckedOut The IHandModels currently in use by active HandRepresentations
29
+ * @param modelList The IHandModels available for use by HandProxys
30
+ * @param modelsCheckedOut The IHandModels currently in use by active HandProxys
31
31
* @param IsEnabled determines whether the ModelGroup is active at app Start(), though ModelGroup's are controlled with the EnableGroup() & DisableGroup methods.
32
32
* @param CanDuplicate Allows a IHandModels in the ModelGroup to be cloned at runtime if a suitable IHandModel isn't available.
33
33
*/
@@ -53,9 +53,7 @@ public class ModelGroup {
53
53
* If not, one can be cloned*/
54
54
public IHandModel TryGetModel ( Chirality chirality , ModelType modelType ) {
55
55
for ( int i = 0 ; i < modelList . Count ; i ++ ) {
56
- if ( modelList [ i ] . HandModelType == modelType
57
- && modelList [ i ] . Handedness == chirality
58
- || modelList [ i ] . Handedness == Chirality . Either ) {
56
+ if ( modelList [ i ] . HandModelType == modelType && modelList [ i ] . Handedness == chirality ) {
59
57
IHandModel model = modelList [ i ] ;
60
58
modelList . RemoveAt ( i ) ;
61
59
modelsCheckedOut . Add ( model ) ;
@@ -64,9 +62,7 @@ public IHandModel TryGetModel(Chirality chirality, ModelType modelType) {
64
62
}
65
63
if ( CanDuplicate ) {
66
64
for ( int i = 0 ; i < modelsCheckedOut . Count ; i ++ ) {
67
- if ( modelsCheckedOut [ i ] . HandModelType == modelType
68
- && modelsCheckedOut [ i ] . Handedness == chirality
69
- || modelsCheckedOut [ i ] . Handedness == Chirality . Either ) {
65
+ if ( modelsCheckedOut [ i ] . HandModelType == modelType && modelsCheckedOut [ i ] . Handedness == chirality ) {
70
66
IHandModel modelToSpawn = modelsCheckedOut [ i ] ;
71
67
IHandModel spawnedModel = GameObject . Instantiate ( modelToSpawn ) ;
72
68
spawnedModel . transform . parent = _handPool . transform ;
@@ -87,10 +83,34 @@ public void ReturnToGroup(IHandModel model) {
87
83
public void ReturnToPool ( IHandModel model ) {
88
84
ModelGroup modelGroup ;
89
85
bool groupFound = modelGroupMapping . TryGetValue ( model , out modelGroup ) ;
90
- modelGroup . ReturnToGroup ( model ) ;
91
86
Assert . IsTrue ( groupFound ) ;
87
+ //First see if there is another active representation that can use this model
88
+ for ( int i = 0 ; i < activeHandReps . Count ; i ++ ) {
89
+ HandProxy rep = activeHandReps [ i ] ;
90
+ if ( rep . RepChirality == model . Handedness && rep . RepType == model . HandModelType ) {
91
+ bool modelFromGroupFound = false ;
92
+ if ( rep . handModels != null ) {
93
+ //And that represention does not contain a model from this model's modelGroup
94
+ for ( int j = 0 ; j < modelGroup . modelsCheckedOut . Count ; j ++ ) {
95
+ IHandModel modelToCompare = modelGroup . modelsCheckedOut [ j ] ;
96
+ for ( int k = 0 ; k < rep . handModels . Count ; k ++ ) {
97
+ if ( rep . handModels [ k ] == modelToCompare ) {
98
+ modelFromGroupFound = true ;
99
+ }
100
+ }
101
+ }
102
+ }
103
+ if ( ! modelFromGroupFound ) {
104
+ rep . AddModel ( model ) ;
105
+ modelToHandRepMapping [ model ] = rep ;
106
+ return ;
107
+ }
108
+ }
109
+ }
110
+ //Otherwise return to pool
111
+ modelGroup . ReturnToGroup ( model ) ;
92
112
}
93
- public void RemoveHandRepresentation ( HandRepresentation handRep ) {
113
+ public void RemoveHandRepresentation ( HandProxy handRep ) {
94
114
activeHandReps . Remove ( handRep ) ;
95
115
}
96
116
/** Popuates the ModelPool with the contents of the ModelCollection */
@@ -104,8 +124,7 @@ void Start() {
104
124
GameObject spawnedGO = GameObject . Instantiate ( modelToSpawn . gameObject ) ;
105
125
leftModel = spawnedGO . GetComponent < IHandModel > ( ) ;
106
126
leftModel . transform . parent = transform ;
107
- }
108
- else {
127
+ } else {
109
128
leftModel = collectionGroup . LeftModel ;
110
129
}
111
130
collectionGroup . modelList . Add ( leftModel ) ;
@@ -116,8 +135,7 @@ void Start() {
116
135
GameObject spawnedGO = GameObject . Instantiate ( modelToSpawn . gameObject ) ;
117
136
rightModel = spawnedGO . GetComponent < IHandModel > ( ) ;
118
137
rightModel . transform . parent = transform ;
119
- }
120
- else {
138
+ } else {
121
139
rightModel = collectionGroup . RightModel ;
122
140
}
123
141
collectionGroup . modelList . Add ( rightModel ) ;
@@ -126,29 +144,31 @@ void Start() {
126
144
}
127
145
128
146
/**
129
- * MakeHandRepresentation receives a Hand and combines that with an IHandModel to create a HandRepresentation
147
+ * MakeHandRepresentation receives a Hand and combines that with an IHandModel to create a HandProxy
130
148
* @param hand The Leap Hand data to be drive an IHandModel
131
149
* @param modelType Filters for a type of hand model, for example, physics or graphics hands.
132
150
*/
133
151
134
152
public override HandRepresentation MakeHandRepresentation ( Hand hand , ModelType modelType ) {
135
153
Chirality handChirality = hand . IsRight ? Chirality . Right : Chirality . Left ;
136
- HandRepresentation handRep = new HandProxy ( this , hand , handChirality , modelType ) ;
154
+ HandProxy handRep = new HandProxy ( this , hand , handChirality , modelType ) ;
137
155
for ( int i = 0 ; i < ModelPool . Count ; i ++ ) {
138
156
ModelGroup group = ModelPool [ i ] ;
139
157
if ( group . IsEnabled ) {
140
158
IHandModel model = group . TryGetModel ( handChirality , modelType ) ;
141
- if ( model != null ) {
159
+ if ( model != null ) {
142
160
handRep . AddModel ( model ) ;
143
- modelToHandRepMapping . Add ( model , handRep ) ;
161
+ if ( ! modelToHandRepMapping . ContainsKey ( model ) ) {
162
+ modelToHandRepMapping . Add ( model , handRep ) ;
163
+ }
144
164
}
145
165
}
146
166
}
147
167
activeHandReps . Add ( handRep ) ;
148
168
return handRep ;
149
169
}
150
170
/**
151
- * EnableGroup finds suitable HandRepresentations and adds IHandModels from the ModelGroup, returns them to their ModelGroup and sets the groups IsEnabled to true.
171
+ * EnableGroup finds suitable HandProxys and adds IHandModels from the ModelGroup, returns them to their ModelGroup and sets the groups IsEnabled to true.
152
172
* @param groupName Takes a string that matches the ModelGroup's groupName serialized in the Inspector
153
173
*/
154
174
public void EnableGroup ( string groupName ) {
@@ -161,7 +181,7 @@ private IEnumerator enableGroup(string groupName) {
161
181
if ( ModelPool [ i ] . GroupName == groupName ) {
162
182
group = ModelPool [ i ] ;
163
183
for ( int hp = 0 ; hp < activeHandReps . Count ; hp ++ ) {
164
- HandRepresentation handRep = activeHandReps [ hp ] ;
184
+ HandProxy handRep = activeHandReps [ hp ] ;
165
185
IHandModel model = group . TryGetModel ( handRep . RepChirality , handRep . RepType ) ;
166
186
if ( model != null ) {
167
187
handRep . AddModel ( model ) ;
@@ -176,7 +196,7 @@ private IEnumerator enableGroup(string groupName) {
176
196
}
177
197
}
178
198
/**
179
- * DisableGroup finds and removes the ModelGroup's IHandModels from their HandRepresentations , returns them to their ModelGroup and sets the groups IsEnabled to false.
199
+ * DisableGroup finds and removes the ModelGroup's IHandModels from their HandProxys , returns them to their ModelGroup and sets the groups IsEnabled to false.
180
200
* @param groupName Takes a string that matches the ModelGroup's groupName serialized in the Inspector
181
201
*/
182
202
public void DisableGroup ( string groupName ) {
@@ -190,7 +210,7 @@ private IEnumerator disableGroup(string groupName) {
190
210
group = ModelPool [ i ] ;
191
211
for ( int m = 0 ; m < group . modelsCheckedOut . Count ; m ++ ) {
192
212
IHandModel model = group . modelsCheckedOut [ m ] ;
193
- HandRepresentation handRep ;
213
+ HandProxy handRep ;
194
214
if ( modelToHandRepMapping . TryGetValue ( model , out handRep ) ) {
195
215
handRep . RemoveModel ( model ) ;
196
216
group . ReturnToGroup ( model ) ;
@@ -215,13 +235,11 @@ private IEnumerator toggleGroup(string groupName) {
215
235
if ( modelGroup . IsEnabled == true ) {
216
236
DisableGroup ( groupName ) ;
217
237
modelGroup . IsEnabled = false ;
218
- }
219
- else {
238
+ } else {
220
239
EnableGroup ( groupName ) ;
221
240
modelGroup . IsEnabled = true ;
222
241
}
223
- }
224
- else Debug . LogWarning ( "A group matching that name does not exisit in the modelPool" ) ;
242
+ } else Debug . LogWarning ( "A group matching that name does not exisit in the modelPool" ) ;
225
243
}
226
244
public void AddNewGroup ( string groupName , IHandModel leftModel , IHandModel rightModel ) {
227
245
ModelGroup newGroup = new ModelGroup ( ) ;
@@ -247,15 +265,24 @@ void OnValidate() {
247
265
for ( int i = 0 ; i < ModelPool . Count ; i ++ ) {
248
266
if ( ModelPool [ i ] != null ) {
249
267
if ( ModelPool [ i ] . LeftModel ) {
250
- ModelPool [ i ] . IsLeftToBeSpawned = PrefabUtility . GetPrefabType ( ModelPool [ i ] . LeftModel ) == PrefabType . Prefab ;
268
+ ModelPool [ i ] . IsLeftToBeSpawned = shouldBeSpawned ( ModelPool [ i ] . LeftModel ) ;
251
269
}
252
270
if ( ModelPool [ i ] . RightModel ) {
253
- ModelPool [ i ] . IsRightToBeSpawned = PrefabUtility . GetPrefabType ( ModelPool [ i ] . RightModel ) == PrefabType . Prefab ;
271
+ ModelPool [ i ] . IsRightToBeSpawned = shouldBeSpawned ( ModelPool [ i ] . RightModel ) ;
254
272
}
255
273
}
256
274
}
257
275
}
258
276
277
+ private bool shouldBeSpawned ( Object model ) {
278
+ var prefabType = PrefabUtility . GetPrefabType ( model ) ;
279
+ if ( PrefabUtility . GetPrefabType ( this ) != PrefabType . Prefab ) {
280
+ return prefabType == PrefabType . Prefab ;
281
+ } else {
282
+ return PrefabUtility . GetPrefabObject ( model ) != PrefabUtility . GetPrefabObject ( this ) ;
283
+ }
284
+ }
285
+
259
286
#endif
260
287
}
261
288
}
0 commit comments