-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Interactable reset API and behaviour using it (#144)
* Define API to reset an interactable * Implement AutoResetBehaviour based on new API * Docs
- Loading branch information
Showing
27 changed files
with
319 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) Reality Collective. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using RealityToolkit.Input.InteractionBehaviours; | ||
using UnityEditor; | ||
using UnityEditor.UIElements; | ||
using UnityEngine.UIElements; | ||
|
||
namespace RealityToolkit.Editor.Inspectors | ||
{ | ||
[CustomEditor(typeof(AutoResetBehaviour), true)] | ||
public class AutoResetBehaviourInspector : BaseInteractionBehaviourInspector | ||
{ | ||
private const string resetDelay = "resetDelay"; | ||
private const string resetPose = "resetPose"; | ||
|
||
/// <summary> | ||
/// The <see cref="AutoResetBehaviour"/> does not care what the interactor handedness | ||
/// is and thus we can hide this setting from the user. | ||
/// </summary> | ||
protected override bool ShowHandedness => false; | ||
|
||
public override VisualElement CreateInspectorGUI() | ||
{ | ||
var inspector = base.CreateInspectorGUI(); | ||
|
||
inspector.Add(new PropertyField(serializedObject.FindProperty(resetDelay))); | ||
inspector.Add(new PropertyField(serializedObject.FindProperty(resetPose))); | ||
|
||
return inspector; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) Reality Collective. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using RealityToolkit.Editor.Utilities; | ||
using RealityToolkit.Input.InteractionBehaviours; | ||
using UnityEditor; | ||
using UnityEditor.UIElements; | ||
using UnityEngine.UIElements; | ||
|
||
namespace RealityToolkit.Editor.Inspectors | ||
{ | ||
[CustomEditor(typeof(InteractionEventsBehaviour), true)] | ||
public class InteractionEventsBehaviourInspector : BaseInteractionBehaviourInspector | ||
{ | ||
/// <summary> | ||
/// The events behaviour does not need to display handedness as it will and should | ||
/// forward events for any handedness and leave it to the user to decide what to do with it. | ||
/// </summary> | ||
protected override bool ShowHandedness => false; | ||
|
||
public override VisualElement CreateInspectorGUI() | ||
{ | ||
var inspector = base.CreateInspectorGUI(); | ||
|
||
inspector.Add(new PropertyField(serializedObject.FindProperty("firstFocusEntered"))); | ||
inspector.Add(new PropertyField(serializedObject.FindProperty("focusEntered"))); | ||
inspector.Add(new PropertyField(serializedObject.FindProperty("focusExited"))); | ||
inspector.Add(new PropertyField(serializedObject.FindProperty("lastFocusExited"))); | ||
|
||
inspector.Add(UIElementsUtilities.VerticalSpace()); | ||
inspector.Add(new PropertyField(serializedObject.FindProperty("firstSelectEntered"))); | ||
inspector.Add(new PropertyField(serializedObject.FindProperty("selectEntered"))); | ||
inspector.Add(new PropertyField(serializedObject.FindProperty("selectExited"))); | ||
inspector.Add(new PropertyField(serializedObject.FindProperty("lastSelectExited"))); | ||
|
||
inspector.Add(UIElementsUtilities.VerticalSpace()); | ||
inspector.Add(new PropertyField(serializedObject.FindProperty("firstGrabEntered"))); | ||
inspector.Add(new PropertyField(serializedObject.FindProperty("grabEntered"))); | ||
inspector.Add(new PropertyField(serializedObject.FindProperty("grabExited"))); | ||
inspector.Add(new PropertyField(serializedObject.FindProperty("lastGrabExited"))); | ||
|
||
inspector.Add(UIElementsUtilities.VerticalSpace()); | ||
inspector.Add(new PropertyField(serializedObject.FindProperty("reset"))); | ||
|
||
return inspector; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Editor/Inspectors/InteractionEventsBehaviourInspector.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
Runtime/Input/InteractionBehaviours/AutoResetBehaviour.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Copyright (c) Reality Collective. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using RealityToolkit.Input.Events; | ||
using UnityEngine; | ||
|
||
namespace RealityToolkit.Input.InteractionBehaviours | ||
{ | ||
/// <summary> | ||
/// This <see cref="IInteractionBehaviour"/> will monitor interactions and once ALL interaction on the interactable has ended it will execute a reset | ||
/// using the <see cref="Interactables.IInteractable.ResetInteractable"/> API. | ||
/// </summary> | ||
[HelpURL(RealityToolkitRuntimePreferences.Toolkit_Docs_BaseUrl + "docs/interactions/interaction-behaviours/default-behaviours/auto-reset-behaviour")] | ||
[AddComponentMenu(RealityToolkitRuntimePreferences.Toolkit_InteractionsAddComponentMenu + "/" + nameof(AutoResetBehaviour))] | ||
public class AutoResetBehaviour : BaseInteractionBehaviour | ||
{ | ||
[SerializeField, Tooltip("The delay in seconds after interaction has stopped before the reset will fire.")] | ||
private float resetDelay = 5f; | ||
|
||
[SerializeField, Tooltip("If set, the interactable will be reset to its initial pose upon reset.")] | ||
private bool resetPose = true; | ||
|
||
private bool didReset; | ||
private float interactionStoppedTime; | ||
private Pose initialPose; | ||
|
||
/// <inheritdoc/> | ||
protected override void Awake() | ||
{ | ||
initialPose = new Pose(transform.position, transform.rotation); | ||
base.Awake(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void OnEnable() | ||
{ | ||
didReset = true; | ||
base.OnEnable(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void Update() | ||
{ | ||
base.Update(); | ||
|
||
if (didReset) | ||
{ | ||
return; | ||
} | ||
|
||
var timePassed = Time.time - interactionStoppedTime; | ||
if (timePassed > resetDelay) | ||
{ | ||
didReset = true; | ||
Interactable.ResetInteractable(); | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void OnDisable() | ||
{ | ||
didReset = true; | ||
base.OnDisable(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void OnFocusEntered(InteractionEventArgs eventArgs) => OnInteractionDetected(); | ||
|
||
/// <inheritdoc/> | ||
protected override void OnLastFocusExited(InteractionExitEventArgs eventArgs) | ||
{ | ||
if (Interactable.IsSelected || Interactable.IsGrabbed) | ||
{ | ||
return; | ||
} | ||
|
||
OnInteractionEnded(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void OnSelectEntered(InteractionEventArgs eventArgs) => OnInteractionDetected(); | ||
|
||
/// <inheritdoc/> | ||
protected override void OnLastSelectExited(InteractionExitEventArgs eventArgs) | ||
{ | ||
if (Interactable.IsFocused || Interactable.IsGrabbed) | ||
{ | ||
return; | ||
} | ||
|
||
OnInteractionEnded(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void OnGrabEntered(InteractionEventArgs eventArgs) => OnInteractionDetected(); | ||
|
||
/// <inheritdoc/> | ||
protected override void OnLastGrabExited(InteractionExitEventArgs eventArgs) | ||
{ | ||
if (Interactable.IsSelected || Interactable.IsFocused) | ||
{ | ||
return; | ||
} | ||
|
||
OnInteractionEnded(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void OnResetBehaviour() | ||
{ | ||
didReset = true; | ||
|
||
if (resetPose) | ||
{ | ||
ResetPose(); | ||
} | ||
} | ||
|
||
private void OnInteractionDetected() | ||
{ | ||
didReset = true; | ||
} | ||
|
||
private void OnInteractionEnded() | ||
{ | ||
interactionStoppedTime = Time.time; | ||
didReset = false; | ||
} | ||
|
||
private void ResetPose() | ||
{ | ||
transform.SetPositionAndRotation(initialPose.position, initialPose.rotation); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Runtime/Input/InteractionBehaviours/AutoResetBehaviour.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.