From 7e19ce65e8bd5eafaad9a033760c837d378efa1d Mon Sep 17 00:00:00 2001 From: Vadim Grigoruk Date: Sun, 26 Aug 2018 09:34:27 +0300 Subject: [PATCH] #707 one more attempt --- include/tic80.h | 14 ++++-- src/code.c | 12 +++-- src/console.c | 2 +- src/music.c | 9 ++-- src/studio.c | 30 ----------- src/studio.h | 1 - src/system.c | 130 +++++++++++++++--------------------------------- src/tic.c | 2 +- src/tic.h | 2 +- 9 files changed, 65 insertions(+), 137 deletions(-) diff --git a/include/tic80.h b/include/tic80.h index 4b746a5..3846049 100644 --- a/include/tic80.h +++ b/include/tic80.h @@ -110,10 +110,17 @@ typedef struct typedef u8 tic_key; -typedef union +typedef struct { - tic_key keys[TIC80_KEY_BUFFER]; - u32 data; + union + { + tic_key keys[TIC80_KEY_BUFFER]; + u32 data; + }; + + s8 text; + u8 temp[3]; + } tic80_keyboard; typedef struct @@ -121,7 +128,6 @@ typedef struct tic80_gamepads gamepads; tic80_mouse mouse; tic80_keyboard keyboard; - } tic80_input; TIC80_API tic80* tic80_create(s32 samplerate); diff --git a/src/code.c b/src/code.c index d405dcc..647f29c 100644 --- a/src/code.c +++ b/src/code.c @@ -1046,7 +1046,7 @@ static void textEditTick(Code* code) if(!tic->api.key(tic, tic_key_ctrl) && !tic->api.key(tic, tic_key_alt)) { - char sym = getKeyboardText(); + char sym = tic->ram.input.keyboard.text; if(sym) { @@ -1113,6 +1113,8 @@ static char* downStrStr(const char* start, const char* from, const char* substr) static void textFindTick(Code* code) { + tic_mem* tic = code->tic; + if(keyWasPressed(tic_key_return)) setCodeMode(code, TEXT_EDIT_MODE); else if(keyWasPressed(tic_key_up) || keyWasPressed(tic_key_down) @@ -1137,7 +1139,7 @@ static void textFindTick(Code* code) } } - char sym = getKeyboardText(); + char sym = tic->ram.input.keyboard.text; if(sym) { @@ -1195,7 +1197,7 @@ static void textGoToTick(Code* code) } } - char sym = getKeyboardText(); + char sym = tic->ram.input.keyboard.text; if(sym) { @@ -1265,6 +1267,8 @@ static void drawOutlineBar(Code* code, s32 x, s32 y) static void textOutlineTick(Code* code) { + tic_mem* tic = code->tic; + if(keyWasPressed(tic_key_up)) { if(code->outline.index > 0) @@ -1295,7 +1299,7 @@ static void textOutlineTick(Code* code) } } - char sym = getKeyboardText(); + char sym = tic->ram.input.keyboard.text; if(sym) { diff --git a/src/console.c b/src/console.c index 2df3be9..8665d31 100644 --- a/src/console.c +++ b/src/console.c @@ -2850,7 +2850,7 @@ static void tick(Console* console) console->cursor.delay = CONSOLE_CURSOR_DELAY; } - char sym = getKeyboardText(); + char sym = tic->ram.input.keyboard.text; if(sym) { diff --git a/src/music.c b/src/music.c index 978c770..4084909 100644 --- a/src/music.c +++ b/src/music.c @@ -887,7 +887,7 @@ static void processTrackerKeyboard(Music* music) { s32 octave = -1; - char sym = getKeyboardText(); + char sym = tic->ram.input.keyboard.text; if(sym >= '1' && sym <= '8') octave = sym - '1'; @@ -904,7 +904,7 @@ static void processTrackerKeyboard(Music* music) { s32 val = -1; - char sym = getKeyboardText(); + char sym = tic->ram.input.keyboard.text; if (sym >= '0' && sym <= '9') val = sym - '0'; @@ -929,7 +929,7 @@ static void processTrackerKeyboard(Music* music) { s32 val = -1; - char sym = getKeyboardText(); + char sym = tic->ram.input.keyboard.text; if(sym >= '0' && sym <= '9') val = sym - '0'; if(sym >= 'a' && sym <= 'f') val = sym - 'a' + 10; @@ -949,6 +949,7 @@ static void processTrackerKeyboard(Music* music) static void processPatternKeyboard(Music* music) { + tic_mem* tic = music->tic; s32 channel = music->tracker.col / CHANNEL_COLS; if(keyWasPressed(tic_key_delete)) setChannelPatternValue(music, 0, channel); @@ -962,7 +963,7 @@ static void processPatternKeyboard(Music* music) { s32 val = -1; - char sym = getKeyboardText(); + char sym = tic->ram.input.keyboard.text; if(sym >= '0' && sym <= '9') val = sym - '0'; diff --git a/src/studio.c b/src/studio.c index cfff72c..46da742 100644 --- a/src/studio.c +++ b/src/studio.c @@ -234,36 +234,6 @@ static struct .argv = NULL, }; - -char getKeyboardText() -{ - tic_mem* tic = impl.studio.tic; - - static const char Symbols[] = " abcdefghijklmnopqrstuvwxyz0123456789-=[]\\;'`,./ "; - static const char Shift[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ)!@#$%^&*(_+{}|:\"~<>? "; - - enum{Count = sizeof Symbols}; - - for(s32 i = 0; i < TIC80_KEY_BUFFER; i++) - { - tic_key key = tic->ram.input.keyboard.keys[i]; - - if(key > 0 && key < Count && tic->api.keyp(tic, key, KEYBOARD_HOLD, KEYBOARD_PERIOD)) - { - bool caps = tic->api.key(tic, tic_key_capslock); - bool shift = tic->api.key(tic, tic_key_shift); - - return caps - ? key >= tic_key_a && key <= tic_key_z - ? shift ? Symbols[key] : Shift[key] - : shift ? Shift[key] : Symbols[key] - : shift ? Shift[key] : Symbols[key]; - } - } - - return 0; -} - bool keyWasPressed(tic_key key) { tic_mem* tic = impl.studio.tic; diff --git a/src/studio.h b/src/studio.h index aecf933..1978066 100644 --- a/src/studio.h +++ b/src/studio.h @@ -157,7 +157,6 @@ tic_tiles* getBankTiles(); tic_palette* getBankPalette(); tic_map* getBankMap(); -char getKeyboardText(); bool keyWasPressed(tic_key key); bool anyKeyWasPressed(); diff --git a/src/system.c b/src/system.c index 282a7a2..86a6935 100644 --- a/src/system.c +++ b/src/system.c @@ -65,7 +65,7 @@ static struct GPU_Image* down; } texture; - char text; + bool state[tic_keys_count]; } keyboard; u32 touchCounter; @@ -397,96 +397,23 @@ static void processMouse() static void processKeyboard() { + { + SDL_Keymod mod = SDL_GetModState(); + + platform.keyboard.state[tic_key_shift] = mod & KMOD_SHIFT; + platform.keyboard.state[tic_key_ctrl] = mod & KMOD_CTRL; + platform.keyboard.state[tic_key_alt] = mod & KMOD_ALT; + platform.keyboard.state[tic_key_capslock] = mod & KMOD_CAPS; + } + tic80_input* input = &platform.studio->tic->ram.input; input->keyboard.data = 0; enum{BufSize = COUNT_OF(input->keyboard.keys)}; - SDL_Keymod mod = SDL_GetModState(); - - // TODO: the ugliest hack ever - // will try to remove it - 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}, - }; - - u8 text = platform.keyboard.text; - - if(text && text < COUNT_OF(TextCodes)) - { - input->keyboard.keys[0] = TextCodes[text].code; - input->keyboard.keys[1] = TextCodes[text].shift; - } - } - else - { - static const u32 KeyboardCodes[tic_keys_count] = - { - #include "keycodes.inl" - }; - - s32 c = 0; - { - if(mod & KMOD_SHIFT) input->keyboard.keys[c++] = tic_key_shift; - if(mod & KMOD_CTRL) input->keyboard.keys[c++] = tic_key_ctrl; - if(mod & KMOD_CAPS) input->keyboard.keys[c++] = tic_key_capslock; - } - - const u8* keyboard = SDL_GetKeyboardState(NULL); - - for(s32 i = 0; i < SDL_NUM_SCANCODES && c < BufSize; i++) - { - if(keyboard[i]) - { - SDL_Keycode keycode = i == SDL_SCANCODE_AC_BACK - ? SDLK_ESCAPE - : SDL_GetKeyFromScancode(i); - - for(s32 k = 0; k < COUNT_OF(KeyboardCodes); k++) - { - if(KeyboardCodes[k] == keycode) - { - input->keyboard.keys[c++] = k; - break; - } - } - } - } - } + for(s32 i = 0, c = 0; i < COUNT_OF(platform.keyboard.state) && c < BufSize; i++) + if(platform.keyboard.state[i]) + input->keyboard.keys[c++] = i; } #if !defined(__EMSCRIPTEN__) && !defined(__MACOSX__) @@ -791,15 +718,30 @@ static void processTouchInput() #endif } +static void handleKeydown(SDL_Keycode keycode, bool down) +{ + static const u32 KeyboardCodes[tic_keys_count] = + { + #include "keycodes.inl" + }; + + for(tic_key i = 0; i < COUNT_OF(KeyboardCodes); i++) + { + if(KeyboardCodes[i] == keycode) + { + platform.keyboard.state[i] = down; + break; + } + } +} + static void pollEvent() { tic_mem* tic = platform.studio->tic; tic80_input* input = &tic->ram.input; - { - input->mouse.btns = 0; - platform.keyboard.text = 0; - } + input->mouse.btns = 0; + tic->ram.input.keyboard.text = 0; SDL_Event event; @@ -853,12 +795,18 @@ static void pollEvent() case SDL_WINDOWEVENT_FOCUS_GAINED: platform.studio->updateProject(); break; } break; + case SDL_KEYDOWN: + handleKeydown(event.key.keysym.sym, true); + break; + case SDL_KEYUP: + handleKeydown(event.key.keysym.sym, false); + break; case SDL_TEXTINPUT: { const char* text = event.text.text; if(strlen(text) == 1) - platform.keyboard.text = *text; + tic->ram.input.keyboard.text = *text; } break; case SDL_QUIT: diff --git a/src/tic.c b/src/tic.c index 28f5299..6997346 100644 --- a/src/tic.c +++ b/src/tic.c @@ -81,7 +81,7 @@ STATIC_ASSERT(tic_track, sizeof(tic_track) == 3*MUSIC_FRAMES+3); STATIC_ASSERT(tic_vram, sizeof(tic_vram) == TIC_VRAM_SIZE); STATIC_ASSERT(tic_ram, sizeof(tic_ram) == TIC_RAM_SIZE); STATIC_ASSERT(tic_sound_register, sizeof(tic_sound_register) == 16+2); -STATIC_ASSERT(tic80_input, sizeof(tic80_input) == 12); +STATIC_ASSERT(tic80_input, sizeof(tic80_input) == 16); static void update_amp(blip_buffer_t* blip, tic_sound_register_data* data, s32 new_amp ) { diff --git a/src/tic.h b/src/tic.h index 6cc867f..dfd9eb1 100644 --- a/src/tic.h +++ b/src/tic.h @@ -419,7 +419,7 @@ typedef union tic_tiles sprites; tic_map map; tic80_input input; - u8 unknown[16]; + u8 unknown[12]; tic_sound_register registers[TIC_SOUND_CHANNELS]; tic_sfx sfx; tic_music music;