input refactor

main
cosmonaut 2021-01-22 14:41:34 -08:00
parent 12c5f37ae9
commit a3307c0066
7 changed files with 102 additions and 127 deletions

View File

@ -1,4 +1,4 @@
namespace MoonWorks.Graphics
namespace MoonWorks
{
public static class Conversions
{
@ -6,5 +6,10 @@ namespace MoonWorks.Graphics
{
return (byte)(b ? 1 : 0);
}
public static bool ByteToBool(byte b)
{
return b == 0 ? false : true;
}
}
}

View File

@ -1,18 +1,31 @@
namespace MoonWorks.Input
{
public enum ButtonState
public class ButtonState
{
/// <summary>
/// Indicates that the input is not pressed.
/// </summary>
Released,
/// <summary>
/// Indicates that the input was pressed this frame.
/// </summary>
Pressed,
/// <summary>
/// Indicates that the input has been held for multiple frames.
/// </summary>
Held
private ButtonStatus ButtonStatus { get; set; }
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;
internal void Update(bool isPressed)
{
if (isPressed)
{
if (ButtonStatus == ButtonStatus.Pressed)
{
ButtonStatus = ButtonStatus.Held;
}
else if (ButtonStatus == ButtonStatus.Released)
{
ButtonStatus = ButtonStatus.Pressed;
}
}
else
{
ButtonStatus = ButtonStatus.Released;
}
}
}
}

18
src/Input/ButtonStatus.cs Normal file
View File

@ -0,0 +1,18 @@
namespace MoonWorks.Input
{
internal enum ButtonStatus
{
/// <summary>
/// Indicates that the input is not pressed.
/// </summary>
Released,
/// <summary>
/// Indicates that the input was pressed this frame.
/// </summary>
Pressed,
/// <summary>
/// Indicates that the input has been held for multiple frames.
/// </summary>
Held
}
}

View File

@ -7,21 +7,21 @@ namespace MoonWorks.Input
{
internal IntPtr Handle;
public ButtonState A { get; private set; }
public ButtonState B { get; private set; }
public ButtonState X { get; private set; }
public ButtonState Y { get; private set; }
public ButtonState Back { get; private set; }
public ButtonState Guide { get; private set; }
public ButtonState Start { get; private set; }
public ButtonState LeftStick { get; private set; }
public ButtonState RightStick { get; private set; }
public ButtonState LeftShoulder { get; private set; }
public ButtonState RightShoulder { get; private set; }
public ButtonState DpadUp { get; private set; }
public ButtonState DpadDown { get; private set; }
public ButtonState DpadLeft { get; private set; }
public ButtonState DpadRight { get; private set; }
public ButtonState A { get; } = new ButtonState();
public ButtonState B { get; } = new ButtonState();
public ButtonState X { get; } = new ButtonState();
public ButtonState Y { get; } = new ButtonState();
public ButtonState Back { get; } = new ButtonState();
public ButtonState Guide { get; } = new ButtonState();
public ButtonState Start { get; } = new ButtonState();
public ButtonState LeftStick { get; } = new ButtonState();
public ButtonState RightStick { get; } = new ButtonState();
public ButtonState LeftShoulder { get; } = new ButtonState();
public ButtonState RightShoulder { get; } = new ButtonState();
public ButtonState DpadUp { get; } = new ButtonState();
public ButtonState DpadDown { get; } = new ButtonState();
public ButtonState DpadLeft { get; } = new ButtonState();
public ButtonState DpadRight { get; } = new ButtonState();
public float LeftX { get; private set; }
public float LeftY { get; private set; }
@ -37,21 +37,21 @@ namespace MoonWorks.Input
internal void Update()
{
A = UpdateState(A, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_A);
B = UpdateState(B, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_B);
X = UpdateState(X, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_X);
Y = UpdateState(Y, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_Y);
Back = UpdateState(Back, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_BACK);
Guide = UpdateState(Guide, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_GUIDE);
Start = UpdateState(Start, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_START);
LeftStick = UpdateState(LeftStick, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSTICK);
RightStick = UpdateState(RightStick, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSTICK);
LeftShoulder = UpdateState(LeftShoulder, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
RightShoulder = UpdateState(RightShoulder, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
DpadUp = UpdateState(DpadUp, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_UP);
DpadDown = UpdateState(DpadDown, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_DOWN);
DpadLeft = UpdateState(DpadLeft, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_LEFT);
DpadRight = UpdateState(DpadRight, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
A.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_A));
B.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_B));
X.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_X));
Y.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_Y));
Back.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_BACK));
Guide.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_GUIDE));
Start.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_START));
LeftStick.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSTICK));
RightStick.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSTICK));
LeftShoulder.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSHOULDER));
RightShoulder.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSHOULDER));
DpadUp.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_UP));
DpadDown.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_DOWN));
DpadLeft.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_LEFT));
DpadRight.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_RIGHT));
LeftX = UpdateAxis(SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_LEFTX);
LeftY = UpdateAxis(SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_LEFTY);
@ -61,23 +61,9 @@ namespace MoonWorks.Input
TriggerRight = UpdateTrigger(SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERRIGHT);
}
private ButtonState UpdateState(ButtonState state, SDL.SDL_GameControllerButton button)
private bool IsPressed(SDL.SDL_GameControllerButton button)
{
var isPressed = SDL.SDL_GameControllerGetButton(Handle, button);
if (isPressed == 1)
{
if (state == ButtonState.Pressed)
{
return ButtonState.Held;
}
else if (state == ButtonState.Released)
{
return ButtonState.Pressed;
}
}
return ButtonState.Released;
return MoonWorks.Conversions.ByteToBool(SDL.SDL_GameControllerGetButton(Handle, button));
}
private float UpdateAxis(SDL.SDL_GameControllerAxis axis)

