diff --git a/src/Input/ButtonState.cs b/src/Input/ButtonState.cs index 20e60f84..17306bfb 100644 --- a/src/Input/ButtonState.cs +++ b/src/Input/ButtonState.cs @@ -1,6 +1,6 @@ namespace MoonWorks.Input { - public class ButtonState + public struct ButtonState { private ButtonStatus ButtonStatus { get; set; } @@ -9,23 +9,30 @@ public bool IsDown => ButtonStatus == ButtonStatus.Pressed || ButtonStatus == ButtonStatus.Held; 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 (ButtonStatus == ButtonStatus.Pressed) { - ButtonStatus = ButtonStatus.Held; + return new ButtonState(ButtonStatus.Held); } else if (ButtonStatus == ButtonStatus.Released) { - ButtonStatus = ButtonStatus.Pressed; + return new ButtonState(ButtonStatus.Pressed); + } + else if (ButtonStatus == ButtonStatus.Held) + { + return new ButtonState(ButtonStatus.Held); } } - else - { - ButtonStatus = ButtonStatus.Released; - } + + return new ButtonState(ButtonStatus.Released); } } } diff --git a/src/Input/ButtonStatus.cs b/src/Input/ButtonStatus.cs index 13bd6a7b..6f341d70 100644 --- a/src/Input/ButtonStatus.cs +++ b/src/Input/ButtonStatus.cs @@ -1,6 +1,6 @@ namespace MoonWorks.Input { - internal enum ButtonStatus + public enum ButtonStatus { /// /// Indicates that the input is not pressed. diff --git a/src/Input/Gamepad.cs b/src/Input/Gamepad.cs index 7475f56a..44fa317b 100644 --- a/src/Input/Gamepad.cs +++ b/src/Input/Gamepad.cs @@ -8,21 +8,21 @@ namespace MoonWorks.Input { internal IntPtr Handle; - 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 ButtonState A { get; private set; } = new ButtonState(); + public ButtonState B { get; private set; } = new ButtonState(); + public ButtonState X { get; private set; } = new ButtonState(); + public ButtonState Y { get; private set; } = new ButtonState(); + public ButtonState Back { get; private set; } = new ButtonState(); + public ButtonState Guide { get; private set; } = new ButtonState(); + public ButtonState Start { get; private set; } = new ButtonState(); + public ButtonState LeftStick { get; private set; } = new ButtonState(); + public ButtonState RightStick { get; private set; } = new ButtonState(); + public ButtonState LeftShoulder { get; private set; } = new ButtonState(); + public ButtonState RightShoulder { get; private set; } = new ButtonState(); + public ButtonState DpadUp { get; private set; } = new ButtonState(); + public ButtonState DpadDown { get; private set; } = new ButtonState(); + public ButtonState DpadLeft { get; private set; } = new ButtonState(); + public ButtonState DpadRight { get; private set; } = new ButtonState(); public float LeftX { get; private set; } public float LeftY { get; private set; } @@ -48,21 +48,21 @@ namespace MoonWorks.Input internal void Update() { - 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)); + A = A.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_A)); + B = B.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_B)); + X = X.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_X)); + Y = Y.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_Y)); + Back = Back.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_BACK)); + Guide = Guide.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_GUIDE)); + Start = Start.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_START)); + LeftStick = LeftStick.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSTICK)); + RightStick = RightStick.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSTICK)); + LeftShoulder = LeftShoulder.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSHOULDER)); + RightShoulder = RightShoulder.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSHOULDER)); + DpadUp = DpadUp.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_UP)); + DpadDown = DpadDown.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_DOWN)); + DpadLeft = DpadLeft.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_LEFT)); + DpadRight = 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); diff --git a/src/Input/Keyboard.cs b/src/Input/Keyboard.cs index 821060ad..2aa939fe 100644 --- a/src/Input/Keyboard.cs +++ b/src/Input/Keyboard.cs @@ -50,7 +50,7 @@ namespace MoonWorks.Input foreach (int keycode in Enum.GetValues(typeof(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)) { @@ -85,5 +85,10 @@ namespace MoonWorks.Input { return Keys[(int) keycode].IsReleased; } + + public ButtonState ButtonState(Keycode keycode) + { + return Keys[(int) keycode]; + } } } diff --git a/src/Input/Mouse.cs b/src/Input/Mouse.cs index 9ff84614..27a3df8a 100644 --- a/src/Input/Mouse.cs +++ b/src/Input/Mouse.cs @@ -4,9 +4,9 @@ namespace MoonWorks.Input { public class Mouse { - public ButtonState LeftButton { get; } = new ButtonState(); - public ButtonState MiddleButton { get; } = new ButtonState(); - public ButtonState RightButton { get; } = new ButtonState(); + public ButtonState LeftButton { get; private set; } = new ButtonState(); + public ButtonState MiddleButton { get; private set; } = new ButtonState(); + public ButtonState RightButton { get; private set; } = new ButtonState(); public int X { get; private set; } public int Y { get; private set; } @@ -40,9 +40,9 @@ namespace MoonWorks.Input DeltaX = deltaX; DeltaY = deltaY; - LeftButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_LMASK)); - MiddleButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_MMASK)); - RightButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_RMASK)); + LeftButton = LeftButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_LMASK)); + MiddleButton = MiddleButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_MMASK)); + RightButton = RightButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_RMASK)); } private bool IsPressed(uint buttonMask, uint buttonFlag)