blittable button identifiers

main
cosmonaut 2022-04-27 11:37:35 -07:00
parent 27e0404ec0
commit 810f270a41
6 changed files with 100 additions and 10 deletions

View File

@ -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;
}
}
}

9
src/Input/DeviceKind.cs Normal file
View File

@ -0,0 +1,9 @@
namespace MoonWorks.Input
{
public enum DeviceKind
{
Keyboard,
Mouse,
Gamepad
}
}

View File

@ -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<SDL.SDL_GameControllerAxis, Axis> EnumToAxis;
private Dictionary<SDL.SDL_GameControllerAxis, Trigger> EnumToTrigger;
internal Gamepad(IntPtr handle)
internal Gamepad(IntPtr handle, int index)
{
Handle = handle;
Index = index;
EnumToButton = new Dictionary<SDL.SDL_GameControllerButton, Button>
{

View File

@ -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)

View File

@ -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<MouseButtonCode, Button> CodeToButton;
public Mouse()
{
CodeToButton = new Dictionary<MouseButtonCode, Button>
{
{ 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)

View File

@ -0,0 +1,9 @@
namespace MoonWorks.Input
{
public enum MouseButtonCode
{
Left,
Right,
Middle
}
}