StickControl.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using UnityEngine.InputSystem.Layouts;
  2. using UnityEngine.InputSystem.Processors;
  3. namespace UnityEngine.InputSystem.Controls
  4. {
  5. /// <summary>
  6. /// A two-axis thumbstick control that can act as both a vector and a four-way dpad.
  7. /// </summary>
  8. /// <remarks>
  9. /// Stick controls are used to represent the thumbsticks on gamepads (see <see cref="Gamepad.leftStick"/>
  10. /// and <see cref="Gamepad.rightStick"/>) as well as the main stick control of joysticks (see
  11. /// <see cref="Joystick.stick"/>).
  12. ///
  13. /// Essentially, a stick is an extended <c>Vector2</c> control that can function either
  14. /// as a combined 2D vector, as independent vertical and horizontal axes, or as four
  15. /// individual, directional buttons. The following example demonstrates this based on the
  16. /// gamepad's left stick.
  17. ///
  18. /// <example>
  19. /// <code>
  20. /// // Read stick as a combined 2D vector.
  21. /// Gamepad.current.leftStick.ReadValue();
  22. ///
  23. /// // Read X and Y axis of stick individually.
  24. /// Gamepad.current.leftStick.x.ReadValue();
  25. /// Gamepad.current.leftStick.y.ReadValue();
  26. ///
  27. /// // Read the stick as four individual directional buttons.
  28. /// Gamepad.current.leftStick.up.ReadValue();
  29. /// Gamepad.current.leftStick.down.ReadValue();
  30. /// Gamepad.current.leftStick.left.ReadValue();
  31. /// Gamepad.current.leftStick.right.ReadValue();
  32. /// </code>
  33. /// </example>
  34. ///
  35. /// In terms of memory, a stick controls is still just from one value for the X axis
  36. /// and one value for the Y axis.
  37. ///
  38. /// Unlike dpads (see <see cref="DpadControl"/>), sticks will usually have deadzone processors
  39. /// (see <see cref="StickDeadzoneProcessor"/>) applied to them to get rid of noise around the
  40. /// resting point of the stick. The X and Y axis also have deadzones applied to them by
  41. /// default (<see cref="AxisDeadzoneProcessor"/>). Note, however, that the deadzoning of
  42. /// individual axes is different from the deadzoning applied to the stick as a whole and
  43. /// thus does not have to result in exactly the same values. Deadzoning of individual axes
  44. /// is linear (i.e. the result is simply clamped and normalized back into [0..1] range) whereas
  45. /// the deadzoning of sticks is radial (i.e. the length of the vector is taken into account
  46. /// which means that <em>both</em> the X and Y axis contribute).
  47. /// </remarks>
  48. [Scripting.Preserve]
  49. public class StickControl : Vector2Control
  50. {
  51. ////REVIEW: should X and Y have "Horizontal" and "Vertical" as long display names and "X" and "Y" as short names?
  52. // Buttons for each of the directions. Allows the stick to function as a dpad.
  53. // Note that these controls are marked as synthetic as there isn't real buttons for the half-axes
  54. // on the device. This aids in interactive picking by making sure that if we have to decide between,
  55. // say, leftStick/x and leftStick/left, leftStick/x wins out.
  56. ////REVIEW: up/down/left/right should probably prohibit being written to
  57. ////REVIEW: Should up/down/left/control actually be their own control types that *read* the values
  58. //// from X and Y instead of sharing their state? The current setup easily leads to various
  59. //// problems because more than just the state block is needed to read the value of a control
  60. //// from state correctly.
  61. /// <summary>
  62. /// A synthetic button representing the upper half of the stick's Y axis.
  63. /// </summary>
  64. /// <value>Control representing the stick's upper half Y axis.</value>
  65. /// <remarks>
  66. ///
  67. /// The control is marked as <see cref="InputControl.synthetic"/>.
  68. /// </remarks>
  69. [InputControl(useStateFrom = "y", processors = "axisDeadzone", parameters = "clamp=2,clampMin=0,clampMax=1", synthetic = true, displayName = "Up")]
  70. // Set min&max on XY axes. We do this here as the documentation generator will not be happy
  71. // if we place this above the doc comment.
  72. // Also puts AxisDeadzones on the axes.
  73. [InputControl(name = "x", minValue = -1f, maxValue = 1f, layout = "Axis", processors = "axisDeadzone")]
  74. [InputControl(name = "y", minValue = -1f, maxValue = 1f, layout = "Axis", processors = "axisDeadzone")]
  75. [Scripting.Preserve]
  76. public ButtonControl up { get; private set; }
  77. [InputControl(useStateFrom = "y", processors = "axisDeadzone", parameters = "clamp=2,clampMin=-1,clampMax=0,invert", synthetic = true, displayName = "Down")]
  78. [Scripting.Preserve]
  79. public ButtonControl down { get; private set; }
  80. [InputControl(useStateFrom = "x", processors = "axisDeadzone", parameters = "clamp=2,clampMin=-1,clampMax=0,invert", synthetic = true, displayName = "Left")]
  81. [Scripting.Preserve]
  82. public ButtonControl left { get; private set; }
  83. [InputControl(useStateFrom = "x", processors = "axisDeadzone", parameters = "clamp=2,clampMin=0,clampMax=1", synthetic = true, displayName = "Right")]
  84. [Scripting.Preserve]
  85. public ButtonControl right { get; private set; }
  86. protected override void FinishSetup()
  87. {
  88. base.FinishSetup();
  89. up = GetChildControl<ButtonControl>("up");
  90. down = GetChildControl<ButtonControl>("down");
  91. left = GetChildControl<ButtonControl>("left");
  92. right = GetChildControl<ButtonControl>("right");
  93. }
  94. }
  95. }