LongGunInputDevice.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5. using UnityEngine.InputSystem.LowLevel;
  6. using UnityEngine.InputSystem.Utilities;
  7. using UnityEngine.InputSystem.Layouts;
  8. using UnityEngine.InputSystem.Controls;
  9. namespace Ximmerse.XR.InputSystems
  10. {
  11. /// <summary>
  12. /// Long gun input state descriptor.
  13. /// </summary>
  14. public struct LongGunInputState : IInputStateTypeInfo
  15. {
  16. public FourCC format => new FourCC('X', 'M', 'L', 'N');
  17. #region Properties
  18. /// <summary>
  19. /// 0 = none, 3 (1+2) = position + rotation
  20. /// </summary>
  21. [InputControl(name = "TrackingState", layout = "Integer")]
  22. public uint TrackingState;
  23. /// <summary>
  24. /// Palm position
  25. /// </summary>
  26. [InputControl(name = "Position", layout = "Vector3")]
  27. public Vector3 Position;
  28. /// <summary>
  29. /// 手掌沿手指前向的法线
  30. /// </summary>
  31. [InputControl(name = "Rotation", layout = "Quaternion")]
  32. public Quaternion Rotation;
  33. /// <summary>
  34. /// Power key
  35. /// </summary>
  36. [InputControl(name = "Power", layout = "Button")]
  37. public bool Power;
  38. /// <summary>
  39. /// Trigger key
  40. /// </summary>
  41. [InputControl(name = "Trigger", layout = "Button")]
  42. public bool Trigger;
  43. /// <summary>
  44. /// Trigger value
  45. /// </summary>
  46. [InputControl(name = "TriggerValue", layout = "Axis")]
  47. public float TriggerValue;
  48. /// <summary>
  49. /// MagLoad key
  50. /// </summary>
  51. [InputControl(name = "MagLoad", layout = "Button")]
  52. public bool MagLoad;
  53. /// <summary>
  54. /// 保险按钮状态
  55. /// </summary>
  56. [InputControl(name = "SecureKeyState", layout = "Integer")]
  57. public uint SecureKeyState;
  58. /// <summary>
  59. /// 0 = loose, 1 = release
  60. /// </summary>
  61. [InputControl(name = "ChamberSlide", layout = "Axis")]
  62. public float ChamberSlide;
  63. #endregion
  64. }
  65. [InputControlLayout(commonUsages = new[] { "LongGun" }, isGenericTypeOfDevice = false, displayName = "LongGun", stateType = typeof(LongGunInputState))]
  66. public class LongGunInputDevice : InputDevice, IInputUpdateCallbackReceiver
  67. {
  68. static LongGunInputDevice longGunInputDevice;
  69. /// <summary>
  70. /// Adds or gets long gun input devices
  71. /// </summary>
  72. /// <returns></returns>
  73. #if UNITY_EDITOR
  74. [UnityEditor.MenuItem("Ximmerse/Create LongGun Device")]
  75. #endif
  76. public static LongGunInputDevice GetLongGunInputDevice()
  77. {
  78. if (longGunInputDevice != null)
  79. {
  80. return longGunInputDevice;
  81. }
  82. else
  83. {
  84. InputSystem.RegisterLayout<LongGunInputDevice>(matches: new InputDeviceMatcher()
  85. .WithInterface("LongGunInputState"));
  86. LongGunInputDevice _gun = (LongGunInputDevice)InputSystem.AddDevice("LongGunInputDevice", "LongGun");
  87. InputSystem.SetDeviceUsage(_gun, "LongGun");
  88. InputSystem.EnableDevice(_gun);
  89. LongGunInputDevice.longGunInputDevice = _gun;
  90. return _gun;
  91. }
  92. }
  93. System.Func<LongGunInputState> m_getter = null;
  94. public void RegisterStateGetter(System.Func<LongGunInputState> getter)
  95. {
  96. m_getter = getter;
  97. }
  98. /// <summary>
  99. /// Tracking state, the value mapped to UnityEngine.XR.InputTrackingState:
  100. /// None = 0, Position = 1, Rotation = 2,Velocity = 4,AngularVelocity = 8,
  101. /// Acceleration = 16,AngularAcceleration = 32, All = 63
  102. /// </summary>
  103. [InputControl(name = "LongGunInputState/TrackingState")]
  104. public IntegerControl TrackingState
  105. {
  106. get; internal set;
  107. }
  108. [InputControl(name = "LongGunInputState/Position")]
  109. public Vector3Control Position
  110. {
  111. get; internal set;
  112. }
  113. [InputControl(name = "LongGunInputState/Rotation")]
  114. public QuaternionControl Rotation
  115. {
  116. get; internal set;
  117. }
  118. /// <summary>
  119. /// Power key
  120. /// </summary>
  121. [InputControl(name = "LongGunInputState/Power")]
  122. public ButtonControl Power
  123. {
  124. get; internal set;
  125. }
  126. /// <summary>
  127. /// Trigger key
  128. /// </summary>
  129. [InputControl(name = "LongGunInputState/Trigger")]
  130. public ButtonControl Trigger
  131. {
  132. get; internal set;
  133. }
  134. /// <summary>
  135. /// Trigger value
  136. /// </summary>
  137. [InputControl(name = "LongGunInputState/TriggerValue")]
  138. public AxisControl TriggerValue
  139. {
  140. get; internal set;
  141. }
  142. /// <summary>
  143. /// MagLoad key
  144. /// </summary>
  145. [InputControl(name = "LongGunInputState/MagLoad")]
  146. public ButtonControl MagLoad
  147. {
  148. get; internal set;
  149. }
  150. /// <summary>
  151. /// MagRelease key
  152. /// </summary>
  153. [InputControl(name = "LongGunInputState/SecureKeyState")]
  154. public IntegerControl SecureKeyState
  155. {
  156. get; internal set;
  157. }
  158. /// <summary>
  159. /// ChamberSlide value
  160. /// </summary>
  161. [InputControl(name = "HandGunInputState/ChamberSlide")]
  162. public AxisControl ChamberSlide
  163. {
  164. get; internal set;
  165. }
  166. protected override void FinishSetup()
  167. {
  168. base.FinishSetup();
  169. TrackingState = GetChildControl<IntegerControl>("TrackingState");
  170. Position = GetChildControl<Vector3Control>("Position");
  171. Rotation = GetChildControl<QuaternionControl>("Rotation");
  172. Power = GetChildControl<ButtonControl>("Power");
  173. Trigger = GetChildControl<ButtonControl>("Trigger");
  174. TriggerValue = GetChildControl<AxisControl>("TriggerValue");
  175. MagLoad = GetChildControl<ButtonControl>("MagLoad");
  176. SecureKeyState = GetChildControl<IntegerControl>("SecureKeyState");
  177. ChamberSlide = GetChildControl<AxisControl>("ChamberSlide");
  178. }
  179. public void OnUpdate()
  180. {
  181. if (m_getter != null)
  182. {
  183. var _state = new LongGunInputState();
  184. _state = m_getter();
  185. InputSystem.QueueStateEvent(this, _state);
  186. }
  187. else
  188. {
  189. Debug.LogWarning("LongGun Input device: missing state getter !");
  190. var _state = new LongGunInputState();
  191. InputSystem.QueueStateEvent(this, _state);
  192. }
  193. }
  194. }
  195. }