using System; using Unity.XR.CoreUtils.Bindings.Variables; using UnityEngine.Events; using UnityEngine.InputSystem; using UnityEngine.XR.Interaction.Toolkit.Inputs; #if XR_HANDS_1_1_OR_NEWER using UnityEngine.XR.Hands; #endif namespace UnityEngine.XR.Interaction.Toolkit.Samples.Hands { /// /// Behavior that provides events for when the system gesture starts and ends and when the /// menu palm pinch gesture occurs while hand tracking is in use. /// /// /// See Meta Hand Tracking Aim. /// /// public class MetaSystemGestureDetector : MonoBehaviour { /// /// The state of the system gesture. /// /// public enum SystemGestureState { /// /// The system gesture has fully ended. /// Ended, /// /// The system gesture has started or is ongoing. Typically, this means the user is looking at /// their palm at eye level or has not yet released the palm pinch gesture or turned their hand around. /// Started, } [SerializeField] InputActionProperty m_AimFlagsAction = new InputActionProperty(new InputAction(expectedControlType: "Integer")); /// /// The Input System action to read the Aim Flags. /// /// /// Typically a Value action type with an Integer control type with a binding to either: /// /// /// <MetaAimHand>{LeftHand}/aimFlags /// /// /// <MetaAimHand>{RightHand}/aimFlags /// /// /// public InputActionProperty aimFlagsAction { get => m_AimFlagsAction; set { if (Application.isPlaying) UnbindAimFlags(); m_AimFlagsAction = value; if (Application.isPlaying && isActiveAndEnabled) BindAimFlags(); } } [SerializeField] UnityEvent m_SystemGestureStarted; /// /// Calls the methods in its invocation list when the system gesture starts, which typically occurs when /// the user looks at their palm at eye level. /// /// /// public UnityEvent systemGestureStarted { get => m_SystemGestureStarted; set => m_SystemGestureStarted = value; } [SerializeField] UnityEvent m_SystemGestureEnded; /// /// Calls the methods in its invocation list when the system gesture ends. /// /// /// This behavior postpones ending the system gesture until the user has turned their hand around. /// In other words, it isn't purely based on the /// being cleared from the aim flags in order to better replicate the native visual feedback in the Meta Home menu. /// /// /// public UnityEvent systemGestureEnded { get => m_SystemGestureEnded; set => m_SystemGestureEnded = value; } [SerializeField] UnityEvent m_MenuPressed; /// /// Calls the methods in its invocation list when the menu button is triggered by a palm pinch gesture. /// /// /// This is triggered by the non-dominant hand, which is the one with the menu icon (☰). /// The universal menu (Oculus icon) on the dominant hand does not trigger this event. /// /// public UnityEvent menuPressed { get => m_MenuPressed; set => m_MenuPressed = value; } /// /// The state of the system gesture. /// /// /// /// public IReadOnlyBindableVariable systemGestureState => m_SystemGestureState; readonly BindableEnum m_SystemGestureState = new BindableEnum(checkEquality: false); #if XR_HANDS_1_1_OR_NEWER [NonSerialized] // NonSerialized is required to avoid an "Unsupported enum base type" error about the Flags enum being ulong MetaAimFlags m_AimFlags; #endif bool m_AimFlagsBound; /// /// See . /// protected void OnEnable() { BindAimFlags(); #if XR_HANDS_1_1_OR_NEWER var action = m_AimFlagsAction.action; if (action != null) // Force invoking the events upon initialization to simplify making sure the callback's desired results are synced UpdateAimFlags((MetaAimFlags)action.ReadValue(), true); #else Debug.LogWarning("Script requires XR Hands (com.unity.xr.hands) package to monitor Meta Aim Flags. Install using Window > Package Manager or click Fix on the related issue in Edit > Project Settings > XR Plug-in Management > Project Validation.", this); SetGestureState(SystemGestureState.Ended, true); #endif } /// /// See . /// protected void OnDisable() { UnbindAimFlags(); } void BindAimFlags() { if (m_AimFlagsBound) return; var action = m_AimFlagsAction.action; if (action == null) return; action.performed += OnAimFlagsActionPerformedOrCanceled; action.canceled += OnAimFlagsActionPerformedOrCanceled; m_AimFlagsBound = true; m_AimFlagsAction.EnableDirectAction(); } void UnbindAimFlags() { if (!m_AimFlagsBound) return; var action = m_AimFlagsAction.action; if (action == null) return; m_AimFlagsAction.DisableDirectAction(); action.performed -= OnAimFlagsActionPerformedOrCanceled; action.canceled -= OnAimFlagsActionPerformedOrCanceled; m_AimFlagsBound = false; } void SetGestureState(SystemGestureState state, bool forceInvoke) { if (!forceInvoke && m_SystemGestureState.Value == state) return; m_SystemGestureState.Value = state; switch (state) { case SystemGestureState.Ended: m_SystemGestureEnded?.Invoke(); break; case SystemGestureState.Started: m_SystemGestureStarted?.Invoke(); break; } } #if XR_HANDS_1_1_OR_NEWER void UpdateAimFlags(MetaAimFlags value, bool forceInvoke = false) { var hadMenuPressed = (m_AimFlags & MetaAimFlags.MenuPressed) != 0; m_AimFlags = value; var hasSystemGesture = (m_AimFlags & MetaAimFlags.SystemGesture) != 0; var hasMenuPressed = (m_AimFlags & MetaAimFlags.MenuPressed) != 0; var hasValid = (m_AimFlags & MetaAimFlags.Valid) != 0; var hasIndexPinching = (m_AimFlags & MetaAimFlags.IndexPinching) != 0; if (!hadMenuPressed && hasMenuPressed) { m_MenuPressed?.Invoke(); } if (hasSystemGesture || hasMenuPressed) { SetGestureState(SystemGestureState.Started, forceInvoke); return; } if (hasValid) { SetGestureState(SystemGestureState.Ended, forceInvoke); return; } // We want to keep the system gesture going when the user is still index pinching // even though the SystemGesture flag is no longer set. if (hasIndexPinching && m_SystemGestureState.Value != SystemGestureState.Ended) { SetGestureState(SystemGestureState.Started, forceInvoke); return; } SetGestureState(SystemGestureState.Ended, forceInvoke); } #endif void OnAimFlagsActionPerformedOrCanceled(InputAction.CallbackContext context) { #if XR_HANDS_1_1_OR_NEWER UpdateAimFlags((MetaAimFlags)context.ReadValue()); #endif } } }