ButtonControl.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using UnityEngine.InputSystem.LowLevel;
  2. using UnityEngine.Scripting;
  3. ////REVIEW: introduce separate base class for ButtonControl and AxisControl instead of deriving ButtonControl from AxisControl?
  4. namespace UnityEngine.InputSystem.Controls
  5. {
  6. /// <summary>
  7. /// An axis that has a trigger point beyond which it is considered to be pressed.
  8. /// </summary>
  9. /// <remarks>
  10. /// By default stored as a single bit. In that format, buttons will only yield 0
  11. /// and 1 as values. However, buttons return are <see cref="AxisControl"/>s and
  12. /// yield full floating-point values and may thus have a range of values. See
  13. /// <see cref="pressPoint"/> for how button presses on such buttons are handled.
  14. /// </remarks>
  15. [Preserve]
  16. public class ButtonControl : AxisControl
  17. {
  18. /// <summary>
  19. /// The minimum value the button has to reach for it to be considered pressed.
  20. /// </summary>
  21. /// <value>Button press threshold.</value>
  22. /// <remarks>
  23. /// The button is considered pressed, if it has a value equal to or greater than
  24. /// this value.
  25. ///
  26. /// By default, this property is set to -1. If the value of the property is negative,
  27. /// <see cref="InputSettings.defaultButtonPressPoint"/> is used.
  28. ///
  29. /// The value can be configured as a parameter in a layout.
  30. ///
  31. /// <example>
  32. /// <code>
  33. /// public class MyDevice : InputDevice
  34. /// {
  35. /// [InputControl(parameters = "pressPoint=0.234")]
  36. /// public ButtonControl button { get; private set; }
  37. ///
  38. /// //...
  39. /// }
  40. /// </code>
  41. /// </example>
  42. /// </remarks>
  43. /// <seealso cref="InputSettings.defaultButtonPressPoint"/>
  44. /// <seealso cref="pressPointOrDefault"/>
  45. /// <seealso cref="isPressed"/>
  46. public float pressPoint = -1;
  47. /// <summary>
  48. /// Return <see cref="pressPoint"/> if set, otherwise return <see cref="InputSettings.defaultButtonPressPoint"/>.
  49. /// </summary>
  50. /// <value>Effective value to use for press point thresholds.</value>
  51. /// <seealso cref="InputSettings.defaultButtonPressPoint"/>
  52. public float pressPointOrDefault => pressPoint >= 0 ? pressPoint : s_GlobalDefaultButtonPressPoint;
  53. /// <summary>
  54. /// Default-initialize the control.
  55. /// </summary>
  56. /// <remarks>
  57. /// The default format for the control is <see cref="InputStateBlock.FormatBit"/>.
  58. /// The control's minimum value is set to 0 and the maximum value to 1.
  59. /// </remarks>
  60. public ButtonControl()
  61. {
  62. m_StateBlock.format = InputStateBlock.FormatBit;
  63. m_MinValue = 0f;
  64. m_MaxValue = 1f;
  65. }
  66. /// <summary>
  67. /// Whether the given value would be considered pressed for this button.
  68. /// </summary>
  69. /// <param name="value">Value for the button.</param>
  70. /// <returns>True if <paramref name="value"/> crosses the threshold to be considered pressed.</returns>
  71. /// <seealso cref="pressPoint"/>
  72. /// <seealso cref="InputSettings.defaultButtonPressPoint"/>
  73. public bool IsValueConsideredPressed(float value)
  74. {
  75. return value >= pressPointOrDefault;
  76. }
  77. /// <summary>
  78. /// Whether the button is currently pressed.
  79. /// </summary>
  80. /// <value>True if button is currently pressed.</value>
  81. /// <remarks>
  82. /// A button is considered press if it's value is equal to or greater
  83. /// than its button press threshold (<see cref="pressPointOrDefault"/>).
  84. /// </remarks>
  85. /// <seealso cref="InputSettings.defaultButtonPressPoint"/>
  86. /// <seealso cref="pressPoint"/>
  87. public bool isPressed => IsValueConsideredPressed(ReadValue());
  88. public bool wasPressedThisFrame => device.wasUpdatedThisFrame && IsValueConsideredPressed(ReadValue()) && !IsValueConsideredPressed(ReadValueFromPreviousFrame());
  89. public bool wasReleasedThisFrame => device.wasUpdatedThisFrame && !IsValueConsideredPressed(ReadValue()) && IsValueConsideredPressed(ReadValueFromPreviousFrame());
  90. // We make the current global default button press point available as a static so that we don't have to
  91. // constantly make the hop from InputSystem.settings -> InputManager.m_Settings -> defaultButtonPressPoint.
  92. internal static float s_GlobalDefaultButtonPressPoint;
  93. }
  94. }