Skip to main content
Version: 21 Aug 2024

MagicLeapController.cs

Source code

// %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
{
public class MagicLeapController
{
private static MagicLeapController instance;

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<InputActionManager>();
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();
}

public event Action<InputAction.CallbackContext> TriggerPressed
{
add => triggerAction.performed += value;
remove => triggerAction.performed -= value;
}

public event Action<InputAction.CallbackContext> BumperPressed
{
add => bumperAction.performed += value;
remove => bumperAction.performed -= value;
}

public event Action<InputAction.CallbackContext> MenuPressed
{
add => menuButtonAction.performed += value;
remove => menuButtonAction.performed -= value;
}

public event Action<InputAction.CallbackContext> TrackpadClicked
{
add => trackpadClickAction.performed += value;
remove => trackpadClickAction.performed -= value;
}

public bool IsTracked => isTrackedAction.IsPressed();

public Vector3 Position => positionAction.ReadValue<Vector3>();

public Quaternion Rotation => rotationAction.ReadValue<Quaternion>();

public Vector3 PointerPosition => pointerPositionAction.ReadValue<Vector3>();

public Quaternion PointerRotation => pointerRotationAction.ReadValue<Quaternion>();

public Vector3 Velocity => velocityAction.ReadValue<Vector3>();

public Vector3 AngularVelocity => angularVelocityAction.ReadValue<Vector3>();

public InputTrackingState TrackingState => (InputTrackingState)trackingStatusAction.ReadValue<int>();

public bool TriggerIsPressed => triggerAction.IsPressed();

public float TriggerValue => triggerValueAction.ReadValue<float>();

public bool BumperIsPressed => bumperAction.IsPressed();

public bool MenuIsPressed => menuButtonAction.IsPressed();

public Vector2 TouchPosition => trackpadAction.ReadValue<Vector2>();

public float TouchPressure => trackpadForceAction.ReadValue<float>();
}
}