MoonWorks/src/Input/ButtonState.cs

62 lines
1.3 KiB
C#
Raw Normal View History

2022-02-23 05:14:32 +00:00
namespace MoonWorks.Input
2021-01-19 07:29:07 +00:00
{
2022-03-07 18:54:52 +00:00
public struct ButtonState
2022-02-23 05:14:32 +00:00
{
2022-03-18 18:46:44 +00:00
public ButtonStatus ButtonStatus { get; }
2021-01-22 22:41:34 +00:00
2022-02-23 05:14:32 +00:00
public bool IsPressed => ButtonStatus == ButtonStatus.Pressed;
public bool IsHeld => ButtonStatus == ButtonStatus.Held;
public bool IsDown => ButtonStatus == ButtonStatus.Pressed || ButtonStatus == ButtonStatus.Held;
public bool IsReleased => ButtonStatus == ButtonStatus.Released;
2021-01-22 22:41:34 +00:00
public ButtonState(ButtonStatus buttonStatus)
2022-03-07 18:54:52 +00:00
{
ButtonStatus = buttonStatus;
}
internal ButtonState Update(bool isPressed)
2022-02-23 05:14:32 +00:00
{
if (isPressed)
{
if (ButtonStatus == ButtonStatus.Pressed)
{
2022-03-07 18:54:52 +00:00
return new ButtonState(ButtonStatus.Held);
2022-02-23 05:14:32 +00:00
}
else if (ButtonStatus == ButtonStatus.Released)
{
2022-03-07 18:54:52 +00:00
return new ButtonState(ButtonStatus.Pressed);
}
else if (ButtonStatus == ButtonStatus.Held)
{
return new ButtonState(ButtonStatus.Held);
2022-02-23 05:14:32 +00:00
}
}
2022-03-07 18:54:52 +00:00
return new ButtonState(ButtonStatus.Released);
2022-02-23 05:14:32 +00:00
}
2022-07-13 00:57:27 +00:00
public static ButtonState operator |(ButtonState a, ButtonState b)
{
if (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;
}
}
2022-02-23 05:14:32 +00:00
}
2021-01-19 07:29:07 +00:00
}