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.<nome>").
- Harness: cenarios 37 (teclado) e 38 (.sb com define).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-30 00:30:53 -03:00
1 parent c9ff13bb77
commit cea7cb5ba4
6 files changed
+125 -5

No files matched your search

@@ -197,7 +197,7 @@ enum { CV_CODE, CV_STAGE };
#define MAX_GROUPS 32 #define MAX_GROUPS 32
#define GRP_HEAD_H 28 /* faixa do titulo (arrastar = mover tudo) */ #define GRP_HEAD_H 28 /* faixa do titulo (arrastar = mover tudo) */
#define GRP_GRIP 16 /* canto de baixo a direita: redimensionar */ #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_CALL_DEPTH 32
#define MAX_RUN_BLOCKS 200000 #define MAX_RUN_BLOCKS 200000
#define STAGE_BUF 16384 #define STAGE_BUF 16384
@@ -1115,6 +1115,8 @@ typedef struct Ed {
bool te_ws; /* desenhado na area de scripts (zoom) */ bool te_ws; /* desenhado na area de scripts (zoom) */
bool ws_drawing; /* desenhando a area de scripts agora */ bool ws_drawing; /* desenhando a area de scripts agora */
int te_owner; /* TE_FIELD / TE_CODE */ 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_drag; /* mouse estendendo a selecao */
bool te_handles; /* toque: alcas + barra de acoes */ bool te_handles; /* toque: alcas + barra de acoes */
int te_handle_drag; /* alca arrastada (0 = inicio, 1 = fim) */ int te_handle_drag; /* alca arrastada (0 = inicio, 1 = fim) */
+25 -2
View File
@@ -2429,8 +2429,9 @@ static bool type_hidden(Ed* ed, const TinyBlocks_TypeBlock* t) {
static void cats_rebuild(Ed* ed) { static void cats_rebuild(Ed* ed) {
ed->cat_n = 0; ed->cat_n = 0;
/* 1) Categoria declarada por cada library, na ordem de registro */ /* 1) Categoria declarada por cada library, na ordem de registro (a ultima
for (uint32_t i = 0; i < tb_rt_library_count(ed->rt) && ed->cat_n < MAX_CATS; i++) { 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); TinyBlocks_LibraryBlock* lib = tb_rt_library_at(ed->rt, i);
const TinyBlocks_Category* cat = tb_library_category(lib); const TinyBlocks_Category* cat = tb_library_category(lib);
if (!cat || !cat->name) continue; 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_load(ed, B, &CT);
bp_tab_dispose(B); bp_tab_dispose(B);
define_sync(ed); /* tipos User.* dos "define" ja ao abrir, nao so na bandeira */
layout_tab(&CT); layout_tab(&CT);
cleanup_ws(ed); /* arruma as pilhas uma embaixo da outra */ cleanup_ws(ed); /* arruma as pilhas uma embaixo da outra */
palette_rebuild(ed); 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); te_field_xy(ed, off, x, y, lh);
return true; 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) { 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); 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)) { 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); 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_touch = tj && tb_json_array_length(tj) > 0; /* dedo, nao mouse */
ed->press_t = now_ms(); ed->press_t = now_ms();
@@ -9641,6 +9663,7 @@ static bool editor_on_draw(TinyBlocks_Block* self, TinyBlocks* rt,
render_ctx_menu(ed, rb); render_ctx_menu(ed, rb);
modals_render(ed, rb); /* de baixo para cima; configuracoes por cima de tudo */ 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_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 */ render_toasts(ed, rb); /* avisos flutuam por cima de tudo */
tb_a11y_end(ax(ed)); tb_a11y_end(ax(ed));
ed->ax_on = false; ed->ax_on = false;
@@ -127,6 +127,9 @@ typedef struct TinyBlocks_GraphicsBackend {
/* Area de transferencia (opcionais; NULL = memoria interna do graphics) */ /* Area de transferencia (opcionais; NULL = memoria interna do graphics) */
bool (*clipboard_set)(const char* text); bool (*clipboard_set)(const char* text);
char* (*clipboard_get)(void); /* malloc; NULL = vazia */ 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; } TinyBlocks_GraphicsBackend;
/* Símbolo exportado por um backend carregado como .so */ /* 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 bool tb_graphics_set_clipboard(const char* text);
TB_API char* tb_graphics_get_clipboard(void); 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 */ /* Loop principal: roda todas as janelas abertas até a principal fechar */
TB_API void tb_graphics_run(TinyBlocks* rt); TB_API void tb_graphics_run(TinyBlocks* rt);
TB_API bool tb_graphics_window_is_open(TinyBlocks_Block* window); TB_API bool tb_graphics_window_is_open(TinyBlocks_Block* window);
@@ -162,6 +162,11 @@ char* tb_graphics_get_clipboard(void) {
return s_clip ? strdup(s_clip) : NULL; 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) { float tb_graphics_measure_text(const char* text, float font_size) {
if (!text || !text[0]) return 0.f; if (!text || !text[0]) return 0.f;
const TinyBlocks_GraphicsBackend* b = backend_ready(); const TinyBlocks_GraphicsBackend* b = backend_ready();
+17 -1
View File
@@ -682,7 +682,10 @@ static bool be_init(void) {
return false; return false;
} }
SDL_EventState(SDL_DROPFILE, SDL_ENABLE); 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(); ui_scale_begin();
sensors_open(); sensors_open();
s_be_inited = true; s_be_inited = true;
@@ -925,6 +928,18 @@ static char* be_clipboard_get(void) {
return r; 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: /* Captura para conferir o visual sem olhar a tela de ninguem:
TINYBLOCKS_SHOT=arquivo.bmp salva a 1a janela depois de TINYBLOCKS_SHOT=arquivo.bmp salva a 1a janela depois de
TINYBLOCKS_SHOT_FRAMES quadros (padrao 30) e sai. Com TINYBLOCKS_SHOT_COUNT=n 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, .frame_wait = be_frame_wait,
.clipboard_set = be_clipboard_set, .clipboard_set = be_clipboard_set,
.clipboard_get = be_clipboard_get, .clipboard_get = be_clipboard_get,
.text_input = be_text_input,
}; };
const TinyBlocks_GraphicsBackend* tb_sdl2_graphics_backend(void) { const TinyBlocks_GraphicsBackend* tb_sdl2_graphics_backend(void) {
+66 -1
View File
@@ -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) #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 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) { static void drag(float x0, float y0, float x1, float y1) {
frame(x0, y0, 1, 0, 0, 0, NULL); frame(x0, y0, 1, 0, 0, 0, NULL);
@@ -392,7 +403,9 @@ int main(void) {
unsetenv("TINYBLOCKS_SETTINGS"); unsetenv("TINYBLOCKS_SETTINGS");
remove("/tmp/tb_harness_cfg/tinyblocks/settings.csv"); remove("/tmp/tb_harness_cfg/tinyblocks/settings.csv");
tinyblocks_init(&rt, NULL); 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_events_get_library());
tb_library_register(rt, tb_control_get_library()); tb_library_register(rt, tb_control_get_library());
tb_library_register(rt, tb_errors_get_library()); tb_library_register(rt, tb_errors_get_library());
@@ -1923,6 +1936,58 @@ int main(void) {
free(got_parts); 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.<nome>" 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); tinyblocks_deinit(&rt);
printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails); printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails);
return fails != 0; return fails != 0;