View File

@ -1,14 +0,0 @@
namespace MoonWorks.Input
{
internal class Key
{
public Keycode Keycode { get; }
public ButtonState InputState { get; internal set; }
public Key(Keycode keycode)
{
Keycode = keycode;
InputState = ButtonState.Released;
}
}
}

View File

@ -6,17 +6,17 @@ namespace MoonWorks.Input
{
public class Keyboard
{
private Key[] Keys { get; }
private ButtonState[] Keys { get; }
private int numKeys;
internal Keyboard()
{
SDL.SDL_GetKeyboardState(out numKeys);
Keys = new Key[numKeys];
Keys = new ButtonState[numKeys];
foreach (Keycode keycode in Enum.GetValues(typeof(Keycode)))
{
Keys[(int)keycode] = new Key(keycode);
Keys[(int)keycode] = new ButtonState();
}
}
@ -27,47 +27,28 @@ namespace MoonWorks.Input
foreach (int keycode in Enum.GetValues(typeof(Keycode)))
{
var keyDown = Marshal.ReadByte(keyboardState, keycode);
if (keyDown == 1)
{
if (Keys[keycode].InputState == ButtonState.Released)
{
Keys[keycode].InputState = ButtonState.Pressed;
}
else if (Keys[keycode].InputState == ButtonState.Pressed)
{
Keys[keycode].InputState = ButtonState.Held;
}
}
else
{
Keys[keycode].InputState = ButtonState.Released;
}
Keys[keycode].Update(Conversions.ByteToBool(keyDown));
}
}
public bool IsDown(Keycode keycode)
{
var key = Keys[(int)keycode];
return (key.InputState == ButtonState.Pressed) || (key.InputState == ButtonState.Held);
return Keys[(int)keycode].IsDown;
}
public bool IsPressed(Keycode keycode)
{
var key = Keys[(int)keycode];
return key.InputState == ButtonState.Pressed;
return Keys[(int)keycode].IsPressed;
}
public bool IsHeld(Keycode keycode)
{
var key = Keys[(int)keycode];
return key.InputState == ButtonState.Held;
return Keys[(int)keycode].IsHeld;
}
public bool IsUp(Keycode keycode)
public bool IsReleased(Keycode keycode)
{
var key = Keys[(int)keycode];
return key.InputState == ButtonState.Released;
return Keys[(int)keycode].IsReleased;
}
}
}

View File

@ -4,9 +4,9 @@ namespace MoonWorks.Input
{
public class Mouse
{
public ButtonState LeftButton { get; private set; }
public ButtonState MiddleButton { get; private set; }
public ButtonState RightButton { get; private set; }
public ButtonState LeftButton { get; } = new ButtonState();
public ButtonState MiddleButton { get; } = new ButtonState();
public ButtonState RightButton { get; } = new ButtonState();
public int X { get; private set; }
public int Y { get; private set; }
@ -30,7 +30,7 @@ namespace MoonWorks.Input
internal void Update()
{
var buttons = SDL.SDL_GetMouseState(out var x, out var y);
var buttonMask = SDL.SDL_GetMouseState(out var x, out var y);
var _ = SDL.SDL_GetRelativeMouseState(out var deltaX, out var deltaY);
X = x;
@ -38,28 +38,14 @@ namespace MoonWorks.Input
DeltaX = deltaX;
DeltaY = deltaY;
LeftButton = UpdateState(LeftButton, buttons, SDL.SDL_BUTTON_LMASK);
MiddleButton = UpdateState(MiddleButton, buttons, SDL.SDL_BUTTON_MMASK);
RightButton = UpdateState(RightButton, buttons, SDL.SDL_BUTTON_RMASK);
LeftButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_LMASK));
MiddleButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_MMASK));
RightButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_RMASK));
}
private ButtonState UpdateState(ButtonState state, uint buttonMask, uint buttonFlag)
private bool IsPressed(uint buttonMask, uint buttonFlag)
{
var isPressed = buttonMask & buttonFlag;
if (isPressed != 0)
{
if (state == ButtonState.Pressed)
{
return ButtonState.Held;
}
else if (state == ButtonState.Released)
{
return ButtonState.Pressed;
}
}
return ButtonState.Released;
return (buttonMask & buttonFlag) != 0;
}
}
}