No Description
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.

ButtonControl.cs 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System.Runtime.CompilerServices;
  2. using UnityEngine.InputSystem.LowLevel;
  3. using UnityEngine.Scripting;
  4. ////REVIEW: introduce separate base class for ButtonControl and AxisControl instead of deriving ButtonControl from AxisControl?
  5. namespace UnityEngine.InputSystem.Controls
  6. {
  7. /// <summary>
  8. /// An axis that has a trigger point beyond which it is considered to be pressed.
  9. /// </summary>
  10. /// <remarks>
  11. /// By default stored as a single bit. In that format, buttons will only yield 0
  12. /// and 1 as values. However, buttons return are <see cref="AxisControl"/>s and
  13. /// yield full floating-point values and may thus have a range of values. See
  14. /// <see cref="pressPoint"/> for how button presses on such buttons are handled.
  15. /// </remarks>
  16. public class ButtonControl : AxisControl
  17. {
  18. ////REVIEW: are per-control press points really necessary? can we just drop them?
  19. /// <summary>
  20. /// The minimum value the button has to reach for it to be considered pressed.
  21. /// </summary>
  22. /// <value>Button press threshold.</value>
  23. /// <remarks>
  24. /// The button is considered pressed, if it has a value equal to or greater than
  25. /// this value.
  26. ///
  27. /// By default, this property is set to -1. If the value of the property is negative,
  28. /// <see cref="InputSettings.defaultButtonPressPoint"/> is used.
  29. ///
  30. /// The value can be configured as a parameter in a layout.
  31. ///
  32. /// <example>
  33. /// <code>
  34. /// public class MyDevice : InputDevice
  35. /// {
  36. /// [InputControl(parameters = "pressPoint=0.234")]
  37. /// public ButtonControl button { get; private set; }
  38. ///
  39. /// //...
  40. /// }
  41. /// </code>
  42. /// </example>
  43. /// </remarks>
  44. /// <seealso cref="InputSettings.defaultButtonPressPoint"/>
  45. /// <seealso cref="pressPointOrDefault"/>
  46. /// <seealso cref="isPressed"/>
  47. public float pressPoint = -1;
  48. /// <summary>
  49. /// Return <see cref="pressPoint"/> if set, otherwise return <see cref="InputSettings.defaultButtonPressPoint"/>.
  50. /// </summary>
  51. /// <value>Effective value to use for press point thresholds.</value>
  52. /// <seealso cref="InputSettings.defaultButtonPressPoint"/>
  53. public float pressPointOrDefault => pressPoint > 0 ? pressPoint : s_GlobalDefaultButtonPressPoint;
  54. /// <summary>
  55. /// Default-initialize the control.
  56. /// </summary>
  57. /// <remarks>
  58. /// The default format for the control is <see cref="InputStateBlock.FormatBit"/>.
  59. /// The control's minimum value is set to 0 and the maximum value to 1.
  60. /// </remarks>
  61. public ButtonControl()
  62. {
  63. m_StateBlock.format = InputStateBlock.FormatBit;
  64. m_MinValue = 0f;
  65. m_MaxValue = 1f;
  66. }
  67. /// <summary>
  68. /// Whether the given value would be considered pressed for this button.
  69. /// </summary>
  70. /// <param name="value">Value for the button.</param>
  71. /// <returns>True if <paramref name="value"/> crosses the threshold to be considered pressed.</returns>
  72. /// <seealso cref="pressPoint"/>
  73. /// <seealso cref="InputSettings.defaultButtonPressPoint"/>
  74. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  75. public new bool IsValueConsideredPressed(float value)
  76. {
  77. return value >= pressPointOrDefault;
  78. }
  79. /// <summary>
  80. /// Whether the button is currently pressed.
  81. /// </summary>
  82. /// <value>True if button is currently pressed.</value>
  83. /// <remarks>
  84. /// A button is considered press if it's value is equal to or greater
  85. /// than its button press threshold (<see cref="pressPointOrDefault"/>).
  86. /// </remarks>
  87. /// <seealso cref="InputSettings.defaultButtonPressPoint"/>
  88. /// <seealso cref="pressPoint"/>
  89. /// <seealso cref="InputSystem.onAnyButtonPress"/>
  90. public bool isPressed => IsValueConsideredPressed(value);
  91. /// <summary>
  92. /// Whether the press started this frame.
  93. /// </summary>
  94. /// <value>True if the current press of the button started this frame.</value>
  95. /// <remarks>
  96. /// <example>
  97. /// <code>
  98. /// // An example showing the use of this property on a gamepad button and a keyboard key.
  99. ///
  100. /// using UnityEngine;
  101. /// using UnityEngine.InputSystem;
  102. ///
  103. /// public class ExampleScript : MonoBehaviour
  104. /// {
  105. /// void Update()
  106. /// {
  107. /// bool buttonPressed = Gamepad.current.aButton.wasPressedThisFrame;
  108. /// bool spaceKeyPressed = Keyboard.current.spaceKey.wasPressedThisFrame;
  109. /// }
  110. /// }
  111. /// </code>
  112. /// </example>
  113. /// </remarks>
  114. public bool wasPressedThisFrame => device.wasUpdatedThisFrame && IsValueConsideredPressed(value) && !IsValueConsideredPressed(ReadValueFromPreviousFrame());
  115. public bool wasReleasedThisFrame => device.wasUpdatedThisFrame && !IsValueConsideredPressed(value) && IsValueConsideredPressed(ReadValueFromPreviousFrame());
  116. // We make the current global default button press point available as a static so that we don't have to
  117. // constantly make the hop from InputSystem.settings -> InputManager.m_Settings -> defaultButtonPressPoint.
  118. internal static float s_GlobalDefaultButtonPressPoint;
  119. internal static float s_GlobalDefaultButtonReleaseThreshold;
  120. // We clamp button press points to this value as allowing 0 as the press point causes all buttons
  121. // to implicitly be pressed all the time. Not useful.
  122. internal const float kMinButtonPressPoint = 0.0001f;
  123. }
  124. }