using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.InputSystem.LowLevel; using UnityEngine.InputSystem.Utilities; using UnityEngine.InputSystem.Layouts; using UnityEngine.InputSystem.Controls; namespace Ximmerse.XR.InputSystems { /// /// Short gun input state descriptor. /// public struct HandGunInputState : IInputStateTypeInfo { public FourCC format => new FourCC('X', 'M', 'G', 'N'); #region Properties /// /// 0 = none, 3 (1+2) = position + rotation /// [InputControl(name = "TrackingState", layout = "Integer")] public uint TrackingState; /// /// Palm position /// [InputControl(name = "Position", layout = "Vector3")] public Vector3 Position; /// /// 手掌沿手指前向的法线 /// [InputControl(name = "Rotation", layout = "Quaternion")] public Quaternion Rotation; /// /// Function key /// [InputControl(name = "Function", layout = "Button")] public bool Function; /// /// Power key /// [InputControl(name = "Power", layout = "Button")] public bool Power; /// /// Trigger key /// [InputControl(name = "Trigger", layout = "Button")] public bool Trigger; /// /// Trigger value /// [InputControl(name = "TriggerValue", layout = "Axis")] public float TriggerValue; /// /// MagLoad key /// [InputControl(name = "MagLoad", layout = "Button")] public bool MagLoad; /// /// MagLoad key /// [InputControl(name = "MagRelease", layout = "Button")] public bool MagRelease; /// /// Grip key /// [InputControl(name = "Grip", layout = "Button")] public bool Grip; /// /// TriggerFingerDetection key /// [InputControl(name = "TriggerFingerDetection", layout = "Button")] public bool TriggerFingerDetection; /// /// 0 = loose, 1 = release /// [InputControl(name = "ChamberSlide", layout = "Axis")] public float ChamberSlide; #endregion } [InputControlLayout(commonUsages = new[] { "HandGun" }, isGenericTypeOfDevice = false, displayName = "HandGun", stateType = typeof(HandGunInputState))] public class HandGunInputDevice : InputDevice, IInputUpdateCallbackReceiver { static HandGunInputDevice handGunInputDevice; /// /// Adds or gets hand gun input devices /// /// #if UNITY_EDITOR [UnityEditor.MenuItem("Ximmerse/Create HandGun Device")] #endif public static HandGunInputDevice GetHandGunInputDevice() { if (handGunInputDevice != null) { return handGunInputDevice; } else { InputSystem.RegisterLayout(matches: new InputDeviceMatcher() .WithInterface("HandGunInputState")); HandGunInputDevice _gun = (HandGunInputDevice)InputSystem.AddDevice("HandGunInputDevice", "HandGun"); InputSystem.SetDeviceUsage(_gun, "HandGun"); InputSystem.EnableDevice(_gun); HandGunInputDevice.handGunInputDevice = _gun; return _gun; } } System.Func m_getter = null; public void RegisterStateGetter(System.Func getter) { m_getter = getter; } /// /// Tracking state, the value mapped to UnityEngine.XR.InputTrackingState: /// None = 0, Position = 1, Rotation = 2,Velocity = 4,AngularVelocity = 8, /// Acceleration = 16,AngularAcceleration = 32, All = 63 /// [InputControl(name = "HandGunInputState/TrackingState")] public IntegerControl TrackingState { get; internal set; } [InputControl(name = "HandGunInputState/Position")] public Vector3Control Position { get; internal set; } [InputControl(name = "HandGunInputState/Rotation")] public QuaternionControl Rotation { get; internal set; } /// /// Function key /// [InputControl(name = "HandGunInputState/Function")] public ButtonControl Function { get; internal set; } /// /// Power key /// [InputControl(name = "HandGunInputState/Power")] public ButtonControl Power { get; internal set; } /// /// Trigger key /// [InputControl(name = "HandGunInputState/Trigger")] public ButtonControl Trigger { get; internal set; } /// /// Trigger value /// [InputControl(name = "HandGunInputState/TriggerValue")] public AxisControl TriggerValue { get; internal set; } /// /// MagLoad key /// [InputControl(name = "HandGunInputState/MagLoad")] public ButtonControl MagLoad { get; internal set; } /// /// MagRelease key /// [InputControl(name = "HandGunInputState/MagRelease")] public ButtonControl MagRelease { get; internal set; } /// /// Grip key /// [InputControl(name = "HandGunInputState/Grip")] public ButtonControl Grip { get; internal set; } /// /// TriggerFingerDetection key /// [InputControl(name = "HandGunInputState/TriggerFingerDetection")] public ButtonControl TriggerFingerDetection { get; internal set; } /// /// ChamberSlide value /// [InputControl(name = "HandGunInputState/ChamberSlide")] public AxisControl ChamberSlide { get; internal set; } protected override void FinishSetup() { base.FinishSetup(); TrackingState = GetChildControl("TrackingState"); Position = GetChildControl("Position"); Rotation = GetChildControl("Rotation"); Function = GetChildControl("Function"); Power = GetChildControl("Power"); Trigger = GetChildControl("Trigger"); TriggerValue = GetChildControl("TriggerValue"); MagLoad = GetChildControl("MagLoad"); MagRelease = GetChildControl("MagRelease"); Grip = GetChildControl("Grip"); TriggerFingerDetection = GetChildControl("TriggerFingerDetection"); ChamberSlide = GetChildControl("ChamberSlide"); } public void OnUpdate() { if (m_getter != null) { var _state = new HandGunInputState(); _state = m_getter(); InputSystem.QueueStateEvent(this, _state); } else { Debug.LogWarning("HandGun Input device: missing state getter !"); var _state = new HandGunInputState(); InputSystem.QueueStateEvent(this, _state); } } } }