diff --git a/src/Game.cs b/src/Game.cs index 8c744853..aef068c2 100644 --- a/src/Game.cs +++ b/src/Game.cs @@ -2,6 +2,7 @@ using SDL2; using MoonWorks.Audio; using MoonWorks.Graphics; +using MoonWorks.Input; namespace MoonWorks { @@ -16,7 +17,7 @@ namespace MoonWorks public Window Window { get; } public GraphicsDevice GraphicsDevice { get; } public AudioDevice AudioDevice { get; } - public Input Input { get; } + public Inputs Inputs { get; } private Dictionary moonWorksToRefreshPresentMode = new Dictionary { @@ -42,7 +43,7 @@ namespace MoonWorks Logger.Initialize(); - Input = new Input(); + Inputs = new Inputs(); Window = new Window(windowCreateInfo); @@ -81,7 +82,7 @@ namespace MoonWorks { HandleSDLEvents(); - Input.Update(); + Inputs.Update(); AudioDevice.Update(); Update(timestep); diff --git a/src/Input/ButtonState.cs b/src/Input/ButtonState.cs index a2cb54ff..f08fb7a9 100644 --- a/src/Input/ButtonState.cs +++ b/src/Input/ButtonState.cs @@ -1,4 +1,4 @@ -namespace MoonWorks +namespace MoonWorks.Input { public enum ButtonState { diff --git a/src/Input/Gamepad.cs b/src/Input/Gamepad.cs index 9f6ef22a..921fddb8 100644 --- a/src/Input/Gamepad.cs +++ b/src/Input/Gamepad.cs @@ -1,7 +1,7 @@ using System; using SDL2; -namespace MoonWorks +namespace MoonWorks.Input { public class Gamepad { diff --git a/src/Input/Input.cs b/src/Input/Input.cs index f9eb5e00..679303c5 100644 --- a/src/Input/Input.cs +++ b/src/Input/Input.cs @@ -1,17 +1,19 @@ using SDL2; using System.Collections.Generic; -namespace MoonWorks +namespace MoonWorks.Input { - public class Input + public class Inputs { public Keyboard Keyboard { get; } + public Mouse Mouse { get; } List gamepads = new List(); - internal Input() + internal Inputs() { Keyboard = new Keyboard(); + Mouse = new Mouse(); for (int i = 0; i < SDL.SDL_NumJoysticks(); i++) { @@ -26,6 +28,7 @@ namespace MoonWorks internal void Update() { Keyboard.Update(); + Mouse.Update(); foreach (var gamepad in gamepads) { diff --git a/src/Input/Key.cs b/src/Input/Key.cs index 19a56d55..d1875965 100644 --- a/src/Input/Key.cs +++ b/src/Input/Key.cs @@ -1,4 +1,4 @@ -namespace MoonWorks +namespace MoonWorks.Input { internal class Key { diff --git a/src/Input/Keyboard.cs b/src/Input/Keyboard.cs index 0348be85..60c4451d 100644 --- a/src/Input/Keyboard.cs +++ b/src/Input/Keyboard.cs @@ -2,7 +2,7 @@ using System; using System.Runtime.InteropServices; using SDL2; -namespace MoonWorks +namespace MoonWorks.Input { public class Keyboard { diff --git a/src/Input/Keycode.cs b/src/Input/Keycode.cs index 855350c5..40edabd3 100644 --- a/src/Input/Keycode.cs +++ b/src/Input/Keycode.cs @@ -1,4 +1,4 @@ -namespace MoonWorks +namespace MoonWorks.Input { // Enum values are equivalent to the SDL Scancode value. public enum Keycode : int diff --git a/src/Input/Mouse.cs b/src/Input/Mouse.cs new file mode 100644 index 00000000..b9b1531a --- /dev/null +++ b/src/Input/Mouse.cs @@ -0,0 +1,65 @@ +using SDL2; + +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 int X { get; private set; } + public int Y { get; private set; } + public int DeltaX { get; private set; } + public int DeltaY { get; private set; } + + private bool relativeMode; + public bool RelativeMode + { + get => relativeMode; + set + { + relativeMode = value; + SDL.SDL_SetRelativeMouseMode( + relativeMode ? + SDL.SDL_bool.SDL_TRUE : + SDL.SDL_bool.SDL_FALSE + ); + } + } + + internal void Update() + { + var buttons = SDL.SDL_GetMouseState(out var x, out var y); + var _ = SDL.SDL_GetRelativeMouseState(out var deltaX, out var deltaY); + + X = x; + Y = y; + DeltaX = deltaX; + DeltaY = deltaY; + + LeftButton = UpdateState(LeftButton, buttons, SDL.SDL_BUTTON_LEFT); + MiddleButton = UpdateState(MiddleButton, buttons, SDL.SDL_BUTTON_MIDDLE); + RightButton = UpdateState(RightButton, buttons, SDL.SDL_BUTTON_RIGHT); + } + + private ButtonState UpdateState(ButtonState state, 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; + } + } +}