Skip to content

Commit

Permalink
Merge pull request #1 from StephenHodgson/MRTK-James-Local
Browse files Browse the repository at this point in the history
Same change requests
  • Loading branch information
taylorjames9 authored Oct 17, 2017
2 parents 01404d1 + 5e2d9df commit 163ed39
Show file tree
Hide file tree
Showing 32 changed files with 507 additions and 395 deletions.
Original file line number Diff line number Diff line change
@@ -1,36 +1,24 @@
using System;
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


/// <summary>
/// //Intended Usage//
/// Attach a "grabbable_x" script (a script that inherits from this) to any object that is meant to be grabbed
/// create more specific grab behavior by adding additional scripts/components to the game object, such as scalableObject, rotatableObject, throwableObject
/// </summary>

namespace MRTK.Grabbables
namespace HoloToolkit.Unity.InputModule.Examples.Grabbables
{
public enum GrabStateEnum
{
Inactive,
Single,
Multi,
}

public enum GrabStyleEnum
{
Exclusive,
Multi
}

/// <summary>
/// //Intended Usage//
/// Attach a "grabbable_x" script (a script that inherits from this) to any object that is meant to be grabbed
/// create more specific grab behavior by adding additional scripts/components to the game object, such as scalableObject, rotatableObject, throwableObject
/// </summary>
public abstract class BaseGrabbable : MonoBehaviour
{
public Action<BaseGrabbable> OnGrabStateChange;
public Action<BaseGrabbable> OnContactStateChange;
public Action<BaseGrabbable> OnGrabbed;
public Action<BaseGrabbable> OnReleased;
public event Action<BaseGrabbable> OnGrabStateChange;
public event Action<BaseGrabbable> OnContactStateChange;
public event Action<BaseGrabbable> OnGrabbed;
public event Action<BaseGrabbable> OnReleased;

public BaseGrabber GrabberPrimary
{
Expand All @@ -40,7 +28,6 @@ public BaseGrabber GrabberPrimary
}
}


public BaseGrabber[] ActiveGrabbers
{
get
Expand All @@ -65,11 +52,11 @@ public GrabStateEnum GrabState
get
{
if (activeGrabbers.Count > 1)
{
return GrabStateEnum.Multi;
else if (activeGrabbers.Count > 0)
return GrabStateEnum.Single;
else
return GrabStateEnum.Inactive;
}

return activeGrabbers.Count > 0 ? GrabStateEnum.Single : GrabStateEnum.Inactive;
}
}

Expand All @@ -89,7 +76,32 @@ public GrabStateEnum ContactState
}
}

public virtual bool TryGrabWith (BaseGrabber grabber)
/// <summary>
/// Grabbers that could potentially grab this object
/// This list is maintained by the grabbers
/// </summary>
protected HashSet<BaseGrabber> availableGrabbers = new HashSet<BaseGrabber>();

/// <summary>
/// Grabbers that are currently grabbing this object
/// The top-most grabber is the primary grabber
/// </summary>
protected List<BaseGrabber> activeGrabbers = new List<BaseGrabber>();

//left protected unless we have the occasion to use them publicly, then switch to public access
[SerializeField]
protected Transform grabSpot;

[SerializeField]
protected GrabStyleEnum grabStyle = GrabStyleEnum.Exclusive;

private GrabStateEnum prevGrabState = GrabStateEnum.Inactive;
private GrabStateEnum prevContactState = GrabStateEnum.Inactive;
private Vector3 velocity;
private Vector3 averageVelocity;
private Vector3 previousVel;

public virtual bool TryGrabWith(BaseGrabber grabber)
{
// TODO error checking, mult-grab checking
if (GrabState != GrabStateEnum.Inactive)
Expand All @@ -104,14 +116,17 @@ public virtual bool TryGrabWith (BaseGrabber grabber)
// Remove from grabbable list and detatch
activeGrabbers.Remove(primary);
DetachFromGrabber(primary);
} else {
}
else
{
// If we can't, it's a no-go
return false;
}
break;

default:
case GrabStyleEnum.Multi:
break;
default:
throw new ArgumentOutOfRangeException();
}
}

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

//the next three functions provide basic behaviour. Extend from this base script in order to provide more specific functionality.

protected virtual void AttachToGrabber(BaseGrabber grabber)
{
// By default this does nothing
Expand Down Expand Up @@ -164,18 +180,19 @@ protected virtual void StartGrab(BaseGrabber grabber)
// Otherwise just push the grabber
activeGrabbers.Add(grabber);
}

// Attach ourselves to this grabber
AttachToGrabber(grabber);
if (OnGrabbed != null)
{
OnGrabbed(this);
}
}

/// <summary>
/// As long as the grabber script (usually attached to the controller, but not always) reports more than one grabber,
/// we stay inside of StayGrab.
/// </summary>
/// <param name="grabber"></param>
/// <returns></returns>
protected virtual IEnumerator StayGrab()
{
yield return null;
Expand All @@ -191,19 +208,22 @@ protected virtual IEnumerator StayGrab()
{
Debug.Log("no longer being grabbed by active grabber");
if (activeGrabbers[i] != null)
{
DetachFromGrabber(activeGrabbers[i]);
}

activeGrabbers.RemoveAt(i);
}
}
yield return null;
}

EndGrab();
}
/// <summary>
/// Grab end fires off a GrabEnded event, but also cleans up some of the variables associated with an active grab, such
/// as which grabber was grabbing this object and so forth.
/// </summary>
/// <param name="grabber"></param>
protected virtual void EndGrab()
{
if (OnReleased != null)
Expand All @@ -217,17 +237,16 @@ protected virtual void EndGrab()
/// </summary>
protected virtual void OnGrabStay()
{

}

protected virtual void Start()
{

}

protected virtual void Update()
{
if (prevGrabState != GrabState && OnGrabStateChange != null) {
if (prevGrabState != GrabState && OnGrabStateChange != null)
{
Debug.Log("Calling on grab change in grabbable");
OnGrabStateChange(this);
}
Expand All @@ -240,31 +259,6 @@ protected virtual void Update()

prevGrabState = GrabState;
prevContactState = ContactState;

}


/// <summary>
/// Grabbers that could potentially grab this object
/// This list is maintained by the grabbers
/// </summary>
protected HashSet<BaseGrabber> availableGrabbers = new HashSet<BaseGrabber>();
/// <summary>
/// Grabbers that are currently grabbing this object
/// The top-most grabber is the primary grabber
/// </summary>
protected List<BaseGrabber> activeGrabbers = new List<BaseGrabber>();

//left protected unless we have the occasion to use them publicly, then switch to public access
[SerializeField]
protected Transform grabSpot;
[SerializeField]
protected GrabStyleEnum grabStyle = GrabStyleEnum.Exclusive;

private GrabStateEnum prevGrabState = GrabStateEnum.Inactive;
private GrabStateEnum prevContactState = GrabStateEnum.Inactive;
private Vector3 velocity;
private Vector3 averageVelocity;
private Vector3 previousVel;
}
}
Loading

0 comments on commit 163ed39

Please sign in to comment.