Editor: diálogo "Criar um Bloco" estilo Scratch e nomes de parâmetros no bloco

- Diálogo modal (novo/editar) com protótipo ao vivo: campo de nome,
  cards para adicionar entrada numérica/texto, booleana e rótulo,
  controles <lixeira> de reordenar/apagar a parte selecionada,
  paleta de cores e Cancelar/OK (atalhos Esc/Enter).
- UserBlockDef guarda o protótipo em parts[] (NUM/BOOL/LABEL);
  build_parts monta o rótulo do bloco com os nomes dos parâmetros,
  blk_defaults usa o nome como valor inicial e slot_is_bool consulta
  as parts. Renomear o bloco atualiza as referências User.*.
- A aba de definição é criada com os Port.In sincronizados
  (comp_sync_inputs) e os Port.Out definem a saída; a barra do bloco
  composto ganhou "Editar bloco" + "Salvar" e perdeu os swatches.
- tests/editor_harness.c portado do scratchpad para o repositório,
  com o cenário do diálogo; alvo "make test-editor" (ASan/UBSan).
  Os 18 cenários passam.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude committed 2026-10-20 19:17:53 -03:00
1 parent 4950ad6006
commit 278d3f4473
3 files changed
+848 -73

No files matched your search

+528 -72
View File
@@ -122,6 +122,7 @@ TB_API void tb_editor_preload(const char* path) {
#define MAX_HOT_TYPES 64
#define MAX_USER_BLOCKS 16
#define MAX_UBLK_SLOTS 8
#define MAX_UBLK_PARTS 16
/* ══════════════════════════════════════════════════════════════════════════
* Cores (paleta do Scratch 3) — componentes 0..1
@@ -280,11 +281,16 @@ typedef struct {
int color_idx;
} Tab;
typedef enum { PARAM_NUM, PARAM_BOOL, PARAM_LABEL } ParamKind;
typedef struct { ParamKind kind; char name[32]; } UserParam;
typedef struct {
char name[48];
int input_count, output_count;
char input_names[MAX_UBLK_SLOTS][32];
char output_names[MAX_UBLK_SLOTS][32];
UserParam parts[MAX_UBLK_PARTS]; /* protótipo: entradas, booleanas e rótulos */
int part_n;
Tab* snap; /* cópia da aba no momento do "Salvar" */
TinyBlocks_TypeBlock* reg_type;
int tab_idx;
@@ -332,7 +338,19 @@ typedef struct {
char comp_name_buf[48];
int comp_name_len;
bool comp_name_active;
/* Diálogo modal "Criar um Bloco" (estilo Scratch) */
bool comp_dialog_open;
int comp_dialog_def; /* -1 = novo; senão índice em user_blocks */
bool comp_dialog_editing; /* true = edição de nome/rótulo em andamento */
int comp_dialog_edit_part; /* índice da parte em edição, -1 = nome do bloco */
char comp_dialog_name[48];
UserParam comp_dialog_parts[MAX_UBLK_PARTS];
int comp_dialog_part_n;
int comp_dialog_sel; /* parte selecionada (-1 = nenhuma) */
int comp_dialog_color;
float comp_dialog_geom[MAX_UBLK_PARTS][2]; /* x,w desenhados (para hit test) */
float comp_dialog_name_x, comp_dialog_name_w;
bool ctx_menu_open;
int ctx_menu_bi;
@@ -404,6 +422,13 @@ typedef struct {
static Ed* g_active_ed = NULL;
static void palette_rebuild(Ed* ed);
static void utf8_pop(char* s);
static void append_text(char* dst, size_t cap, const char* add);
static void dlg_open(Ed* ed, int def_idx);
static void dlg_render(Ed* ed, TinyBlocks_RenderableBlock* rb);
static bool dlg_click(Ed* ed, float mx, float my);
static void dlg_key(Ed* ed, int sc);
static void dlg_text(Ed* ed, const char* typed);
/* ══════════════════════════════════════════════════════════════════════════
* Helpers de desenho — emitem TinyBlocks_DrawCmd em um RenderableBlock
@@ -585,9 +610,28 @@ static Shape shape_of(const TinyBlocks_TypeBlock* t) {
static bool shape_is_value(Shape s) { return s == SH_REPORTER || s == SH_BOOL; }
static UserBlockDef* user_def_find(Ed* ed, const char* name);
/* idx-ésima entrada (NUM/BOOL) do protótipo; NULL se fora do intervalo */
static const UserParam* user_param_at(const UserBlockDef* def, int idx) {
if (!def || idx < 0) return NULL;
int k = 0;
for (int i = 0; i < def->part_n; i++) {
if (def->parts[i].kind == PARAM_LABEL) continue;
if (k == idx) return &def->parts[i];
k++;
}
return NULL;
}
static bool slot_is_bool(const TinyBlocks_TypeBlock* t, int i) {
if (!t || !t->type_name) return false;
const char* n = t->type_name;
if (strncmp(n, "User.", 5) == 0) {
UserBlockDef* def = g_active_ed ? user_def_find(g_active_ed, n + 5) : NULL;
const UserParam* p = user_param_at(def, i);
return p && p->kind == PARAM_BOOL;
}
if (i == 0 && (strcmp(n,"Control.If") == 0 || strcmp(n,"Control.IfElse") == 0 ||
strcmp(n,"Control.RepeatUntil") == 0)) return true;
return strcmp(n,"Operators.And") == 0 || strcmp(n,"Operators.Or") == 0 ||
@@ -660,6 +704,21 @@ static void blk_defaults(EdBlock* b) {
if ((k == TB_VALUE_INTEGER || k == TB_VALUE_REAL) && !slot_is_bool(t, i))
strcpy(b->in_text[i], "0");
}
/* Blocos compostos mostram o nome do parâmetro como valor inicial */
if (t && t->type_name && strncmp(t->type_name, "User.", 5) == 0 && g_active_ed) {
UserBlockDef* def = user_def_find(g_active_ed, t->type_name + 5);
if (def) {
int k = 0;
for (int i = 0; i < def->part_n && k < n; i++) {
if (def->parts[i].kind == PARAM_LABEL) continue;
if (def->parts[i].kind == PARAM_NUM)
snprintf(b->in_text[k], sizeof(b->in_text[k]), "%s", def->parts[i].name);
else
b->in_text[k][0] = 0;
k++;
}
}
}
const BlockSpec* sp = t ? spec_of(t->type_name) : NULL;
const char* p = sp ? sp->defs : NULL;
for (int i = 0; i < n && p; i++) {
@@ -930,9 +989,26 @@ static void build_parts(EdBlock* b) {
} else p++;
}
add_part(b, -1, seg, (int)(p - seg));
} else if (t->type_name && strncmp(t->type_name, "User.", 5) == 0) {
UserBlockDef* def = g_active_ed ? user_def_find(g_active_ed, t->type_name + 5) : NULL;
if (!def) {
add_part(b, -1, t->type_name + 5, (int)strlen(t->type_name + 5));
} else {
add_part(b, -1, def->name, (int)strlen(def->name));
int ii = 0;
for (int k = 0; k < def->part_n; k++) {
UserParam* pp = &def->parts[k];
if (pp->kind == PARAM_LABEL) {
add_part(b, -1, pp->name, (int)strlen(pp->name));
} else if (ii < n) {
add_part(b, ii, NULL, 0);
used[ii] = true;
ii++;
}
}
}
} else {
const char* nm = strncmp(t->type_name, "User.", 5) == 0 ? t->type_name + 5
: display_name(t->type_name);
const char* nm = display_name(t->type_name);
add_part(b, -1, nm, (int)strlen(nm));
}
/* Campos que o rótulo não cita: "nome [campo]" ao final */
@@ -1766,7 +1842,6 @@ static void tab_select(Ed* ed, int i) {
if (i < 0 || i >= ed->tab_n) return;
ed->cur_tab = i;
ed->editing = false;
ed->comp_name_active = false;
ed->bubble_bi = -1;
if (CT.is_composite) {
const char* nm = CT.comp_id >= 0 ? ed->user_blocks[CT.comp_id].name : "";
@@ -1808,13 +1883,7 @@ static void tab_close(Ed* ed, int ti) {
}
static void new_composite_tab(Ed* ed) {
if (!tab_new(ed, "Novo bloco", true)) return;
ed->comp_name_buf[0] = 0;
ed->comp_name_len = 0;
ed->comp_name_active = true;
snprintf(ed->hot_status, sizeof(ed->hot_status),
"Monte o bloco com \"parâmetro\" e \"devolva\" (Meus Blocos) e clique em Salvar");
ed->hot_status_ok = true;
dlg_open(ed, -1);
}
static void open_def(Ed* ed, const char* uname) {
@@ -1848,39 +1917,63 @@ static void collect_labels(Tab* T, const char* type_name, char names[][32], int*
}
}
static void save_composite_from_tab(Ed* ed) {
if (!ed->comp_name_buf[0]) {
snprintf(ed->hot_status, sizeof(ed->hot_status), "Digite um nome para o bloco antes de salvar.");
ed->hot_status_ok = false;
ed->comp_name_active = true;
return;
/* Reconcilia os blocos Port.In do corpo com as entradas do protótipo */
static void comp_sync_inputs(Ed* ed, UserBlockDef* def, Tab* T) {
int ids[MAX_WS], n = 0;
for (int i = 0; i < MAX_WS; i++) {
EdBlock* b = &T->ws[i];
if (b->used && strcmp(b->type_name, "Port.In") == 0) ids[n++] = i;
}
UserBlockDef* def = user_def_find(ed, ed->comp_name_buf);
if (!def) {
if (ed->user_block_n >= MAX_USER_BLOCKS) {
snprintf(ed->hot_status, sizeof(ed->hot_status), "Limite de blocos compostos atingido.");
ed->hot_status_ok = false;
return;
TinyBlocks_TypeBlock* t_in = tb_type_block_find(ed->rt, "Port.In");
for (int k = 0; k < def->input_count; k++) {
int bi = k < n ? ids[k] : (t_in ? blk_new(T, t_in) : -1);
if (bi < 0) break;
snprintf(T->ws[bi].in_text[0], sizeof(T->ws[bi].in_text[0]), "%s", def->input_names[k]);
if (k >= n) {
T->ws[bi].x = 40;
T->ws[bi].y = 70 + (k - n) * 52;
top_add(T, bi);
}
def = &ed->user_blocks[ed->user_block_n++];
memset(def, 0, sizeof(*def));
snprintf(def->name, sizeof(def->name), "%s", ed->comp_name_buf);
}
int def_idx = (int)(def - ed->user_blocks);
snprintf(CT.name, sizeof(CT.name), "%s", def->name);
CT.comp_id = def_idx;
def->tab_idx = ed->cur_tab;
def->color_idx = CT.color_idx;
for (int k = def->input_count; k < n; k++) free_tree(ed, T, ids[k]);
}
collect_labels(&CT, "Port.In", def->input_names, &def->input_count, "in%d");
collect_labels(&CT, "Port.Out", def->output_names, &def->output_count, "out%d");
/* Deriva os nomes das entradas a partir das partes do protótipo */
static void comp_build_input_names(UserBlockDef* def) {
def->input_count = 0;
for (int i = 0; i < def->part_n && def->input_count < MAX_UBLK_SLOTS; i++) {
if (def->parts[i].kind == PARAM_LABEL) continue;
char nm[64];
if (def->parts[i].name[0]) snprintf(nm, sizeof(nm), "%s", def->parts[i].name);
else snprintf(nm, sizeof(nm), "entrada %d", def->input_count + 1);
char base[40]; snprintf(base, sizeof(base), "%.31s", nm);
for (int suffix = 1; ; suffix++) {
bool dup = false;
for (int j = 0; j < def->input_count; j++)
if (strcmp(def->input_names[j], nm) == 0) { dup = true; break; }
if (!dup) break;
snprintf(nm, sizeof(nm), "%.24s %d", base, suffix + 1);
}
snprintf(def->input_names[def->input_count], 32, "%.31s", nm);
def->input_count++;
}
}
/* Salva corpo da aba e re-registra o tipo do bloco composto */
static void comp_register(Ed* ed, UserBlockDef* def, Tab* T) {
snprintf(T->name, sizeof(T->name), "%s", def->name);
T->is_composite = true;
T->comp_id = (int)(def - ed->user_blocks);
T->color_idx = def->color_idx;
comp_sync_inputs(ed, def, T);
collect_labels(T, "Port.Out", def->output_names, &def->output_count, "out%d");
/* Instâncias e referências ao tipo antigo precisam sumir antes de destruí-lo */
invalidate_types(ed);
if (def->reg_type) { tb_block_destroy(&def->reg_type->base, ed->rt); def->reg_type = NULL; }
if (!def->snap) def->snap = (Tab*)malloc(sizeof(Tab));
if (def->snap) *def->snap = CT;
if (def->snap) *def->snap = *T;
TinyBlocks_SlotDesc in_s[MAX_UBLK_SLOTS], out_s[MAX_UBLK_SLOTS];
for (int i = 0; i < def->input_count; i++) in_s[i] = (TinyBlocks_SlotDesc){def->input_names[i], TB_VALUE_NONE};
@@ -1892,11 +1985,384 @@ static void save_composite_from_tab(Ed* ed) {
def->output_count ? out_s : NULL, (uint32_t)def->output_count,
eval_composite);
def->tab_idx = (int)(T - ed->tabs);
retype_all(ed);
}
static void save_composite_from_tab(Ed* ed) {
if (!ed->comp_name_buf[0]) {
snprintf(ed->hot_status, sizeof(ed->hot_status), "Digite um nome para o bloco antes de salvar.");
ed->hot_status_ok = false;
return;
}
UserBlockDef* def = user_def_find(ed, ed->comp_name_buf);
if (!def) {
if (ed->user_block_n >= MAX_USER_BLOCKS) {
snprintf(ed->hot_status, sizeof(ed->hot_status), "Limite de blocos compostos atingido.");
ed->hot_status_ok = false;
return;
}
def = &ed->user_blocks[ed->user_block_n++];
memset(def, 0, sizeof(*def));
def->tab_idx = ed->cur_tab;
def->color_idx = CT.color_idx;
snprintf(def->name, sizeof(def->name), "%s", ed->comp_name_buf);
collect_labels(&CT, "Port.In", def->input_names, &def->input_count, "in%d");
}
comp_register(ed, def, &CT);
snprintf(ed->hot_status, sizeof(ed->hot_status), "Bloco \"%s\" salvo em Meus Blocos", def->name);
ed->hot_status_ok = true;
}
/* ══════════════════════════════════════════════════════════════════════════
* Diálogo modal "Criar um Bloco" (estilo Scratch)
* ══════════════════════════════════════════════════════════════════════════ */
#define DLG_W 580
#define DLG_H 380
#define DLG_HEAD_H 50
/* Geometria do protótipo: preenche comp_dialog_geom e devolve o retângulo */
static void dlg_geom(Ed* ed, float* bx, float* by, float* bw, float* bh) {
const float pad = 14.f, gap = 8.f;
float x = pad;
ed->comp_dialog_name_x = x;
ed->comp_dialog_name_w = fmaxf(96.f,
tw(ed->comp_dialog_name[0] ? ed->comp_dialog_name : "nome do bloco", FS) + 26.f);
x += ed->comp_dialog_name_w + gap;
for (int i = 0; i < ed->comp_dialog_part_n; i++) {
UserParam* p = &ed->comp_dialog_parts[i];
const char* label = p->name[0] ? p->name : (p->kind == PARAM_LABEL ? "rótulo" : "entrada");
float w;
if (p->kind == PARAM_LABEL) w = tw(label, FS) + 4.f;
else w = fmaxf(p->kind == PARAM_BOOL ? 36.f : 48.f, tw(label, FS) + 24.f);
ed->comp_dialog_geom[i][0] = x;
ed->comp_dialog_geom[i][1] = w;
x += w + gap;
}
float w = x - gap + pad;
float dx = (WIN_W - DLG_W) * 0.5f, dy = (WIN_H - DLG_H) * 0.5f;
*bw = w; *bh = 40.f;
*bx = dx + (DLG_W - w) * 0.5f;
*by = dy + DLG_HEAD_H + 46.f;
}
/* Dimensões de um card "Adicionar…" */
static void dlg_card_rect(float* w, float* h) {
*w = (DLG_W - 56.f - 32.f) / 3.f;
*h = 88.f;
}
static void dlg_open(Ed* ed, int def_idx) {
ed->comp_dialog_open = true;
ed->comp_dialog_def = def_idx;
ed->comp_dialog_editing = false;
ed->comp_dialog_edit_part = -1;
ed->comp_dialog_sel = -1;
ed->comp_dialog_name[0] = 0;
ed->comp_dialog_part_n = 0;
ed->comp_dialog_color = 0;
if (def_idx >= 0 && def_idx < ed->user_block_n) {
UserBlockDef* def = &ed->user_blocks[def_idx];
snprintf(ed->comp_dialog_name, sizeof(ed->comp_dialog_name), "%s", def->name);
ed->comp_dialog_color = def->color_idx;
ed->comp_dialog_part_n = def->part_n;
memcpy(ed->comp_dialog_parts, def->parts, sizeof(UserParam) * def->part_n);
} else {
ed->comp_dialog_part_n = 0;
}
ed->comp_dialog_editing = true;
ed->comp_dialog_edit_part = -1;
}
static char* dlg_edit_target(Ed* ed, size_t* cap) {
if (ed->comp_dialog_edit_part < 0) {
*cap = sizeof(ed->comp_dialog_name);
return ed->comp_dialog_name;
}
*cap = sizeof(ed->comp_dialog_parts[0].name);
return ed->comp_dialog_parts[ed->comp_dialog_edit_part].name;
}
static void dlg_add_part(Ed* ed, ParamKind kind) {
if (ed->comp_dialog_part_n >= MAX_UBLK_PARTS) return;
int i = ed->comp_dialog_part_n++;
ed->comp_dialog_parts[i].kind = kind;
ed->comp_dialog_parts[i].name[0] = 0;
ed->comp_dialog_sel = i;
ed->comp_dialog_editing = true;
ed->comp_dialog_edit_part = i;
}
static void dlg_delete_part(Ed* ed, int i) {
if (i < 0 || i >= ed->comp_dialog_part_n) return;
for (int k = i; k < ed->comp_dialog_part_n - 1; k++)
ed->comp_dialog_parts[k] = ed->comp_dialog_parts[k + 1];
ed->comp_dialog_part_n--;
ed->comp_dialog_sel = -1;
ed->comp_dialog_editing = false;
ed->comp_dialog_edit_part = -1;
}
static void dlg_move_part(Ed* ed, int dir) {
int i = ed->comp_dialog_sel, j = i + dir;
if (i < 0 || j < 0 || j >= ed->comp_dialog_part_n) return;
UserParam tmp = ed->comp_dialog_parts[i];
ed->comp_dialog_parts[i] = ed->comp_dialog_parts[j];
ed->comp_dialog_parts[j] = tmp;
ed->comp_dialog_sel = j;
if (ed->comp_dialog_edit_part == i) ed->comp_dialog_edit_part = j;
else if (ed->comp_dialog_edit_part == j) ed->comp_dialog_edit_part = i;
}
static void dlg_ok(Ed* ed) {
char nm[48];
snprintf(nm, sizeof(nm), "%s", ed->comp_dialog_name);
if (!nm[0]) snprintf(nm, sizeof(nm), "meu bloco");
UserBlockDef* def;
if (ed->comp_dialog_def >= 0 && ed->comp_dialog_def < ed->user_block_n)
def = &ed->user_blocks[ed->comp_dialog_def];
else {
if (ed->user_block_n >= MAX_USER_BLOCKS) {
snprintf(ed->hot_status, sizeof(ed->hot_status), "Limite de blocos compostos atingido.");
ed->hot_status_ok = false;
return;
}
def = &ed->user_blocks[ed->user_block_n++];
memset(def, 0, sizeof(*def));
def->tab_idx = -1;
}
char old_name[48];
snprintf(old_name, sizeof(old_name), "%s", def->name);
bool rename = old_name[0] && strcmp(old_name, nm) != 0;
snprintf(def->name, sizeof(def->name), "%s", nm);
def->color_idx = ed->comp_dialog_color;
def->part_n = ed->comp_dialog_part_n;
memcpy(def->parts, ed->comp_dialog_parts, sizeof(UserParam) * def->part_n);
comp_build_input_names(def);
if (rename) {
if (def->reg_type) {
invalidate_types(ed);
tb_block_destroy(&def->reg_type->base, ed->rt);
def->reg_type = NULL;
}
char old_type[64], new_type[64];
snprintf(old_type, sizeof(old_type), "User.%s", old_name);
snprintf(new_type, sizeof(new_type), "User.%s", def->name);
for (int t = 0; t < ed->tab_n; t++)
for (int i = 0; i < MAX_WS; i++) {
EdBlock* b = &ed->tabs[t].ws[i];
if (b->used && strcmp(b->type_name, old_type) == 0)
snprintf(b->type_name, sizeof(b->type_name), "%s", new_type);
}
if (def->snap) snprintf(def->snap->name, sizeof(def->snap->name), "%s", def->name);
}
/* Aba de definição: reusa a existente, ou restaura/cria */
if (def->tab_idx >= 0 && def->tab_idx < ed->tab_n) {
tab_select(ed, def->tab_idx);
} else {
if (!tab_new(ed, def->name, true)) return;
def->tab_idx = ed->cur_tab;
if (def->snap) { CT = *def->snap; clear_live(&CT); }
}
CT.is_composite = true;
CT.color_idx = def->color_idx;
comp_register(ed, def, &CT);
ed->comp_dialog_open = false;
ed->comp_dialog_editing = false;
snprintf(ed->comp_name_buf, sizeof(ed->comp_name_buf), "%s", def->name);
ed->comp_name_len = (int)strlen(ed->comp_name_buf);
snprintf(ed->hot_status, sizeof(ed->hot_status), "Bloco \"%s\" salvo em Meus Blocos", def->name);
ed->hot_status_ok = true;
}
static void dlg_render(Ed* ed, TinyBlocks_RenderableBlock* rb) {
if (!ed->comp_dialog_open) return;
rb_fill_rect(rb, 0, 0, WIN_W, WIN_H, COLA(0, 0, 0, 120));
float dx = (WIN_W - DLG_W) * 0.5f, dy = (WIN_H - DLG_H) * 0.5f;
Col accent = s_user_colors[ed->comp_dialog_color];
Col dark = col_mul(accent, 0.78f);
rb_fill_rrect(rb, dx + 3, dy + 4, DLG_W, DLG_H, 12, COLA(0, 0, 0, 70));
rb_fill_rrect(rb, dx, dy, DLG_W, DLG_H, 12, COL_PANEL);
/* Cabeçalho */
rb_fill_rrect(rb, dx, dy, DLG_W, DLG_HEAD_H, 12, accent);
rb_fill_rect(rb, dx, dy + DLG_HEAD_H - 12, DLG_W, 12, accent);
const char* title = ed->comp_dialog_def >= 0 ? "Editar Bloco" : "Criar um Bloco";
rb_text_c(rb, dx + (DLG_W - tw(title, 18)) * 0.5f, dy + DLG_HEAD_H * 0.5f, title, COL_WHITE, 18);
rb_fill_circle(rb, dx + DLG_W - 26, dy + DLG_HEAD_H * 0.5f, 12, COLA(255, 255, 255, 60));
rb_text_c(rb, dx + DLG_W - 30, dy + DLG_HEAD_H * 0.5f, "×", COL_WHITE, 18);
/* Protótipo do bloco */
float bx, by, bw, bh;
dlg_geom(ed, &bx, &by, &bw, &bh);
rb_fill_rrect(rb, bx, by, bw, bh, 4, dark);
rb_fill_rrect(rb, bx + 1, by + 1, bw - 2, bh - 2, 3, accent);
draw_bump(rb, bx, by + bh, accent, dark);
/* Campo do nome do bloco */
{
float nfx = bx + ed->comp_dialog_name_x, nfy = by + 7, nfw = ed->comp_dialog_name_w, nfh = bh - 14;
rb_fill_rrect(rb, nfx, nfy, nfw, nfh, nfh * 0.5f, COLA(0, 0, 0, 40));
rb_fill_rrect(rb, nfx + 1, nfy + 1, nfw - 2, nfh - 2, nfh * 0.5f - 1, COL_WHITE);
const char* nm = ed->comp_dialog_name[0] ? ed->comp_dialog_name : "nome do bloco";
float lw = tw(nm, FS);
rb_text_c(rb, nfx + (nfw - lw) * 0.5f, nfy + nfh * 0.5f, nm,
ed->comp_dialog_name[0] ? COL_TEXT : COL_TEXT_DIM, FS);
if (ed->comp_dialog_editing && ed->comp_dialog_edit_part < 0)
rb_fill_rect(rb, nfx + (nfw + lw) * 0.5f + 2, nfy + 4, 1.5f, nfh - 8, COL_TEXT);
}
/* Partes do protótipo */
for (int i = 0; i < ed->comp_dialog_part_n; i++) {
UserParam* p = &ed->comp_dialog_parts[i];
const char* label = p->name[0] ? p->name : (p->kind == PARAM_LABEL ? "rótulo" : "entrada");
float px = bx + ed->comp_dialog_geom[i][0], pw = ed->comp_dialog_geom[i][1];
float py = by + 7, ph = bh - 14;
bool sel = ed->comp_dialog_sel == i;
if (p->kind == PARAM_LABEL) {
rb_text_c(rb, px, by + bh * 0.5f, label,
p->name[0] ? COL_WHITE : COLA(255, 255, 255, 150), FS);
} else if (p->kind == PARAM_BOOL) {
draw_hex(rb, px, py, pw, ph, sel ? COL_WHITE : COLA(0, 0, 0, 40));
rb_text_c(rb, px + (pw - tw(label, FS)) * 0.5f, py + ph * 0.5f, label,
p->name[0] ? COL_TEXT : COL_TEXT_DIM, FS);
} else {
rb_fill_rrect(rb, px, py, pw, ph, ph * 0.5f, sel ? COL_MENU : COLA(0, 0, 0, 40));
rb_fill_rrect(rb, px + 1, py + 1, pw - 2, ph - 2, ph * 0.5f - 1, COL_WHITE);
rb_text_c(rb, px + (pw - tw(label, FS)) * 0.5f, py + ph * 0.5f, label,
p->name[0] ? COL_TEXT : COL_TEXT_DIM, FS);
}
if (sel) rb_stroke_rect(rb, px - 2, py - 2, pw + 4, ph + 4, 2, COL_TEXT);
if (ed->comp_dialog_editing && ed->comp_dialog_edit_part == i && p->kind != PARAM_LABEL) {
float lw = tw(label, FS);
rb_fill_rect(rb, px + (pw + lw) * 0.5f + 2, py + 5, 1.5f, ph - 10, COL_TEXT);
}
}
/* Controles ‹ 🗑 › da parte selecionada */
if (ed->comp_dialog_sel >= 0) {
float ctr = bx + bw * 0.5f, cty = by - 26;
rb_text_c(rb, ctr - 44, cty, "‹", COL_TEXT, 22);
rb_text_c(rb, ctr + 40, cty, "›", COL_TEXT, 22);
rb_fill_rect(rb, ctr - 7, cty - 8, 14, 3, COL_TEXT);
rb_fill_rect(rb, ctr - 4, cty - 12, 8, 4, COL_TEXT);
rb_fill_rect(rb, ctr - 6, cty - 4, 12, 12, COL_TEXT);
}
/* Cards de adicionar */
float cw, ch; dlg_card_rect(&cw, &ch);
float cx0 = dx + 28.f;
float cy0 = by + bh + 34.f;
const char* lines[3][3] = {
{"Adicionar uma", "entrada", "numérica ou texto"},
{"Adicionar uma", "entrada", "booleana"},
{"Adicionar um", "rótulo", ""},
};
for (int i = 0; i < 3; i++) {
float cx = cx0 + i * (cw + 16.f);
bool hover = in_rect(ed->mouse_x, ed->mouse_y, cx, cy0, cw, ch);
rb_fill_rrect(rb, cx, cy0, cw, ch, 8, hover ? COL_HOVER : COL_PANEL);
rb_stroke_rect(rb, cx + 0.5f, cy0 + 0.5f, cw - 1, ch - 1, 1, hover ? COL_MENU : COL_BORDER);
float iy = cy0 + 16.f;
if (i == 0) {
rb_fill_rrect(rb, cx + cw * 0.5f - 22, iy, 44, 22, 11, accent);
rb_text_c(rb, cx + cw * 0.5f - 14, iy + 11, "· · ·", COL_WHITE, FS);
} else if (i == 1) {
draw_hex(rb, cx + cw * 0.5f - 20, iy, 40, 22, accent);
} else {
rb_fill_rrect(rb, cx + cw * 0.5f - 22, iy, 44, 22, 4, accent);
rb_text_c(rb, cx + cw * 0.5f - 11, iy + 11, "text", COL_WHITE, FS);
}
float ty = cy0 + 46.f;
for (int l = 0; l < 3 && lines[i][l][0]; l++) {
rb_text_c(rb, cx + (cw - tw(lines[i][l], FS)) * 0.5f, ty, lines[i][l], COL_TEXT, FS);
ty += 16.f;
}
}
/* Cores + Cancelar/OK */
float fy = dy + DLG_H - 40.f;
for (int ci = 0; ci < USER_COLOR_COUNT; ci++) {
float sx = dx + 28.f + ci * 30.f;
if (ed->comp_dialog_color == ci) rb_fill_rrect(rb, sx - 2, fy - 2, 26, 26, 6, COL_TEXT);
rb_fill_rrect(rb, sx, fy, 22, 22, 5, s_user_colors[ci]);
}
float okx = dx + DLG_W - 28.f - 84.f, cax = okx - 96.f;
rb_fill_rrect(rb, cax, fy - 8, 84, 36, 8, COL_BORDER);
rb_text_c(rb, cax + (84 - tw("Cancelar", FS_UI)) * 0.5f, fy + 10, "Cancelar", COL_TEXT, FS_UI);
rb_fill_rrect(rb, okx, fy - 8, 84, 36, 8, accent);
rb_text_c(rb, okx + (84 - tw("OK", FS_UI)) * 0.5f, fy + 10, "OK", COL_WHITE, FS_UI);
}
static bool dlg_click(Ed* ed, float mx, float my) {
if (!ed->comp_dialog_open) return false;
float dx = (WIN_W - DLG_W) * 0.5f, dy = (WIN_H - DLG_H) * 0.5f;
if (!in_rect(mx, my, dx, dy, DLG_W, DLG_H)) { ed->comp_dialog_open = false; return true; }
if (in_rect(mx, my, dx + DLG_W - 44, dy, 44, DLG_HEAD_H)) { ed->comp_dialog_open = false; return true; }
float bx, by, bw, bh;
dlg_geom(ed, &bx, &by, &bw, &bh);
if (in_rect(mx, my, bx + ed->comp_dialog_name_x, by + 7, ed->comp_dialog_name_w, bh - 14)) {
ed->comp_dialog_editing = true; ed->comp_dialog_edit_part = -1; ed->comp_dialog_sel = -1;
return true;
}
for (int i = 0; i < ed->comp_dialog_part_n; i++) {
if (in_rect(mx, my, bx + ed->comp_dialog_geom[i][0], by + 7, ed->comp_dialog_geom[i][1], bh - 14)) {
ed->comp_dialog_sel = i; ed->comp_dialog_editing = true; ed->comp_dialog_edit_part = i;
return true;
}
}
if (ed->comp_dialog_sel >= 0) {
float ctr = bx + bw * 0.5f, cty = by - 26;
if (in_rect(mx, my, ctr - 60, cty - 16, 34, 32)) { dlg_move_part(ed, -1); return true; }
if (in_rect(mx, my, ctr - 16, cty - 16, 32, 32)) { dlg_delete_part(ed, ed->comp_dialog_sel); return true; }
if (in_rect(mx, my, ctr + 26, cty - 16, 34, 32)) { dlg_move_part(ed, 1); return true; }
}
float cw, ch; dlg_card_rect(&cw, &ch);
float cx0 = dx + 28.f, cy0 = by + bh + 34.f;
for (int i = 0; i < 3; i++) {
float cx = cx0 + i * (cw + 16.f);
if (in_rect(mx, my, cx, cy0, cw, ch)) {
dlg_add_part(ed, i == 0 ? PARAM_NUM : i == 1 ? PARAM_BOOL : PARAM_LABEL);
return true;
}
}
float fy = dy + DLG_H - 40.f;
for (int ci = 0; ci < USER_COLOR_COUNT; ci++)
if (in_rect(mx, my, dx + 28.f + ci * 30.f, fy, 22, 22)) { ed->comp_dialog_color = ci; return true; }
float okx = dx + DLG_W - 28.f - 84.f, cax = okx - 96.f;
if (in_rect(mx, my, okx, fy - 8, 84, 36)) { dlg_ok(ed); return true; }
if (in_rect(mx, my, cax, fy - 8, 84, 36)) { ed->comp_dialog_open = false; return true; }
ed->comp_dialog_editing = false;
ed->comp_dialog_edit_part = -1;
return true;
}
static void dlg_key(Ed* ed, int sc) {
if (sc == TB_KEY_ESCAPE) { ed->comp_dialog_open = false; return; }
if (sc == TB_KEY_RETURN) { dlg_ok(ed); return; }
if (ed->comp_dialog_editing && sc == TB_KEY_BACKSPACE) {
size_t cap; char* t = dlg_edit_target(ed, &cap); (void)cap; utf8_pop(t);
}
}
static void dlg_text(Ed* ed, const char* typed) {
if (!ed->comp_dialog_editing) return;
size_t cap; char* t = dlg_edit_target(ed, &cap);
append_text(t, cap, typed);
}
/* ══════════════════════════════════════════════════════════════════════════
* Hot reload de libraries .so
* ══════════════════════════════════════════════════════════════════════════ */
@@ -2136,9 +2602,9 @@ static void render_workspace(Ed* ed, TinyBlocks_RenderableBlock* rb) {
clip_clear(ed, rb);
}
/* ── Painel de bloco composto (canto superior direito do workspace) ───── */
/* ── Barra do bloco composto (canto superior direito do workspace) ───── */
#define CP_W 440
#define CP_H 40
#define CP_H 44
static void comp_panel_pos(Ed* ed, float* px, float* py) {
*px = WS_X + WS_W - CP_W - 10;
*py = WS_Y + 10;
@@ -2149,24 +2615,20 @@ static void render_comp_panel(Ed* ed, TinyBlocks_RenderableBlock* rb) {
float px, py; comp_panel_pos(ed, &px, &py);
rb_fill_rrect(rb, px, py, CP_W, CP_H, 8, COL_BORDER);
rb_fill_rrect(rb, px+1, py+1, CP_W-2, CP_H-2, 7, COL_PANEL);
rb_text_c(rb, px+10, py+CP_H*0.5f, "Nome:", COL_TEXT, FS_UI);
rb_fill_rrect(rb, px+56, py+6, 140, 28, 5, ed->comp_name_active ? COL_MENU : COL_BORDER);
rb_fill_rrect(rb, px+57, py+7, 138, 26, 4, COL_WHITE);
bool empty = !ed->comp_name_buf[0];
rb_text_c(rb, px+64, py+CP_H*0.5f, empty ? "nome do bloco" : ed->comp_name_buf,
empty ? COL_TEXT_DIM : COL_TEXT, FS_UI);
if (ed->comp_name_active)
rb_fill_rect(rb, px+65 + tw(ed->comp_name_buf, FS_UI), py+12, 1.5f, 16, COL_TEXT);
Col c = s_user_colors[CT.color_idx];
rb_fill_rrect(rb, px+10, py+10, 24, 24, 6, col_mul(c, 0.8f));
rb_fill_rrect(rb, px+11, py+11, 22, 22, 5, c);
const char* nm = ed->comp_name_buf[0] ? ed->comp_name_buf : "Novo bloco";
rb_text_c(rb, px+44, py+CP_H*0.5f, nm, COL_TEXT, FS_UI);
for (int ci = 0; ci < USER_COLOR_COUNT; ci++) {
float sx = px + 206 + ci*20;
if (CT.color_idx == ci) rb_fill_rrect(rb, sx-2, py+10, 20, 20, 5, COL_TEXT);
rb_fill_rrect(rb, sx, py+12, 16, 16, 4, s_user_colors[ci]);
}
float bx = px + 206 + USER_COLOR_COUNT*20 + 4;
rb_fill_rrect(rb, bx, py+6, 64, 28, 6, s_user_colors[CT.color_idx]);
rb_text_c(rb, bx + (64 - tw("Salvar", FS_UI))*0.5f, py+CP_H*0.5f, "Salvar", COL_WHITE, FS_UI);
float bx = px + CP_W - 10;
bx -= 78;
rb_fill_rrect(rb, bx, py+8, 78, 28, 6, c);
rb_text_c(rb, bx + (78 - tw("Salvar", FS_UI))*0.5f, py+CP_H*0.5f, "Salvar", COL_WHITE, FS_UI);
bx -= 96;
rb_fill_rrect(rb, bx, py+8, 88, 28, 6, COL_BORDER);
rb_text_c(rb, bx + (88 - tw("Editar bloco", FS_UI))*0.5f, py+CP_H*0.5f, "Editar bloco", COL_TEXT, FS_UI);
}
/* Altura da área do palco no painel (4:3 embutido; faixa curta se destacado) */
@@ -2532,6 +2994,7 @@ static void ctx_exec(Ed* ed, int id) {
}
static void on_lmb_down(Ed* ed, float mx, float my) {
if (ed->comp_dialog_open) { dlg_click(ed, mx, my); return; }
if (ed->ctx_menu_open) {
int it = ctx_hit(ed, mx, my);
ed->ctx_menu_open = false;
@@ -2540,8 +3003,6 @@ static void on_lmb_down(Ed* ed, float mx, float my) {
}
ed->editing = false;
ed->bubble_bi = -1;
bool was_naming = ed->comp_name_active;
ed->comp_name_active = false;
/* Barra de menu */
if (my < MENU_H) {
@@ -2577,15 +3038,18 @@ static void on_lmb_down(Ed* ed, float mx, float my) {
return;
}
/* Painel do bloco composto */
/* Barra do bloco composto */
if (CT.is_composite) {
float px, py; comp_panel_pos(ed, &px, &py);
if (in_rect(mx, my, px, py, CP_W, CP_H)) {
if (in_rect(mx, my, px+56, py+6, 140, 28)) { ed->comp_name_active = true; return; }
for (int ci = 0; ci < USER_COLOR_COUNT; ci++)
if (in_rect(mx, my, px+206+ci*20, py+10, 18, 20)) { CT.color_idx = ci; return; }
if (in_rect(mx, my, px+206+USER_COLOR_COUNT*20+4, py+6, 64, 28)) save_composite_from_tab(ed);
ed->comp_name_active = was_naming;
float bx = px + CP_W - 10;
bx -= 78;
if (in_rect(mx, my, bx, py+8, 78, 28)) { save_composite_from_tab(ed); return; }
bx -= 96;
if (in_rect(mx, my, bx, py+8, 88, 28)) {
if (CT.comp_id >= 0) dlg_open(ed, CT.comp_id);
return;
}
return;
}
}
@@ -2632,6 +3096,7 @@ static void on_lmb_down(Ed* ed, float mx, float my) {
static void on_lmb_up(Ed* ed, float mx, float my) {
(void)my;
if (ed->comp_dialog_open) return;
if (ed->state == ES_DRAG && ed->drag_bi >= 0) {
mark_drag(&CT, ed->drag_bi, false);
if (mx < PALETTE_W) free_tree(ed, &CT, ed->drag_bi); /* solto na paleta = apagar */
@@ -2668,6 +3133,7 @@ static void on_rmb_down(Ed* ed, float mx, float my) {
static void on_key(Ed* ed, int sc, bool ctrl) {
bool enter = sc == TB_KEY_RETURN;
if (ed->comp_dialog_open) { dlg_key(ed, sc); return; }
if (ed->ctx_menu_open) { if (sc == TB_KEY_ESCAPE) ed->ctx_menu_open = false; return; }
if (ed->path_input_active) {
@@ -2681,15 +3147,6 @@ static void on_key(Ed* ed, int sc, bool ctrl) {
}
return;
}
if (ed->comp_name_active) {
if (sc == TB_KEY_ESCAPE) ed->comp_name_active = false;
else if (enter) { ed->comp_name_active = false; save_composite_from_tab(ed); }
else if (sc == TB_KEY_BACKSPACE) {
utf8_pop(ed->comp_name_buf);
ed->comp_name_len = (int)strlen(ed->comp_name_buf);
}
return;
}
if (ed->editing) {
size_t cap; char* t = edit_target(ed, &cap);
bool multi = is_lit_text(CT.ws[ed->edit_bi].type);
@@ -2715,12 +3172,10 @@ static void on_key(Ed* ed, int sc, bool ctrl) {
}
static void on_text(Ed* ed, const char* typed) {
if (ed->comp_dialog_open) { dlg_text(ed, typed); return; }
if (ed->path_input_active) {
append_text(ed->path_input_buf, sizeof(ed->path_input_buf), typed);
ed->path_input_len = (int)strlen(ed->path_input_buf);
} else if (ed->comp_name_active) {
append_text(ed->comp_name_buf, sizeof(ed->comp_name_buf), typed);
ed->comp_name_len = (int)strlen(ed->comp_name_buf);
} else if (ed->editing) {
size_t cap; char* t = edit_target(ed, &cap);
append_text(t, cap, typed);
@@ -2839,6 +3294,7 @@ static bool editor_on_draw(TinyBlocks_Block* self, TinyBlocks* rt,
render_chain(ed, rb, &CT, ed->drag_bi, WS_X - CT.ws_sx, WS_Y - CT.ws_sy);
render_ctx_menu(ed, rb);
dlg_render(ed, rb);
*dst = rb;
return true;