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 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) */
+25 -2
View File
@@ -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;
@@ -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);
@@ -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();
+17 -1
View File
@@ -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) {