From f95e638e91f70ea4128803f81baeff3ece000204 Mon Sep 17 00:00:00 2001 From: Matt Thorson Date: Tue, 16 Jun 2020 22:38:00 -0700 Subject: [PATCH] Generalized keystroke->String to a method in Input --- src/Input/Input.bf | 109 ++++++++++++++++++++++++++++++++++++++++++ src/Static/Console.bf | 105 ++-------------------------------------- 2 files changed, 112 insertions(+), 102 deletions(-) diff --git a/src/Input/Input.bf b/src/Input/Input.bf index 9784437..7ca147e 100644 --- a/src/Input/Input.bf +++ b/src/Input/Input.bf @@ -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"); diff --git a/src/Static/Console.bf b/src/Static/Console.bf index cff3946..735044c 100644 --- a/src/Static/Console.bf +++ b/src/Static/Console.bf @@ -42,109 +42,10 @@ namespace Strawberry Open = !Open; if (Open) - RegisterKeypresses(); - } - } - - static private void RegisterKeypresses() - { - for (let i < (int)SDL2.SDL.Scancode.NUMSCANCODES) - { - let key = (SDL2.SDL.Scancode)i; - if (Input.KeyPressed(key, 0.25f, 0.05f)) { - if (key >= .A && key <= .Z) - { - char8 c; - if (Input.Shift || Input.CapsLock) - c = 'A' + (int)(key - .A); - else - c = 'a' + (int)(key - .A); - entry.Append(c); - } - else if (key >= .Key1 && key <= .Key9 && !Input.Shift) - { - char8 c = '1' + (int)(key - .Key1); - entry.Append(c); - } - else - { - switch (key) - { - case .Key1 when Input.Shift: - entry.Append('!'); - case .Key2 when Input.Shift: - entry.Append('@'); - case .Key3 when Input.Shift: - entry.Append('#'); - case .Key4 when Input.Shift: - entry.Append('$'); - case .Key5 when Input.Shift: - entry.Append('%'); - case .Key6 when Input.Shift: - entry.Append('^'); - case .Key7 when Input.Shift: - entry.Append('&'); - case .Key8 when Input.Shift: - entry.Append('*'); - case .Key9 when Input.Shift: - entry.Append('('); - case .Key0 when Input.Shift: - entry.Append(')'); - case .Key0: - entry.Append('0'); - case .Space: - entry.Append(' '); - case .Minus when Input.Shift: - entry.Append('_'); - case .Minus: - entry.Append('-'); - case .Equals when Input.Shift: - entry.Append('+'); - case .Equals: - entry.Append('='); - case .LeftBracket when Input.Shift: - entry.Append('{'); - case .LeftBracket: - entry.Append('['); - case .RightBracket when Input.Shift: - entry.Append('}'); - case .RightBracket: - entry.Append(']'); - case .BackSlash when Input.Shift: - entry.Append('|'); - case .BackSlash: - entry.Append('\\'); - case .Semicolon when Input.Shift: - entry.Append(':'); - case .Semicolon: - entry.Append(';'); - case .Apostrophe when Input.Shift: - entry.Append('"'); - case .Apostrophe: - entry.Append('\''); - case .Comma when Input.Shift: - entry.Append('<'); - case .Comma: - entry.Append(','); - case .Period when Input.Shift: - entry.Append('>'); - case .Period: - entry.Append('.'); - case .Slash when Input.Shift: - entry.Append('?'); - case .Slash: - entry.Append('/'); - - case .BackSpace: - if (entry.Length > 0) - entry.RemoveFromEnd(1); - case .Delete: - entry.Clear(); - - default: - } - } + Input.KeystrokesIntoString(entry, 0.25f, 0.05f); + if (Input.KeyPressed(.Delete)) + entry.Clear(); } } }