From 408100744f2878d19930a35ca7b783f5bbf86b32 Mon Sep 17 00:00:00 2001 From: Vadim Grigoruk Date: Fri, 24 Aug 2018 21:49:48 +0300 Subject: [PATCH] #701 --- src/system.c | 113 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 93 insertions(+), 20 deletions(-) diff --git a/src/system.c b/src/system.c index 5c8d009..961ddc6 100644 --- a/src/system.c +++ b/src/system.c @@ -61,6 +61,8 @@ static struct GPU_Image* down; } texture; + u8 text; + } keyboard; u32 touchCounter; @@ -390,40 +392,100 @@ static void processMouse() } } +static void appendKeycode(tic_keycode code) +{ + tic80_input* input = &platform.studio->tic->ram.input; + + enum{BufSize = COUNT_OF(input->keyboard.keys)}; + + for(s32 i = 0; i < BufSize; i++) + { + if(input->keyboard.keys[i] == tic_key_unknown) + { + input->keyboard.keys[i] = code; + return; + } + else if(input->keyboard.keys[i] == code) return; + } +} + static void processKeyboard() { - static const u32 KeyboardCodes[tic_keys_count] = + tic80_input* input = &platform.studio->tic->ram.input; + input->keyboard.data = 0; + + SDL_Keymod mod = SDL_GetModState(); + + if(mod & KMOD_RALT) + { + static const struct {tic_keycode code; tic_keycode shift;} TextCodes[] = + { + ['-'] = {tic_key_minus, tic_key_unknown}, + ['='] = {tic_key_equals, tic_key_unknown}, + ['['] = {tic_key_leftbracket, tic_key_unknown}, + [']'] = {tic_key_rightbracket, tic_key_unknown}, + ['\\'] = {tic_key_backslash, tic_key_unknown}, + [';'] = {tic_key_semicolon, tic_key_unknown}, + ['\''] = {tic_key_apostrophe, tic_key_unknown}, + ['`'] = {tic_key_grave, tic_key_unknown}, + [','] = {tic_key_comma, tic_key_unknown}, + ['.'] = {tic_key_period, tic_key_unknown}, + ['/'] = {tic_key_slash, tic_key_unknown}, + [')'] = {tic_key_0, tic_key_shift}, + ['!'] = {tic_key_1, tic_key_shift}, + ['@'] = {tic_key_2, tic_key_shift}, + ['#'] = {tic_key_3, tic_key_shift}, + ['$'] = {tic_key_4, tic_key_shift}, + ['%'] = {tic_key_5, tic_key_shift}, + ['^'] = {tic_key_6, tic_key_shift}, + ['&'] = {tic_key_7, tic_key_shift}, + ['*'] = {tic_key_8, tic_key_shift}, + ['('] = {tic_key_9, tic_key_shift}, + ['_'] = {tic_key_minus, tic_key_shift}, + ['+'] = {tic_key_equals, tic_key_shift}, + ['{'] = {tic_key_leftbracket, tic_key_shift}, + ['}'] = {tic_key_rightbracket, tic_key_shift}, + ['|'] = {tic_key_backslash, tic_key_shift}, + [':'] = {tic_key_semicolon, tic_key_shift}, + ['"'] = {tic_key_apostrophe, tic_key_shift}, + ['~'] = {tic_key_grave, tic_key_shift}, + ['<'] = {tic_key_comma, tic_key_shift}, + ['>'] = {tic_key_period, tic_key_shift}, + ['?'] = {tic_key_slash, tic_key_shift}, + }; + + if(platform.keyboard.text && platform.keyboard.text < COUNT_OF(TextCodes)) + { + appendKeycode(TextCodes[platform.keyboard.text].code); + appendKeycode(TextCodes[platform.keyboard.text].shift); + platform.keyboard.text = 0; + } + + return; + } + + const u8* keyboard = SDL_GetKeyboardState(NULL); + + if(mod & KMOD_SHIFT) appendKeycode(tic_key_shift); + if(mod & (KMOD_CTRL | KMOD_GUI)) appendKeycode(tic_key_ctrl); + if(mod & KMOD_CAPS) appendKeycode(tic_key_capslock); + + static const SDL_Keycode KeyboardCodes[tic_keys_count] = { #include "keycodes.inl" }; - tic80_input* input = &platform.studio->tic->ram.input; - input->keyboard.data = 0; - - const u8* keyboard = SDL_GetKeyboardState(NULL); - - enum{BufSize = COUNT_OF(input->keyboard.keys)}; - - s32 c = 0; - { - SDL_Keymod mod = SDL_GetModState(); - if(mod & KMOD_RSHIFT) input->keyboard.keys[c++] = tic_key_shift; - if(mod & (KMOD_RCTRL | KMOD_GUI)) input->keyboard.keys[c++] = tic_key_ctrl; - if(mod & KMOD_RALT) input->keyboard.keys[c++] = tic_key_alt; - if(mod & KMOD_CAPS) input->keyboard.keys[c++] = tic_key_capslock; - } - - for(s32 i = 0; i < SDL_NUM_SCANCODES && c < BufSize; i++) + for(s32 i = 0; i < SDL_NUM_SCANCODES; i++) { if(keyboard[i]) { - u32 keycode = SDL_GetKeyFromScancode(i); + SDL_Keycode keycode = SDL_GetKeyFromScancode(i); for(s32 k = 0; k < COUNT_OF(KeyboardCodes); k++) { if(KeyboardCodes[k] == keycode) { - input->keyboard.keys[c++] = k; + appendKeycode(k); break; } } @@ -794,6 +856,17 @@ static void pollEvent() case SDL_WINDOWEVENT_FOCUS_GAINED: platform.studio->updateProject(); break; } break; + case SDL_TEXTINPUT: + { + const char* symbol = event.text.text; + if(strlen(symbol) == 1) + platform.keyboard.text = *symbol; + } + break; + case SDL_KEYDOWN: + if(event.key.keysym.sym == SDLK_RALT) + platform.keyboard.text = 0; + break; case SDL_QUIT: platform.studio->exit(); break;