diff --git a/src/Input/ButtonIdentifier.cs b/src/Input/ButtonIdentifier.cs new file mode 100644 index 00000000..a1388d3b --- /dev/null +++ b/src/Input/ButtonIdentifier.cs @@ -0,0 +1,31 @@ +namespace MoonWorks.Input +{ + // Blittable identifier that can be used for button state lookups. + public struct ButtonIdentifier + { + public DeviceKind DeviceKind { get; } + public int Index { get; } // 1-4 for gamepads, 0 otherwise + public int Code { get; } + + public ButtonIdentifier(Gamepad gamepad, ButtonCode buttonCode) + { + DeviceKind = DeviceKind.Gamepad; + Index = gamepad.Index; + Code = (int) buttonCode; + } + + public ButtonIdentifier(KeyCode keyCode) + { + DeviceKind = DeviceKind.Keyboard; + Index = 0; + Code = (int) keyCode; + } + + public ButtonIdentifier(MouseButtonCode mouseCode) + { + DeviceKind = DeviceKind.Mouse; + Index = 0; + Code = (int) mouseCode; + } + } +} diff --git a/src/Input/DeviceKind.cs b/src/Input/DeviceKind.cs new file mode 100644 index 00000000..58b14b49 --- /dev/null +++ b/src/Input/DeviceKind.cs @@ -0,0 +1,9 @@ +namespace MoonWorks.Input +{ + public enum DeviceKind + { + Keyboard, + Mouse, + Gamepad + } +} diff --git a/src/Input/Gamepad.cs b/src/Input/Gamepad.cs index f0259864..d771e13d 100644 --- a/src/Input/Gamepad.cs +++ b/src/Input/Gamepad.cs @@ -8,6 +8,7 @@ namespace MoonWorks.Input public class Gamepad { internal IntPtr Handle; + internal int Index; public Button A { get; } = new Button(); public Button B { get; } = new Button(); @@ -39,9 +40,10 @@ namespace MoonWorks.Input private Dictionary EnumToAxis; private Dictionary EnumToTrigger; - internal Gamepad(IntPtr handle) + internal Gamepad(IntPtr handle, int index) { Handle = handle; + Index = index; EnumToButton = new Dictionary { diff --git a/src/Input/Input.cs b/src/Input/Input.cs index e35b7675..b662cb10 100644 --- a/src/Input/Input.cs +++ b/src/Input/Input.cs @@ -25,11 +25,11 @@ namespace MoonWorks.Input { if (SDL.SDL_IsGameController(i) == SDL.SDL_bool.SDL_TRUE) { - gamepads[i] = new Gamepad(SDL.SDL_GameControllerOpen(i)); + gamepads[i] = new Gamepad(SDL.SDL_GameControllerOpen(i), i); } else { - gamepads[i] = new Gamepad(IntPtr.Zero); + gamepads[i] = new Gamepad(IntPtr.Zero, -1); } } } @@ -56,6 +56,27 @@ namespace MoonWorks.Input return gamepads[slot]; } + public ButtonState ButtonState(ButtonIdentifier identifier) + { + if (identifier.DeviceKind == DeviceKind.Gamepad) + { + var gamepad = GetGamepad(identifier.Index); + return gamepad.ButtonState((ButtonCode) identifier.Code); + } + else if (identifier.DeviceKind == DeviceKind.Keyboard) + { + return Keyboard.ButtonState((KeyCode) identifier.Code); + } + else if (identifier.DeviceKind == DeviceKind.Mouse) + { + return Mouse.ButtonState((MouseButtonCode) identifier.Code); + } + else + { + throw new System.ArgumentException("Invalid button identifier!"); + } + } + internal static void OnTextInput(char c) { if (TextInput != null) diff --git a/src/Input/Mouse.cs b/src/Input/Mouse.cs index 27a3df8a..766b9561 100644 --- a/src/Input/Mouse.cs +++ b/src/Input/Mouse.cs @@ -1,12 +1,13 @@ -using SDL2; +using System.Collections.Generic; +using SDL2; namespace MoonWorks.Input { public class Mouse { - 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 Button LeftButton { get; } = new Button(); + public Button MiddleButton { get; } = new Button(); + public Button RightButton { get; } = new Button(); public int X { get; private set; } public int Y { get; private set; } @@ -30,6 +31,18 @@ namespace MoonWorks.Input } } + private readonly Dictionary CodeToButton; + + public Mouse() + { + CodeToButton = new Dictionary + { + { MouseButtonCode.Left, LeftButton }, + { MouseButtonCode.Right, RightButton }, + { MouseButtonCode.Middle, MiddleButton } + }; + } + internal void Update() { var buttonMask = SDL.SDL_GetMouseState(out var x, out var y); @@ -40,9 +53,14 @@ namespace MoonWorks.Input DeltaX = deltaX; DeltaY = deltaY; - 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)); + LeftButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_LMASK)); + MiddleButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_MMASK)); + RightButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_RMASK)); + } + + public ButtonState ButtonState(MouseButtonCode buttonCode) + { + return CodeToButton[buttonCode].State; } private bool IsPressed(uint buttonMask, uint buttonFlag) diff --git a/src/Input/MouseButtonCode.cs b/src/Input/MouseButtonCode.cs new file mode 100644 index 00000000..83b6c61d --- /dev/null +++ b/src/Input/MouseButtonCode.cs @@ -0,0 +1,9 @@ +namespace MoonWorks.Input +{ + public enum MouseButtonCode + { + Left, + Right, + Middle + } +}