blittable button identifiers
parent
27e0404ec0
commit
810f270a41
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
namespace MoonWorks.Input
|
||||||
|
{
|
||||||
|
public enum DeviceKind
|
||||||
|
{
|
||||||
|
Keyboard,
|
||||||
|
Mouse,
|
||||||
|
Gamepad
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,6 +8,7 @@ namespace MoonWorks.Input
|
||||||
public class Gamepad
|
public class Gamepad
|
||||||
{
|
{
|
||||||
internal IntPtr Handle;
|
internal IntPtr Handle;
|
||||||
|
internal int Index;
|
||||||
|
|
||||||
public Button A { get; } = new Button();
|
public Button A { get; } = new Button();
|
||||||
public Button B { 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, Axis> EnumToAxis;
|
||||||
private Dictionary<SDL.SDL_GameControllerAxis, Trigger> EnumToTrigger;
|
private Dictionary<SDL.SDL_GameControllerAxis, Trigger> EnumToTrigger;
|
||||||
|
|
||||||
internal Gamepad(IntPtr handle)
|
internal Gamepad(IntPtr handle, int index)
|
||||||
{
|
{
|
||||||
Handle = handle;
|
Handle = handle;
|
||||||
|
Index = index;
|
||||||
|
|
||||||
EnumToButton = new Dictionary<SDL.SDL_GameControllerButton, Button>
|
EnumToButton = new Dictionary<SDL.SDL_GameControllerButton, Button>
|
||||||
{
|
{
|
||||||
|
|
|
@ -25,11 +25,11 @@ namespace MoonWorks.Input
|
||||||
{
|
{
|
||||||
if (SDL.SDL_IsGameController(i) == SDL.SDL_bool.SDL_TRUE)
|
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
|
else
|
||||||
{
|
{
|
||||||
gamepads[i] = new Gamepad(IntPtr.Zero);
|
gamepads[i] = new Gamepad(IntPtr.Zero, -1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,6 +56,27 @@ namespace MoonWorks.Input
|
||||||
return gamepads[slot];
|
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)
|
internal static void OnTextInput(char c)
|
||||||
{
|
{
|
||||||
if (TextInput != null)
|
if (TextInput != null)
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
using SDL2;
|
using System.Collections.Generic;
|
||||||
|
using SDL2;
|
||||||
|
|
||||||
namespace MoonWorks.Input
|
namespace MoonWorks.Input
|
||||||
{
|
{
|
||||||
public class Mouse
|
public class Mouse
|
||||||
{
|
{
|
||||||
public ButtonState LeftButton { get; private set; } = new ButtonState();
|
public Button LeftButton { get; } = new Button();
|
||||||
public ButtonState MiddleButton { get; private set; } = new ButtonState();
|
public Button MiddleButton { get; } = new Button();
|
||||||
public ButtonState RightButton { get; private set; } = new ButtonState();
|
public Button RightButton { get; } = new Button();
|
||||||
|
|
||||||
public int X { get; private set; }
|
public int X { get; private set; }
|
||||||
public int Y { 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()
|
internal void Update()
|
||||||
{
|
{
|
||||||
var buttonMask = SDL.SDL_GetMouseState(out var x, out var y);
|
var buttonMask = SDL.SDL_GetMouseState(out var x, out var y);
|
||||||
|
@ -40,9 +53,14 @@ namespace MoonWorks.Input
|
||||||
DeltaX = deltaX;
|
DeltaX = deltaX;
|
||||||
DeltaY = deltaY;
|
DeltaY = deltaY;
|
||||||
|
|
||||||
LeftButton = LeftButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_LMASK));
|
LeftButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_LMASK));
|
||||||
MiddleButton = MiddleButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_MMASK));
|
MiddleButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_MMASK));
|
||||||
RightButton = RightButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_RMASK));
|
RightButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_RMASK));
|
||||||
|
}
|
||||||
|
|
||||||
|
public ButtonState ButtonState(MouseButtonCode buttonCode)
|
||||||
|
{
|
||||||
|
return CodeToButton[buttonCode].State;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsPressed(uint buttonMask, uint buttonFlag)
|
private bool IsPressed(uint buttonMask, uint buttonFlag)
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
namespace MoonWorks.Input
|
||||||
|
{
|
||||||
|
public enum MouseButtonCode
|
||||||
|
{
|
||||||
|
Left,
|
||||||
|
Right,
|
||||||
|
Middle
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue