설명 없음
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StickControl.cs 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. public class StickControl : Vector2Control
  49. {
  50. ////REVIEW: should X and Y have "Horizontal" and "Vertical" as long display names and "X" and "Y" as short names?
  51. // Buttons for each of the directions. Allows the stick to function as a dpad.
  52. // Note that these controls are marked as synthetic as there isn't real buttons for the half-axes
  53. // on the device. This aids in interactive picking by making sure that if we have to decide between,
  54. // say, leftStick/x and leftStick/left, leftStick/x wins out.
  55. ////REVIEW: up/down/left/right should probably prohibit being written to
  56. ////REVIEW: Should up/down/left/control actually be their own control types that *read* the values
  57. //// from X and Y instead of sharing their state? The current setup easily leads to various
  58. //// problems because more than just the state block is needed to read the value of a control
  59. //// from state correctly.
  60. /// <summary>
  61. /// A synthetic button representing the upper half of the stick's Y axis, i.e. the 0 to 1 range.
  62. /// </summary>
  63. /// <value>Control representing the stick's upper half Y axis.</value>
  64. /// <remarks>
  65. /// The control is marked as <see cref="InputControl.synthetic"/>.
  66. /// </remarks>
  67. [InputControl(useStateFrom = "y", processors = "axisDeadzone", parameters = "clamp=2,clampMin=0,clampMax=1", synthetic = true, displayName = "Up")]
  68. // Set min&max on XY axes. We do this here as the documentation generator will not be happy
  69. // if we place this above the doc comment.
  70. // Also puts AxisDeadzones on the axes.
  71. [InputControl(name = "x", minValue = -1f, maxValue = 1f, layout = "Axis", processors = "axisDeadzone")]
  72. [InputControl(name = "y", minValue = -1f, maxValue = 1f, layout = "Axis", processors = "axisDeadzone")]
  73. public ButtonControl up { get; set; }
  74. /// <summary>
  75. /// A synthetic button representing the lower half of the stick's Y axis, i.e. the -1 to 0 range (inverted).
  76. /// </summary>
  77. /// <value>Control representing the stick's lower half Y axis.</value>
  78. /// <remarks>
  79. /// The control is marked as <see cref="InputControl.synthetic"/>.
  80. /// </remarks>
  81. [InputControl(useStateFrom = "y", processors = "axisDeadzone", parameters = "clamp=2,clampMin=-1,clampMax=0,invert", synthetic = true, displayName = "Down")]
  82. public ButtonControl down { get; set; }
  83. /// <summary>
  84. /// A synthetic button representing the left half of the stick's X axis, i.e. the -1 to 0 range (inverted).
  85. /// </summary>
  86. /// <value>Control representing the stick's left half X axis.</value>
  87. /// <remarks>
  88. /// The control is marked as <see cref="InputControl.synthetic"/>.
  89. /// </remarks>
  90. [InputControl(useStateFrom = "x", processors = "axisDeadzone", parameters = "clamp=2,clampMin=-1,clampMax=0,invert", synthetic = true, displayName = "Left")]
  91. public ButtonControl left { get; set; }
  92. /// <summary>
  93. /// A synthetic button representing the right half of the stick's X axis, i.e. the 0 to 1 range.
  94. /// </summary>
  95. /// <value>Control representing the stick's right half X axis.</value>
  96. /// <remarks>
  97. /// The control is marked as <see cref="InputControl.synthetic"/>.
  98. /// </remarks>
  99. [InputControl(useStateFrom = "x", processors = "axisDeadzone", parameters = "clamp=2,clampMin=0,clampMax=1", synthetic = true, displayName = "Right")]
  100. public ButtonControl right { get; set; }
  101. protected override void FinishSetup()
  102. {
  103. base.FinishSetup();
  104. up = GetChildControl<ButtonControl>("up");
  105. down = GetChildControl<ButtonControl>("down");
  106. left = GetChildControl<ButtonControl>("left");
  107. right = GetChildControl<ButtonControl>("right");
  108. }
  109. }
  110. }