Skip to content

Commit 163ed39

Browse files
authored
Merge pull request #1 from StephenHodgson/MRTK-James-Local
Same change requests
2 parents 01404d1 + 5e2d9df commit 163ed39

32 files changed

+507
-395
lines changed

Assets/HoloToolkit-Examples/MotionControllers-GrabMechanics/Scripts/BaseGrabbable.cs

+60-66
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,24 @@
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;
25
using System.Collections;
36
using System.Collections.Generic;
47
using UnityEngine;
58

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
1410
{
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>
2816
public abstract class BaseGrabbable : MonoBehaviour
2917
{
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;
3422

3523
public BaseGrabber GrabberPrimary
3624
{
@@ -40,7 +28,6 @@ public BaseGrabber GrabberPrimary
4028
}
4129
}
4230

43-
4431
public BaseGrabber[] ActiveGrabbers
4532
{
4633
get
@@ -65,11 +52,11 @@ public GrabStateEnum GrabState
6552
get
6653
{
6754
if (activeGrabbers.Count > 1)
55+
{
6856
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;
7360
}
7461
}
7562

@@ -89,7 +76,32 @@ public GrabStateEnum ContactState
8976
}
9077
}
9178

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)
93105
{
94106
// TODO error checking, mult-grab checking
95107
if (GrabState != GrabStateEnum.Inactive)
@@ -104,14 +116,17 @@ public virtual bool TryGrabWith (BaseGrabber grabber)
104116
// Remove from grabbable list and detatch
105117
activeGrabbers.Remove(primary);
106118
DetachFromGrabber(primary);
107-
} else {
119+
}
120+
else
121+
{
108122
// If we can't, it's a no-go
109123
return false;
110124
}
111125
break;
112-
113-
default:
126+
case GrabStyleEnum.Multi:
114127
break;
128+
default:
129+
throw new ArgumentOutOfRangeException();
115130
}
116131
}
117132

@@ -136,6 +151,7 @@ public void RemoveContact(BaseGrabber availableObject)
136151
}
137152

138153
//the next three functions provide basic behaviour. Extend from this base script in order to provide more specific functionality.
154+
139155
protected virtual void AttachToGrabber(BaseGrabber grabber)
140156
{
141157
// By default this does nothing
@@ -164,18 +180,19 @@ protected virtual void StartGrab(BaseGrabber grabber)
164180
// Otherwise just push the grabber
165181
activeGrabbers.Add(grabber);
166182
}
183+
167184
// Attach ourselves to this grabber
168185
AttachToGrabber(grabber);
169186
if (OnGrabbed != null)
187+
{
170188
OnGrabbed(this);
189+
}
171190
}
172191

173192
/// <summary>
174193
/// As long as the grabber script (usually attached to the controller, but not always) reports more than one grabber,
175194
/// we stay inside of StayGrab.
176195
/// </summary>
177-
/// <param name="grabber"></param>
178-
/// <returns></returns>
179196
protected virtual IEnumerator StayGrab()
180197
{
181198
yield return null;
@@ -191,19 +208,22 @@ protected virtual IEnumerator StayGrab()
191208
{
192209
Debug.Log("no longer being grabbed by active grabber");
193210
if (activeGrabbers[i] != null)
211+
{
194212
DetachFromGrabber(activeGrabbers[i]);
213+
}
214+
195215
activeGrabbers.RemoveAt(i);
196216
}
197217
}
198218
yield return null;
199219
}
220+
200221
EndGrab();
201222
}
202223
/// <summary>
203224
/// Grab end fires off a GrabEnded event, but also cleans up some of the variables associated with an active grab, such
204225
/// as which grabber was grabbing this object and so forth.
205226
/// </summary>
206-
/// <param name="grabber"></param>
207227
protected virtual void EndGrab()
208228
{
209229
if (OnReleased != null)
@@ -217,17 +237,16 @@ protected virtual void EndGrab()
217237
/// </summary>
218238
protected virtual void OnGrabStay()
219239
{
220-
221240
}
222241

223242
protected virtual void Start()
224243
{
225-
226244
}
227245

228246
protected virtual void Update()
229247
{
230-
if (prevGrabState != GrabState && OnGrabStateChange != null) {
248+
if (prevGrabState != GrabState && OnGrabStateChange != null)
249+
{
231250
Debug.Log("Calling on grab change in grabbable");
232251
OnGrabStateChange(this);
233252
}
@@ -240,31 +259,6 @@ protected virtual void Update()
240259

241260
prevGrabState = GrabState;
242261
prevContactState = ContactState;
243-
244262
}
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;
269263
}
270264
}

0 commit comments

Comments
 (0)