using UnityEngine.InputSystem.LowLevel; using UnityEngine.Scripting; ////REVIEW: introduce separate base class for ButtonControl and AxisControl instead of deriving ButtonControl from AxisControl? namespace UnityEngine.InputSystem.Controls { /// /// An axis that has a trigger point beyond which it is considered to be pressed. /// /// /// By default stored as a single bit. In that format, buttons will only yield 0 /// and 1 as values. However, buttons return are s and /// yield full floating-point values and may thus have a range of values. See /// for how button presses on such buttons are handled. /// [Preserve] public class ButtonControl : AxisControl { /// /// The minimum value the button has to reach for it to be considered pressed. /// /// Button press threshold. /// /// The button is considered pressed, if it has a value equal to or greater than /// this value. /// /// By default, this property is set to -1. If the value of the property is negative, /// is used. /// /// The value can be configured as a parameter in a layout. /// /// /// /// public class MyDevice : InputDevice /// { /// [InputControl(parameters = "pressPoint=0.234")] /// public ButtonControl button { get; private set; } /// /// //... /// } /// /// /// /// /// /// public float pressPoint = -1; /// /// Return if set, otherwise return . /// /// Effective value to use for press point thresholds. /// public float pressPointOrDefault => pressPoint >= 0 ? pressPoint : s_GlobalDefaultButtonPressPoint; /// /// Default-initialize the control. /// /// /// The default format for the control is . /// The control's minimum value is set to 0 and the maximum value to 1. /// public ButtonControl() { m_StateBlock.format = InputStateBlock.FormatBit; m_MinValue = 0f; m_MaxValue = 1f; } /// /// Whether the given value would be considered pressed for this button. /// /// Value for the button. /// True if crosses the threshold to be considered pressed. /// /// public bool IsValueConsideredPressed(float value) { return value >= pressPointOrDefault; } /// /// Whether the button is currently pressed. /// /// True if button is currently pressed. /// /// A button is considered press if it's value is equal to or greater /// than its button press threshold (). /// /// /// public bool isPressed => IsValueConsideredPressed(ReadValue()); public bool wasPressedThisFrame => device.wasUpdatedThisFrame && IsValueConsideredPressed(ReadValue()) && !IsValueConsideredPressed(ReadValueFromPreviousFrame()); public bool wasReleasedThisFrame => device.wasUpdatedThisFrame && !IsValueConsideredPressed(ReadValue()) && IsValueConsideredPressed(ReadValueFromPreviousFrame()); // We make the current global default button press point available as a static so that we don't have to // constantly make the hop from InputSystem.settings -> InputManager.m_Settings -> defaultButtonPressPoint. internal static float s_GlobalDefaultButtonPressPoint; } }