Permalink
Browse files

feat(ControllerReference): add new way of referencing controllers

Previously, controller calls were referenced in a number of ways from
utilising the controller's id, to passing a game object of either the
actual controller game object or the script alias game object.

This meant that ways to call certain methods was fragmented and lead
to confusing different options between methods.

The new ControllerReference script is used to get a reference to
a controller based on any number of things such as an index, the
actual GameObject, the script alias Object, the model Object or
even the controller hand.

Once a reference is retrieved, it can then access all of the elements
of a controller, such as:

 * index
 * actual
 * scriptAlias
 * model
 * hand

The ControllerReference script also has a collection of static methods
that are preferably used rather than needing to instantiate a new
object as the static methods do some nice checking to make sure
the ControllerReference is valid.

The Controller References are also cached for performance and
different instances of a Controller Reference object can be compared
as a custom comparator has been provided that compares on the index
of the controller which would always be universally unique.

A large collection of methods in the SDK implementations and SDK Bridge
have been re-written to utilise the Controller Reference, whilst a
collection of methods in the SDK Bridge have been deprecated and the
relevant methods in the SDK implementations have been removed.

All of the event payloads that passed a controller index around now
pass a controller reference, with the index being deprecated.

Added a basic test script to the Examples directory to test the
functionality of the Controller Reference script to ensure it is
all working as expected.
  • Loading branch information...
thestonefox committed Apr 18, 2017
1 parent af7ccb6 commit d564410ab1417aa8ef1e00390379e64e2240f249
Showing with 2,975 additions and 7,922 deletions.
  1. +6 −0 .gitignore
  2. +2 −2 Assets/VRTK/Examples/ExampleResources/Scripts/Archery/BowAim.cs
  3. +1 −1 Assets/VRTK/Examples/ExampleResources/Scripts/FireExtinguisher_Base.cs
  4. +5 −5 Assets/VRTK/Examples/ExampleResources/Scripts/ModelVillage_TeleportLocation.cs
  5. +2 −2 Assets/VRTK/Examples/ExampleResources/Scripts/RealGun.cs
  6. +15 −9 Assets/VRTK/Examples/ExampleResources/Scripts/SceneChanger.cs
  7. +7 −7 Assets/VRTK/Examples/ExampleResources/Scripts/Sword.cs
  8. +9 −0 Assets/VRTK/Examples/ExampleResources/Scripts/Tests.meta
  9. +62 −0 Assets/VRTK/Examples/ExampleResources/Scripts/Tests/VRTK_BaseTest.cs
  10. +12 −0 Assets/VRTK/Examples/ExampleResources/Scripts/Tests/VRTK_BaseTest.cs.meta
  11. +209 −0 Assets/VRTK/Examples/ExampleResources/Scripts/Tests/VRTK_ControllerReference_Setup.cs
  12. +12 −0 Assets/VRTK/Examples/ExampleResources/Scripts/Tests/VRTK_ControllerReference_Setup.cs.meta
  13. +36 −36 Assets/VRTK/Examples/ExampleResources/Scripts/VRTK_ControllerEvents_ListenerExample.cs
  14. +4 −4 Assets/VRTK/Examples/ExampleResources/Scripts/VRTK_ControllerInteract_ListenerExample.cs
  15. +4 −4 Assets/VRTK/Examples/ExampleResources/Scripts/VRTK_ControllerPointerEvents_ListenerExample.cs
  16. +5 −5 Assets/VRTK/Examples/ExampleResources/Scripts/VRTK_ControllerUIPointerEvents_ListenerExample.cs
  17. +4 −4 Assets/VRTK/Prefabs/Resources/Scripts/VRTK_ControllerTooltips.cs
  18. +3 −3 Assets/VRTK/Prefabs/Resources/Scripts/VRTK_DestinationPoint.cs
  19. +1 −1 Assets/VRTK/Prefabs/Resources/Scripts/VRTK_IndependentRadialMenuController.cs
  20. +1 −1 Assets/VRTK/Prefabs/Resources/Scripts/VRTK_RadialMenuController.cs
  21. +95 −341 Assets/VRTK/SDK/Base/SDK_BaseController.cs
  22. +76 −471 Assets/VRTK/SDK/Daydream/SDK_DaydreamController.cs
  23. +33 −449 Assets/VRTK/SDK/Fallback/SDK_FallbackController.cs
  24. +195 −555 Assets/VRTK/SDK/OculusVR/SDK_OculusVRController.cs
  25. +138 −521 Assets/VRTK/SDK/Simulator/SDK_SimController.cs
  26. +130 −508 Assets/VRTK/SDK/SteamVR/SDK_SteamVRController.cs
  27. +190 −69 Assets/VRTK/SDK/VRTK_SDK_Bridge.cs
  28. +149 −553 Assets/VRTK/SDK/Ximmerse/SDK_XimmerseVRController.cs
  29. +38 −33 Assets/VRTK/Scripts/Interactions/GrabAttachMechanics/VRTK_BaseGrabAttach.cs
  30. +51 −45 Assets/VRTK/Scripts/Interactions/VRTK_ControllerEvents.cs
  31. +13 −12 Assets/VRTK/Scripts/Interactions/VRTK_InteractGrab.cs
  32. +36 −5 Assets/VRTK/Scripts/Interactions/VRTK_InteractHaptics.cs
  33. +11 −3 Assets/VRTK/Scripts/Interactions/VRTK_InteractTouch.cs
  34. +8 −6 Assets/VRTK/Scripts/Interactions/VRTK_InteractUse.cs
  35. +29 −16 Assets/VRTK/Scripts/Internal/InstanceMethods/VRTK_Haptics.cs
  36. +210 −0 Assets/VRTK/Scripts/Internal/VRTK_ControllerReference.cs
  37. +12 −0 Assets/VRTK/Scripts/Internal/VRTK_ControllerReference.cs.meta
  38. +4 −5 Assets/VRTK/Scripts/Internal/VRTK_TrackedController.cs
  39. +3 −3 Assets/VRTK/Scripts/Locomotion/VRTK_MoveInPlace.cs
  40. +19 −14 Assets/VRTK/Scripts/Locomotion/VRTK_PlayerClimb.cs
  41. +13 −13 Assets/VRTK/Scripts/Pointers/VRTK_BasePointer.cs
  42. +9 −3 Assets/VRTK/Scripts/Pointers/VRTK_DestinationMarker.cs
  43. +9 −9 Assets/VRTK/Scripts/Pointers/VRTK_Pointer.cs
  44. +1 −1 Assets/VRTK/Scripts/Presence/VRTK_BodyPhysics.cs
  45. +16 −8 Assets/VRTK/Scripts/Presence/VRTK_HeadsetControllerAware.cs
  46. +13 −4 Assets/VRTK/Scripts/UI/VRTK_UIPointer.cs
  47. +36 −5 Assets/VRTK/Scripts/Utilities/VRTK_DeviceFinder.cs
  48. +40 −4 Assets/VRTK/Scripts/Utilities/VRTK_SharedMethods.cs
  49. +998 −4,182 DOCUMENTATION.md
