distinguish between idle and released on buttons

pull/37/head
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 IsHeld => ButtonStatus == ButtonStatus.Held;
public bool IsDown => ButtonStatus == ButtonStatus.Pressed || ButtonStatus == ButtonStatus.Held; public bool IsDown => ButtonStatus == ButtonStatus.Pressed || ButtonStatus == ButtonStatus.Held;
public bool IsReleased => ButtonStatus == ButtonStatus.Released; 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) public ButtonState(ButtonStatus buttonStatus)
{ {
@ -18,26 +20,34 @@
{ {
if (isPressed) if (isPressed)
{ {
if (ButtonStatus == ButtonStatus.Pressed) if (IsUp)
{
return new ButtonState(ButtonStatus.Held);
}
else if (ButtonStatus == ButtonStatus.Released)
{ {
return new ButtonState(ButtonStatus.Pressed); return new ButtonState(ButtonStatus.Pressed);
} }
else if (ButtonStatus == ButtonStatus.Held) else
{ {
return new ButtonState(ButtonStatus.Held); return new ButtonState(ButtonStatus.Held);
} }
} }
else
return new ButtonState(ButtonStatus.Released); {
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) public static ButtonState operator |(ButtonState a, ButtonState b)
{ {
if (a.ButtonStatus == ButtonStatus.Released) if (a.ButtonStatus == ButtonStatus.Idle || a.ButtonStatus == ButtonStatus.Released)
{ {
return b; return b;
} }

View File

@ -3,15 +3,19 @@
public enum ButtonStatus public enum ButtonStatus
{ {
/// <summary> /// <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> /// </summary>
Released, Released,
/// <summary> /// <summary>
/// Indicates that the input was pressed this frame. /// Indicates that the button was pressed this frame.
/// </summary> /// </summary>
Pressed, Pressed,
/// <summary> /// <summary>
/// Indicates that the input has been held for multiple frames. /// Indicates that the button has been held for multiple frames.
/// </summary> /// </summary>
Held Held
} }

View File

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