Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Joystick.cs 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using UnityEngine.InputSystem.Controls;
  2. using UnityEngine.InputSystem.Layouts;
  3. using UnityEngine.InputSystem.LowLevel;
  4. using UnityEngine.InputSystem.Utilities;
  5. namespace UnityEngine.InputSystem.LowLevel
  6. {
  7. internal struct JoystickState : IInputStateTypeInfo
  8. {
  9. public static FourCC kFormat => new FourCC('J', 'O', 'Y');
  10. [InputControl(name = "trigger", displayName = "Trigger", layout = "Button", usages = new[] { "PrimaryTrigger", "PrimaryAction", "Submit" }, bit = (int)Button.Trigger)]
  11. public int buttons;
  12. [InputControl(displayName = "Stick", layout = "Stick", usage = "Primary2DMotion", processors = "stickDeadzone")]
  13. public Vector2 stick;
  14. public enum Button
  15. {
  16. // IMPORTANT: Order has to match what is expected by DpadControl.
  17. HatSwitchUp,
  18. HatSwitchDown,
  19. HatSwitchLeft,
  20. HatSwitchRight,
  21. Trigger
  22. }
  23. public FourCC format => kFormat;
  24. }
  25. }
  26. namespace UnityEngine.InputSystem
  27. {
  28. /// <summary>
  29. /// A joystick with an arbitrary number of buttons and axes.
  30. /// </summary>
  31. /// <remarks>
  32. /// Joysticks are somewhat hard to classify as there is little commonality other
  33. /// than that there is one main stick 2D control and at least one button. From the
  34. /// input system perspective, everything that is not a <see cref="Gamepad"/> and
  35. /// that has at least one <see cref="stick"/> and one <see cref="trigger"/> control
  36. /// is considered a candidate for being a joystick.
  37. ///
  38. /// Optionally, a joystick may also have the ability to <see cref="twist"/>, i.e.
  39. /// for the stick to rotate around its own axis, and at least one <see cref="hatswitch"/>.
  40. ///
  41. /// Note that devices based on Joystick may have many more controls. Joystick
  42. /// itself only defines a minimum required to separate joysticks as a concept
  43. /// from other types of devices.
  44. /// </remarks>
  45. [InputControlLayout(stateType = typeof(JoystickState), isGenericTypeOfDevice = true)]
  46. public class Joystick : InputDevice
  47. {
  48. /// <summary>
  49. /// The primary trigger button of the joystick.
  50. /// </summary>
  51. /// <value>Control representing the primary trigger button.</value>
  52. /// <remarks>
  53. /// This is the <see cref="ButtonControl"/> type control on the joystick
  54. /// that has the <see cref="CommonUsages.PrimaryTrigger"/> usage.
  55. /// </remarks>
  56. public ButtonControl trigger { get; protected set; }
  57. /// <summary>
  58. /// The 2D axis of the stick itself.
  59. /// </summary>
  60. /// <value>Control representing the main joystick axis.</value>
  61. /// <remarks>
  62. /// This is the <see cref="StickControl"/> type control on the joystick
  63. /// that has the <see cref="CommonUsages.Primary2DMotion"/> usage.
  64. /// </remarks>
  65. public StickControl stick { get; protected set; }
  66. /// <summary>
  67. /// An optional control representing the rotation of the stick around its
  68. /// own axis (i.e. side-to-side circular motion). If not supported, will be
  69. /// <c>null</c>.
  70. /// </summary>
  71. /// <value>Control representing the twist motion of the joystick.</value>
  72. /// <remarks>
  73. /// This is the <see cref="AxisControl"/> type control on the joystick
  74. /// that has the <see cref="CommonUsages.Twist"/> usage.
  75. /// </remarks>
  76. public AxisControl twist { get; protected set; }
  77. /// <summary>
  78. /// An optional control representing a four-way "hat switch" on the
  79. /// joystick. If not supported, will be <c>null</c>.
  80. /// </summary>
  81. /// <value>Control representing a hatswitch on the joystick.</value>
  82. /// <remarks>
  83. /// Hat switches are usually thumb-operated four-way switches that operate
  84. /// much like the "d-pad" on a gamepad (see <see cref="Gamepad.dpad"/>).
  85. /// If present, this is the <see cref="Vector2Control"/> type control on the
  86. /// joystick that has the <see cref="CommonUsages.Hatswitch"/> usage.
  87. /// </remarks>
  88. public Vector2Control hatswitch { get; protected set; }
  89. /// <summary>
  90. /// The joystick that was added or used last. Null if there is none.
  91. /// </summary>
  92. /// <value>Joystick that was added or used last.</value>
  93. /// <remarks>
  94. /// See <see cref="InputDevice.MakeCurrent"/> for details about when a device
  95. /// is made current.
  96. /// </remarks>
  97. /// <seealso cref="all"/>
  98. public static Joystick current { get; private set; }
  99. /// <summary>
  100. /// A list of joysticks currently connected to the system.
  101. /// </summary>
  102. /// <value>All currently connected joystick.</value>
  103. /// <remarks>
  104. /// Does not cause GC allocation.
  105. ///
  106. /// Do <em>not</em> hold on to the value returned by this getter but rather query it whenever
  107. /// you need it. Whenever the joystick setup changes, the value returned by this getter
  108. /// is invalidated.
  109. /// </remarks>
  110. /// <seealso cref="current"/>
  111. public new static ReadOnlyArray<Joystick> all => new ReadOnlyArray<Joystick>(s_Joysticks, 0, s_JoystickCount);
  112. /// <summary>
  113. /// Called when the joystick has been created but before it is added
  114. /// to the system.
  115. /// </summary>
  116. protected override void FinishSetup()
  117. {
  118. // Mandatory controls.
  119. trigger = GetChildControl<ButtonControl>("{PrimaryTrigger}");
  120. stick = GetChildControl<StickControl>("{Primary2DMotion}");
  121. // Optional controls.
  122. twist = TryGetChildControl<AxisControl>("{Twist}");
  123. hatswitch = TryGetChildControl<Vector2Control>("{Hatswitch}");
  124. base.FinishSetup();
  125. }
  126. /// <summary>
  127. /// Make the joystick the <see cref="current"/> one.
  128. /// </summary>
  129. /// <remarks>
  130. /// This is called automatically by the input system when a device
  131. /// receives input or is added to the system. See <see cref="InputDevice.MakeCurrent"/>
  132. /// for details.
  133. /// </remarks>
  134. public override void MakeCurrent()
  135. {
  136. base.MakeCurrent();
  137. current = this;
  138. }
  139. /// <summary>
  140. /// Called when the joystick is added to the system.
  141. /// </summary>
  142. protected override void OnAdded()
  143. {
  144. ArrayHelpers.AppendWithCapacity(ref s_Joysticks, ref s_JoystickCount, this);
  145. }
  146. /// <summary>
  147. /// Called when the joystick is removed from the system.
  148. /// </summary>
  149. protected override void OnRemoved()
  150. {
  151. base.OnRemoved();
  152. if (current == this)
  153. current = null;
  154. // Remove from `all`.
  155. var index = ArrayHelpers.IndexOfReference(s_Joysticks, this, s_JoystickCount);
  156. if (index != -1)
  157. ArrayHelpers.EraseAtWithCapacity(s_Joysticks, ref s_JoystickCount, index);
  158. else
  159. {
  160. Debug.Assert(false,
  161. $"Joystick {this} seems to not have been added but is being removed (joystick list: {string.Join(", ", all)})"); // Put in else to not allocate on normal path.
  162. }
  163. }
  164. private static int s_JoystickCount;
  165. private static Joystick[] s_Joysticks;
  166. }
  167. }