From b610a0590445f3794b4febf61dea68018c888993 Mon Sep 17 00:00:00 2001 From: mguinhos Date: Thu, 22 Oct 2026 22:35:58 -0300 Subject: [PATCH] =?UTF-8?q?"defina"=20mostra=20o=20prot=C3=B3tipo=20do=20b?= =?UTF-8?q?loco,=20com=20os=20par=C3=A2metros=20arrast=C3=A1veis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - O chapéu "defina" desenha o protótipo (nome, rótulos e parâmetros como repórteres) em vez de um campo de texto com o nome; arrastar um parâmetro de dentro dele cria o "parâmetro [x]" (como no Scratch). Os parâmetros não são mais criados soltos ao lado do chapéu. - O nome não é mais editável no chapéu (mudá-lo ali deixava o bloco registrado com o nome antigo e um segundo bloco não aparecia na paleta). Botão direito no "defina": "Editar definição" abre o diálogo; o "defina" não se duplica. - Um "defina" sem bloco registrado (projeto antigo, nome mudado) é registrado ao rodar. - "parâmetro" sai da paleta nas abas comuns (vem do "defina"). - Harness: protótipo, arrastar o parâmetro e dois blocos na paleta. Co-Authored-By: Claude Opus 5.5 --- libraries/tinyblocks-editor/src/tb_editor.c | 151 +++++++++++++++++--- tests/editor_harness.c | 27 +++- 2 files changed, 156 insertions(+), 22 deletions(-) diff --git a/libraries/tinyblocks-editor/src/tb_editor.c b/libraries/tinyblocks-editor/src/tb_editor.c index 65e1fef..09f0459 100644 --- a/libraries/tinyblocks-editor/src/tb_editor.c +++ b/libraries/tinyblocks-editor/src/tb_editor.c @@ -538,9 +538,11 @@ static const char* part_str(const Part* p, char* buf, size_t cap) { return buf; } +#define PART_PROTO (-2) /* prototipo do bloco dentro do chapeu "defina" */ + static void add_part(EdBlock* b, int arg, const char* s, int len) { if (b->part_n >= MAX_PARTS) return; - if (arg < 0) { + if (arg == -1) { while (len > 0 && *s == ' ') { s++; len--; } while (len > 0 && s[len-1] == ' ') len--; if (len <= 0) return; @@ -557,6 +559,12 @@ static void build_parts(EdBlock* b) { int n = input_count(t); bool used[MAX_INPUTS] = {0}; const BlockSpec* sp = spec_of(t->type_name); + if (t->type_name && strcmp(t->type_name, "Port.Define") == 0) { + /* "defina [prototipo]": o nome (campo 0) nao e editavel aqui */ + add_part(b, -1, "defina", 6); + add_part(b, PART_PROTO, NULL, 0); + return; + } if (sp) { const char* p = sp->label; const char* seg = p; while (*p) { @@ -601,6 +609,63 @@ static void build_parts(EdBlock* b) { static void measure(Tab* T, int bi); +/* == Prototipo dentro do "defina" (como no Scratch) === + * Um bloco interno com o nome e os parametros do bloco definido; cada + * parametro e um reporter que se arrasta para fora ("parâmetro [x]"). */ +#define PROTO_H 30.f +#define PROTO_PAD 8.f +#define PILL_H 22.f +#define PROTO_ITEMS (MAX_UBLK_PARTS + 1) + +typedef struct { + int n; + float x[PROTO_ITEMS], w[PROTO_ITEMS]; + const char* txt[PROTO_ITEMS]; + bool param[PROTO_ITEMS]; +} ProtoLayout; + +/* Itens do prototipo, x relativo ao comeco do bloco interno */ +static void proto_layout(const EdBlock* b, ProtoLayout* L) { + const char* name = b->in_text[0]; + UserBlockDef* def = g_active_ed ? user_def_find(g_active_ed, name) : NULL; + float x = PROTO_PAD; + L->n = 0; + L->txt[0] = name[0] ? name : "bloco"; L->param[0] = false; + L->x[0] = x; L->w[0] = tw(L->txt[0], FS); x += L->w[0] + BLK_GAP; L->n = 1; + for (int k = 0; def && k < def->part_n && L->n < PROTO_ITEMS; k++) { + const UserParam* pp = &def->parts[k]; + bool par = pp->kind != PARAM_LABEL; + const char* t = pp->name[0] ? pp->name : (par ? "entrada" : ""); + if (!t[0]) continue; + int i = L->n++; + L->txt[i] = t; L->param[i] = par; L->x[i] = x; + L->w[i] = tw(t, FS) + (par ? 20.f : 0.f); + x += L->w[i] + BLK_GAP; + } +} + +static float proto_measure(const EdBlock* b) { + ProtoLayout L; + proto_layout(b, &L); + return L.x[L.n - 1] + L.w[L.n - 1] + PROTO_PAD; +} + +/* Parametro sob (wx, wy) num "defina" ja medido; -1 se nenhum */ +static int proto_param_at(const EdBlock* b, float wx, float wy, ProtoLayout* L, float* px, float* py) { + for (int i = 0; i < b->part_n; i++) { + if (b->parts[i].arg != PART_PROTO) continue; + proto_layout(b, L); + float bx = b->ax + b->parts[i].x; + float cy = b->ay + b->row_y + b->row_h * 0.5f; + for (int k = 0; k < L->n; k++) { + if (!L->param[k]) continue; + float x0 = bx + L->x[k], y0 = cy - PILL_H * 0.5f; + if (in_rect(wx, wy, x0, y0, L->w[k], PILL_H)) { *px = x0; *py = y0; return k; } + } + } + return -1; +} + static float measure_chain(Tab* T, int head) { float h = 0; for (int c = head; c >= 0; c = T->ws[c].next) { measure(T, c); h += T->ws[c].h; } @@ -620,7 +685,10 @@ static void measure(Tab* T, int bi) { for (int i = 0; i < b->part_n; i++) { Part* p = &b->parts[i]; p->x = x; - if (p->arg < 0) { + if (p->arg == PART_PROTO) { + p->w = proto_measure(b); + if (PROTO_H + 8 > row_h) row_h = PROTO_H + 8; + } else if (p->arg < 0) { p->w = tw(part_str(p, buf, sizeof(buf)), FS); } else { int s = p->arg; float aw, ah; @@ -848,7 +916,23 @@ static void render_block(Ed* ed, TinyBlocks_RenderableBlock* rb, Tab* T, int bi, bool can_edit = ed->editing && T == &CT && ed->edit_bi == bi; for (int i = 0; i < b->part_n; i++) { const Part* p = &b->parts[i]; - if (p->arg < 0) + if (p->arg == PART_PROTO) { + ProtoLayout L; + proto_layout(b, &L); + float px = x + p->x, py = cy - PROTO_H * 0.5f; + rb_fill_rrect(rb, px, py, p->w, PROTO_H, 6, d); + rb_fill_rrect(rb, px + 1, py + 1, p->w - 2, PROTO_H - 2, 5, col_mul(c, 0.92f)); + for (int k = 0; k < L.n; k++) { + if (L.param[k]) { /* parametro: reporter arrastavel */ + float qx = px + L.x[k], qy = cy - PILL_H * 0.5f; + rb_fill_rrect(rb, qx, qy, L.w[k], PILL_H, PILL_H * 0.5f, d); + rb_fill_rrect(rb, qx + 1, qy + 1, L.w[k] - 2, PILL_H - 2, PILL_H * 0.5f - 1, c); + rb_text_c(rb, qx + 10, cy, L.txt[k], COL_WHITE, FS); + } else { + rb_text_c(rb, px + L.x[k], cy, L.txt[k], COL_WHITE, FS); + } + } + } else if (p->arg < 0) rb_text_c(rb, x + p->x, cy, part_str(p, buf, sizeof(buf)), COL_WHITE, FS); else if (b->args[p->arg] < 0) draw_slot(rb, b, p->arg, x, y, d, can_edit && ed->edit_in == p->arg); @@ -1490,6 +1574,19 @@ static void comp_make_type(Ed* ed, UserBlockDef* def); Nao mexe durante a execucao. */ static void define_sync(Ed* ed) { if (ed->vm_running) return; + for (int t = 0; t < ed->tab_n; t++) { + Tab* T = &ed->tabs[t]; + if (!tab_class(T)->blocks) continue; + for (int i = 0; i < T->top_n; i++) { + EdBlock* b = &T->ws[T->top[i]]; + if (!b->used || strcmp(b->type_name, "Port.Define") != 0 || !b->in_text[0][0]) continue; + if (user_def_find(ed, b->in_text[0]) || ed->user_block_n >= MAX_USER_BLOCKS) continue; + UserBlockDef* def = &ed->user_blocks[ed->user_block_n++]; + memset(def, 0, sizeof(*def)); + def->tab_idx = -1; + snprintf(def->name, sizeof(def->name), "%.47s", b->in_text[0]); + } + } for (int u = 0; u < ed->user_block_n; u++) { UserBlockDef* def = &ed->user_blocks[u]; int t, hat = define_find(ed, def->name, &t); @@ -1510,7 +1607,6 @@ static void define_sync(Ed* ed) { "parametro" de cada entrada ao lado (para arrastar para o corpo) */ static void define_place(Ed* ed, UserBlockDef* def) { TinyBlocks_TypeBlock* t_def = tb_type_block_find(ed->rt, "Port.Define"); - TinyBlocks_TypeBlock* t_in = tb_type_block_find(ed->rt, "Port.In"); if (!t_def) return; Tab* T = &CT; layout_tab(T); @@ -1527,17 +1623,6 @@ static void define_place(Ed* ed, UserBlockDef* def) { T->ws[hat].y = y; top_add(T, hat); layout_tab(T); - float px = 40 + T->ws[hat].w + 24; - for (int k = 0; t_in && k < def->input_count; k++) { - int bi = blk_new(T, t_in); - if (bi < 0) break; - snprintf(T->ws[bi].in_text[0], sizeof(T->ws[bi].in_text[0]), "%s", def->input_names[k]); - T->ws[bi].x = px; - T->ws[bi].y = y + 6; - top_add(T, bi); - layout_tab(T); - px += T->ws[bi].w + 12; - } /* rola para o chapeu novo ficar a vista */ T->ws_sx = 0; if (y - T->ws_sy > WS_H - 120 || y < T->ws_sy) T->ws_sy = y - 60; @@ -1636,6 +1721,7 @@ static bool type_hidden(Ed* ed, const TinyBlocks_TypeBlock* t) { const char* n = t->type_name; if (!n || n[0] == '_' || strstr(n, "._")) return true; if (t->flags & TB_TYPE_HIDDEN) return true; /* infraestrutura / "defina" */ + if (strcmp(n, "Port.In") == 0) return !CT.is_composite; /* sai de dentro do "defina" */ if (strncmp(n, "Port.", 5) == 0) return !CT.is_composite && !tab_has_define(&CT); return false; } @@ -5662,7 +5748,14 @@ static void ctx_exec(Ed* ed, int id) { break; } case CM_DELETE: delete_heal(ed, T, bi); break; - case CM_EDIT_DEF: if (T->ws[bi].type) open_def(ed, T->ws[bi].type->type_name + 5); break; + case CM_EDIT_DEF: + if (T->ws[bi].type && !strcmp(T->ws[bi].type->type_name, "Port.Define")) { + UserBlockDef* def = user_def_find(ed, T->ws[bi].in_text[0]); /* nome e parametros */ + if (def) dlg_open(ed, (int)(def - ed->user_blocks)); + } else if (T->ws[bi].type) { + open_def(ed, T->ws[bi].type->type_name + 5); + } + break; case CM_CLEANUP: cleanup_ws(ed); break; case CM_NEW_COMPOSITE: new_composite_tab(ed); break; case CM_GROUP_NEW: { @@ -5940,6 +6033,25 @@ static void on_lmb_down(Ed* ed, float mx, float my) { ed->pan_sx = CT.ws_sx; ed->pan_sy = CT.ws_sy; return; } + { + ProtoLayout L; float qx, qy; + int k = proto_param_at(&CT.ws[bi], wx, wy, &L, &qx, &qy); + TinyBlocks_TypeBlock* t_in = k >= 0 ? tb_type_block_find(ed->rt, "Port.In") : NULL; + int ni = t_in ? blk_new(&CT, t_in) : -1; + if (ni >= 0) { + snprintf(CT.ws[ni].in_text[0], sizeof(CT.ws[ni].in_text[0]), "%s", L.txt[k]); + top_add(&CT, ni); + CT.ws[ni].x = qx; CT.ws[ni].y = qy; + float sx = qx + WS_X - CT.ws_sx, sy = qy + WS_Y - CT.ws_sy; + ed->drag_bi = ni; + ed->drag_ox = mx - sx; ed->drag_oy = my - sy; + mark_drag(&CT, ni, true); + ed->state = ES_DRAG; + ed->snap_kind = SN_NONE; + layout_tab(&CT); + return; + } + } int s = hit_slot(&CT, bi, wx, wy); if (s >= 0 && !slot_is_bool(CT.ws[bi].type, s)) { ed->editing = true; ed->edit_bi = bi; ed->edit_in = s; @@ -6067,10 +6179,11 @@ static void on_rmb_down(Ed* ed, float mx, float my) { ed->ctx_menu_grp = g; ed->ctx_wx = wx; ed->ctx_wy = wy; if (bi >= 0) { - ed->ctx_items[ed->ctx_n++] = CM_DUPLICATE; - ed->ctx_items[ed->ctx_n++] = CM_DELETE; const TinyBlocks_TypeBlock* t = CT.ws[bi].type; - if (t && strncmp(t->type_name, "User.", 5) == 0) ed->ctx_items[ed->ctx_n++] = CM_EDIT_DEF; + bool is_def = t && strcmp(t->type_name, "Port.Define") == 0; + if (!is_def) ed->ctx_items[ed->ctx_n++] = CM_DUPLICATE; /* dois "defina" iguais nao */ + ed->ctx_items[ed->ctx_n++] = CM_DELETE; + if (is_def || (t && strncmp(t->type_name, "User.", 5) == 0)) ed->ctx_items[ed->ctx_n++] = CM_EDIT_DEF; ed->ctx_items[ed->ctx_n++] = CM_GROUP_STACK; } else if (g >= 0) { ed->ctx_items[ed->ctx_n++] = CM_GROUP_RENAME; diff --git a/tests/editor_harness.c b/tests/editor_harness.c index 6c68822..bb1b4a0 100644 --- a/tests/editor_harness.c +++ b/tests/editor_harness.c @@ -317,10 +317,12 @@ int main(void) { type("n"); key(TB_KEY_RETURN, 0, 0); /* OK no diálogo */ /* como no Scratch: o "defina" aparece na própria aba, com o parâmetro ao lado */ - CHECK(ws_has("defina") && ws_has("parâmetro"), "Criar um Bloco põe \"defina dobro\" e o parâmetro na mesma aba"); + CHECK(ws_has("defina") && !ws_has("parâmetro"), "Criar um Bloco põe \"defina dobro\" na mesma aba (sem parâmetros soltos)"); CHECK(ws_has("quando a bandeira for clicado"), "sem abrir aba nova: os scripts continuam ali"); hx = 0; hy = 0; need("defina", R_WS, 0, &hx, &hy); + { float nx, ny; CHECK(find_near("dobro", hx, hy, &nx, &ny) && find_near("n", nx, ny, &nx, &ny), + "o protótipo \"dobro (n)\" aparece dentro do defina"); } if (need("Meus Blocos", R_CAT, 0, &x, &y)) click(x, y); if (need("devolva", R_FLY, 0, &x, &y)) drag(x, y, hx, hy + 44); /* embaixo do "defina" */ @@ -329,13 +331,17 @@ int main(void) { /* encaixa o parâmetro n no slot esquerdo do "+" */ { float px, py, zx, zy; - if (need("parâmetro", R_WS, 0, &px, &py) && block_plus(&x, &y, hy + 20)) { + /* o parâmetro sai arrastando de dentro do "defina" */ + float dx0, dy0; + if (find_near("dobro", hx, hy, &dx0, &dy0) && find_near("n", dx0, dy0, &px, &py) && + block_plus(&x, &y, hy + 20)) { int k = 0; bool got = false; while (find("0", R_WS, k++, &zx, &zy)) if (zy > y - 4 && zy < y + 4 && zx < x) { got = true; break; } if (!got) { printf("FAIL slot esquerdo do + (+ em %.0f,%.0f)\n", x, y); fails++; } - else drag(px, py, px + ((zx - 3 - 10) - (px - 3 - 10)), zy); + else drag(px, py, zx - 3, zy); } + CHECK(ws_has("parâmetro"), "arrastar o parâmetro do defina cria \"parâmetro n\""); } /* digita 1 no slot direito do "+" */ if (block_plus(&x, &y, hy + 20)) { @@ -1104,6 +1110,21 @@ int main(void) { } } + /* 27. Dois blocos definidos na mesma aba: os dois aparecem em Meus Blocos */ + { + const char* nomes[2] = { "render", "draw button" }; + for (int k = 0; k < 2; k++) { + if (need("Meus Blocos", R_CAT, 0, &x, &y)) click(x, y); + if (need("Criar um Bloco", R_FLY, 0, &x, &y)) click(x, y); + if (need("nome do bloco", R_WS, 0, &x, &y)) click(x, y); + type(nomes[k]); + key(TB_KEY_RETURN, 0, 0); + } + if (need("Meus Blocos", R_CAT, 0, &x, &y)) click(x, y); + CHECK(find("render", R_FLY, 0, &x, &y) && find("draw button", R_FLY, 0, &x, &y), + "dois blocos definidos aparecem na paleta"); + } + tinyblocks_deinit(&rt); printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails); return fails != 0;