using System.Collections.Generic;
using UnityEngine.InputSystem.Controls;
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.XR;
using UnityEngine.Scripting;
using UnityEngine.XR.OpenXR.Input;
#if UNITY_EDITOR
using UnityEditor;
#endif
#if USE_INPUT_SYSTEM_POSE_CONTROL
using PoseControl = UnityEngine.InputSystem.XR.PoseControl;
#else
using PoseControl = UnityEngine.XR.OpenXR.Input.PoseControl;
#endif
namespace UnityEngine.XR.OpenXR.Features.Interactions
{
///
/// This enables the use of KHR Simple Controllers interaction profiles in OpenXR.
///
#if UNITY_EDITOR
[UnityEditor.XR.OpenXR.Features.OpenXRFeature(UiName = "Khronos Simple Controller Profile",
BuildTargetGroups = new[] { BuildTargetGroup.Standalone, BuildTargetGroup.WSA, BuildTargetGroup.Android},
Company = "Unity",
Desc = "Allows for mapping input to the Khronos Simple Controller interaction profile.",
DocumentationLink = Constants.k_DocumentationManualURL + "features/khrsimplecontrollerprofile.html",
OpenxrExtensionStrings = "",
Version = "0.0.1",
Category = UnityEditor.XR.OpenXR.Features.FeatureCategory.Interaction,
FeatureId = featureId)]
#endif
public class KHRSimpleControllerProfile : OpenXRInteractionFeature
{
///
/// The feature id string. This is used to give the feature a well known id for reference.
///
public const string featureId = "com.unity.openxr.feature.input.khrsimpleprofile";
///
/// An Input System device based off the Khronos Simple Controller interaction profile. This device contains one haptic output motor.
///
[Preserve, InputControlLayout(displayName = "Khronos Simple Controller (OpenXR)", commonUsages = new[] { "LeftHand", "RightHand" })]
public class KHRSimpleController : XRControllerWithRumble
{
///
/// A [ButtonControl](xref:UnityEngine.InputSystem.Controls.ButtonControl) that represents the OpenXR binding.
///
[Preserve, InputControl(aliases = new[] { "Secondary", "selectbutton" }, usage = "PrimaryButton")]
public ButtonControl select { get; private set; }
///
/// A [ButtonControl](xref:UnityEngine.InputSystem.Controls.ButtonControl) that represents the OpenXR binding.
///
[Preserve, InputControl(aliases = new[] { "Primary", "menubutton" }, usage = "MenuButton")]
public ButtonControl menu { get; private set; }
///
/// A that represents information from the OpenXR binding.
///
[Preserve, InputControl(offset = 0, aliases = new[] { "device", "gripPose" }, usage = "Device")]
public PoseControl devicePose { get; private set; }
///
/// A that represents information from the OpenXR binding.
///
[Preserve, InputControl(offset = 0, alias = "aimPose", usage = "Pointer")]
public PoseControl pointer { get; private set; }
///
/// A [ButtonControl](xref:UnityEngine.InputSystem.Controls.ButtonControl) required for backwards compatibility with the XRSDK layouts. This represents the overall tracking state of the device. This value is equivalent to mapping devicePose/isTracked.
///
[Preserve, InputControl(offset = 2)]
new public ButtonControl isTracked { get; private set; }
///
/// A [IntegerControl](xref:UnityEngine.InputSystem.Controls.IntegerControl) required for backwards compatibility with the XRSDK layouts. This represents the bit flag set indicating what data is valid. This value is equivalent to mapping devicePose/trackingState.
///
[Preserve, InputControl(offset = 4)]
new public IntegerControl trackingState { get; private set; }
///
/// A [Vector3Control](xref:UnityEngine.InputSystem.Controls.Vector3Control) required for backwards compatibility with the XRSDK layouts. This is the device position, or grip position. This value is equivalent to mapping devicePose/position.
///
[Preserve, InputControl(offset = 8, alias = "gripPosition")]
new public Vector3Control devicePosition { get; private set; }
///
/// A [QuaternionControl](xref:UnityEngine.InputSystem.Controls.QuaternionControl) required for backwards compatibility with the XRSDK layouts. This is the device orientation, or grip orientation. This value is equivalent to mapping devicePose/rotation.
///
[Preserve, InputControl(offset = 20, alias = "gripOrientation")]
new public QuaternionControl deviceRotation { get; private set; }
///
/// A [Vector3Control](xref:UnityEngine.InputSystem.Controls.Vector3Control) required for backwards compatibility with the XRSDK layouts. This is the pointer position. This value is equivalent to mapping pointerPose/position.
///
[Preserve, InputControl(offset = 68)]
public Vector3Control pointerPosition { get; private set; }
///
/// A [QuaternionControl](xref:UnityEngine.InputSystem.Controls.QuaternionControl) required for backwards compatibility with the XRSDK layouts. This is the pointer rotation. This value is equivalent to mapping pointerPose/rotation.
///
[Preserve, InputControl(offset = 80, alias = "pointerOrientation")]
public QuaternionControl pointerRotation { get; private set; }
///
/// A that represents the binding.
///
[Preserve, InputControl(usage = "Haptic")]
public HapticControl haptic { get; private set; }
///
protected override void FinishSetup()
{
base.FinishSetup();
menu = GetChildControl("menu");
select = GetChildControl("select");
devicePose = GetChildControl("devicePose");
pointer = GetChildControl("pointer");
isTracked = GetChildControl("isTracked");
trackingState = GetChildControl("trackingState");
devicePosition = GetChildControl("devicePosition");
deviceRotation = GetChildControl("deviceRotation");
pointerPosition = GetChildControl("pointerPosition");
pointerRotation = GetChildControl("pointerRotation");
haptic = GetChildControl("haptic");
}
}
///
/// OpenXR string that represents the Interaction Profile.
///
public const string profile = "/interaction_profiles/khr/simple_controller";
///
/// Constant for a boolean interaction binding '.../input/select/click' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs.
///
public const string select = "/input/select/click";
///
/// Constant for a boolean interaction binding '.../input/menu/click' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs.
///
public const string menu = "/input/menu/click";
///
/// Constant for a pose interaction binding '.../input/grip/pose' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs.
///
public const string grip = "/input/grip/pose";
///
/// Constant for a pose interaction binding '.../input/aim/pose' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs.
///
public const string aim = "/input/aim/pose";
///
/// Constant for a haptic interaction binding '.../input/output/haptic' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs.
///
public const string haptic = "/output/haptic";
private const string kDeviceLocalizedName = "KHR Simple Controller OpenXR";
///
/// Registers the layout with the Input System.
///
protected override void RegisterDeviceLayout()
{
#if UNITY_EDITOR
if (!OpenXRLoaderEnabledForSelectedBuildTarget(EditorUserBuildSettings.selectedBuildTargetGroup))
return;
#endif
InputSystem.InputSystem.RegisterLayout(typeof(KHRSimpleController),
matches: new InputDeviceMatcher()
.WithInterface(XRUtilities.InterfaceMatchAnyVersion)
.WithProduct(kDeviceLocalizedName));
}
///
/// Removes the layout from the Input System.
///
protected override void UnregisterDeviceLayout()
{
#if UNITY_EDITOR
if (!OpenXRLoaderEnabledForSelectedBuildTarget(EditorUserBuildSettings.selectedBuildTargetGroup))
return;
#endif
InputSystem.InputSystem.RemoveLayout(typeof(KHRSimpleController).Name);
}
///
/// Return device layout string that used for registering device for the Input System.
///
/// Device layout string.
protected override string GetDeviceLayoutName()
{
return nameof(KHRSimpleController);
}
///
protected override void RegisterActionMapsWithRuntime()
{
ActionMapConfig actionMap = new ActionMapConfig()
{
name = "khrsimplecontroller",
localizedName = kDeviceLocalizedName,
desiredInteractionProfile = profile,
manufacturer = "Khronos",
serialNumber = "",
deviceInfos = new List()
{
new DeviceConfig()
{
characteristics = (InputDeviceCharacteristics)(InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.TrackedDevice | InputDeviceCharacteristics.Controller | InputDeviceCharacteristics.Left),
userPath = UserPaths.leftHand
},
new DeviceConfig()
{
characteristics = (InputDeviceCharacteristics)(InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.TrackedDevice | InputDeviceCharacteristics.Controller | InputDeviceCharacteristics.Right),
userPath = UserPaths.rightHand
}
},
actions = new List()
{
// Menu
new ActionConfig()
{
name = "select",
localizedName = "Select",
type = ActionType.Binary,
usages = new List()
{
"PrimaryButton"
},
bindings = new List()
{
new ActionBinding()
{
interactionPath = select,
interactionProfileName = profile,
}
}
},
// Trigger Pressed
new ActionConfig()
{
name = "menu",
localizedName = "Menu",
type = ActionType.Binary,
usages = new List()
{
"MenuButton"
},
bindings = new List()
{
new ActionBinding()
{
interactionPath = menu,
interactionProfileName = profile,
}
}
},
// Device Pose
new ActionConfig()
{
name = "devicePose",
localizedName = "Device Pose",
type = ActionType.Pose,
usages = new List()
{
"Device"
},
bindings = new List()
{
new ActionBinding()
{
interactionPath = grip,
interactionProfileName = profile,
}
}
},
// Pointer Pose
new ActionConfig()
{
name = "pointer",
localizedName = "Pointer Pose",
type = ActionType.Pose,
usages = new List()
{
"Pointer"
},
bindings = new List()
{
new ActionBinding()
{
interactionPath = aim,
interactionProfileName = profile,
}
}
},
// Haptics
new ActionConfig()
{
name = "haptic",
localizedName = "Haptic Output",
type = ActionType.Vibrate,
usages = new List() { "Haptic" },
bindings = new List()
{
new ActionBinding()
{
interactionPath = haptic,
interactionProfileName = profile,
}
}
}
}
};
AddActionMap(actionMap);
}
}
}