ButtonState is now a struct

pull/17/head
cosmonaut 2022-03-07 10:54:52 -08:00
parent cf2d8473a1
commit 8f9aaf6d61
5 changed files with 58 additions and 46 deletions

View File

@ -1,6 +1,6 @@
namespace MoonWorks.Input namespace MoonWorks.Input
{ {
public class ButtonState public struct ButtonState
{ {
private ButtonStatus ButtonStatus { get; set; } private ButtonStatus ButtonStatus { get; set; }
@ -9,23 +9,30 @@
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;
internal void Update(bool isPressed) public ButtonState(ButtonStatus buttonStatus)
{
ButtonStatus = buttonStatus;
}
internal ButtonState Update(bool isPressed)
{ {
if (isPressed) if (isPressed)
{ {
if (ButtonStatus == ButtonStatus.Pressed) if (ButtonStatus == ButtonStatus.Pressed)
{ {
ButtonStatus = ButtonStatus.Held; return new ButtonState(ButtonStatus.Held);
} }
else if (ButtonStatus == ButtonStatus.Released) else if (ButtonStatus == ButtonStatus.Released)
{ {
ButtonStatus = ButtonStatus.Pressed; return new ButtonState(ButtonStatus.Pressed);
}
else if (ButtonStatus == ButtonStatus.Held)
{
return new ButtonState(ButtonStatus.Held);
} }
} }
else
{ return new ButtonState(ButtonStatus.Released);
ButtonStatus = ButtonStatus.Released;
}
} }
} }
} }

View File

@ -1,6 +1,6 @@
namespace MoonWorks.Input namespace MoonWorks.Input
{ {
internal enum ButtonStatus public enum ButtonStatus
{ {
/// <summary> /// <summary>
/// Indicates that the input is not pressed. /// Indicates that the input is not pressed.

View File

@ -8,21 +8,21 @@ namespace MoonWorks.Input
{ {
internal IntPtr Handle; internal IntPtr Handle;
public ButtonState A { get; } = new ButtonState(); public ButtonState A { get; private set; } = new ButtonState();
public ButtonState B { get; } = new ButtonState(); public ButtonState B { get; private set; } = new ButtonState();
public ButtonState X { get; } = new ButtonState(); public ButtonState X { get; private set; } = new ButtonState();
public ButtonState Y { get; } = new ButtonState(); public ButtonState Y { get; private set; } = new ButtonState();
public ButtonState Back { get; } = new ButtonState(); public ButtonState Back { get; private set; } = new ButtonState();
public ButtonState Guide { get; } = new ButtonState(); public ButtonState Guide { get; private set; } = new ButtonState();
public ButtonState Start { get; } = new ButtonState(); public ButtonState Start { get; private set; } = new ButtonState();
public ButtonState LeftStick { get; } = new ButtonState(); public ButtonState LeftStick { get; private set; } = new ButtonState();
public ButtonState RightStick { get; } = new ButtonState(); public ButtonState RightStick { get; private set; } = new ButtonState();
public ButtonState LeftShoulder { get; } = new ButtonState(); public ButtonState LeftShoulder { get; private set; } = new ButtonState();
public ButtonState RightShoulder { get; } = new ButtonState(); public ButtonState RightShoulder { get; private set; } = new ButtonState();
public ButtonState DpadUp { get; } = new ButtonState(); public ButtonState DpadUp { get; private set; } = new ButtonState();
public ButtonState DpadDown { get; } = new ButtonState(); public ButtonState DpadDown { get; private set; } = new ButtonState();
public ButtonState DpadLeft { get; } = new ButtonState(); public ButtonState DpadLeft { get; private set; } = new ButtonState();
public ButtonState DpadRight { get; } = new ButtonState(); public ButtonState DpadRight { get; private set; } = new ButtonState();
public float LeftX { get; private set; } public float LeftX { get; private set; }
public float LeftY { get; private set; } public float LeftY { get; private set; }
@ -48,21 +48,21 @@ namespace MoonWorks.Input
internal void Update() internal void Update()
{ {
A.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_A)); A = A.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_A));
B.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_B)); B = B.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_B));
X.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_X)); X = X.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_X));
Y.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_Y)); Y = Y.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_Y));
Back.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_BACK)); Back = Back.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_BACK));
Guide.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_GUIDE)); Guide = Guide.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_GUIDE));
Start.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_START)); Start = Start.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_START));
LeftStick.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSTICK)); LeftStick = LeftStick.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSTICK));
RightStick.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSTICK)); RightStick = RightStick.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSTICK));
LeftShoulder.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSHOULDER)); LeftShoulder = LeftShoulder.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSHOULDER));
RightShoulder.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSHOULDER)); RightShoulder = RightShoulder.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSHOULDER));
DpadUp.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_UP)); DpadUp = DpadUp.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_UP));
DpadDown.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_DOWN)); DpadDown = DpadDown.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_DOWN));
DpadLeft.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_LEFT)); DpadLeft = DpadLeft.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_LEFT));
DpadRight.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_RIGHT)); DpadRight = DpadRight.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_RIGHT));
LeftX = UpdateAxis(SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_LEFTX); LeftX = UpdateAxis(SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_LEFTX);
LeftY = UpdateAxis(SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_LEFTY); LeftY = UpdateAxis(SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_LEFTY);

View File

@ -50,7 +50,7 @@ namespace MoonWorks.Input
foreach (int keycode in Enum.GetValues(typeof(Keycode))) foreach (int keycode in Enum.GetValues(typeof(Keycode)))
{ {
var keyDown = Marshal.ReadByte(keyboardState, keycode); var keyDown = Marshal.ReadByte(keyboardState, keycode);
Keys[keycode].Update(Conversions.ByteToBool(keyDown)); Keys[keycode] = Keys[keycode].Update(Conversions.ByteToBool(keyDown));
if (Conversions.ByteToBool(keyDown)) if (Conversions.ByteToBool(keyDown))
{ {
@ -85,5 +85,10 @@ namespace MoonWorks.Input
{ {
return Keys[(int) keycode].IsReleased; return Keys[(int) keycode].IsReleased;
} }
public ButtonState ButtonState(Keycode keycode)
{
return Keys[(int) keycode];
}
} }
} }

View File

@ -4,9 +4,9 @@ namespace MoonWorks.Input
{ {
public class Mouse public class Mouse
{ {
public ButtonState LeftButton { get; } = new ButtonState(); public ButtonState LeftButton { get; private set; } = new ButtonState();
public ButtonState MiddleButton { get; } = new ButtonState(); public ButtonState MiddleButton { get; private set; } = new ButtonState();
public ButtonState RightButton { get; } = new ButtonState(); public ButtonState RightButton { get; private set; } = new ButtonState();
public int X { get; private set; } public int X { get; private set; }
public int Y { get; private set; } public int Y { get; private set; }
@ -40,9 +40,9 @@ namespace MoonWorks.Input
DeltaX = deltaX; DeltaX = deltaX;
DeltaY = deltaY; DeltaY = deltaY;
LeftButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_LMASK)); LeftButton = LeftButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_LMASK));
MiddleButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_MMASK)); MiddleButton = MiddleButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_MMASK));
RightButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_RMASK)); RightButton = RightButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_RMASK));
} }
private bool IsPressed(uint buttonMask, uint buttonFlag) private bool IsPressed(uint buttonMask, uint buttonFlag)