distinguish between idle and released on buttons

moar-validation
cosmonaut 2022-11-27 12:41:09 -08:00
parent ca9fb45536
commit de9d13757a
3 changed files with 26 additions and 13 deletions

View File

@ -8,6 +8,8 @@
public bool IsHeld => ButtonStatus == ButtonStatus.Held;
public bool IsDown => ButtonStatus == ButtonStatus.Pressed || ButtonStatus == ButtonStatus.Held;
public bool IsReleased => ButtonStatus == ButtonStatus.Released;
public bool IsIdle => ButtonStatus == ButtonStatus.Idle;
public bool IsUp => ButtonStatus == ButtonStatus.Idle || ButtonStatus == ButtonStatus.Released;
public ButtonState(ButtonStatus buttonStatus)
{
@ -18,26 +20,34 @@
{
if (isPressed)
{
if (ButtonStatus == ButtonStatus.Pressed)
{
return new ButtonState(ButtonStatus.Held);
}
else if (ButtonStatus == ButtonStatus.Released)
if (IsUp)
{
return new ButtonState(ButtonStatus.Pressed);
}
else if (ButtonStatus == ButtonStatus.Held)
else
{
return new ButtonState(ButtonStatus.Held);
}
}
else
{
if (IsDown)
{
return new ButtonState(ButtonStatus.Released);
}
else
{
return new ButtonState(ButtonStatus.Idle);
}
}
}
/// <summary>
/// Combines two button states. Useful for alt controls or input buffering.
/// </summary>
public static ButtonState operator |(ButtonState a, ButtonState b)
{
if (a.ButtonStatus == ButtonStatus.Released)
if (a.ButtonStatus == ButtonStatus.Idle || a.ButtonStatus == ButtonStatus.Released)
{
return b;
}

View File

@ -3,15 +3,19 @@
public enum ButtonStatus
{
/// <summary>
/// Indicates that the input is not pressed.
/// Indicates that the button was not pressed last frame and is still not pressed.
/// </summary>
Idle,
/// <summary>
/// Indicates that the button was released this frame.
/// </summary>
Released,
/// <summary>
/// Indicates that the input was pressed this frame.
/// Indicates that the button was pressed this frame.
/// </summary>
Pressed,
/// <summary>
/// Indicates that the input has been held for multiple frames.
/// Indicates that the button has been held for multiple frames.
/// </summary>
Held
}

View File

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using SDL2;
namespace MoonWorks.Input