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 { /// /// Long gun input state descriptor. /// public struct LongGunInputState : IInputStateTypeInfo { public FourCC format => new FourCC('X', 'M', 'L', '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; /// /// 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; /// /// 保险按钮状态 /// [InputControl(name = "SecureKeyState", layout = "Integer")] public uint SecureKeyState; /// /// 0 = loose, 1 = release /// [InputControl(name = "ChamberSlide", layout = "Axis")] public float ChamberSlide; #endregion } [InputControlLayout(commonUsages = new[] { "LongGun" }, isGenericTypeOfDevice = false, displayName = "LongGun", stateType = typeof(LongGunInputState))] public class LongGunInputDevice : InputDevice, IInputUpdateCallbackReceiver { static LongGunInputDevice longGunInputDevice; /// /// Adds or gets long gun input devices /// /// #if UNITY_EDITOR [UnityEditor.MenuItem("Ximmerse/Create LongGun Device")] #endif public static LongGunInputDevice GetLongGunInputDevice() { if (longGunInputDevice != null) { return longGunInputDevice; } else { InputSystem.RegisterLayout(matches: new InputDeviceMatcher() .WithInterface("LongGunInputState")); LongGunInputDevice _gun = (LongGunInputDevice)InputSystem.AddDevice("LongGunInputDevice", "LongGun"); InputSystem.SetDeviceUsage(_gun, "LongGun"); InputSystem.EnableDevice(_gun); LongGunInputDevice.longGunInputDevice = _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 = "LongGunInputState/TrackingState")] public IntegerControl TrackingState { get; internal set; } [InputControl(name = "LongGunInputState/Position")] public Vector3Control Position { get; internal set; } [InputControl(name = "LongGunInputState/Rotation")] public QuaternionControl Rotation { get; internal set; } /// /// Power key /// [InputControl(name = "LongGunInputState/Power")] public ButtonControl Power { get; internal set; } /// /// Trigger key /// [InputControl(name = "LongGunInputState/Trigger")] public ButtonControl Trigger { get; internal set; } /// /// Trigger value /// [InputControl(name = "LongGunInputState/TriggerValue")] public AxisControl TriggerValue { get; internal set; } /// /// MagLoad key /// [InputControl(name = "LongGunInputState/MagLoad")] public ButtonControl MagLoad { get; internal set; } /// /// MagRelease key /// [InputControl(name = "LongGunInputState/SecureKeyState")] public IntegerControl SecureKeyState { 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"); Power = GetChildControl("Power"); Trigger = GetChildControl("Trigger"); TriggerValue = GetChildControl("TriggerValue"); MagLoad = GetChildControl("MagLoad"); SecureKeyState = GetChildControl("SecureKeyState"); ChamberSlide = GetChildControl("ChamberSlide"); } public void OnUpdate() { if (m_getter != null) { var _state = new LongGunInputState(); _state = m_getter(); InputSystem.QueueStateEvent(this, _state); } else { Debug.LogWarning("LongGun Input device: missing state getter !"); var _state = new LongGunInputState(); InputSystem.QueueStateEvent(this, _state); } } } }