Generalized keystroke->String to a method in Input

This commit is contained in:
Matt Thorson
2020-06-16 22:38:00 -07:00
parent 3af854f3f0
commit f95e638e91
2 changed files with 112 additions and 102 deletions

View File

@ -83,6 +83,115 @@ namespace Strawberry
return !keyboard[(int)key] && previousKeyboard[(int)key];
}
static public void KeystrokesIntoString(String toString, float keyRepeatDelay, float keyRepeatInterval)
{
KeystrokesIntoStringUtil(toString, scope (k) => Input.KeyPressed(k, keyRepeatDelay, keyRepeatInterval));
}
static public void KeystrokesIntoString(String toString)
{
KeystrokesIntoStringUtil(toString, scope => Input.KeyPressed);
}
static private void KeystrokesIntoStringUtil(String toString, delegate bool(SDL.Scancode) pressed)
{
for (let i < (int)SDL2.SDL.Scancode.NUMSCANCODES)
{
let key = (SDL2.SDL.Scancode)i;
if (pressed(key))
{
if (key >= .A && key <= .Z)
{
char8 c;
if (Input.Shift || Input.CapsLock)
c = 'A' + (int)(key - .A);
else
c = 'a' + (int)(key - .A);
toString.Append(c);
}
else if (key >= .Key1 && key <= .Key9 && !Input.Shift)
{
char8 c = '1' + (int)(key - .Key1);
toString.Append(c);
}
else
{
switch (key)
{
case .Key1 when Input.Shift:
toString.Append('!');
case .Key2 when Input.Shift:
toString.Append('@');
case .Key3 when Input.Shift:
toString.Append('#');
case .Key4 when Input.Shift:
toString.Append('$');
case .Key5 when Input.Shift:
toString.Append('%');
case .Key6 when Input.Shift:
toString.Append('^');
case .Key7 when Input.Shift:
toString.Append('&');
case .Key8 when Input.Shift:
toString.Append('*');
case .Key9 when Input.Shift:
toString.Append('(');
case .Key0 when Input.Shift:
toString.Append(')');
case .Key0:
toString.Append('0');
case .Space:
toString.Append(' ');
case .Minus when Input.Shift:
toString.Append('_');
case .Minus:
toString.Append('-');
case .Equals when Input.Shift:
toString.Append('+');
case .Equals:
toString.Append('=');
case .LeftBracket when Input.Shift:
toString.Append('{');
case .LeftBracket:
toString.Append('[');
case .RightBracket when Input.Shift:
toString.Append('}');
case .RightBracket:
toString.Append(']');
case .BackSlash when Input.Shift:
toString.Append('|');
case .BackSlash:
toString.Append('\\');
case .Semicolon when Input.Shift:
toString.Append(':');
case .Semicolon:
toString.Append(';');
case .Apostrophe when Input.Shift:
toString.Append('"');
case .Apostrophe:
toString.Append('\'');
case .Comma when Input.Shift:
toString.Append('<');
case .Comma:
toString.Append(',');
case .Period when Input.Shift:
toString.Append('>');
case .Period:
toString.Append('.');
case .Slash when Input.Shift:
toString.Append('?');
case .Slash:
toString.Append('/');
case .BackSpace:
if (toString.Length > 0)
toString.RemoveFromEnd(1);
default:
}
}
}
}
}
static public bool GamepadButtonCheck(int gamepadID, SDL.SDL_GameControllerButton button)
{
Debug.Assert(gamepads != null, "Polling gamepad before Input.Init");