Editor/scratchblocks: projeto inteiro por QR e texto em campo redondo

- tb_editor_qr_show_part (para testes) fixa a parte mostrada no painel
  "Compartilhar blueprint" em vez do loop de 1 s; -1 volta ao loop.
- scratchblocks: campo redondo com texto que nao e numero sai entre
  colchetes ("= [#]"), senao a volta lia como variavel; o texto importado
  num campo redondo volta como texto. "exportar(importar(exportado))"
  fica estavel.
- Harness cenario 36: o labirinto 3D (tests/data/labirinto.sb) entra pelo
  MCP, roda (37 cristais), exporta e importa com o mesmo resultado e passa
  pelo QR: cada parte desenhada e lida de volta, recebida fora de ordem e
  com repetidas.
- Fixture tests/data/labirinto.sb (mapa em notacao do forum).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-29 00:25:40 -03:00
1 parent 9d5d4748a9
commit 545aa86a5a
7 files changed
+434 -4

No files matched your search

+161
View File
@@ -28,6 +28,7 @@
#include <tb_environ.h>
#include <tb_a11y.h>
#include <tb_mcp_server.h>
#include <tb_qr.h>
static TinyBlocks* rt;
static TinyBlocks_Block *ev, *scene;
@@ -146,6 +147,7 @@ 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)
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) {
frame(x0, y0, 1, 0, 0, 0, NULL);
@@ -301,6 +303,87 @@ static bool drawn_text(int node, const char* sub) {
/* Texto -> string JSON (para mandar codigo pelo MCP) */
static char* json_str(const char* t) {
size_t n = strlen(t);
char* o = malloc(n * 6 + 3), *p = o;
*p++ = '"';
for (; *t; t++) {
unsigned char c = (unsigned char)*t;
if (c == '"' || c == '\\') { *p++ = '\\'; *p++ = (char)c; }
else if (c == '\n') { *p++ = '\\'; *p++ = 'n'; }
else if (c < 0x20) p += sprintf(p, "\\u%04x", c);
else *p++ = (char)c;
}
*p++ = '"'; *p = 0;
return o;
}
/* O QR desenhado no quadro (faixas na cor de tinta) -> imagem cinza -> texto */
static char g_qr_text[2048];
static void qr_found_h(const char* t, int n, void* u) { (void)u; snprintf(g_qr_text, sizeof(g_qr_text), "%.*s", n, t); }
static bool read_drawn_qr(void) {
int W = g_win_w, H = g_win_h;
unsigned char* g = malloc((size_t)W * H);
memset(g, 255, (size_t)W * H);
for (uint32_t i = 0; i < ncmd; i++) {
const TinyBlocks_DrawCmd* c = &cmds[i];
if (c->kind != TB_DRAW_FILL_RECT || fabsf(c->r - 20.f/255.f) > 0.01f || fabsf(c->g - 22.f/255.f) > 0.01f ||
fabsf(c->b - 30.f/255.f) > 0.01f) continue;
for (int y = (int)c->y; y < (int)(c->y + c->h) && y < H; y++)
for (int x = (int)c->x; x < (int)(c->x + c->w) && x < W; x++)
if (x >= 0 && y >= 0) g[(size_t)y * W + x] = 0;
}
TbQrReader* r = tb_qr_reader_new();
g_qr_text[0] = 0;
int n = tb_qr_reader_scan(r, g, W, H, qr_found_h, NULL);
tb_qr_reader_free(r);
free(g);
return n == 1;
}
/* Cenario 36: aba de blueprint nova, codigo pelo MCP, saida do programa */
static void sc_new_tab(void) {
ax_do(ax_in(ax_in(0, TB_A11Y_TAB_LIST, "Abas"), TB_A11Y_BUTTON, "Nova aba"), TB_A11Y_ACT_ACTIVATE);
ax_do(ax_in(ax_in(0, TB_A11Y_MENU, "Nova aba"), TB_A11Y_MENU_ITEM, "Blueprint"), TB_A11Y_ACT_ACTIVATE);
frame(0, 0, 0, 0, 0, 0, NULL);
}
static void sc_add_code(const char* code, bool* err) {
char* js = json_str(code);
char* a = malloc(strlen(js) + 64);
sprintf(a, "{\"code\":%s,\"x\":40,\"y\":40}", js);
free(mcp("add_scratchblocks", a, err, NULL));
free(a); free(js);
}
static int sc_stack_id(void) {
bool err = false;
char* t = mcp("list_stacks", "{}", &err, NULL);
const char* p = t ? strstr(t, "pilha ") : NULL;
int id = p ? atoi(p + 6) : -1;
free(t);
return id;
}
/* Apaga todas as pilhas da aba atual (a aba nova ja vem com uma) */
static void sc_clear_tab(void) {
for (int i = 0, id; i < 16 && (id = sc_stack_id()) >= 0; i++) {
bool err = false;
char a[64];
snprintf(a, sizeof(a), "{\"stack\":%d}", id);
free(mcp("delete_stack", a, &err, NULL));
}
}
static bool sc_output_has(const char* sub) {
for (int i = 0; i < 3; i++) frame(0, 0, 0, 0, 0, 0, NULL);
bool err = false;
char* o = mcp("get_output", "{}", &err, NULL);
bool r = o && strstr(o, sub);
if (!r) printf(" saida: %.300s\n", o ? o : "(nada)");
free(o);
return r;
}
int main(void) {
/* configurações isoladas: o harness não lê nem grava o ~/.config real */
setenv("XDG_CONFIG_HOME", "/tmp/tb_harness_cfg", 1);
@@ -1762,6 +1845,84 @@ int main(void) {
key(TB_KEY_ESCAPE, 0, 0);
}
/* 36. Projeto inteiro (o labirinto 3D): roda; exportar em scratchblocks e
* importar noutra aba da o mesmo resultado; compartilhar por QR (cada
* parte desenhada e lida de volta) e receber noutra aba tambem */
{
FILE* f = fopen("tests/data/labirinto.sb", "rb");
static char src[16384];
size_t got = f ? fread(src, 1, sizeof(src) - 1, f) : 0;
if (f) fclose(f);
src[got] = 0;
bool err = false;
sc_new_tab();
sc_clear_tab();
sc_add_code(src, &err);
CHECK(!err, "projeto: o labirinto entra pelo MCP");
flag();
CHECK(sc_output_has("total = 37\n"), "projeto: roda e acha 37 cristais (mapa com as paredes)");
stop();
/* exportar -> importar no lugar da pilha (as variaveis sao globais e a
bandeira roda todas as abas: uma copia so do jogo por vez) */
char* exp = mcp("get_scratchblocks", "{}", &err, NULL);
CHECK(!err && exp && strstr(exp, "= [#]") && !strstr(exp, "(#)"), "exportar: o \"#\" sai como texto");
sc_clear_tab();
sc_add_code(exp ? exp : "", &err);
flag();
CHECK(sc_output_has("total = 37\n"), "importar o exportado: mesmo resultado (37 cristais)");
stop();
free(exp);
/* compartilhar por QR: cada parte desenhada vira texto de novo */
int w = ws();
float rx = N(w)->x + N(w)->w - 30, ry = N(w)->y + 20; /* canto livre, longe da pilha */
frame(rx, ry, 0, 0, 1, 0, NULL);
frame(rx, ry, 0, 0, 0, 0, NULL);
ax_do(ax_need(ax_in(0, TB_A11Y_MENU, "Menu de contexto"), TB_A11Y_MENU_ITEM, "Compartilhar blueprint (QR)"),
TB_A11Y_ACT_ACTIVATE);
frame(0, 0, 0, 0, 0, 0, NULL);
int dlg = ax_in(0, TB_A11Y_DIALOG, "Compartilhar blueprint");
int parts = 0;
const char* lab = ax_value(ax_in(dlg, TB_A11Y_LABEL, "Parte"));
sscanf(lab, "Parte %*d de %d", &parts);
CHECK(parts > 5, "compartilhar: o projeto vira varias partes");
char** got_parts = calloc((size_t)(parts > 0 ? parts : 1), sizeof(char*));
int read_ok = 0;
for (int i = 0; i < parts; i++) {
tb_editor_qr_show_part(scene, i);
frame(0, 0, 0, 0, 0, 0, NULL);
char want[16];
snprintf(want, sizeof(want), "|%d|%d|", i + 1, parts);
if (read_drawn_qr() && !strncmp(g_qr_text, "TB1|", 4) && strstr(g_qr_text, want)) {
got_parts[i] = strdup(g_qr_text);
read_ok++;
}
}
CHECK(read_ok == parts, "compartilhar: cada QR desenhado e lido de volta (parte certa)");
tb_editor_qr_show_part(scene, -1);
key(TB_KEY_ESCAPE, 0, 0);
/* receber na mesma aba, ja sem a pilha: partes de tras para frente, com repetidas */
sc_clear_tab();
frame(0, 0, 0, 0, 0, 0, NULL);
CHECK(sc_stack_id() < 0, "compartilhar: a aba fica vazia antes de receber");
frame(rx, ry, 0, 0, 1, 0, NULL);
frame(rx, ry, 0, 0, 0, 0, NULL);
ax_do(ax_need(ax_in(0, TB_A11Y_MENU, "Menu de contexto"), TB_A11Y_MENU_ITEM, "Receber blocos (QR)"),
TB_A11Y_ACT_ACTIVATE);
frame(0, 0, 0, 0, 0, 0, NULL);
if (parts > 1 && got_parts[1]) tb_editor_qr_feed(scene, got_parts[1]); /* repetida */
for (int i = parts - 1; i >= 0; i--) if (got_parts[i]) tb_editor_qr_feed(scene, got_parts[i]);
frame(0, 0, 0, 0, 0, 0, NULL);
CHECK(ax_in(0, TB_A11Y_DIALOG, "Receber blocos") < 0, "receber: todas as partes (fora de ordem, repetidas)");
flag();
CHECK(sc_output_has("total = 37\n"), "receber por QR: o projeto roda igual (37 cristais)");
stop();
for (int i = 0; i < parts; i++) free(got_parts[i]);
free(got_parts);
}
tinyblocks_deinit(&rt);
printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails);
return fails != 0;