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:
1 parent
9d5d4748a9
commit
545aa86a5a
7 files changed
+434
-4
No files matched your search
@@ -43,6 +43,9 @@ TB_API TinyBlocks_A11yTree* tb_editor_accessible(TinyBlocks_Block* scene);
|
||||
/* Para testes: entrega ao leitor de QR aberto um texto como se a camera o
|
||||
* tivesse lido ("Receber blocos") */
|
||||
TB_API void tb_editor_qr_feed(TinyBlocks_Block* scene, const char* text);
|
||||
/* Para testes: o painel "Compartilhar" mostra a parte i (0..n-1) em vez do
|
||||
* loop de 1 s; -1 volta ao loop */
|
||||
TB_API void tb_editor_qr_show_part(TinyBlocks_Block* scene, int i);
|
||||
|
||||
/* Servidor MCP do editor (tb_mcp_server.h, da tinyblocks-mcp-server): as
|
||||
* ferramentas para agentes lerem e editarem as pilhas. Escuta em
|
||||
|
||||
@@ -1089,6 +1089,7 @@ typedef struct Ed {
|
||||
unsigned qrs_t0;
|
||||
char qrs_id[8];
|
||||
char qrs_title[64];
|
||||
int qrs_force; /* testes: parte fixa no painel (-1 = loop) */
|
||||
bool qrr_open, qrr_cam;
|
||||
struct TbQrReader* qrr_reader;
|
||||
unsigned char* qrr_gray;
|
||||
|
||||
@@ -8089,6 +8089,7 @@ static void qr_share_open(Ed* ed, const char* title, char* text) {
|
||||
for (int p = 0; p < len; p = qr_cut(text, len, p)) n++;
|
||||
ed->qrs_n = n;
|
||||
ed->qrs_t0 = ed->t_ms ? ed->t_ms : 1;
|
||||
ed->qrs_force = -1;
|
||||
snprintf(ed->qrs_id, sizeof(ed->qrs_id), "%04x", (unsigned)((now_ms() * 2654435761u) >> 16) & 0xffffu);
|
||||
scopy(ed->qrs_title, sizeof(ed->qrs_title), title);
|
||||
ed->qrs_open = true;
|
||||
@@ -8137,6 +8138,7 @@ static void qrs_render(Ed* ed, TinyBlocks_RenderableBlock* rb) {
|
||||
rb_text_f(rb, x + 24 + 24 + 12, y + 34, ed->qrs_title, M3_ON_SURFACE, M3_HEADLINE_S, TB_FONT_REGULAR);
|
||||
|
||||
int n = ed->qrs_n, i = n > 1 ? (int)(((ed->t_ms - ed->qrs_t0) / QR_PART_MS) % (unsigned)n) : 0;
|
||||
if (ed->qrs_force >= 0 && ed->qrs_force < n) i = ed->qrs_force;
|
||||
static char part[QR_CHUNK + 64];
|
||||
static unsigned char mods[TB_QR_MAX_SIDE * TB_QR_MAX_SIDE];
|
||||
qr_part_text(ed, i, part, sizeof(part));
|
||||
@@ -8277,6 +8279,11 @@ static void qr_recv_poll(Ed* ed) {
|
||||
tb_qr_reader_scan(ed->qrr_reader, ed->qrr_gray, w, h, qr_found_cb, ed);
|
||||
}
|
||||
|
||||
TB_API void tb_editor_qr_show_part(TinyBlocks_Block* scene, int i) {
|
||||
Ed* ed = scene && scene->userdata ? *(Ed**)scene->userdata : NULL;
|
||||
if (ed) ed->qrs_force = i;
|
||||
}
|
||||
|
||||
TB_API void tb_editor_qr_feed(TinyBlocks_Block* scene, const char* text) {
|
||||
Ed* ed = scene && scene->userdata ? *(Ed**)scene->userdata : NULL;
|
||||
if (ed && ed->qrr_open && text) qr_recv_text(ed, text, (int)strlen(text));
|
||||
|
||||
@@ -514,6 +514,15 @@ static bool model_fits(const Model* m, const TbBpTab* tab, int bi) {
|
||||
|
||||
static void render_block(const Models* M, const TbBpTab* tab, int bi, Sb* out, bool outer);
|
||||
|
||||
static bool is_number(const char* s);
|
||||
|
||||
/* Delimitador de um valor: campo redondo com texto que nao e numero vai entre
|
||||
colchetes, como no Scratch ("= [a]"); "(a)" seria lido de volta como a
|
||||
variavel a */
|
||||
static char slot_open(int kind, const char* v) {
|
||||
return kind == T_ROUND && is_number(v) ? '(' : '[';
|
||||
}
|
||||
|
||||
/* Valor de um campo, no parentese pedido pelo modelo ('(' '[' '<') */
|
||||
static void render_slot(const Models* M, const TbBpTab* tab, int bi, int s, int kind, bool dropdown, Sb* out) {
|
||||
const TbBpBlock* b = &tab->blocks[bi];
|
||||
@@ -521,7 +530,8 @@ static void render_slot(const Models* M, const TbBpTab* tab, int bi, int s, int
|
||||
if (c >= 0 && is_literal(tab->blocks[c].type)) {
|
||||
const TbBpBlock* L = &tab->blocks[c];
|
||||
const char* v = strcmp(L->type, "Literal.Text") == 0 && L->lit[0] ? L->lit : L->in_text[0];
|
||||
sb_str(out, kind == T_ROUND ? "(" : "["); sb_esc(out, v); sb_str(out, kind == T_ROUND ? ")" : "]");
|
||||
char o = slot_open(kind, v);
|
||||
sb_str(out, o == '(' ? "(" : "["); sb_esc(out, v); sb_str(out, o == '(' ? ")" : "]");
|
||||
return;
|
||||
}
|
||||
if (c >= 0 && kind == T_ANGLE && !strcmp(tab->blocks[c].type, "Port.In")) { /* <ok :: custom-arg> */
|
||||
@@ -531,10 +541,11 @@ static void render_slot(const Models* M, const TbBpTab* tab, int bi, int s, int
|
||||
if (c >= 0) { render_block(M, tab, c, out, true); return; }
|
||||
const char* v = s < TB_BP_MAX_INPUTS ? b->in_text[s] : "";
|
||||
if (kind == T_ANGLE) { sb_str(out, "<>"); return; }
|
||||
sb_str(out, kind == T_ROUND ? "(" : "[");
|
||||
char o = dropdown ? (kind == T_ROUND ? '(' : '[') : slot_open(kind, v);
|
||||
sb_str(out, o == '(' ? "(" : "[");
|
||||
sb_esc(out, v);
|
||||
if (dropdown) sb_str(out, " v");
|
||||
sb_str(out, kind == T_ROUND ? ")" : "]");
|
||||
sb_str(out, o == '(' ? ")" : "]");
|
||||
}
|
||||
|
||||
static void render_tokens(const Models* M, const Model* m, const TbBpTab* tab, int bi, Sb* out) {
|
||||
|
||||
@@ -0,0 +1,202 @@
|
||||
when flag clicked
|
||||
nova cena 3D
|
||||
cor do céu [#0f1630]
|
||||
luz vindo de x (-0.3) y (1) z (0.6)
|
||||
luz ambiente (0.55)
|
||||
set [M v] to (junte [#########] com (junte [#...#...#] com (junte [#.#.#.#.#] com (junte [#.......#] com (junte [##.#.#.##] com (junte [#.......#] com (junte [#.#.#.#.#] com (junte [#...#...#] com [#########]))))))))
|
||||
crie plano [chao] em x (0) y (0) z (0)
|
||||
mude o tamanho de [chao] para x (9) y (1) z (9)
|
||||
mude a cor de [chao] para [#1c2445]
|
||||
set [total v] to (0)
|
||||
set [n v] to (1)
|
||||
repeat (81)
|
||||
set [r v] to ([floor v] of (((n) - (1)) / (9)))
|
||||
set [c v] to (((n) - (1)) - ((r) * (9)))
|
||||
if <(letra (n) de (M)) = [#]> then
|
||||
crie cubo (junte [w] com (n)) em x ((c) - (4)) y (0.5) z ((r) - (4))
|
||||
mude a cor de (junte [w] com (n)) para [#3b5bdb]
|
||||
else
|
||||
crie esfera (junte [c] com (n)) em x ((c) - (4)) y (0.45) z ((r) - (4))
|
||||
mude o tamanho de (junte [c] com (n)) para x (0.3) y (0.3) z (0.3)
|
||||
mude a cor de (junte [c] com (n)) para [#7ef0ff]
|
||||
change [total v] by (1)
|
||||
end
|
||||
change [n v] by (1)
|
||||
end
|
||||
set [pr v] to (5)
|
||||
set [pc v] to (4)
|
||||
set [dr v] to (0)
|
||||
set [dc v] to (0)
|
||||
crie cubo [eu] em x (0) y (0.45) z (1)
|
||||
mude o tamanho de [eu] para x (0.7) y (0.7) z (0.7)
|
||||
mude a cor de [eu] para [#ffd23f]
|
||||
set [e1r v] to (1)
|
||||
set [e1c v] to (1)
|
||||
crie esfera [drone1] em x (-3) y (0.45) z (-3)
|
||||
mude o tamanho de [drone1] para x (0.75) y (0.75) z (0.75)
|
||||
mude a cor de [drone1] para [#ff5d73]
|
||||
set [e2r v] to (7)
|
||||
set [e2c v] to (7)
|
||||
crie esfera [drone2] em x (3) y (0.45) z (3)
|
||||
mude o tamanho de [drone2] para x (0.75) y (0.75) z (0.75)
|
||||
mude a cor de [drone2] para [#c77dff]
|
||||
câmera em x (0) y (10) z (6.5) olhando para x (0) y (0) z (0.6)
|
||||
set [pontos v] to (0)
|
||||
set [vivo v] to (1)
|
||||
set [t v] to (0)
|
||||
forever
|
||||
if <(vivo) = (1)> then
|
||||
if <tecla [direita] pressionada?> then
|
||||
set [dr v] to (0)
|
||||
set [dc v] to (1)
|
||||
end
|
||||
if <tecla [esquerda] pressionada?> then
|
||||
set [dr v] to (0)
|
||||
set [dc v] to (-1)
|
||||
end
|
||||
if <tecla [cima] pressionada?> then
|
||||
set [dr v] to (-1)
|
||||
set [dc v] to (0)
|
||||
end
|
||||
if <tecla [baixo] pressionada?> then
|
||||
set [dr v] to (1)
|
||||
set [dc v] to (0)
|
||||
end
|
||||
if <tocando a tela?> then
|
||||
if <(valor absoluto de (x do toque (1))) > (valor absoluto de (y do toque (1)))> then
|
||||
set [dr v] to (0)
|
||||
if <(x do toque (1)) > (0)> then
|
||||
set [dc v] to (1)
|
||||
else
|
||||
set [dc v] to (-1)
|
||||
end
|
||||
else
|
||||
set [dc v] to (0)
|
||||
if <(y do toque (1)) > (0)> then
|
||||
set [dr v] to (-1)
|
||||
else
|
||||
set [dr v] to (1)
|
||||
end
|
||||
end
|
||||
end
|
||||
change [t v] by (1)
|
||||
if <(resto de (t) por (8)) = (0)> then
|
||||
if <not <(letra (((((pr) + (dr)) * (9)) + ((pc) + (dc))) + (1)) de (M)) = [#]>> then
|
||||
change [pr v] by (dr)
|
||||
change [pc v] by (dc)
|
||||
end
|
||||
mova [eu] para x ((pc) - (4)) y (0.45) z ((pr) - (4))
|
||||
set [cel v] to (junte [c] com ((((pr) * (9)) + (pc)) + (1)))
|
||||
set [antes v] to (número de objetos 3D)
|
||||
apague o objeto (cel)
|
||||
if <(número de objetos 3D) < (antes)> then
|
||||
change [pontos v] by (1)
|
||||
end
|
||||
end
|
||||
if <<(t) > (180)> and <(resto de (t) por (18)) = (0)>> then
|
||||
set [mr v] to (0)
|
||||
set [mc v] to (0)
|
||||
if <(pr) > (e1r)> then
|
||||
set [mr v] to (1)
|
||||
end
|
||||
if <(pr) < (e1r)> then
|
||||
set [mr v] to (-1)
|
||||
end
|
||||
if <(pc) > (e1c)> then
|
||||
set [mc v] to (1)
|
||||
end
|
||||
if <(pc) < (e1c)> then
|
||||
set [mc v] to (-1)
|
||||
end
|
||||
if <<not <(mr) = (0)>> and <not <(mc) = (0)>>> then
|
||||
if <(número aleatório entre (1) e (2)) = (1)> then
|
||||
set [mr v] to (0)
|
||||
else
|
||||
set [mc v] to (0)
|
||||
end
|
||||
end
|
||||
if <(letra (((((e1r) + (mr)) * (9)) + ((e1c) + (mc))) + (1)) de (M)) = [#]> then
|
||||
set [k v] to (número aleatório entre (1) e (4))
|
||||
set [mr v] to (0)
|
||||
set [mc v] to (0)
|
||||
if <(k) = (1)> then
|
||||
set [mr v] to (1)
|
||||
end
|
||||
if <(k) = (2)> then
|
||||
set [mr v] to (-1)
|
||||
end
|
||||
if <(k) = (3)> then
|
||||
set [mc v] to (1)
|
||||
end
|
||||
if <(k) = (4)> then
|
||||
set [mc v] to (-1)
|
||||
end
|
||||
end
|
||||
if <not <(letra (((((e1r) + (mr)) * (9)) + ((e1c) + (mc))) + (1)) de (M)) = [#]>> then
|
||||
change [e1r v] by (mr)
|
||||
change [e1c v] by (mc)
|
||||
end
|
||||
mova [drone1] para x ((e1c) - (4)) y (0.45) z ((e1r) - (4))
|
||||
set [mr v] to (0)
|
||||
set [mc v] to (0)
|
||||
if <(pr) > (e2r)> then
|
||||
set [mr v] to (1)
|
||||
end
|
||||
if <(pr) < (e2r)> then
|
||||
set [mr v] to (-1)
|
||||
end
|
||||
if <(pc) > (e2c)> then
|
||||
set [mc v] to (1)
|
||||
end
|
||||
if <(pc) < (e2c)> then
|
||||
set [mc v] to (-1)
|
||||
end
|
||||
if <<not <(mr) = (0)>> and <not <(mc) = (0)>>> then
|
||||
if <(número aleatório entre (1) e (2)) = (1)> then
|
||||
set [mr v] to (0)
|
||||
else
|
||||
set [mc v] to (0)
|
||||
end
|
||||
end
|
||||
if <(letra (((((e2r) + (mr)) * (9)) + ((e2c) + (mc))) + (1)) de (M)) = [#]> then
|
||||
set [k v] to (número aleatório entre (1) e (4))
|
||||
set [mr v] to (0)
|
||||
set [mc v] to (0)
|
||||
if <(k) = (1)> then
|
||||
set [mr v] to (1)
|
||||
end
|
||||
if <(k) = (2)> then
|
||||
set [mr v] to (-1)
|
||||
end
|
||||
if <(k) = (3)> then
|
||||
set [mc v] to (1)
|
||||
end
|
||||
if <(k) = (4)> then
|
||||
set [mc v] to (-1)
|
||||
end
|
||||
end
|
||||
if <not <(letra (((((e2r) + (mr)) * (9)) + ((e2c) + (mc))) + (1)) de (M)) = [#]>> then
|
||||
change [e2r v] by (mr)
|
||||
change [e2c v] by (mc)
|
||||
end
|
||||
mova [drone2] para x ((e2c) - (4)) y (0.45) z ((e2r) - (4))
|
||||
end
|
||||
if <<<(e1r) = (pr)> and <(e1c) = (pc)>> or <<(e2r) = (pr)> and <(e2c) = (pc)>>> then
|
||||
set [vivo v] to (0)
|
||||
mude a cor de [eu] para [cinza]
|
||||
end
|
||||
if <(pontos) = (total)> then
|
||||
set [vivo v] to (2)
|
||||
end
|
||||
end
|
||||
apague tudo
|
||||
desenhe (imagem da cena de (240) por (180)) em x (0) y (0) escala (2)
|
||||
mude a cor da caneta para r (255) g (255) b (255)
|
||||
escreva (junte [Cristais: ] com (junte (pontos) com (junte [ / ] com (total)))) no palco em x (-225) y (165)
|
||||
if <(vivo) = (0)> then
|
||||
escreva [Pego pelo drone! Clique na bandeira para jogar de novo] no palco em x (-200) y (0)
|
||||
end
|
||||
if <(vivo) = (2)> then
|
||||
escreva [Voce pegou todos os cristais!] no palco em x (-120) y (0)
|
||||
end
|
||||
end
|
||||
@@ -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;
|
||||
|
||||
@@ -243,6 +243,51 @@ int main(void) {
|
||||
}
|
||||
|
||||
if (fails) printf("\n%d FALHA(S)\n", fails);
|
||||
else printf("\nTUDO OK\n");
|
||||
else /* projeto inteiro (o labirinto 3D): importar -> exportar -> importar e
|
||||
exportar de novo da o mesmo texto e a mesma arvore de blocos */
|
||||
{
|
||||
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;
|
||||
CHECK(got > 1000, "fixture tests/data/labirinto.sb");
|
||||
TbBpTab* a = fresh();
|
||||
int na = tb_scratchblocks_import(src, a, &CX, warn, sizeof(warn));
|
||||
/* sem a biblioteca 3D no contexto os blocos dela ficam como desconhecidos
|
||||
(e voltam iguais na exportacao); o harness roda o projeto de verdade */
|
||||
CHECK(na == 1, "labirinto: importa numa pilha");
|
||||
char* e1 = tb_scratchblocks_export(a, &CX);
|
||||
TbBpTab* b = fresh();
|
||||
tb_scratchblocks_import(e1 ? e1 : "", b, &CX, warn, sizeof(warn));
|
||||
char* e2 = tb_scratchblocks_export(b, &CX);
|
||||
CHECK(e1 && e2 && !strcmp(e1, e2), "labirinto: exportar(importar(exportado)) e estavel");
|
||||
bool same = a->blocks_n == b->blocks_n;
|
||||
for (int k = 0; same && k < a->blocks_n; k++)
|
||||
same = !strcmp(a->blocks[k].type, b->blocks[k].type) &&
|
||||
!strcmp(a->blocks[k].in_text[0], b->blocks[k].in_text[0]);
|
||||
CHECK(same, "labirinto: a ida e volta preserva os tipos e os campos de cada bloco");
|
||||
int gets = 0;
|
||||
for (int k = 0; k < b->blocks_n; k++)
|
||||
if (!strcmp(b->blocks[k].type, "Variables.Get") && !strcmp(b->blocks[k].in_text[0], "#")) gets++;
|
||||
CHECK(gets == 0, "labirinto: o \"#\" do mapa nao vira variavel");
|
||||
free(e1); free(e2); drop(a); drop(b);
|
||||
}
|
||||
|
||||
/* texto num campo redondo volta como texto: "= [#]", nao "(#)" (variavel) */
|
||||
{
|
||||
TbBpTab* a = fresh();
|
||||
tb_scratchblocks_import("when flag clicked\nif <(x) = [#]> then\nend\n", a, &CX, warn, sizeof(warn));
|
||||
char* e = tb_scratchblocks_export(a, &CX);
|
||||
CHECK(e && strstr(e, "= [#]") && !strstr(e, "(#)"), "texto em campo redondo exporta entre colchetes");
|
||||
TbBpTab* b = fresh();
|
||||
tb_scratchblocks_import(e ? e : "", b, &CX, warn, sizeof(warn));
|
||||
int gets = 0;
|
||||
for (int k = 0; k < b->blocks_n; k++) if (!strcmp(b->blocks[k].type, "Variables.Get")) gets++;
|
||||
CHECK(gets == 1, "ida e volta: o \"#\" continua texto (so a variavel x vira repórter)");
|
||||
free(e); drop(a); drop(b);
|
||||
}
|
||||
|
||||
printf("\nTUDO OK\n");
|
||||
return fails != 0;
|
||||
}
|
||||
Reference in new issue
Block a user