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;
|
|
|
|
|
|
2022-07-08 23:47:12 +00:00
|
|
|
|
public bool AnyPressed { get; private set; }
|
2022-07-12 23:09:23 +00:00
|
|
|
|
public VirtualButton AnyPressedButton { get; private set; }
|
2022-07-08 23:47:12 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
internal Inputs()
|
|
|
|
|
{
|
|
|
|
|
Keyboard = new Keyboard();
|
|
|
|
|
Mouse = new Mouse();
|
|
|
|
|
|
2022-03-18 19:11:00 +00:00
|
|
|
|
gamepads = new Gamepad[MAX_GAMEPADS];
|
|
|
|
|
|
2022-07-13 18:53:09 +00:00
|
|
|
|
// initialize dummy controllers
|
|
|
|
|
for (var slot = 0; slot < MAX_GAMEPADS; slot += 1)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
2022-07-13 18:53:09 +00:00
|
|
|
|
gamepads[slot] = new Gamepad(IntPtr.Zero, slot);
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Assumes that SDL_PumpEvents has been called!
|
|
|
|
|
internal void Update()
|
|
|
|
|
{
|
2022-07-08 23:47:12 +00:00
|
|
|
|
AnyPressed = false;
|
2022-07-08 23:52:37 +00:00
|
|
|
|
AnyPressedButton = default; // DeviceKind.None
|
2022-07-08 23:47:12 +00:00
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
Keyboard.Update();
|
2022-07-08 23:47:12 +00:00
|
|
|
|
|
|
|
|
|
if (Keyboard.AnyPressed)
|
|
|
|
|
{
|
|
|
|
|
AnyPressed = true;
|
2022-07-12 23:09:23 +00:00
|
|
|
|
AnyPressedButton = Keyboard.AnyPressedButton;
|
2022-07-08 23:47:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
Mouse.Update();
|
|
|
|
|
|
2022-07-08 23:47:12 +00:00
|
|
|
|
if (Mouse.AnyPressed)
|
|
|
|
|
{
|
|
|
|
|
AnyPressed = true;
|
2022-07-12 23:09:23 +00:00
|
|
|
|
AnyPressedButton = Mouse.AnyPressedButton;
|
2022-07-08 23:47:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 05:14:32 +00:00
|
|
|
|
foreach (var gamepad in gamepads)
|
|
|
|
|
{
|
|
|
|
|
gamepad.Update();
|
2022-07-08 23:47:12 +00:00
|
|
|
|
|
|
|
|
|
if (gamepad.AnyPressed)
|
|
|
|
|
{
|
|
|
|
|
AnyPressed = true;
|
2022-07-12 23:09:23 +00:00
|
|
|
|
AnyPressedButton = gamepad.AnyPressedButton;
|
2022-07-08 23:47:12 +00:00
|
|
|
|
}
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool GamepadExists(int slot)
|
|
|
|
|
{
|
2022-07-13 18:53:09 +00:00
|
|
|
|
if (slot < 0 || slot >= MAX_GAMEPADS)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 19:11:00 +00:00
|
|
|
|
return !gamepads[slot].IsDummy;
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-13 18:53:09 +00:00
|
|
|
|
// From 0-4
|
2022-02-23 05:14:32 +00:00
|
|
|
|
public Gamepad GetGamepad(int slot)
|
|
|
|
|
{
|
|
|
|
|
return gamepads[slot];
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-13 18:53:09 +00:00
|
|
|
|
internal void AddGamepad(int index)
|
|
|
|
|
{
|
|
|
|
|
for (var slot = 0; slot < MAX_GAMEPADS; slot += 1)
|
|
|
|
|
{
|
|
|
|
|
if (!GamepadExists(slot))
|
|
|
|
|
{
|
2023-02-22 00:00:16 +00:00
|
|
|
|
var openResult = SDL.SDL_GameControllerOpen(index);
|
|
|
|
|
if (openResult == 0)
|
|
|
|
|
{
|
|
|
|
|
System.Console.WriteLine($"Error opening gamepad!");
|
|
|
|
|
System.Console.WriteLine(SDL.SDL_GetError());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-02-24 00:59:19 +00:00
|
|
|
|
gamepads[slot].Handle = openResult;
|
2023-02-22 00:00:16 +00:00
|
|
|
|
System.Console.WriteLine($"Gamepad added to slot {slot}!");
|
|
|
|
|
}
|
2022-07-13 18:53:09 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.Console.WriteLine("Too many gamepads already!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void RemoveGamepad(int joystickInstanceID)
|
|
|
|
|
{
|
|
|
|
|
for (int slot = 0; slot < MAX_GAMEPADS; slot += 1)
|
|
|
|
|
{
|
|
|
|
|
if (joystickInstanceID == gamepads[slot].JoystickInstanceID)
|
|
|
|
|
{
|
|
|
|
|
SDL.SDL_GameControllerClose(gamepads[slot].Handle);
|
|
|
|
|
gamepads[slot].Handle = IntPtr.Zero;
|
|
|
|
|
System.Console.WriteLine($"Removing gamepad from slot {slot}!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|