2022-02-23 05:14:32 +00:00
|
|
|
|
using SDL2;
|
2021-03-25 22:57:26 +00:00
|
|
|
|
using System;
|
2021-01-19 07:29:07 +00:00
|
|
|
|
|
2021-01-22 08:20:07 +00:00
|
|
|
|
namespace MoonWorks.Input
|
2021-01-19 07:29:07 +00:00
|
|
|
|
{
|
2022-02-23 05:14:32 +00:00
|
|
|
|
public class Inputs
|
|
|
|
|
{
|
2022-03-18 19:11:00 +00:00
|
|
|
|
public const int MAX_GAMEPADS = 4;
|
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
public Keyboard Keyboard { get; }
|
|
|
|
|
public Mouse Mouse { get; }
|
|
|
|
|
|
2022-03-18 19:11:00 +00:00
|
|
|
|
Gamepad[] gamepads;
|
2022-02-23 05:14:32 +00:00
|
|
|
|
|
|
|
|
|
public static event Action<char> TextInput;
|
|
|
|
|
|
|
|
|
|
internal Inputs()
|
|
|
|
|
{
|
|
|
|
|
Keyboard = new Keyboard();
|
|
|
|
|
Mouse = new Mouse();
|
|
|
|
|
|
2022-03-18 19:11:00 +00:00
|
|
|
|
gamepads = new Gamepad[MAX_GAMEPADS];
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < 4; i += 1)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
|
|
|
|
if (SDL.SDL_IsGameController(i) == SDL.SDL_bool.SDL_TRUE)
|
|
|
|
|
{
|
2022-04-27 18:37:35 +00:00
|
|
|
|
gamepads[i] = new Gamepad(SDL.SDL_GameControllerOpen(i), i);
|
2022-03-18 19:11:00 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-04-27 18:37:35 +00:00
|
|
|
|
gamepads[i] = new Gamepad(IntPtr.Zero, -1);
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Assumes that SDL_PumpEvents has been called!
|
|
|
|
|
internal void Update()
|
|
|
|
|
{
|
2022-06-04 22:51:19 +00:00
|
|
|
|
Mouse.Wheel = 0;
|
2022-02-23 05:14:32 +00:00
|
|
|
|
Keyboard.Update();
|
|
|
|
|
Mouse.Update();
|
|
|
|
|
|
|
|
|
|
foreach (var gamepad in gamepads)
|
|
|
|
|
{
|
|
|
|
|
gamepad.Update();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool GamepadExists(int slot)
|
|
|
|
|
{
|
2022-03-18 19:11:00 +00:00
|
|
|
|
return !gamepads[slot].IsDummy;
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Gamepad GetGamepad(int slot)
|
|
|
|
|
{
|
|
|
|
|
return gamepads[slot];
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-27 18:37:35 +00:00
|
|
|
|
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!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
internal static void OnTextInput(char c)
|
|
|
|
|
{
|
|
|
|
|
if (TextInput != null)
|
|
|
|
|
{
|
|
|
|
|
TextInput(c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-19 07:29:07 +00:00
|
|
|
|
}
|