forked from MoonsideGames/MoonWorks
initial input handling
parent
f89907abf7
commit
7d3932da9b
@ -1,2 +1,3 @@
|
||||
bin/
|
||||
obj/
|
||||
.vs/
|
||||
|
@ -0,0 +1,6 @@
|
||||
[submodule "lib/SDL2-CS"]
|
||||
path = lib/SDL2-CS
|
||||
url = https://github.com/flibitijibibo/SDL2-CS.git
|
||||
[submodule "lib/Campari"]
|
||||
path = lib/Campari
|
||||
url = https://gitea.moonside.games/MoonsideGames/Campari.git
|
@ -0,0 +1 @@
|
||||
Subproject commit f01b9a3a9acff8672e0d7da253ee2402fcdaa281
|
@ -0,0 +1 @@
|
||||
Subproject commit 1e01bc8eebb501bf6df24ec98784c32843308e0a
|
@ -0,0 +1,18 @@
|
||||
namespace MoonWorks
|
||||
{
|
||||
public enum ButtonState
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicates that the input is not pressed.
|
||||
/// </summary>
|
||||
Released,
|
||||
/// <summary>
|
||||
/// Indicates that the input was pressed this frame.
|
||||
/// </summary>
|
||||
Pressed,
|
||||
/// <summary>
|
||||
/// Indicates that the input has been held for multiple frames.
|
||||
/// </summary>
|
||||
Held
|
||||
}
|
||||
}
|
@ -0,0 +1,101 @@
|
||||
using System;
|
||||
using SDL2;
|
||||
|
||||
namespace MoonWorks
|
||||
{
|
||||
public class Gamepad
|
||||
{
|
||||
internal IntPtr Handle;
|
||||
|
||||
public ButtonState A { get; private set; }
|
||||
public ButtonState B { get; private set; }
|
||||
public ButtonState X { get; private set; }
|
||||
public ButtonState Y { get; private set; }
|
||||
public ButtonState Back { get; private set; }
|
||||
public ButtonState Guide { get; private set; }
|
||||
public ButtonState Start { get; private set; }
|
||||
public ButtonState LeftStick { get; private set; }
|
||||
public ButtonState RightStick { get; private set; }
|
||||
public ButtonState LeftShoulder { get; private set; }
|
||||
public ButtonState RightShoulder { get; private set; }
|
||||
public ButtonState DpadUp { get; private set; }
|
||||
public ButtonState DpadDown { get; private set; }
|
||||
public ButtonState DpadLeft { get; private set; }
|
||||
public ButtonState DpadRight { get; private set; }
|
||||
|
||||
public float LeftX { get; private set; }
|
||||
public float LeftY { get; private set; }
|
||||
public float RightX { get; private set; }
|
||||
public float RightY { get; private set; }
|
||||
public float TriggerLeft { get; private set; }
|
||||
public float TriggerRight { get; private set; }
|
||||
|
||||
public Gamepad(IntPtr handle)
|
||||
{
|
||||
Handle = handle;
|
||||
}
|
||||
|
||||
internal void Update()
|
||||
{
|
||||
A = UpdateState(A, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_A);
|
||||
B = UpdateState(B, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_B);
|
||||
X = UpdateState(X, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_X);
|
||||
Y = UpdateState(Y, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_Y);
|
||||
Back = UpdateState(Back, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_BACK);
|
||||
Guide = UpdateState(Guide, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_GUIDE);
|
||||
Start = UpdateState(Start, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_START);
|
||||
LeftStick = UpdateState(LeftStick, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSTICK);
|
||||
RightStick = UpdateState(RightStick, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSTICK);
|
||||
LeftShoulder = UpdateState(LeftShoulder, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
|
||||
RightShoulder = UpdateState(RightShoulder, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
|
||||
DpadUp = UpdateState(DpadUp, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_UP);
|
||||
DpadDown = UpdateState(DpadDown, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_DOWN);
|
||||
DpadLeft = UpdateState(DpadLeft, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_LEFT);
|
||||
DpadRight = UpdateState(DpadRight, 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);
|
||||
RightX = UpdateAxis(SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_RIGHTX);
|
||||
RightY = UpdateAxis(SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_RIGHTY);
|
||||
TriggerLeft = UpdateTrigger(SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERLEFT);
|
||||
TriggerRight = UpdateTrigger(SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERRIGHT);
|
||||
}
|
||||
|
||||
private ButtonState UpdateState(ButtonState state, SDL.SDL_GameControllerButton button)
|
||||
{
|
||||
var isPressed = SDL.SDL_GameControllerGetButton(Handle, button);
|
||||
|
||||
if (isPressed == 1)
|
||||
{
|
||||
if (state == ButtonState.Pressed)
|
||||
{
|
||||
return ButtonState.Held;
|
||||
}
|
||||
else if (state == ButtonState.Released)
|
||||
{
|
||||
return ButtonState.Pressed;
|
||||
}
|
||||
}
|
||||
|
||||
return ButtonState.Released;
|
||||
}
|
||||
|
||||
private float UpdateAxis(SDL.SDL_GameControllerAxis axis)
|
||||
{
|
||||
var axisValue = SDL.SDL_GameControllerGetAxis(Handle, axis);
|
||||
return Normalize(axisValue, short.MinValue, short.MaxValue);
|
||||
}
|
||||
|
||||
// Triggers only go from 0 to short.MaxValue
|
||||
private float UpdateTrigger(SDL.SDL_GameControllerAxis trigger)
|
||||
{
|
||||
var triggerValue = SDL.SDL_GameControllerGetAxis(Handle, trigger);
|
||||
return Normalize(triggerValue, 0, short.MaxValue);
|
||||
}
|
||||
|
||||
private float Normalize(float value, short min, short max)
|
||||
{
|
||||
return (value - min) / (max - min);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
using SDL2;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MoonWorks
|
||||
{
|
||||
public class Input
|
||||
{
|
||||
public Keyboard Keyboard { get; }
|
||||
|
||||
List<Gamepad> gamepads = new List<Gamepad>();
|
||||
|
||||
internal Input()
|
||||
{
|
||||
Keyboard = new Keyboard();
|
||||
|
||||
for (int i = 0; i < SDL.SDL_NumJoysticks(); i++)
|
||||
{
|
||||
if (SDL.SDL_IsGameController(i) == SDL.SDL_bool.SDL_TRUE)
|
||||
{
|
||||
gamepads.Add(new Gamepad(SDL.SDL_GameControllerOpen(i)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Assumes that SDL_PumpEvents has been called!
|
||||
internal void Update()
|
||||
{
|
||||
Keyboard.Update();
|
||||
|
||||
foreach (var gamepad in gamepads)
|
||||
{
|
||||
gamepad.Update();
|
||||
}
|
||||
}
|
||||
|
||||
public Gamepad GetGamepad(int slot)
|
||||
{
|
||||
return gamepads[slot];
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
namespace MoonWorks
|
||||
{
|
||||
internal class Key
|
||||
{
|
||||
public Keycode Keycode { get; }
|
||||
public ButtonState InputState { get; internal set; }
|
||||
|
||||
public Key(Keycode keycode)
|
||||
{
|
||||
Keycode = keycode;
|
||||
InputState = ButtonState.Released;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using SDL2;
|
||||
|
||||
namespace MoonWorks
|
||||
{
|
||||
public class Keyboard
|
||||
{
|
||||
private Key[] Keys { get; }
|
||||
private int numKeys;
|
||||
|
||||
internal Keyboard()
|
||||
{
|
||||
SDL.SDL_GetKeyboardState(out numKeys);
|
||||
|
||||
Keys = new Key[numKeys];
|
||||
foreach (Keycode keycode in Enum.GetValues(typeof(Keycode)))
|
||||
{
|
||||
Keys[(int)keycode] = new Key(keycode);
|
||||
}
|
||||
}
|
||||
|
||||
internal void Update()
|
||||
{
|
||||
IntPtr keyboardState = SDL.SDL_GetKeyboardState(out _);
|
||||
|
||||
foreach (int keycode in Enum.GetValues(typeof(Keycode)))
|
||||
{
|
||||
var keyDown = Marshal.ReadByte(keyboardState, keycode);
|
||||
|
||||
if (keyDown == 1)
|
||||
{
|
||||
if (Keys[keycode].InputState == ButtonState.Released)
|
||||
{
|
||||
Keys[keycode].InputState = ButtonState.Pressed;
|
||||
}
|
||||
else if (Keys[keycode].InputState == ButtonState.Pressed)
|
||||
{
|
||||
Keys[keycode].InputState = ButtonState.Held;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Keys[keycode].InputState = ButtonState.Released;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsDown(Keycode keycode)
|
||||
{
|
||||
var key = Keys[(int)keycode];
|
||||
return (key.InputState == ButtonState.Pressed) || (key.InputState == ButtonState.Held);
|
||||
}
|
||||
|
||||
public bool IsPressed(Keycode keycode)
|
||||
{
|
||||
var key = Keys[(int)keycode];
|
||||
return key.InputState == ButtonState.Pressed;
|
||||
}
|
||||
|
||||
public bool IsHeld(Keycode keycode)
|
||||
{
|
||||
var key = Keys[(int)keycode];
|
||||
return key.InputState == ButtonState.Held;
|
||||
}
|
||||
|
||||
public bool IsUp(Keycode keycode)
|
||||
{
|
||||
var key = Keys[(int)keycode];
|
||||
return key.InputState == ButtonState.Released;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,111 @@
|
||||
namespace MoonWorks
|
||||
{
|
||||
// Enum values are equivalent to the SDL Scancode value.
|
||||
public enum Keycode : uint
|
||||
{
|
||||
Unknown = 0,
|
||||
A = 4,
|
||||
B = 5,
|
||||
C = 6,
|
||||
D = 7,
|
||||
E = 8,
|
||||
F = 9,
|
||||
G = 10,
|
||||
H = 11,
|
||||
I = 12,
|
||||
J = 13,
|
||||
K = 14,
|
||||
L = 15,
|
||||
M = 16,
|
||||
N = 17,
|
||||
O = 18,
|
||||
P = 19,
|
||||
Q = 20,
|
||||
R = 21,
|
||||
S = 22,
|
||||
T = 23,
|
||||
U = 24,
|
||||
V = 25,
|
||||
W = 26,
|
||||
X = 27,
|
||||
Y = 28,
|
||||
Z = 29,
|
||||
D1 = 30,
|
||||
D2 = 31,
|
||||
D3 = 32,
|
||||
D4 = 33,
|
||||
D5 = 34,
|
||||
D6 = 35,
|
||||
D7 = 36,
|
||||
D8 = 37,
|
||||
D9 = 38,
|
||||
D0 = 39,
|
||||
Return = 40,
|
||||
Escape = 41,
|
||||
Backspace = 42,
|
||||
Tab = 43,
|
||||
Space = 44,
|
||||
Minus = 45,
|
||||
Equals = 46,
|
||||
LeftBracket = 47,
|
||||
RightBracket = 48,
|
||||
Backslash = 49,
|
||||
NonUSHash = 50,
|
||||
Semicolon = 51,
|
||||
Apostrophe = 52,
|
||||
Grave = 53,
|
||||
Comma = 54,
|
||||
Period = 55,
|
||||
Slash = 56,
|
||||
CapsLock = 57,
|
||||
F1 = 58,
|
||||
F2 = 59,
|
||||
F3 = 60,
|
||||
F4 = 61,
|
||||
F5 = 62,
|
||||
F6 = 63,
|
||||
F7 = 64,
|
||||
F8 = 65,
|
||||
F9 = 66,
|
||||
F10 = 67,
|
||||
F11 = 68,
|
||||
F12 = 69,
|
||||
PrintScreen = 70,
|
||||
ScrollLock = 71,
|
||||
Pause = 72,
|
||||
Insert = 73,
|
||||
Home = 74,
|
||||
PageUp = 75,
|
||||
Delete = 76,
|
||||
End = 77,
|
||||
PageDown = 78,
|
||||
Right = 79,
|
||||
Left = 80,
|
||||
Down = 81,
|
||||
Up = 82,
|
||||
NumLockClear = 83,
|
||||
KeypadDivide = 84,
|
||||
KeypadMultiply = 85,
|
||||
KeypadMinus = 86,
|
||||
KeypadPlus = 87,
|
||||
KeypadEnter = 88,
|
||||
Keypad1 = 89,
|
||||
Keypad2 = 90,
|
||||
Keypad3 = 91,
|
||||
Keypad4 = 92,
|
||||
Keypad5 = 93,
|
||||
Keypad6 = 94,
|
||||
Keypad7 = 95,
|
||||
Keypad8 = 96,
|
||||
Keypad9 = 97,
|
||||
Keypad0 = 98,
|
||||
KeypadPeriod = 99,
|
||||
NonUSBackslash = 100,
|
||||
LeftControl = 224,
|
||||
LeftShift = 225,
|
||||
LeftAlt = 226,
|
||||
RightControl = 228,
|
||||
RightShift = 229,
|
||||
RightAlt = 230
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
namespace MoonWorks
|
||||
{
|
||||
public enum PresentMode
|
||||
{
|
||||
Immediate,
|
||||
Mailbox,
|
||||
FIFO,
|
||||
FIFORelaxed
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue