// %BANNER_BEGIN%
// ---------------------------------------------------------------------
// %COPYRIGHT_BEGIN%
// Copyright (c) (2024) Magic Leap, Inc. All Rights Reserved.
// Use of this file is governed by the Software License Agreement, located here: https://www.magicleap.com/software-license-agreement-ml2
// Terms and conditions applicable to third-party materials accompanying this distribution may also be found in the top-level NOTICE file appearing herein.
// %COPYRIGHT_END%
// ---------------------------------------------------------------------
// %BANNER_END%
using System;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.XR;
using UnityEngine.XR.Interaction.Toolkit.Inputs;
namespace MagicLeap.Examples
{
///
/// Convenient utility for accessing InputActions bound to the ML2 controller's OpenXR action paths
///
public class MagicLeapController
{
private static MagicLeapController instance;
///
/// Singleton instance of the ML2 Controller action map bindings
///
public static MagicLeapController Instance
{
get
{
instance ??= new MagicLeapController();
return instance;
}
}
private readonly InputActionAsset inputActionsAsset;
private readonly InputActionMap inputMap;
private readonly InputAction triggerAction, triggerValueAction, trackpadClickAction, trackpadForceAction, bumperAction, menuButtonAction, trackpadAction, positionAction,
rotationAction, trackingStatusAction, isTrackedAction, pointerPositionAction, pointerRotationAction, velocityAction, angularVelocityAction;
private MagicLeapController()
{
var manager = UnityEngine.Object.FindObjectOfType();
if (manager == null)
throw new System.NullReferenceException("Could not find an InputActionManager to initialize a MagicLeapController from");
inputActionsAsset = manager.actionAssets[0];
if (inputActionsAsset == null)
throw new System.NullReferenceException("Could not find an InputActionAsset");
inputActionsAsset.Enable();
inputMap = inputActionsAsset.FindActionMap("Controller");
triggerAction = inputMap.FindAction("Trigger");
bumperAction = inputMap.FindAction("Bumper");
menuButtonAction = inputMap.FindAction("MenuButton");
trackpadClickAction = inputMap.FindAction("TrackpadClick");
isTrackedAction = inputMap.FindAction("IsTracked");
positionAction = inputMap.FindAction("Position");
rotationAction = inputMap.FindAction("Rotation");
pointerPositionAction = inputMap.FindAction("PointerPosition");
pointerRotationAction = inputMap.FindAction("PointerRotation");
velocityAction = inputMap.FindAction("Velocity");
angularVelocityAction = inputMap.FindAction("AngularVelocity");
trackingStatusAction = inputMap.FindAction("Status");
triggerAction = inputMap.FindAction("Trigger");
triggerValueAction = inputMap.FindAction("TriggerValue");
trackpadAction = inputMap.FindAction("Trackpad");
trackpadForceAction ??= inputMap.FindAction("TrackpadForce");
}
~MagicLeapController()
{
if (inputActionsAsset != null && instance == this)
inputActionsAsset.Disable();
}
///
/// Trigger press event
///
public event Action TriggerPressed
{
add => triggerAction.performed += value;
remove => triggerAction.performed -= value;
}
///
/// Bumper press event
///
public event Action BumperPressed
{
add => bumperAction.performed += value;
remove => bumperAction.performed -= value;
}
///
/// Menu button press event
///
public event Action MenuPressed
{
add => menuButtonAction.performed += value;
remove => menuButtonAction.performed -= value;
}
///
/// Trackpad click event
///
public event Action TrackpadClicked
{
add => trackpadClickAction.performed += value;
remove => trackpadClickAction.performed -= value;
}
///
/// Is the controller currently being tracked?
///
public bool IsTracked => isTrackedAction.IsPressed();
///
/// The world position of the controller body where the user should be holding it.
///
public Vector3 Position => positionAction.ReadValue();
///
/// The world rotation of the controller body where the user should be holding it.
///
public Quaternion Rotation => rotationAction.ReadValue();
///
/// The world position of the controller's pointer raycast origin.
///
public Vector3 PointerPosition => pointerPositionAction.ReadValue();
///
/// The world rotation of the controller's pointer raycast origin.
///
public Quaternion PointerRotation => pointerRotationAction.ReadValue();
///
/// The current movement velocity of the controller.
///
public Vector3 Velocity => velocityAction.ReadValue();
///
/// The current angular velocity of the controller.
///
public Vector3 AngularVelocity => angularVelocityAction.ReadValue();
///
/// The current of the controller.
///
public InputTrackingState TrackingState => (InputTrackingState)trackingStatusAction.ReadValue();
///
/// Is the controller's trigger button currently pressed?
///
public bool TriggerIsPressed => triggerAction.IsPressed();
///
/// The current analog value of the controller's trigger, from 0 to 1.
///
public float TriggerValue => triggerValueAction.ReadValue();
///
/// Is the controller's bumper currently pressed?
///
public bool BumperIsPressed => bumperAction.IsPressed();
///
/// Is the controller's menu button currently pressed?
///
public bool MenuIsPressed => menuButtonAction.IsPressed();
///
/// The position on the controller's touchpad that is currently touched.
///
public Vector2 TouchPosition => trackpadAction.ReadValue();
///
/// The current touch pressure being applied to the controller's touchpad, from 0 to 1.
///
public float TouchPressure => trackpadForceAction.ReadValue();
}
}