MoonWorks/src/Input/Keyboard.cs

156 lines
3.5 KiB
C#
Raw Normal View History

2022-02-23 05:14:32 +00:00
using System;
2021-03-25 22:57:26 +00:00
using System.Collections.Generic;
2021-01-19 07:29:07 +00:00
using SDL2;
namespace MoonWorks.Input
2021-01-19 07:29:07 +00:00
{
/// <summary>
/// The keyboard input device abstraction.
/// </summary>
2022-02-23 05:14:32 +00:00
public class Keyboard
{
/// <summary>
/// True if any button on the keyboard is active. Useful for input remapping.
/// </summary>
2022-07-08 23:47:12 +00:00
public bool AnyPressed { get; private set; }
/// <summary>
/// Contains a reference to an arbitrary KeyboardButton that was pressed this frame. Useful for input remapping.
/// </summary>
2022-07-12 23:09:23 +00:00
public KeyboardButton AnyPressedButton { get; private set; }
2022-07-08 23:47:12 +00:00
internal IntPtr State { get; private set; }
2022-07-12 23:09:23 +00:00
2022-12-13 08:34:16 +00:00
private KeyCode[] KeyCodes;
2022-07-12 23:09:23 +00:00
private KeyboardButton[] Keys { get; }
2022-02-23 05:14:32 +00:00
private int numKeys;
2021-01-19 07:29:07 +00:00
2021-03-25 22:57:26 +00:00
private static readonly char[] TextInputCharacters = new char[]
{
(char) 2, // Home
(char) 3, // End
(char) 8, // Backspace
(char) 9, // Tab
(char) 13, // Enter
(char) 127, // Delete
(char) 22 // Ctrl+V (Paste)
};
2022-03-18 18:46:44 +00:00
private static readonly Dictionary<KeyCode, int> TextInputBindings = new Dictionary<KeyCode, int>()
2021-03-25 22:57:26 +00:00
{
2022-03-18 18:46:44 +00:00
{ KeyCode.Home, 0 },
{ KeyCode.End, 1 },
{ KeyCode.Backspace, 2 },
{ KeyCode.Tab, 3 },
{ KeyCode.Return, 4 },
{ KeyCode.Delete, 5 }
2021-03-25 22:57:26 +00:00
// Ctrl+V is special!
};
2022-02-23 05:14:32 +00:00
internal Keyboard()
{
SDL.SDL_GetKeyboardState(out numKeys);
2021-01-19 07:29:07 +00:00
2022-12-13 08:34:16 +00:00
KeyCodes = Enum.GetValues<KeyCode>();
2022-07-12 23:09:23 +00:00
Keys = new KeyboardButton[numKeys];
2022-12-13 08:34:16 +00:00
foreach (KeyCode keycode in KeyCodes)
2022-02-23 05:14:32 +00:00
{
2022-07-12 23:09:23 +00:00
Keys[(int) keycode] = new KeyboardButton(this, keycode);
2022-02-23 05:14:32 +00:00
}
}
2021-01-19 07:29:07 +00:00
2022-02-23 05:14:32 +00:00
internal void Update()
{
2022-07-08 23:47:12 +00:00
AnyPressed = false;
2022-07-12 23:09:23 +00:00
State = SDL.SDL_GetKeyboardState(out _);
2021-01-19 07:29:07 +00:00
2022-12-13 08:34:16 +00:00
foreach (KeyCode keycode in KeyCodes)
2022-02-23 05:14:32 +00:00
{
2022-12-13 08:34:16 +00:00
var button = Keys[(int) keycode];
2022-07-12 23:09:23 +00:00
button.Update();
2021-03-25 22:57:26 +00:00
2022-07-12 23:09:23 +00:00
if (button.IsPressed)
2022-02-23 05:14:32 +00:00
{
2022-12-13 08:34:16 +00:00
if (TextInputBindings.TryGetValue(keycode, out var textIndex))
2022-02-23 05:14:32 +00:00
{
Inputs.OnTextInput(TextInputCharacters[(textIndex)]);
}
2022-12-13 08:34:16 +00:00
else if (IsDown(KeyCode.LeftControl) && keycode == KeyCode.V)
2022-02-23 05:14:32 +00:00
{
Inputs.OnTextInput(TextInputCharacters[6]);
}
2022-07-08 23:47:12 +00:00
AnyPressed = true;
2022-07-12 23:09:23 +00:00
AnyPressedButton = button;
2022-07-08 23:47:12 +00:00
}
2022-02-23 05:14:32 +00:00
}
}
2021-01-19 07:29:07 +00:00
/// <summary>
/// True if the button was pressed this frame.
/// </summary>
2022-03-18 18:46:44 +00:00
public bool IsPressed(KeyCode keycode)
2022-02-23 05:14:32 +00:00
{
return Keys[(int) keycode].IsPressed;
}
2021-01-19 07:29:07 +00:00
/// <summary>
/// True if the button was pressed this frame and the previous frame.
/// </summary>
2022-03-18 18:46:44 +00:00
public bool IsHeld(KeyCode keycode)
2022-02-23 05:14:32 +00:00
{
return Keys[(int) keycode].IsHeld;
}
2021-01-19 07:29:07 +00:00
/// <summary>
/// True if the button was either pressed or continued to be held this frame.
/// </summary>
2022-11-28 18:19:03 +00:00
public bool IsDown(KeyCode keycode)
{
return Keys[(int) keycode].IsDown;
}
/// <summary>
/// True if the button was let go this frame.
/// </summary>
2022-03-18 18:46:44 +00:00
public bool IsReleased(KeyCode keycode)
2022-02-23 05:14:32 +00:00
{
return Keys[(int) keycode].IsReleased;
}
2022-03-07 18:54:52 +00:00
/// <summary>
/// True if the button was not pressed this frame or the previous frame.
/// </summary>
2022-11-28 18:19:03 +00:00
public bool IsIdle(KeyCode keycode)
{
return Keys[(int) keycode].IsIdle;
}
/// <summary>
/// True if the button was either idle or released this frame.
/// </summary>
2022-11-28 18:19:03 +00:00
public bool IsUp(KeyCode keycode)
{
return Keys[(int) keycode].IsUp;
}
/// <summary>
/// Gets a reference to a keyboard button object using a key code.
/// </summary>
2022-07-12 23:09:23 +00:00
public KeyboardButton Button(KeyCode keycode)
2022-03-07 18:54:52 +00:00
{
return Keys[(int) keycode];
}
/// <summary>
/// Gets the state of a keyboard button from a key code.
/// </summary>
public ButtonState ButtonState(KeyCode keycode)
{
return Keys[(int) keycode].State;
}
2022-02-23 05:14:32 +00:00
}
2021-01-19 07:29:07 +00:00
}