From cea7cb5ba497ae65d4638afd609729eb8b3cdb26 Mon Sep 17 00:00:00 2001 From: mguinhos Date: Fri, 30 Oct 2026 00:30:53 -0300 Subject: [PATCH] Editor: teclado na tela nos campos, "Meus Blocos" sempre e "define" ao abrir .sb - graphics: text_input(on, x, y, w, h) opcional no backend e tb_graphics_text_input. O editor avisa o campo em foco (e o cursor) quando muda ou quando o campo e tocado de novo; o SDL liga/desliga a entrada de texto (teclado na tela no Android) e posiciona o IME. No desktop a entrada de texto segue sempre ligada. O editor de codigo so pede o teclado depois de um toque nele. - Categorias: MAX_CATS 24 -> 64 e a ultima vaga reservada para "Meus Blocos" (com 3D, Shader, PNG... as ultimas categorias sumiam). - Abrir um .sb com "define": os tipos User.* sao criados ao carregar a aba, nao so ao apertar a bandeira (as chamadas apareciam como "User."). - Harness: cenarios 37 (teclado) e 38 (.sb com define). Co-Authored-By: Claude Opus 5.5 --- .../include/tb_editor_internal.h | 4 +- libraries/tinyblocks-editor/src/tb_editor.c | 27 +++++++- .../tinyblocks-graphics/include/tb_graphics.h | 9 +++ .../tinyblocks-graphics/src/tb_graphics.c | 5 ++ libraries/tinyblocks-sdl2/src/tb_sdl2.c | 18 ++++- tests/editor_harness.c | 67 ++++++++++++++++++- 6 files changed, 125 insertions(+), 5 deletions(-) diff --git a/libraries/tinyblocks-editor/include/tb_editor_internal.h b/libraries/tinyblocks-editor/include/tb_editor_internal.h index d47e09d..7e30859 100644 --- a/libraries/tinyblocks-editor/include/tb_editor_internal.h +++ b/libraries/tinyblocks-editor/include/tb_editor_internal.h @@ -197,7 +197,7 @@ enum { CV_CODE, CV_STAGE }; #define MAX_GROUPS 32 #define GRP_HEAD_H 28 /* faixa do titulo (arrastar = mover tudo) */ #define GRP_GRIP 16 /* canto de baixo a direita: redimensionar */ -#define MAX_CATS 24 +#define MAX_CATS 64 #define MAX_CALL_DEPTH 32 #define MAX_RUN_BLOCKS 200000 #define STAGE_BUF 16384 @@ -1115,6 +1115,8 @@ typedef struct Ed { bool te_ws; /* desenhado na area de scripts (zoom) */ bool ws_drawing; /* desenhando a area de scripts agora */ int te_owner; /* TE_FIELD / TE_CODE */ + bool ti_on, ti_kick; /* campo avisado ao SO; toque neste quadro */ + float ti_x, ti_y, ti_h; /* cursor avisado (teclado na tela / IME) */ bool te_drag; /* mouse estendendo a selecao */ bool te_handles; /* toque: alcas + barra de acoes */ int te_handle_drag; /* alca arrastada (0 = inicio, 1 = fim) */ diff --git a/libraries/tinyblocks-editor/src/tb_editor.c b/libraries/tinyblocks-editor/src/tb_editor.c index cd2416b..859a347 100644 --- a/libraries/tinyblocks-editor/src/tb_editor.c +++ b/libraries/tinyblocks-editor/src/tb_editor.c @@ -2429,8 +2429,9 @@ static bool type_hidden(Ed* ed, const TinyBlocks_TypeBlock* t) { static void cats_rebuild(Ed* ed) { ed->cat_n = 0; - /* 1) Categoria declarada por cada library, na ordem de registro */ - for (uint32_t i = 0; i < tb_rt_library_count(ed->rt) && ed->cat_n < MAX_CATS; i++) { + /* 1) Categoria declarada por cada library, na ordem de registro (a ultima + vaga fica para "Meus Blocos", que nunca pode sumir) */ + for (uint32_t i = 0; i < tb_rt_library_count(ed->rt) && ed->cat_n < MAX_CATS - 1; i++) { TinyBlocks_LibraryBlock* lib = tb_rt_library_at(ed->rt, i); const TinyBlocks_Category* cat = tb_library_category(lib); if (!cat || !cat->name) continue; @@ -3986,6 +3987,7 @@ static bool sb_import_text_(Ed* ed, const char* text, const char* name) { } bp_tab_load(ed, B, &CT); bp_tab_dispose(B); + define_sync(ed); /* tipos User.* dos "define" ja ao abrir, nao so na bandeira */ layout_tab(&CT); cleanup_ws(ed); /* arruma as pilhas uma embaixo da outra */ palette_rebuild(ed); @@ -7440,6 +7442,25 @@ static bool te_owner_xy(Ed* ed, int owner, int off, float* x, float* y, float* l te_field_xy(ed, off, x, y, lh); return true; } +/* Avisa o SO do campo em foco: no celular o teclado na tela abre e fecha + com o foco; no desktop o IME fica junto do cursor. O editor de codigo so + pede o teclado depois de um toque nele (ver codigo nao abre o teclado), e + tocar de novo num campo reabre o teclado que o usuario fechou. */ +static void te_sync_os(Ed* ed) { + int owner = te_cur_owner(ed); + bool want = owner == TE_FIELD || (owner == TE_CODE && (ed->ti_on || ed->ti_kick)); + float x = 0.f, y = 0.f, lh = 0.f; + if (want) { + int a, b; + te_owner_sel(ed, owner, &a, &b); + if (!te_owner_xy(ed, owner, owner == TE_FIELD ? ed->te_caret : b, &x, &y, &lh)) want = false; + } + bool moved = want && (fabsf(x - ed->ti_x) > 0.5f || fabsf(y - ed->ti_y) > 0.5f || fabsf(lh - ed->ti_h) > 0.5f); + if (want != ed->ti_on || moved || (want && ed->ti_kick)) + tb_graphics_text_input(want, x, y, 2.f, lh); + ed->ti_on = want; ed->ti_x = x; ed->ti_y = y; ed->ti_h = lh; + ed->ti_kick = false; +} static void te_owner_word(Ed* ed, int owner, int off) { if (owner == TE_CODE) code_select_word(&CT, off); else te_select_word(ed, off); } @@ -9361,6 +9382,7 @@ static void ed_handle_input(Ed* ed, TinyBlocks_Block* evb, int from) { } if (ev_int(evb, GFX_IN_MOUSE_L_DOWN)) { + ed->ti_kick = true; /* toque num campo reabre o teclado */ const TinyBlocks_JsonValue* tj = ev_json(evb, GFX_IN_TOUCHES); ed->press_touch = tj && tb_json_array_length(tj) > 0; /* dedo, nao mouse */ ed->press_t = now_ms(); @@ -9641,6 +9663,7 @@ static bool editor_on_draw(TinyBlocks_Block* self, TinyBlocks* rt, render_ctx_menu(ed, rb); modals_render(ed, rb); /* de baixo para cima; configuracoes por cima de tudo */ te_render_overlay(ed, rb); /* alcas e barra da selecao (toque) */ + te_sync_os(ed); /* teclado na tela / IME do campo em foco */ render_toasts(ed, rb); /* avisos flutuam por cima de tudo */ tb_a11y_end(ax(ed)); ed->ax_on = false; diff --git a/libraries/tinyblocks-graphics/include/tb_graphics.h b/libraries/tinyblocks-graphics/include/tb_graphics.h index 2dd5f61..44c9ba9 100644 --- a/libraries/tinyblocks-graphics/include/tb_graphics.h +++ b/libraries/tinyblocks-graphics/include/tb_graphics.h @@ -127,6 +127,9 @@ typedef struct TinyBlocks_GraphicsBackend { /* Area de transferencia (opcionais; NULL = memoria interna do graphics) */ bool (*clipboard_set)(const char* text); char* (*clipboard_get)(void); /* malloc; NULL = vazia */ + /* Campo de texto em foco (opcional): o SO mostra o teclado na tela e poe o + IME junto do cursor. x y w h na janela (coordenadas da cena) */ + void (*text_input)(bool on, float x, float y, float w, float h); } TinyBlocks_GraphicsBackend; /* Símbolo exportado por um backend carregado como .so */ @@ -150,6 +153,12 @@ TB_API float tb_graphics_measure_text(const char* text, float font_size); TB_API bool tb_graphics_set_clipboard(const char* text); TB_API char* tb_graphics_get_clipboard(void); +/* Diz ao sistema que um campo de texto esta em foco, com o cursor em + (x, y, w, h) na janela: no celular abre o teclado, no desktop posiciona o + IME. on=false: nenhum campo (o teclado na tela fecha). Chamar de novo com + on=true reabre o teclado que o usuario fechou. */ +TB_API void tb_graphics_text_input(bool on, float x, float y, float w, float h); + /* Loop principal: roda todas as janelas abertas até a principal fechar */ TB_API void tb_graphics_run(TinyBlocks* rt); TB_API bool tb_graphics_window_is_open(TinyBlocks_Block* window); diff --git a/libraries/tinyblocks-graphics/src/tb_graphics.c b/libraries/tinyblocks-graphics/src/tb_graphics.c index 77708bd..cadbece 100644 --- a/libraries/tinyblocks-graphics/src/tb_graphics.c +++ b/libraries/tinyblocks-graphics/src/tb_graphics.c @@ -162,6 +162,11 @@ char* tb_graphics_get_clipboard(void) { return s_clip ? strdup(s_clip) : NULL; } +void tb_graphics_text_input(bool on, float x, float y, float w, float h) { + const TinyBlocks_GraphicsBackend* b = backend_ready(); + if (b->text_input) b->text_input(on, x, y, w, h); +} + float tb_graphics_measure_text(const char* text, float font_size) { if (!text || !text[0]) return 0.f; const TinyBlocks_GraphicsBackend* b = backend_ready(); diff --git a/libraries/tinyblocks-sdl2/src/tb_sdl2.c b/libraries/tinyblocks-sdl2/src/tb_sdl2.c index 562d585..50ec5e3 100644 --- a/libraries/tinyblocks-sdl2/src/tb_sdl2.c +++ b/libraries/tinyblocks-sdl2/src/tb_sdl2.c @@ -682,7 +682,10 @@ static bool be_init(void) { return false; } SDL_EventState(SDL_DROPFILE, SDL_ENABLE); - SDL_StartTextInput(); + /* Com teclado na tela (celular) a entrada de texto so liga com um campo + em foco (be_text_input); senao o teclado abriria ja na partida */ + if (SDL_HasScreenKeyboardSupport()) SDL_StopTextInput(); + else SDL_StartTextInput(); ui_scale_begin(); sensors_open(); s_be_inited = true; @@ -925,6 +928,18 @@ static char* be_clipboard_get(void) { return r; } +static void be_text_input(bool on, float x, float y, float w, float h) { + if (!be_init()) return; + if (on) { + const float S = s_ui_scale > 0.f ? s_ui_scale : 1.f; + SDL_Rect r = { (int)(x * S), (int)(y * S), (int)(w * S + 0.5f), (int)(h * S + 0.5f) }; + SDL_SetTextInputRect(&r); + SDL_StartTextInput(); /* reabre o teclado se foi fechado */ + } else if (SDL_HasScreenKeyboardSupport()) { + SDL_StopTextInput(); /* desktop: o texto segue ligado (atalhos) */ + } +} + /* Captura para conferir o visual sem olhar a tela de ninguem: TINYBLOCKS_SHOT=arquivo.bmp salva a 1a janela depois de TINYBLOCKS_SHOT_FRAMES quadros (padrao 30) e sai. Com TINYBLOCKS_SHOT_COUNT=n @@ -1004,6 +1019,7 @@ static const TinyBlocks_GraphicsBackend s_sdl2_backend = { .frame_wait = be_frame_wait, .clipboard_set = be_clipboard_set, .clipboard_get = be_clipboard_get, + .text_input = be_text_input, }; const TinyBlocks_GraphicsBackend* tb_sdl2_graphics_backend(void) { diff --git a/tests/editor_harness.c b/tests/editor_harness.c index 876919b..c492366 100644 --- a/tests/editor_harness.c +++ b/tests/editor_harness.c @@ -148,6 +148,17 @@ static void dump_console(void) { #define CHECK(cond, msg) do { if (cond) printf("ok %s\n", msg); else { printf("FAIL %s\n", msg); fails++; dump_console(); } } while (0) +/* Backend de teste: o nulo, anotando o que o editor diz do campo em foco */ +static TinyBlocks_GraphicsBackend g_be; +static bool g_ti_on; +static int g_ti_calls; +static float g_ti_x, g_ti_y, g_ti_h; +static void rec_text_input(bool on, float x, float y, float w, float h) { + (void)w; + g_ti_on = on; g_ti_calls++; + g_ti_x = x; g_ti_y = y; g_ti_h = h; +} + static void click(float x, float y) { frame(x, y, 1, 0, 0, 0, NULL); frame(x, y, 0, 1, 0, 0, NULL); } static void drag(float x0, float y0, float x1, float y1) { frame(x0, y0, 1, 0, 0, 0, NULL); @@ -392,7 +403,9 @@ int main(void) { unsetenv("TINYBLOCKS_SETTINGS"); remove("/tmp/tb_harness_cfg/tinyblocks/settings.csv"); tinyblocks_init(&rt, NULL); - tb_graphics_set_backend(tb_graphics_null_backend()); + g_be = *tb_graphics_null_backend(); /* nulo + registro do text_input */ + g_be.text_input = rec_text_input; + tb_graphics_set_backend(&g_be); tb_library_register(rt, tb_events_get_library()); tb_library_register(rt, tb_control_get_library()); tb_library_register(rt, tb_errors_get_library()); @@ -1923,6 +1936,58 @@ int main(void) { free(got_parts); } + /* 37. O SO sabe do campo de texto em foco (teclado na tela / IME): + * abre com o campo, segue o cursor, fecha sem campo e tocar de novo + * no campo reabre o teclado que o usuario fechou */ + { + key(TB_KEY_ESCAPE, 0, 0); + frame(0, 0, 0, 0, 0, 0, NULL); + CHECK(!g_ti_on, "teclado: sem campo em foco, fechado"); + cat("Meus Blocos"); + ax_do(ax_need(pal(), TB_A11Y_BUTTON, "Criar um Bloco"), TB_A11Y_ACT_ACTIVATE); + frame(0, 0, 0, 0, 0, 0, NULL); + int f = ax_in(ax_in(0, TB_A11Y_DIALOG, "Criar um Bloco"), TB_A11Y_TEXT_FIELD, "Nome do bloco"); + CHECK(f >= 0 && g_ti_on, "teclado: abre com o campo \"Nome do bloco\""); + CHECK(f >= 0 && g_ti_x >= N(f)->x && g_ti_x <= N(f)->x + N(f)->w && + g_ti_y + g_ti_h > N(f)->y && g_ti_y < N(f)->y + N(f)->h && g_ti_h > 4.f, + "teclado: o cursor avisado fica dentro do campo"); + float x0 = g_ti_x; + type("abc"); + CHECK(g_ti_on && g_ti_x > x0 + 5.f, "teclado: o cursor avisado anda com o texto"); + g_ti_on = false; /* o usuario fechou o teclado */ + int calls = g_ti_calls; + f = ax_in(ax_in(0, TB_A11Y_DIALOG, "Criar um Bloco"), TB_A11Y_TEXT_FIELD, "Nome do bloco"); + click(N(f)->x + N(f)->w * 0.5f, N(f)->y + N(f)->h * 0.5f); + frame(0, 0, 0, 0, 0, 0, NULL); + CHECK(g_ti_on && g_ti_calls > calls, "teclado: tocar de novo no campo reabre o teclado"); + calls = g_ti_calls; + for (int i = 0; i < 3; i++) frame(0, 0, 0, 0, 0, 0, NULL); + CHECK(g_ti_calls == calls, "teclado: parado, nao repete o aviso a cada quadro"); + key(TB_KEY_ESCAPE, 0, 0); + frame(0, 0, 0, 0, 0, 0, NULL); + CHECK(ax_in(0, TB_A11Y_DIALOG, "Criar um Bloco") < 0 && !g_ti_on, "teclado: fechar o dialogo fecha o teclado"); + } + + /* 38. Abrir um .sb com "define": as chamadas ja aparecem como o bloco + * (nao "User." cinza) antes de apertar a bandeira */ + { + const char* path = "/tmp/tb_harness_define.sb"; + FILE* f = fopen(path, "wb"); + if (f) { + fputs("when flag clicked\nmonte a torre\n\ndefine monte a torre\nsay [oi]\n", f); + fclose(f); + } + bool err = false; + char a[128]; + snprintf(a, sizeof(a), "{\"path\":\"%s\"}", path); + free(mcp("open_project", a, &err, NULL)); + frame(0, 0, 0, 0, 0, 0, NULL); + CHECK(!err && ax_part(ws(), TB_A11Y_BLOCK, "monte a torre") >= 0 && + ax_part(ws(), TB_A11Y_BLOCK, "User.monte a torre") < 0, + "abrir .sb: a chamada do \"define\" ja e o bloco, sem rodar"); + remove(path); + } + tinyblocks_deinit(&rt); printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails); return fails != 0;