HandGunInputDevice.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. /// Short gun input state descriptor.
  13. /// </summary>
  14. public struct HandGunInputState : IInputStateTypeInfo
  15. {
  16. public FourCC format => new FourCC('X', 'M', 'G', '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. /// Function key
  35. /// </summary>
  36. [InputControl(name = "Function", layout = "Button")]
  37. public bool Function;
  38. /// <summary>
  39. /// Power key
  40. /// </summary>
  41. [InputControl(name = "Power", layout = "Button")]
  42. public bool Power;
  43. /// <summary>
  44. /// Trigger key
  45. /// </summary>
  46. [InputControl(name = "Trigger", layout = "Button")]
  47. public bool Trigger;
  48. /// <summary>
  49. /// Trigger value
  50. /// </summary>
  51. [InputControl(name = "TriggerValue", layout = "Axis")]
  52. public float TriggerValue;
  53. /// <summary>
  54. /// MagLoad key
  55. /// </summary>
  56. [InputControl(name = "MagLoad", layout = "Button")]
  57. public bool MagLoad;
  58. /// <summary>
  59. /// MagLoad key
  60. /// </summary>
  61. [InputControl(name = "MagRelease", layout = "Button")]
  62. public bool MagRelease;
  63. /// <summary>
  64. /// Grip key
  65. /// </summary>
  66. [InputControl(name = "Grip", layout = "Button")]
  67. public bool Grip;
  68. /// <summary>
  69. /// TriggerFingerDetection key
  70. /// </summary>
  71. [InputControl(name = "TriggerFingerDetection", layout = "Button")]
  72. public bool TriggerFingerDetection;
  73. /// <summary>
  74. /// 0 = loose, 1 = release
  75. /// </summary>
  76. [InputControl(name = "ChamberSlide", layout = "Axis")]
  77. public float ChamberSlide;
  78. #endregion
  79. }
  80. [InputControlLayout(commonUsages = new[] { "HandGun" }, isGenericTypeOfDevice = false, displayName = "HandGun", stateType = typeof(HandGunInputState))]
  81. public class HandGunInputDevice : InputDevice, IInputUpdateCallbackReceiver
  82. {
  83. static HandGunInputDevice handGunInputDevice;
  84. /// <summary>
  85. /// Adds or gets hand gun input devices
  86. /// </summary>
  87. /// <returns></returns>
  88. #if UNITY_EDITOR
  89. [UnityEditor.MenuItem("Ximmerse/Create HandGun Device")]
  90. #endif
  91. public static HandGunInputDevice GetHandGunInputDevice()
  92. {
  93. if (handGunInputDevice != null)
  94. {
  95. return handGunInputDevice;
  96. }
  97. else
  98. {
  99. InputSystem.RegisterLayout<HandGunInputDevice>(matches: new InputDeviceMatcher()
  100. .WithInterface("HandGunInputState"));
  101. HandGunInputDevice _gun = (HandGunInputDevice)InputSystem.AddDevice("HandGunInputDevice", "HandGun");
  102. InputSystem.SetDeviceUsage(_gun, "HandGun");
  103. InputSystem.EnableDevice(_gun);
  104. HandGunInputDevice.handGunInputDevice = _gun;
  105. return _gun;
  106. }
  107. }
  108. System.Func<HandGunInputState> m_getter = null;
  109. public void RegisterStateGetter(System.Func<HandGunInputState> getter)
  110. {
  111. m_getter = getter;
  112. }
  113. /// <summary>
  114. /// Tracking state, the value mapped to UnityEngine.XR.InputTrackingState:
  115. /// None = 0, Position = 1, Rotation = 2,Velocity = 4,AngularVelocity = 8,
  116. /// Acceleration = 16,AngularAcceleration = 32, All = 63
  117. /// </summary>
  118. [InputControl(name = "HandGunInputState/TrackingState")]
  119. public IntegerControl TrackingState
  120. {
  121. get; internal set;
  122. }
  123. [InputControl(name = "HandGunInputState/Position")]
  124. public Vector3Control Position
  125. {
  126. get; internal set;
  127. }
  128. [InputControl(name = "HandGunInputState/Rotation")]
  129. public QuaternionControl Rotation
  130. {
  131. get; internal set;
  132. }
  133. /// <summary>
  134. /// Function key
  135. /// </summary>
  136. [InputControl(name = "HandGunInputState/Function")]
  137. public ButtonControl Function
  138. {
  139. get; internal set;
  140. }
  141. /// <summary>
  142. /// Power key
  143. /// </summary>
  144. [InputControl(name = "HandGunInputState/Power")]
  145. public ButtonControl Power
  146. {
  147. get; internal set;
  148. }
  149. /// <summary>
  150. /// Trigger key
  151. /// </summary>
  152. [InputControl(name = "HandGunInputState/Trigger")]
  153. public ButtonControl Trigger
  154. {
  155. get; internal set;
  156. }
  157. /// <summary>
  158. /// Trigger value
  159. /// </summary>
  160. [InputControl(name = "HandGunInputState/TriggerValue")]
  161. public AxisControl TriggerValue
  162. {
  163. get; internal set;
  164. }
  165. /// <summary>
  166. /// MagLoad key
  167. /// </summary>
  168. [InputControl(name = "HandGunInputState/MagLoad")]
  169. public ButtonControl MagLoad
  170. {
  171. get; internal set;
  172. }
  173. /// <summary>
  174. /// MagRelease key
  175. /// </summary>
  176. [InputControl(name = "HandGunInputState/MagRelease")]
  177. public ButtonControl MagRelease
  178. {
  179. get; internal set;
  180. }
  181. /// <summary>
  182. /// Grip key
  183. /// </summary>
  184. [InputControl(name = "HandGunInputState/Grip")]
  185. public ButtonControl Grip
  186. {
  187. get; internal set;
  188. }
  189. /// <summary>
  190. /// TriggerFingerDetection key
  191. /// </summary>
  192. [InputControl(name = "HandGunInputState/TriggerFingerDetection")]
  193. public ButtonControl TriggerFingerDetection
  194. {
  195. get; internal set;
  196. }
  197. /// <summary>
  198. /// ChamberSlide value
  199. /// </summary>
  200. [InputControl(name = "HandGunInputState/ChamberSlide")]
  201. public AxisControl ChamberSlide
  202. {
  203. get; internal set;
  204. }
  205. protected override void FinishSetup()
  206. {
  207. base.FinishSetup();
  208. TrackingState = GetChildControl<IntegerControl>("TrackingState");
  209. Position = GetChildControl<Vector3Control>("Position");
  210. Rotation = GetChildControl<QuaternionControl>("Rotation");
  211. Function = GetChildControl<ButtonControl>("Function");
  212. Power = GetChildControl<ButtonControl>("Power");
  213. Trigger = GetChildControl<ButtonControl>("Trigger");
  214. TriggerValue = GetChildControl<AxisControl>("TriggerValue");
  215. MagLoad = GetChildControl<ButtonControl>("MagLoad");
  216. MagRelease = GetChildControl<ButtonControl>("MagRelease");
  217. Grip = GetChildControl<ButtonControl>("Grip");
  218. TriggerFingerDetection = GetChildControl<ButtonControl>("TriggerFingerDetection");
  219. ChamberSlide = GetChildControl<AxisControl>("ChamberSlide");
  220. }
  221. public void OnUpdate()
  222. {
  223. if (m_getter != null)
  224. {
  225. var _state = new HandGunInputState();
  226. _state = m_getter();
  227. InputSystem.QueueStateEvent(this, _state);
  228. }
  229. else
  230. {
  231. Debug.LogWarning("HandGun Input device: missing state getter !");
  232. var _state = new HandGunInputState();
  233. InputSystem.QueueStateEvent(this, _state);
  234. }
  235. }
  236. }
  237. }