Editor como servidor MCP: agentes leem e editam as pilhas por código
tinyblocks-mcp-server (C, sem Python): servidor MCP nas duas eras do protocolo. Moderna (2026-07-28): sem handshake, versão e capacidades em _meta a cada pedido, server/discover, resultType, cabeçalhos MCP-Protocol-Version/Mcp-Method/Mcp-Name conferidos com o corpo (HeaderMismatch, UnsupportedProtocolVersion). Antiga: initialize. HTTP só em 127.0.0.1 (Origin conferido, GET/DELETE 405, sem sessão), sem bloquear (o editor atende a cada quadro), e stdio. test-mcp-server. Ferramentas do editor (tb_editor_mcp.c; http://127.0.0.1:7811/mcp, TINYBLOCKS_MCP_PORT muda a porta, 0 = sem rede): get_editor, list_block_types, list_stacks (scratchblocks + ids dos blocos), get_scratchblocks, add_scratchblocks, replace_stack, move_stack, delete_stack, add_block / move_block (solto, embaixo, num campo ou numa boca; alone = só o bloco) / delete_block / set_inputs, run / stop / get_output, new_tab / select_tab, save_project / open_project e ui_tree / ui_action / ui_type (a interface pela árvore acessível). O editor exporta e importa scratchblocks por pilha (numa aba existente, numa posição), e o modelo de blocos fica na API privada para o servidor. O scratchblocks usa também os rótulos declarados pelas libraries. Duplicar um bloco C de três bocas copiava só duas. O cliente (tinyblocks-mcp) fala as duas eras: sonda com server/discover no stdio, pedido moderno no HTTP, e cai para o initialize com servidores antigos. test-mcp testa contra o servidor antigo de teste e contra o moderno (stdio e HTTP); harness cenário 29 usa as ferramentas do editor. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
10a8099c46
commit
2a2d22a64a
19 files changed
+2669
-116
No files matched your search
@@ -165,7 +165,7 @@ static const BlockSpec* spec_of(const char* tn) {
|
||||
|
||||
/* Rotulo do tipo: o da tabela do editor ou o que a library declarou
|
||||
* (tb_type_block_set_label); NULL = so o nome e os campos */
|
||||
static const BlockSpec* spec_of_type(const TinyBlocks_TypeBlock* t) {
|
||||
const BlockSpec* spec_of_type(const TinyBlocks_TypeBlock* t) {
|
||||
static BlockSpec own;
|
||||
if (!t) return NULL;
|
||||
const BlockSpec* sp = spec_of(t->type_name);
|
||||
@@ -176,14 +176,14 @@ static const BlockSpec* spec_of_type(const TinyBlocks_TypeBlock* t) {
|
||||
return &own;
|
||||
}
|
||||
|
||||
static int input_count(const TinyBlocks_TypeBlock* t) {
|
||||
int input_count(const TinyBlocks_TypeBlock* t) {
|
||||
if (!t) return 0;
|
||||
return t->input_desc_count < MAX_INPUTS ? (int)t->input_desc_count : MAX_INPUTS;
|
||||
}
|
||||
|
||||
/* A forma vem do tipo (a library declara): bloco C = classe de fluxo,
|
||||
chapeu/booleano = shape ou saida TB_SLOT_BOOLEAN */
|
||||
static Shape shape_of(const TinyBlocks_TypeBlock* t) {
|
||||
Shape shape_of(const TinyBlocks_TypeBlock* t) {
|
||||
if (!t) return SH_STACK;
|
||||
if (t->flow) return t->flow->mouths >= 3 ? SH_C3 : t->flow->mouths == 2 ? SH_C2 : SH_C1;
|
||||
switch (t->shape) {
|
||||
@@ -197,19 +197,19 @@ static Shape shape_of(const TinyBlocks_TypeBlock* t) {
|
||||
return t->output_desc_count > 0 ? SH_REPORTER : SH_STACK;
|
||||
}
|
||||
|
||||
static bool shape_is_value(Shape s) { return s == SH_REPORTER || s == SH_BOOL; }
|
||||
bool shape_is_value(Shape s) { return s == SH_REPORTER || s == SH_BOOL; }
|
||||
|
||||
/* Bocas de um bloco C (0 se nao for C) */
|
||||
static int shape_mouths(Shape s) { return s == SH_C1 ? 1 : s == SH_C2 ? 2 : s == SH_C3 ? 3 : 0; }
|
||||
int shape_mouths(Shape s) { return s == SH_C1 ? 1 : s == SH_C2 ? 2 : s == SH_C3 ? 3 : 0; }
|
||||
|
||||
/* Texto da faixa entre a boca k e a k+1 ("senão", "se der erro"...) */
|
||||
static const char* mouth_label(const TinyBlocks_TypeBlock* t, int k) {
|
||||
const char* mouth_label(const TinyBlocks_TypeBlock* t, int k) {
|
||||
if (!t || !t->flow || k < 0 || k >= TB_FLOW_MAX_MOUTHS - 1) return "";
|
||||
return t->flow->labels[k] ? t->flow->labels[k] : "";
|
||||
}
|
||||
|
||||
/* Bloco "tampa" (como o "sempre" do Scratch): nada encaixa embaixo dele */
|
||||
static bool is_cap(const TinyBlocks_TypeBlock* t) {
|
||||
bool is_cap(const TinyBlocks_TypeBlock* t) {
|
||||
return t && t->flow && t->flow->cap;
|
||||
}
|
||||
|
||||
@@ -228,7 +228,7 @@ static const UserParam* user_param_at(const UserBlockDef* def, int idx) {
|
||||
}
|
||||
|
||||
/* Campo hexagonal (recebe booleano): traco do campo, declarado no tipo */
|
||||
static bool slot_is_bool(const TinyBlocks_TypeBlock* t, int i) {
|
||||
bool slot_is_bool(const TinyBlocks_TypeBlock* t, int i) {
|
||||
return i >= 0 && (tb_type_block_input_flags(t, (uint32_t)i) & TB_SLOT_BOOLEAN);
|
||||
}
|
||||
|
||||
@@ -261,7 +261,7 @@ static int cat_of_type(Ed* ed, const TinyBlocks_TypeBlock* t) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int cat_of(Ed* ed, const char* n) {
|
||||
int cat_of(Ed* ed, const char* n) {
|
||||
if (!n) return -1;
|
||||
return cat_of_type(ed, tb_type_block_find(ed->rt, n));
|
||||
}
|
||||
@@ -343,7 +343,7 @@ static int blk_alloc(Tab* T) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int blk_new(Tab* T, TinyBlocks_TypeBlock* type) {
|
||||
int blk_new(Tab* T, TinyBlocks_TypeBlock* type) {
|
||||
int i = blk_alloc(T);
|
||||
if (i < 0) return -1;
|
||||
EdBlock* b = &T->ws[i];
|
||||
@@ -353,7 +353,7 @@ static int blk_new(Tab* T, TinyBlocks_TypeBlock* type) {
|
||||
return i;
|
||||
}
|
||||
|
||||
static void top_remove(Tab* T, int bi) {
|
||||
void top_remove(Tab* T, int bi) {
|
||||
for (int i = 0; i < T->top_n; i++) {
|
||||
if (T->top[i] != bi) continue;
|
||||
memmove(&T->top[i], &T->top[i+1], (size_t)(T->top_n - i - 1) * sizeof(int));
|
||||
@@ -363,13 +363,13 @@ static void top_remove(Tab* T, int bi) {
|
||||
}
|
||||
|
||||
/* Adiciona (ou traz para a frente) uma pilha de topo */
|
||||
static void top_add(Tab* T, int bi) {
|
||||
void top_add(Tab* T, int bi) {
|
||||
top_remove(T, bi);
|
||||
if (T->top_n < MAX_WS) T->top[T->top_n++] = bi;
|
||||
}
|
||||
|
||||
/* Liga child ao pai na relação rel (child - -1 desliga) */
|
||||
static void blk_link(Tab* T, int parent, int rel, int child) {
|
||||
void blk_link(Tab* T, int parent, int rel, int child) {
|
||||
EdBlock* p = &T->ws[parent];
|
||||
if (rel == REL_NEXT) p->next = child;
|
||||
else if (rel < REL_ARG0) p->sub[rel - REL_SUB0] = child;
|
||||
@@ -385,7 +385,7 @@ static int linked(Tab* T, int parent, int rel) {
|
||||
}
|
||||
|
||||
/* Solta bi do pai; vira topo de pilha (levando os blocos abaixo dele) */
|
||||
static void detach(Tab* T, int bi) {
|
||||
void detach(Tab* T, int bi) {
|
||||
EdBlock* b = &T->ws[bi];
|
||||
if (b->parent < 0) return;
|
||||
blk_link(T, b->parent, b->prel, -1);
|
||||
@@ -393,12 +393,12 @@ static void detach(Tab* T, int bi) {
|
||||
top_add(T, bi);
|
||||
}
|
||||
|
||||
static int chain_last(Tab* T, int bi) {
|
||||
int chain_last(Tab* T, int bi) {
|
||||
while (T->ws[bi].next >= 0) bi = T->ws[bi].next;
|
||||
return bi;
|
||||
}
|
||||
|
||||
static int stack_root(Tab* T, int bi) {
|
||||
int stack_root(Tab* T, int bi) {
|
||||
while (T->ws[bi].parent >= 0) bi = T->ws[bi].parent;
|
||||
return bi;
|
||||
}
|
||||
@@ -410,7 +410,7 @@ static float chain_h(Tab* T, int head) {
|
||||
}
|
||||
|
||||
/* Insere a pilha bi (de topo) em parent/rel; o que estava lá vai para baixo dela */
|
||||
static void attach_insert(Tab* T, int parent, int rel, int bi) {
|
||||
void attach_insert(Tab* T, int parent, int rel, int bi) {
|
||||
top_remove(T, bi);
|
||||
int old = linked(T, parent, rel);
|
||||
int last = chain_last(T, bi);
|
||||
@@ -428,7 +428,7 @@ static void attach_above(Tab* T, int target, int bi) {
|
||||
}
|
||||
|
||||
/* Encaixa o repórter bi no campo s; um repórter que já estava lá é expulso */
|
||||
static void attach_arg(Tab* T, int parent, int s, int bi) {
|
||||
void attach_arg(Tab* T, int parent, int s, int bi) {
|
||||
top_remove(T, bi);
|
||||
int old = T->ws[parent].args[s];
|
||||
if (old >= 0) {
|
||||
@@ -466,7 +466,7 @@ static int copy_tree(Tab* S, int si, Tab* D, bool with_next) {
|
||||
int k = copy_tree(S, o->args[s], D, false);
|
||||
if (k >= 0) blk_link(D, ni, REL_ARG0 + s, k);
|
||||
}
|
||||
for (int k = 0; k < 2; k++) {
|
||||
for (int k = 0; k < MAX_MOUTHS; k++) {
|
||||
if (o->sub[k] < 0) continue;
|
||||
int h = copy_tree(S, o->sub[k], D, true);
|
||||
if (h >= 0) blk_link(D, ni, REL_SUB0 + k, h);
|
||||
@@ -478,7 +478,7 @@ static int copy_tree(Tab* S, int si, Tab* D, bool with_next) {
|
||||
}
|
||||
|
||||
/* Libera bi, tudo o que está encaixado nele e os blocos abaixo */
|
||||
static void free_tree(Ed* ed, Tab* T, int bi) {
|
||||
void free_tree(Ed* ed, Tab* T, int bi) {
|
||||
while (bi >= 0) {
|
||||
EdBlock* b = &T->ws[bi];
|
||||
int nx = b->next;
|
||||
@@ -496,7 +496,7 @@ static void free_tree(Ed* ed, Tab* T, int bi) {
|
||||
}
|
||||
|
||||
/* Apaga só bi (com o que está encaixado nele); os blocos abaixo sobem */
|
||||
static void delete_heal(Ed* ed, Tab* T, int bi) {
|
||||
void delete_heal(Ed* ed, Tab* T, int bi) {
|
||||
EdBlock* b = &T->ws[bi];
|
||||
int nx = b->next, par = b->parent, rel = b->prel;
|
||||
if (nx >= 0) { b->next = -1; T->ws[nx].parent = -1; T->ws[nx].prel = REL_NONE; }
|
||||
@@ -512,7 +512,7 @@ static void delete_heal(Ed* ed, Tab* T, int bi) {
|
||||
/* ===
|
||||
* Layout: mede (tamanhos) e posiciona (coordenadas do workspace)
|
||||
* === */
|
||||
static const char* slot_src(const EdBlock* b, int s) {
|
||||
const char* slot_src(const EdBlock* b, int s) {
|
||||
return (is_lit_text(b->type) && s == 0) ? b->lit_text : b->in_text[s];
|
||||
}
|
||||
|
||||
@@ -779,7 +779,7 @@ static void place_chain(Tab* T, int head, float x, float y) {
|
||||
for (int c = head; c >= 0; c = T->ws[c].next) { place(T, c, x, y); y += T->ws[c].h; }
|
||||
}
|
||||
|
||||
static void layout_tab(Tab* T) {
|
||||
void layout_tab(Tab* T) {
|
||||
for (int i = 0; i < T->top_n; i++) {
|
||||
int h = T->top[i];
|
||||
measure_chain(T, h);
|
||||
@@ -935,7 +935,7 @@ static void ax_cat(char* out, size_t cap, size_t* len, const char* s) {
|
||||
|
||||
/* Rotulo falado de um bloco, com o que esta nos campos:
|
||||
* "repita (10) vezes", "se <(x) > (5)> então", "defina desenhe (x)" */
|
||||
static void ax_block_label(Tab* T, int bi, char* out, size_t cap, int depth) {
|
||||
void ax_block_label(Tab* T, int bi, char* out, size_t cap, int depth) {
|
||||
EdBlock* b = &T->ws[bi];
|
||||
size_t len = 0;
|
||||
out[0] = 0;
|
||||
@@ -1513,14 +1513,14 @@ static void vm_frame(Ed* ed) {
|
||||
}
|
||||
|
||||
/* Botao vermelho: para todos os scripts */
|
||||
static void vm_stop(Ed* ed) {
|
||||
void vm_stop(Ed* ed) {
|
||||
bool was = ed->vm_running;
|
||||
run_reset(ed);
|
||||
if (was) run_end(ed);
|
||||
}
|
||||
|
||||
/* Bandeira verde: roda os scripts "quando a bandeira for clicado", de cima para baixo */
|
||||
static void run_flag(Ed* ed) {
|
||||
void run_flag(Ed* ed) {
|
||||
run_begin(ed, true);
|
||||
tb_input_timer_reset(); /* como no Scratch: a bandeira zera o cronometro */
|
||||
int heads[MAX_WS], n = 0;
|
||||
@@ -1625,7 +1625,7 @@ static void bubble_set(Ed* ed, int root, TinyBlocks_Value* v) {
|
||||
}
|
||||
|
||||
/* Clique num bloco: roda a pilha inteira, ou mostra o valor de um repórter */
|
||||
static void run_click(Ed* ed, int bi) {
|
||||
void run_click(Ed* ed, int bi) {
|
||||
int root = stack_root(&CT, bi);
|
||||
Shape sh = shape_of(blk_type(ed, &CT.ws[root]));
|
||||
run_begin(ed, false);
|
||||
@@ -2473,7 +2473,7 @@ static float fly_max_scroll(Ed* ed) {
|
||||
/* ===
|
||||
* Abas e blocos compostos
|
||||
* === */
|
||||
static void tab_select(Ed* ed, int i) {
|
||||
void tab_select(Ed* ed, int i) {
|
||||
if (i < 0 || i >= ed->tab_n) return;
|
||||
ed->cur_tab = i;
|
||||
ed->editing = false;
|
||||
@@ -2745,42 +2745,45 @@ static void save_composite_from_tab(Ed* ed) {
|
||||
/* ===
|
||||
* Blueprint: conversao Ed <-> TbBlueprint e I/O de projeto
|
||||
* === */
|
||||
/* Blocos, pilhas e grupos de uma aba do editor -> aba da blueprint */
|
||||
static void tab_to_bp(const Tab* T, TbBpTab* B) {
|
||||
B->comp_id = T->comp_id;
|
||||
B->color_idx = T->color_idx;
|
||||
B->ws_sx = T->ws_sx; B->ws_sy = T->ws_sy;
|
||||
B->top_n = T->top_n;
|
||||
for (int i = 0; i < T->top_n; i++) B->top[i] = T->top[i];
|
||||
B->group_n = T->group_n < TB_BP_MAX_GROUPS ? T->group_n : TB_BP_MAX_GROUPS;
|
||||
for (int g = 0; g < B->group_n; g++) {
|
||||
const WsGroup* G = &T->groups[g];
|
||||
B->groups[g] = (TbBpGroup){ G->x, G->y, G->w, G->h, G->color, "" };
|
||||
snprintf(B->groups[g].title, sizeof(B->groups[g].title), "%s", G->title);
|
||||
}
|
||||
for (int i = 0; i < MAX_WS; i++) {
|
||||
const EdBlock* b = &T->ws[i];
|
||||
if (!b->used) continue;
|
||||
TbBpBlock* o = &B->blocks[i];
|
||||
o->used = true;
|
||||
snprintf(o->type, sizeof(o->type), "%s", b->type_name);
|
||||
o->x = b->x; o->y = b->y;
|
||||
o->parent = b->parent; o->prel = b->prel; o->next = b->next;
|
||||
for (int k = 0; k < MAX_MOUTHS; k++) o->sub[k] = b->sub[k];
|
||||
for (int s = 0; s < MAX_INPUTS; s++) {
|
||||
o->args[s] = b->args[s];
|
||||
snprintf(o->in_text[s], sizeof(o->in_text[s]), "%s", b->in_text[s]);
|
||||
}
|
||||
snprintf(o->lit, sizeof(o->lit), "%s", b->lit_text);
|
||||
}
|
||||
}
|
||||
|
||||
static TbBlueprint* ed_to_blueprint(Ed* ed) {
|
||||
TbBlueprint* bp = tb_blueprint_new();
|
||||
if (!bp) return NULL;
|
||||
bp->cur_tab = ed->cur_tab;
|
||||
|
||||
for (int ti = 0; ti < ed->tab_n; ti++) {
|
||||
Tab* T = &ed->tabs[ti];
|
||||
int idx = tb_blueprint_add_tab(bp, T->name, T->is_composite);
|
||||
int idx = tb_blueprint_add_tab(bp, ed->tabs[ti].name, ed->tabs[ti].is_composite);
|
||||
if (idx < 0) break;
|
||||
TbBpTab* B = &bp->tabs[idx];
|
||||
B->comp_id = T->comp_id;
|
||||
B->color_idx = T->color_idx;
|
||||
B->ws_sx = T->ws_sx; B->ws_sy = T->ws_sy;
|
||||
B->top_n = T->top_n;
|
||||
for (int i = 0; i < T->top_n; i++) B->top[i] = T->top[i];
|
||||
B->group_n = T->group_n < TB_BP_MAX_GROUPS ? T->group_n : TB_BP_MAX_GROUPS;
|
||||
for (int g = 0; g < B->group_n; g++) {
|
||||
const WsGroup* G = &T->groups[g];
|
||||
B->groups[g] = (TbBpGroup){ G->x, G->y, G->w, G->h, G->color, "" };
|
||||
snprintf(B->groups[g].title, sizeof(B->groups[g].title), "%s", G->title);
|
||||
}
|
||||
for (int i = 0; i < MAX_WS; i++) {
|
||||
EdBlock* b = &T->ws[i];
|
||||
if (!b->used) continue;
|
||||
TbBpBlock* o = &B->blocks[i];
|
||||
o->used = true;
|
||||
snprintf(o->type, sizeof(o->type), "%s", b->type_name);
|
||||
o->x = b->x; o->y = b->y;
|
||||
o->parent = b->parent; o->prel = b->prel; o->next = b->next;
|
||||
for (int k = 0; k < MAX_MOUTHS; k++) o->sub[k] = b->sub[k];
|
||||
for (int s = 0; s < MAX_INPUTS; s++) {
|
||||
o->args[s] = b->args[s];
|
||||
snprintf(o->in_text[s], sizeof(o->in_text[s]), "%s", b->in_text[s]);
|
||||
}
|
||||
snprintf(o->lit, sizeof(o->lit), "%s", b->lit_text);
|
||||
}
|
||||
tab_to_bp(&ed->tabs[ti], &bp->tabs[idx]);
|
||||
}
|
||||
|
||||
for (int u = 0; u < ed->user_block_n && bp->user_block_n < TB_BP_MAX_UBLOCKS; u++) {
|
||||
@@ -2936,7 +2939,7 @@ static bool has_ext(const char* path, const char* ext) {
|
||||
return n > e && !strcasecmp(path + n - e, ext);
|
||||
}
|
||||
|
||||
static bool project_load(Ed* ed, const char* path) {
|
||||
bool project_load(Ed* ed, const char* path) {
|
||||
if (has_ext(path, ".txt") || has_ext(path, ".sb") || has_ext(path, ".scratchblocks"))
|
||||
return sb_import_file(ed, path); /* notacao de texto do forum do Scratch */
|
||||
char err[160] = "";
|
||||
@@ -2953,7 +2956,7 @@ static bool project_load(Ed* ed, const char* path) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool project_save(Ed* ed, const char* path) {
|
||||
bool project_save(Ed* ed, const char* path) {
|
||||
TbBlueprint* bp = ed_to_blueprint(ed);
|
||||
bool ok = bp && tb_blueprint_save_file(bp, path, TB_BP_FMT_JSON, true);
|
||||
tb_blueprint_free(bp);
|
||||
@@ -2978,7 +2981,7 @@ static bool project_export(Ed* ed, const char* path, int fmt) {
|
||||
* Notacao de texto do forum do Scratch ("scratchblocks"): exportar a aba
|
||||
* atual / importar como aba nova (library tinyblocks-scratchblocks)
|
||||
* === */
|
||||
static void cleanup_ws(Ed* ed);
|
||||
void cleanup_ws(Ed* ed);
|
||||
|
||||
static int sb_shape(const char* type, void* ud) {
|
||||
Ed* ed = (Ed*)ud;
|
||||
@@ -3024,28 +3027,108 @@ static const char* sb_category(const char* type, void* ud) {
|
||||
/* Contexto: rotulos em portugues do editor + callbacks */
|
||||
static TbSbContext sb_context(Ed* ed, TbSbLabel** labels_out) {
|
||||
size_t n = sizeof(s_specs) / sizeof(s_specs[0]);
|
||||
TbSbLabel* L = calloc(n, sizeof(TbSbLabel));
|
||||
uint32_t nt = tb_rt_type_count(ed->rt);
|
||||
TbSbLabel* L = calloc(n + nt, sizeof(TbSbLabel));
|
||||
int k = 0;
|
||||
for (size_t i = 0; L && i < n; i++) {
|
||||
L[k].type = s_specs[i].type;
|
||||
L[k].label = s_specs[i].label;
|
||||
k++;
|
||||
}
|
||||
for (uint32_t i = 0; L && i < nt; i++) { /* rotulos declarados pelas libraries */
|
||||
const TinyBlocks_TypeBlock* t = tb_rt_type_at(ed->rt, i);
|
||||
if (!t || !t->label || spec_of(t->type_name)) continue;
|
||||
L[k].type = t->type_name;
|
||||
L[k].label = t->label;
|
||||
k++;
|
||||
}
|
||||
*labels_out = L;
|
||||
TbSbContext cx = { L, L ? k : 0, sb_shape, sb_slot_kind, sb_category, ed };
|
||||
return cx;
|
||||
}
|
||||
|
||||
/* Pilha `head` da aba T (ou a aba inteira, head < 0) em scratchblocks. malloc */
|
||||
char* ed_scratchblocks_export(Ed* ed, const Tab* T, int head) {
|
||||
TbBpTab* B = calloc(1, sizeof(TbBpTab));
|
||||
if (!B) return NULL;
|
||||
tab_to_bp(T, B);
|
||||
if (head >= 0) { B->top_n = 1; B->top[0] = head; }
|
||||
TbSbLabel* labels = NULL;
|
||||
TbSbContext cx = sb_context(ed, &labels);
|
||||
char* txt = tb_scratchblocks_export(B, &cx);
|
||||
free(labels);
|
||||
free(B);
|
||||
return txt;
|
||||
}
|
||||
|
||||
/* Texto em scratchblocks -> pilhas novas na aba T: a primeira com o canto em
|
||||
* (x, y), as outras logo abaixo. Devolve quantas criou (-1 = nada / aba cheia)
|
||||
* e as cabecas em heads (ate max). */
|
||||
int ed_scratchblocks_import(Ed* ed, Tab* T, const char* text, float x, float y,
|
||||
int* heads, int max, char* warn, size_t warn_cap) {
|
||||
TbBpTab* B = calloc(1, sizeof(TbBpTab));
|
||||
if (!B) return -1;
|
||||
TbSbLabel* labels = NULL;
|
||||
TbSbContext cx = sb_context(ed, &labels);
|
||||
int n = tb_scratchblocks_import(text, B, &cx, warn, warn_cap);
|
||||
free(labels);
|
||||
if (n <= 0) { free(B); return -1; }
|
||||
int map[TB_BP_MAX_WS];
|
||||
for (int i = 0; i < TB_BP_MAX_WS; i++) {
|
||||
map[i] = -1;
|
||||
if (!B->blocks[i].used) continue;
|
||||
map[i] = blk_alloc(T);
|
||||
if (map[i] < 0) { /* aba cheia: desfaz */
|
||||
for (int j = 0; j < i; j++) if (map[j] >= 0) T->ws[map[j]].used = false;
|
||||
if (warn && warn_cap) snprintf(warn, warn_cap, "a aba esta cheia (%d blocos)", MAX_WS);
|
||||
free(B);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
#define MAP(i) ((i) >= 0 && (i) < TB_BP_MAX_WS ? map[i] : -1)
|
||||
for (int i = 0; i < TB_BP_MAX_WS; i++) {
|
||||
const TbBpBlock* o = &B->blocks[i];
|
||||
if (!o->used) continue;
|
||||
EdBlock* b = &T->ws[map[i]];
|
||||
snprintf(b->type_name, sizeof(b->type_name), "%s", o->type);
|
||||
b->type = tb_type_block_find(ed->rt, b->type_name);
|
||||
b->parent = MAP(o->parent); b->prel = o->parent >= 0 ? o->prel : REL_NONE;
|
||||
b->next = MAP(o->next);
|
||||
for (int k = 0; k < MAX_MOUTHS; k++) b->sub[k] = MAP(o->sub[k]);
|
||||
for (int s2 = 0; s2 < MAX_INPUTS; s2++) {
|
||||
b->args[s2] = MAP(o->args[s2]);
|
||||
snprintf(b->in_text[s2], sizeof(b->in_text[s2]), "%s", o->in_text[s2]);
|
||||
}
|
||||
snprintf(b->lit_text, sizeof(b->lit_text), "%s", o->lit);
|
||||
}
|
||||
#undef MAP
|
||||
int made = 0;
|
||||
for (int t = 0; t < B->top_n; t++) {
|
||||
int h = B->top[t] >= 0 && B->top[t] < TB_BP_MAX_WS ? map[B->top[t]] : -1;
|
||||
if (h < 0) continue;
|
||||
T->ws[h].x = x; T->ws[h].y = y;
|
||||
top_add(T, h);
|
||||
if (made < max && heads) heads[made] = h;
|
||||
made++;
|
||||
}
|
||||
free(B);
|
||||
layout_tab(T); /* empilha uma embaixo da outra */
|
||||
for (int k = 1; k < made && k < max && heads; k++) {
|
||||
float x0, y0, x1, y1;
|
||||
stack_bounds(T, heads[k - 1], &x0, &y0, &x1, &y1);
|
||||
T->ws[heads[k]].y = y1 + 24.f;
|
||||
layout_tab(T);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
static void sb_export_file(Ed* ed, const char* path) {
|
||||
if (!tab_class(&CT)->blocks) {
|
||||
snprintf(ed->hot_status, sizeof(ed->hot_status), "Só abas de blocos viram scratchblocks");
|
||||
ed->hot_status_ok = false;
|
||||
return;
|
||||
}
|
||||
TbBlueprint* bp = ed_to_blueprint(ed);
|
||||
TbSbLabel* labels = NULL;
|
||||
TbSbContext cx = sb_context(ed, &labels);
|
||||
char* txt = bp ? tb_scratchblocks_export(&bp->tabs[ed->cur_tab], &cx) : NULL;
|
||||
char* txt = ed_scratchblocks_export(ed, &CT, -1);
|
||||
FILE* f = txt ? fopen(path, "wb") : NULL;
|
||||
bool ok = f && fputs(txt, f) >= 0;
|
||||
if (f) fclose(f);
|
||||
@@ -3053,8 +3136,6 @@ static void sb_export_file(Ed* ed, const char* path) {
|
||||
: "Falha ao exportar: %.200s", path);
|
||||
ed->hot_status_ok = ok;
|
||||
free(txt);
|
||||
free(labels);
|
||||
tb_blueprint_free(bp);
|
||||
}
|
||||
|
||||
static bool sb_import_text(Ed* ed, const char* text, const char* name);
|
||||
@@ -3134,12 +3215,7 @@ static void sbp_open(Ed* ed) {
|
||||
ed->hot_status_ok = false;
|
||||
return;
|
||||
}
|
||||
TbBlueprint* bp = ed_to_blueprint(ed);
|
||||
TbSbLabel* labels = NULL;
|
||||
TbSbContext cx = sb_context(ed, &labels);
|
||||
char* txt = bp ? tb_scratchblocks_export(&bp->tabs[ed->cur_tab], &cx) : NULL;
|
||||
free(labels);
|
||||
tb_blueprint_free(bp);
|
||||
char* txt = ed_scratchblocks_export(ed, &CT, -1);
|
||||
if (!txt) return;
|
||||
free(ed->sbp_text);
|
||||
ed->sbp_text = txt;
|
||||
@@ -4759,7 +4835,7 @@ static void groups_render(Ed* ed, TinyBlocks_RenderableBlock* rb, float ox, floa
|
||||
}
|
||||
|
||||
/* Limites de uma pilha (todos os blocos dela, ja com layout) */
|
||||
static void stack_bounds(Tab* T, int root, float* x0, float* y0, float* x1, float* y1) {
|
||||
void stack_bounds(Tab* T, int root, float* x0, float* y0, float* x1, float* y1) {
|
||||
*x0 = *y0 = 1e9f; *x1 = *y1 = -1e9f;
|
||||
for (int i = 0; i < MAX_WS; i++) {
|
||||
EdBlock* b = &T->ws[i];
|
||||
@@ -5819,7 +5895,7 @@ static void apply_snap(Ed* ed) {
|
||||
}
|
||||
}
|
||||
|
||||
static void cleanup_ws(Ed* ed) {
|
||||
void cleanup_ws(Ed* ed) {
|
||||
Tab* T = &CT;
|
||||
layout_tab(T);
|
||||
float y = 24 + T->ws_sy;
|
||||
@@ -6993,6 +7069,9 @@ static bool ax_perform(void* user, const TinyBlocks_A11yTree* t, int node, const
|
||||
}
|
||||
}
|
||||
|
||||
void ed_type_text(Ed* ed, const char* text) { on_text(ed, text); }
|
||||
void ed_press_key(Ed* ed, int scancode) { on_key(ed, scancode, false); }
|
||||
|
||||
static void ed_handle_input(Ed* ed, TinyBlocks_Block* evb, int from) {
|
||||
g_active_ed = ed;
|
||||
if (from < 0) {
|
||||
@@ -7102,6 +7181,7 @@ static void ed_handle_input(Ed* ed, TinyBlocks_Block* evb, int from) {
|
||||
|
||||
if (from >= 0) return; /* o resto e uma vez por quadro (principal) */
|
||||
input_feed(ed, evb); /* Sensores: mouse/teclado/toque/sensores do palco */
|
||||
ed_mcp_poll(ed); /* pedidos de agentes (servidor MCP), na thread do editor */
|
||||
vm_frame(ed); /* scripts rodando (sempre / repita) avancam um quadro */
|
||||
|
||||
/* Hot reload: checa os .so a cada ~30 frames (~0,5 s).
|
||||
@@ -7370,6 +7450,7 @@ static bool editor_instance_init(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
tb_a11y_set_bridge(ed->ax_tree, &b);
|
||||
}
|
||||
}
|
||||
ed_mcp_start(ed);
|
||||
ed->capture_view = -2;
|
||||
ed->tab_press = -1;
|
||||
ed->tabctx = -1;
|
||||
@@ -7448,6 +7529,7 @@ static void editor_instance_deinit(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
for (int u = 0; u < ed->user_block_n; u++) free(ed->user_blocks[u].snap);
|
||||
tb_libman_shutdown();
|
||||
tb_a11y_tree_free(ed->ax_tree);
|
||||
ed_mcp_stop(ed);
|
||||
if (g_active_ed == ed) g_active_ed = NULL;
|
||||
free(ed);
|
||||
*(Ed**)self->userdata = NULL;
|
||||
|
||||
Reference in new issue
Block a user