namespace MoonWorks.Input
{
///
/// Container for the current state of a binary input.
///
public struct ButtonState
{
public ButtonStatus ButtonStatus { get; }
///
/// True if the button was pressed this frame.
///
public bool IsPressed => ButtonStatus == ButtonStatus.Pressed;
///
/// True if the button was pressed this frame and the previous frame.
///
public bool IsHeld => ButtonStatus == ButtonStatus.Held;
///
/// True if the button was either pressed or continued to be held this frame.
///
public bool IsDown => ButtonStatus == ButtonStatus.Pressed || ButtonStatus == ButtonStatus.Held;
///
/// True if the button was let go this frame.
///
public bool IsReleased => ButtonStatus == ButtonStatus.Released;
///
/// True if the button was not pressed this frame or the previous frame.
///
public bool IsIdle => ButtonStatus == ButtonStatus.Idle;
///
/// True if the button was either idle or released this frame.
///
public bool IsUp => ButtonStatus == ButtonStatus.Idle || ButtonStatus == ButtonStatus.Released;
public ButtonState(ButtonStatus buttonStatus)
{
ButtonStatus = buttonStatus;
}
internal ButtonState Update(bool isPressed)
{
if (isPressed)
{
if (IsUp)
{
return new ButtonState(ButtonStatus.Pressed);
}
else
{
return new ButtonState(ButtonStatus.Held);
}
}
else
{
if (IsDown)
{
return new ButtonState(ButtonStatus.Released);
}
else
{
return new ButtonState(ButtonStatus.Idle);
}
}
}
///
/// Combines two button states. Useful for alt control sets or input buffering.
///
public static ButtonState operator |(ButtonState a, ButtonState b)
{
if (a.ButtonStatus == ButtonStatus.Idle || a.ButtonStatus == ButtonStatus.Released)
{
return b;
}
else if (a.ButtonStatus == ButtonStatus.Pressed)
{
if (b.ButtonStatus == ButtonStatus.Held)
{
return new ButtonState(ButtonStatus.Held);
}
else
{
return a;
}
}
else // held
{
return a;
}
}
}
}