View
@@ -17,6 +17,12 @@
/[Aa]ssets/[Rr]esources/OculusPlatformSettings.asset.meta
/[Aa]ssets/[Rr]esources/OvrAvatarSettings.asset
/[Aa]ssets/[Rr]esources/OvrAvatarSettings.asset.meta
/[Aa]ssets/[Gg]oogle[Vv][Rr]/*
/[Aa]ssets/[Gg]oogle[Vv][Rr].meta
/[Aa]ssets/[Xx]immerse [Ss][Dd][Kk]/*
/[Aa]ssets/[Xx]immerse [Ss][Dd][Kk].meta
/[Aa]ssets/[Xx]immerse [Ss][Dd][Kk] [Ee]xt/*
/[Aa]ssets/[Xx]immerse [Ss][Dd][Kk] [Ee]xt.meta
# Autogenerated VS/MD solution and project files
ExportedObj/
@@ -163,8 +163,8 @@ private void PullString()
if (!currentPull.ToString("F2").Equals(previousPull.ToString("F2")))
{
VRTK_SharedMethods.TriggerHapticPulse(VRTK_DeviceFinder.GetControllerIndex(holdControl.gameObject), bowVibration);
VRTK_SharedMethods.TriggerHapticPulse(VRTK_DeviceFinder.GetControllerIndex(stringControl.gameObject), stringVibration);
VRTK_SharedMethods.TriggerHapticPulse(VRTK_ControllerReference.GetControllerReference(holdControl.gameObject), bowVibration);
VRTK_SharedMethods.TriggerHapticPulse(VRTK_ControllerReference.GetControllerReference(stringControl.gameObject), stringVibration);
}
previousPull = currentPull;
}
@@ -29,7 +29,7 @@ protected override void Update()
{
float power = controllerEvents.GetTriggerAxis();
Spray(power);
VRTK_SharedMethods.TriggerHapticPulse(VRTK_DeviceFinder.GetControllerIndex(controllerEvents.gameObject), power * 0.25f, 0.1f, 0.01f);
VRTK_SharedMethods.TriggerHapticPulse(VRTK_ControllerReference.GetControllerReference(controllerEvents.gameObject), power * 0.25f, 0.1f, 0.01f);
}
else
{
@@ -9,14 +9,14 @@ public class ModelVillage_TeleportLocation : VRTK_DestinationMarker
private void OnTriggerStay(Collider collider)
{
var controller = (collider.GetComponent<VRTK_ControllerEvents>() ? collider.GetComponent<VRTK_ControllerEvents>() : collider.GetComponentInParent<VRTK_ControllerEvents>());
if (controller)
VRTK_ControllerEvents controller = (collider.GetComponent<VRTK_ControllerEvents>() ? collider.GetComponent<VRTK_ControllerEvents>() : collider.GetComponentInParent<VRTK_ControllerEvents>());
if (controller != null)
{
if (lastUsePressedState == true && !controller.triggerPressed)
{
var distance = Vector3.Distance(transform.position, destination.position);
var controllerIndex = VRTK_DeviceFinder.GetControllerIndex(controller.gameObject);
OnDestinationMarkerSet(SetDestinationMarkerEvent(distance, destination, new RaycastHit(), destination.position, controllerIndex));
float distance = Vector3.Distance(transform.position, destination.position);
VRTK_ControllerReference controllerReference = VRTK_ControllerReference.GetControllerReference(controller.gameObject);
OnDestinationMarkerSet(SetDestinationMarkerEvent(distance, destination, new RaycastHit(), destination.position, controllerReference));
}
lastUsePressedState = controller.triggerPressed;
}
@@ -97,11 +97,11 @@ public override void StartUsing(GameObject currentUsingObject)
{
slide.Fire();
FireBullet();
VRTK_SharedMethods.TriggerHapticPulse(VRTK_DeviceFinder.GetControllerIndex(controllerEvents.gameObject), 0.63f, 0.2f, 0.01f);
VRTK_SharedMethods.TriggerHapticPulse(VRTK_ControllerReference.GetControllerReference(controllerEvents.gameObject), 0.63f, 0.2f, 0.01f);
}
else
{
VRTK_SharedMethods.TriggerHapticPulse(VRTK_DeviceFinder.GetControllerIndex(controllerEvents.gameObject), 0.08f, 0.1f, 0.01f);
VRTK_SharedMethods.TriggerHapticPulse(VRTK_ControllerReference.GetControllerReference(controllerEvents.gameObject), 0.08f, 0.1f, 0.01f);
}
}
@@ -6,7 +6,7 @@
public class SceneChanger : MonoBehaviour
{
private bool canPress;
private uint controllerIndex;
private VRTK_ControllerReference controllerReference;
private void Awake()
{
@@ -17,11 +17,14 @@ private void Awake()
private bool ForwardPressed()
{
if (controllerIndex >= uint.MaxValue)
if (!VRTK_ControllerReference.IsValid(controllerReference))
{
return false;
}
if (canPress && VRTK_SDK_Bridge.IsTriggerPressedOnIndex(controllerIndex) && VRTK_SDK_Bridge.IsGripPressedOnIndex(controllerIndex) && VRTK_SDK_Bridge.IsTouchpadPressedOnIndex(controllerIndex))
if (canPress &&
VRTK_SDK_Bridge.GetControllerButtonState(SDK_BaseController.ButtonTypes.Trigger, SDK_BaseController.ButtonPressTypes.Press, controllerReference) &&
VRTK_SDK_Bridge.GetControllerButtonState(SDK_BaseController.ButtonTypes.Grip, SDK_BaseController.ButtonPressTypes.Press, controllerReference) &&
VRTK_SDK_Bridge.GetControllerButtonState(SDK_BaseController.ButtonTypes.Touchpad, SDK_BaseController.ButtonPressTypes.Press, controllerReference))
{
return true;
}
@@ -30,12 +33,15 @@ private bool ForwardPressed()
private bool BackPressed()
{
if (controllerIndex >= uint.MaxValue)
if (!VRTK_ControllerReference.IsValid(controllerReference))
{
return false;
}
if (canPress && VRTK_SDK_Bridge.IsTriggerPressedOnIndex(controllerIndex) && VRTK_SDK_Bridge.IsGripPressedOnIndex(controllerIndex) && VRTK_SDK_Bridge.IsButtonTwoPressedOnIndex(controllerIndex))
if (canPress &&
VRTK_SDK_Bridge.GetControllerButtonState(SDK_BaseController.ButtonTypes.Trigger, SDK_BaseController.ButtonPressTypes.Press, controllerReference) &&
VRTK_SDK_Bridge.GetControllerButtonState(SDK_BaseController.ButtonTypes.Grip, SDK_BaseController.ButtonPressTypes.Press, controllerReference) &&
VRTK_SDK_Bridge.GetControllerButtonState(SDK_BaseController.ButtonTypes.ButtonTwo, SDK_BaseController.ButtonPressTypes.Press, controllerReference))
{
return true;
}
@@ -49,11 +55,11 @@ private void ResetPress()
private void Update()
{
var rightHand = VRTK_DeviceFinder.GetControllerRightHand(true);
controllerIndex = VRTK_DeviceFinder.GetControllerIndex(rightHand);
GameObject rightHand = VRTK_DeviceFinder.GetControllerRightHand(true);
controllerReference = VRTK_ControllerReference.GetControllerReference(rightHand);
if (ForwardPressed() || Input.GetKeyUp(KeyCode.Space))
{
var nextSceneIndex = SceneManager.GetActiveScene().buildIndex + 1;
int nextSceneIndex = SceneManager.GetActiveScene().buildIndex + 1;
if (nextSceneIndex >= SceneManager.sceneCountInBuildSettings)
{
nextSceneIndex = 0;
@@ -63,7 +69,7 @@ private void Update()
if (BackPressed() || Input.GetKeyUp(KeyCode.Backspace))
{
var previousSceneIndex = SceneManager.GetActiveScene().buildIndex - 1;
int previousSceneIndex = SceneManager.GetActiveScene().buildIndex - 1;
if (previousSceneIndex < 0)
{
previousSceneIndex = SceneManager.sceneCountInBuildSettings - 1;
@@ -7,7 +7,7 @@ public class Sword : VRTK_InteractableObject
private float impactMagnifier = 120f;
private float collisionForce = 0f;
private float maxCollisionForce = 4000f;
private GameObject grabbingController;
private VRTK_ControllerReference controllerReference;
public float CollisionForce()
{
@@ -17,29 +17,29 @@ public float CollisionForce()
public override void Grabbed(GameObject grabbingObject)
{
base.Grabbed(grabbingObject);
grabbingController = grabbingObject;
controllerReference = VRTK_ControllerReference.GetControllerReference(grabbingObject);
}
public override void Ungrabbed(GameObject previousGrabbingObject)
{
base.Ungrabbed(previousGrabbingObject);
grabbingController = null;
controllerReference = null;
}
protected override void OnEnable()
{
base.OnEnable();
grabbingController = null;
controllerReference = null;
interactableRigidbody.collisionDetectionMode = CollisionDetectionMode.Continuous;
}
private void OnCollisionEnter(Collision collision)
{
if (grabbingController != null && IsGrabbed())
if (VRTK_ControllerReference.IsValid(controllerReference) && IsGrabbed())
{
collisionForce = VRTK_DeviceFinder.GetControllerVelocity(grabbingController).magnitude * impactMagnifier;
collisionForce = VRTK_DeviceFinder.GetControllerVelocity(controllerReference).magnitude * impactMagnifier;
var hapticStrength = collisionForce / maxCollisionForce;
VRTK_SharedMethods.TriggerHapticPulse(VRTK_DeviceFinder.GetControllerIndex(grabbingController), hapticStrength, 0.5f, 0.01f);
VRTK_SharedMethods.TriggerHapticPulse(controllerReference, hapticStrength, 0.5f, 0.01f);
}
else
{

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,62 @@
namespace VRTK.Examples.Tests
{
using UnityEngine;
using System.Collections;
public abstract class VRTK_BaseTest : MonoBehaviour
{
protected string currentTest;
protected string currentSetup;
protected abstract void Test();
protected virtual void OnEnable()
{
StartCoroutine(RunTests());
}
protected virtual void BeginTest(string name, int level = 1)
{
currentTest = name;
Debug.Log("<color=darkblue><b>" + "".PadLeft(level, '#') + " Starting Tests for " + name + "</b></color>");
}
protected virtual void SetUp(string message)
{
currentSetup = message;
Debug.Log("<color=blue><b>#### Preparing test for " + message + "</b></color>");
}
protected virtual void TearDown()
{
Debug.Log("==============================================================================");
}
protected virtual void Assert(string description, bool assertion, string failure, string success = "")
{
if (assertion)
{
Debug.Log("<color=teal><b>## [" + description + "] PASSED ##</b></color>");
}
else
{
Debug.Log("<color=maroon><b>## [" + description + "] FAILED INSIDE [" + currentTest + "." + currentSetup + "]##</b></color>");
}
if (!assertion)
{
Debug.LogException(new System.Exception(failure));
}
else if (success != "")
{
Debug.Log("<color=purple><i> ~~~~~> " + success + "</i></color>");
}
}
protected virtual IEnumerator RunTests()
{
yield return new WaitForEndOfFrame();
Test();
}
}
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit d564410

Please sign in to comment.