diff --git a/include/tic80.h b/include/tic80.h index 3846049..4b746a5 100644 --- a/include/tic80.h +++ b/include/tic80.h @@ -110,17 +110,10 @@ typedef struct typedef u8 tic_key; -typedef struct +typedef union { - union - { - tic_key keys[TIC80_KEY_BUFFER]; - u32 data; - }; - - s8 text; - u8 temp[3]; - + tic_key keys[TIC80_KEY_BUFFER]; + u32 data; } tic80_keyboard; typedef struct @@ -128,6 +121,7 @@ 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 72917b8..d405dcc 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 = tic->ram.input.keyboard.text; + char sym = getKeyboardText(); if(sym) { @@ -1113,8 +1113,6 @@ 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) @@ -1139,7 +1137,7 @@ static void textFindTick(Code* code) } } - char sym = tic->ram.input.keyboard.text; + char sym = getKeyboardText(); if(sym) { @@ -1197,7 +1195,7 @@ static void textGoToTick(Code* code) } } - char sym = tic->ram.input.keyboard.text; + char sym = getKeyboardText(); if(sym) { @@ -1267,8 +1265,6 @@ 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) @@ -1299,7 +1295,7 @@ static void textOutlineTick(Code* code) } } - char sym = tic->ram.input.keyboard.text; + char sym = getKeyboardText(); if(sym) { diff --git a/src/console.c b/src/console.c index 8665d31..2df3be9 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 = tic->ram.input.keyboard.text; + char sym = getKeyboardText(); if(sym) { diff --git a/src/music.c b/src/music.c index d8a7b81..f254c75 100644 --- a/src/music.c +++ b/src/music.c @@ -890,7 +890,7 @@ static void processTrackerKeyboard(Music* music) { s32 octave = -1; - char sym = tic->ram.input.keyboard.text; + char sym = getKeyboardText(); if(sym >= '1' && sym <= '8') octave = sym - '1'; @@ -907,7 +907,7 @@ static void processTrackerKeyboard(Music* music) { s32 val = -1; - char sym = tic->ram.input.keyboard.text; + char sym = getKeyboardText(); if (sym >= '0' && sym <= '9') val = sym - '0'; @@ -932,7 +932,7 @@ static void processTrackerKeyboard(Music* music) { s32 val = -1; - char sym = tic->ram.input.keyboard.text; + char sym = getKeyboardText(); if(sym >= '0' && sym <= '9') val = sym - '0'; if(sym >= 'a' && sym <= 'f') val = sym - 'a' + 10; @@ -969,7 +969,7 @@ static void processPatternKeyboard(Music* music) { s32 val = -1; - char sym = tic->ram.input.keyboard.text; + char sym = getKeyboardText(); if(sym >= '0' && sym <= '9') val = sym - '0'; diff --git a/src/studio.c b/src/studio.c index 9246521..a080c93 100644 --- a/src/studio.c +++ b/src/studio.c @@ -231,6 +231,36 @@ 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 1978066..aecf933 100644 --- a/src/studio.h +++ b/src/studio.h @@ -157,6 +157,7 @@ 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 f0b4b49..d0f6ba6 100644 --- a/src/system.c +++ b/src/system.c @@ -67,11 +67,6 @@ static struct bool state[tic_keys_count]; - struct - { - bool state[tic_keys_count]; - } touch; - } keyboard; u32 touchCounter; @@ -424,33 +419,8 @@ static void processKeyboard() enum{BufSize = COUNT_OF(input->keyboard.keys)}; for(s32 i = 0, c = 0; i < COUNT_OF(platform.keyboard.state) && c < BufSize; i++) - if(platform.keyboard.state[i] || platform.keyboard.touch.state[i]) + if(platform.keyboard.state[i]) input->keyboard.keys[c++] = i; - - if(input->keyboard.text == 0) - { - 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); - - input->keyboard.text = caps - ? key >= tic_key_a && key <= tic_key_z - ? shift ? Symbols[key] : Shift[key] - : shift ? Shift[key] : Symbols[key] - : shift ? Shift[key] : Symbols[key]; - } - } - } } #if !defined(__EMSCRIPTEN__) && !defined(__MACOSX__) @@ -778,7 +748,6 @@ static void pollEvent() tic80_input* input = &tic->ram.input; input->mouse.btns = 0; - input->keyboard.text = 0; SDL_Event event; @@ -829,7 +798,9 @@ static void pollEvent() updateGamepadParts(); } break; - case SDL_WINDOWEVENT_FOCUS_GAINED: platform.studio->updateProject(); break; + case SDL_WINDOWEVENT_FOCUS_GAINED: + platform.studio->updateProject(); + break; } break; case SDL_KEYDOWN: @@ -837,15 +808,7 @@ static void pollEvent() break; case SDL_KEYUP: handleKeydown(event.key.keysym.sym, false); - break; - case SDL_TEXTINPUT: - { - const char* text = event.text.text; - - if(strlen(text) == 1) - tic->ram.input.keyboard.text = *text; - } - break; + break; case SDL_QUIT: platform.studio->exit(); break; diff --git a/src/tic.c b/src/tic.c index 6997346..28f5299 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) == 16); +STATIC_ASSERT(tic80_input, sizeof(tic80_input) == 12); 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 361a285..19b5738 100644 --- a/src/tic.h +++ b/src/tic.h @@ -422,7 +422,7 @@ typedef union tic_tiles sprites; tic_map map; tic80_input input; - u8 unknown[12]; + u8 unknown[16]; tic_sound_register registers[TIC_SOUND_CHANNELS]; tic_sfx sfx; tic_music music;