1
- using System ;
1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE in the project root for license information.
3
+
4
+ using System ;
2
5
using System . Collections ;
3
6
using System . Collections . Generic ;
4
7
using UnityEngine ;
5
8
6
-
7
- /// <summary>
8
- /// //Intended Usage//
9
- /// Attach a "grabbable_x" script (a script that inherits from this) to any object that is meant to be grabbed
10
- /// create more specific grab behavior by adding additional scripts/components to the game object, such as scalableObject, rotatableObject, throwableObject
11
- /// </summary>
12
-
13
- namespace MRTK . Grabbables
9
+ namespace HoloToolkit . Unity . InputModule . Examples . Grabbables
14
10
{
15
- public enum GrabStateEnum
16
- {
17
- Inactive ,
18
- Single ,
19
- Multi ,
20
- }
21
-
22
- public enum GrabStyleEnum
23
- {
24
- Exclusive ,
25
- Multi
26
- }
27
-
11
+ /// <summary>
12
+ /// //Intended Usage//
13
+ /// Attach a "grabbable_x" script (a script that inherits from this) to any object that is meant to be grabbed
14
+ /// create more specific grab behavior by adding additional scripts/components to the game object, such as scalableObject, rotatableObject, throwableObject
15
+ /// </summary>
28
16
public abstract class BaseGrabbable : MonoBehaviour
29
17
{
30
- public Action < BaseGrabbable > OnGrabStateChange ;
31
- public Action < BaseGrabbable > OnContactStateChange ;
32
- public Action < BaseGrabbable > OnGrabbed ;
33
- public Action < BaseGrabbable > OnReleased ;
18
+ public event Action < BaseGrabbable > OnGrabStateChange ;
19
+ public event Action < BaseGrabbable > OnContactStateChange ;
20
+ public event Action < BaseGrabbable > OnGrabbed ;
21
+ public event Action < BaseGrabbable > OnReleased ;
34
22
35
23
public BaseGrabber GrabberPrimary
36
24
{
@@ -40,7 +28,6 @@ public BaseGrabber GrabberPrimary
40
28
}
41
29
}
42
30
43
-
44
31
public BaseGrabber [ ] ActiveGrabbers
45
32
{
46
33
get
@@ -65,11 +52,11 @@ public GrabStateEnum GrabState
65
52
get
66
53
{
67
54
if ( activeGrabbers . Count > 1 )
55
+ {
68
56
return GrabStateEnum . Multi ;
69
- else if ( activeGrabbers . Count > 0 )
70
- return GrabStateEnum . Single ;
71
- else
72
- return GrabStateEnum . Inactive ;
57
+ }
58
+
59
+ return activeGrabbers . Count > 0 ? GrabStateEnum . Single : GrabStateEnum . Inactive ;
73
60
}
74
61
}
75
62
@@ -89,7 +76,32 @@ public GrabStateEnum ContactState
89
76
}
90
77
}
91
78
92
- public virtual bool TryGrabWith ( BaseGrabber grabber )
79
+ /// <summary>
80
+ /// Grabbers that could potentially grab this object
81
+ /// This list is maintained by the grabbers
82
+ /// </summary>
83
+ protected HashSet < BaseGrabber > availableGrabbers = new HashSet < BaseGrabber > ( ) ;
84
+
85
+ /// <summary>
86
+ /// Grabbers that are currently grabbing this object
87
+ /// The top-most grabber is the primary grabber
88
+ /// </summary>
89
+ protected List < BaseGrabber > activeGrabbers = new List < BaseGrabber > ( ) ;
90
+
91
+ //left protected unless we have the occasion to use them publicly, then switch to public access
92
+ [ SerializeField ]
93
+ protected Transform grabSpot ;
94
+
95
+ [ SerializeField ]
96
+ protected GrabStyleEnum grabStyle = GrabStyleEnum . Exclusive ;
97
+
98
+ private GrabStateEnum prevGrabState = GrabStateEnum . Inactive ;
99
+ private GrabStateEnum prevContactState = GrabStateEnum . Inactive ;
100
+ private Vector3 velocity ;
101
+ private Vector3 averageVelocity ;
102
+ private Vector3 previousVel ;
103
+
104
+ public virtual bool TryGrabWith ( BaseGrabber grabber )
93
105
{
94
106
// TODO error checking, mult-grab checking
95
107
if ( GrabState != GrabStateEnum . Inactive )
@@ -104,14 +116,17 @@ public virtual bool TryGrabWith (BaseGrabber grabber)
104
116
// Remove from grabbable list and detatch
105
117
activeGrabbers . Remove ( primary ) ;
106
118
DetachFromGrabber ( primary ) ;
107
- } else {
119
+ }
120
+ else
121
+ {
108
122
// If we can't, it's a no-go
109
123
return false ;
110
124
}
111
125
break ;
112
-
113
- default :
126
+ case GrabStyleEnum . Multi :
114
127
break ;
128
+ default :
129
+ throw new ArgumentOutOfRangeException ( ) ;
115
130
}
116
131
}
117
132
@@ -136,6 +151,7 @@ public void RemoveContact(BaseGrabber availableObject)
136
151
}
137
152
138
153
//the next three functions provide basic behaviour. Extend from this base script in order to provide more specific functionality.
154
+
139
155
protected virtual void AttachToGrabber ( BaseGrabber grabber )
140
156
{
141
157
// By default this does nothing
@@ -164,18 +180,19 @@ protected virtual void StartGrab(BaseGrabber grabber)
164
180
// Otherwise just push the grabber
165
181
activeGrabbers . Add ( grabber ) ;
166
182
}
183
+
167
184
// Attach ourselves to this grabber
168
185
AttachToGrabber ( grabber ) ;
169
186
if ( OnGrabbed != null )
187
+ {
170
188
OnGrabbed ( this ) ;
189
+ }
171
190
}
172
191
173
192
/// <summary>
174
193
/// As long as the grabber script (usually attached to the controller, but not always) reports more than one grabber,
175
194
/// we stay inside of StayGrab.
176
195
/// </summary>
177
- /// <param name="grabber"></param>
178
- /// <returns></returns>
179
196
protected virtual IEnumerator StayGrab ( )
180
197
{
181
198
yield return null ;
@@ -191,19 +208,22 @@ protected virtual IEnumerator StayGrab()
191
208
{
192
209
Debug . Log ( "no longer being grabbed by active grabber" ) ;
193
210
if ( activeGrabbers [ i ] != null )
211
+ {
194
212
DetachFromGrabber ( activeGrabbers [ i ] ) ;
213
+ }
214
+
195
215
activeGrabbers . RemoveAt ( i ) ;
196
216
}
197
217
}
198
218
yield return null ;
199
219
}
220
+
200
221
EndGrab ( ) ;
201
222
}
202
223
/// <summary>
203
224
/// Grab end fires off a GrabEnded event, but also cleans up some of the variables associated with an active grab, such
204
225
/// as which grabber was grabbing this object and so forth.
205
226
/// </summary>
206
- /// <param name="grabber"></param>
207
227
protected virtual void EndGrab ( )
208
228
{
209
229
if ( OnReleased != null )
@@ -217,17 +237,16 @@ protected virtual void EndGrab()
217
237
/// </summary>
218
238
protected virtual void OnGrabStay ( )
219
239
{
220
-
221
240
}
222
241
223
242
protected virtual void Start ( )
224
243
{
225
-
226
244
}
227
245
228
246
protected virtual void Update ( )
229
247
{
230
- if ( prevGrabState != GrabState && OnGrabStateChange != null ) {
248
+ if ( prevGrabState != GrabState && OnGrabStateChange != null )
249
+ {
231
250
Debug . Log ( "Calling on grab change in grabbable" ) ;
232
251
OnGrabStateChange ( this ) ;
233
252
}
@@ -240,31 +259,6 @@ protected virtual void Update()
240
259
241
260
prevGrabState = GrabState ;
242
261
prevContactState = ContactState ;
243
-
244
262
}
245
-
246
-
247
- /// <summary>
248
- /// Grabbers that could potentially grab this object
249
- /// This list is maintained by the grabbers
250
- /// </summary>
251
- protected HashSet < BaseGrabber > availableGrabbers = new HashSet < BaseGrabber > ( ) ;
252
- /// <summary>
253
- /// Grabbers that are currently grabbing this object
254
- /// The top-most grabber is the primary grabber
255
- /// </summary>
256
- protected List < BaseGrabber > activeGrabbers = new List < BaseGrabber > ( ) ;
257
-
258
- //left protected unless we have the occasion to use them publicly, then switch to public access
259
- [ SerializeField ]
260
- protected Transform grabSpot ;
261
- [ SerializeField ]
262
- protected GrabStyleEnum grabStyle = GrabStyleEnum . Exclusive ;
263
-
264
- private GrabStateEnum prevGrabState = GrabStateEnum . Inactive ;
265
- private GrabStateEnum prevContactState = GrabStateEnum . Inactive ;
266
- private Vector3 velocity ;
267
- private Vector3 averageVelocity ;
268
- private Vector3 previousVel ;
269
263
}
270
264
}
0 commit comments