|
| 1 | +// Copyright (c) Reality Collective. All rights reserved. |
| 2 | +// Licensed under the MIT License. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +using RealityToolkit.Input.Events; |
| 5 | +using UnityEngine; |
| 6 | + |
| 7 | +namespace RealityToolkit.Input.InteractionBehaviours |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// This <see cref="IInteractionBehaviour"/> will monitor interactions and once ALL interaction on the interactable has ended it will execute a reset |
| 11 | + /// using the <see cref="Interactables.IInteractable.ResetInteractable"/> API. |
| 12 | + /// </summary> |
| 13 | + [HelpURL(RealityToolkitRuntimePreferences.Toolkit_Docs_BaseUrl + "docs/interactions/interaction-behaviours/default-behaviours/auto-reset-behaviour")] |
| 14 | + [AddComponentMenu(RealityToolkitRuntimePreferences.Toolkit_InteractionsAddComponentMenu + "/" + nameof(AutoResetBehaviour))] |
| 15 | + public class AutoResetBehaviour : BaseInteractionBehaviour |
| 16 | + { |
| 17 | + [SerializeField, Tooltip("The delay in seconds after interaction has stopped before the reset will fire.")] |
| 18 | + private float resetDelay = 5f; |
| 19 | + |
| 20 | + [SerializeField, Tooltip("If set, the interactable will be reset to its initial pose upon reset.")] |
| 21 | + private bool resetPose = true; |
| 22 | + |
| 23 | + private bool didReset; |
| 24 | + private float interactionStoppedTime; |
| 25 | + private Pose initialPose; |
| 26 | + |
| 27 | + /// <inheritdoc/> |
| 28 | + protected override void Awake() |
| 29 | + { |
| 30 | + initialPose = new Pose(transform.position, transform.rotation); |
| 31 | + base.Awake(); |
| 32 | + } |
| 33 | + |
| 34 | + /// <inheritdoc/> |
| 35 | + protected override void OnEnable() |
| 36 | + { |
| 37 | + didReset = true; |
| 38 | + base.OnEnable(); |
| 39 | + } |
| 40 | + |
| 41 | + /// <inheritdoc/> |
| 42 | + protected override void Update() |
| 43 | + { |
| 44 | + base.Update(); |
| 45 | + |
| 46 | + if (didReset) |
| 47 | + { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + var timePassed = Time.time - interactionStoppedTime; |
| 52 | + if (timePassed > resetDelay) |
| 53 | + { |
| 54 | + didReset = true; |
| 55 | + Interactable.ResetInteractable(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /// <inheritdoc/> |
| 60 | + protected override void OnDisable() |
| 61 | + { |
| 62 | + didReset = true; |
| 63 | + base.OnDisable(); |
| 64 | + } |
| 65 | + |
| 66 | + /// <inheritdoc/> |
| 67 | + protected override void OnFocusEntered(InteractionEventArgs eventArgs) => OnInteractionDetected(); |
| 68 | + |
| 69 | + /// <inheritdoc/> |
| 70 | + protected override void OnLastFocusExited(InteractionExitEventArgs eventArgs) |
| 71 | + { |
| 72 | + if (Interactable.IsSelected || Interactable.IsGrabbed) |
| 73 | + { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + OnInteractionEnded(); |
| 78 | + } |
| 79 | + |
| 80 | + /// <inheritdoc/> |
| 81 | + protected override void OnSelectEntered(InteractionEventArgs eventArgs) => OnInteractionDetected(); |
| 82 | + |
| 83 | + /// <inheritdoc/> |
| 84 | + protected override void OnLastSelectExited(InteractionExitEventArgs eventArgs) |
| 85 | + { |
| 86 | + if (Interactable.IsFocused || Interactable.IsGrabbed) |
| 87 | + { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + OnInteractionEnded(); |
| 92 | + } |
| 93 | + |
| 94 | + /// <inheritdoc/> |
| 95 | + protected override void OnGrabEntered(InteractionEventArgs eventArgs) => OnInteractionDetected(); |
| 96 | + |
| 97 | + /// <inheritdoc/> |
| 98 | + protected override void OnLastGrabExited(InteractionExitEventArgs eventArgs) |
| 99 | + { |
| 100 | + if (Interactable.IsSelected || Interactable.IsFocused) |
| 101 | + { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + OnInteractionEnded(); |
| 106 | + } |
| 107 | + |
| 108 | + /// <inheritdoc/> |
| 109 | + protected override void OnResetBehaviour() |
| 110 | + { |
| 111 | + didReset = true; |
| 112 | + |
| 113 | + if (resetPose) |
| 114 | + { |
| 115 | + ResetPose(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private void OnInteractionDetected() |
| 120 | + { |
| 121 | + didReset = true; |
| 122 | + } |
| 123 | + |
| 124 | + private void OnInteractionEnded() |
| 125 | + { |
| 126 | + interactionStoppedTime = Time.time; |
| 127 | + didReset = false; |
| 128 | + } |
| 129 | + |
| 130 | + private void ResetPose() |
| 131 | + { |
| 132 | + transform.SetPositionAndRotation(initialPose.position, initialPose.rotation); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments