Editor: "defina" na própria aba, grupos coloridos e tente/se der erro/finalmente

- Criar um Bloco põe o chapéu "defina <nome>" na aba atual (como no
  Scratch), com os parâmetros ao lado; o corpo vale na hora. Um "devolva"
  no corpo torna o bloco um repórter.
- Menu "+": Blueprint (folha em branco), Novo bloco (aba só do bloco),
  Edição de Imagem e Editor de Código.
- Grupos como os comentários do Unreal: retângulos coloridos com título;
  arrastar o título move junto as pilhas e grupos de dentro, o canto
  redimensiona, clique duplo renomeia. Salvos no projeto.
- Blocos C com três bocas (tente / se der erro / finalmente). Um erro sem
  tratamento para o script e aparece como [ERRO] no palco; erros sobem de
  dentro de Meus Blocos para quem chamou.
- Blueprint v2: 3 bocas (REL_ARG0 = 4); projetos v1 são convertidos ao abrir.
- Rótulos dos blocos de conversão e da categoria Erros.
- Abrir projeto libera os documentos das abas de código/imagem antigas.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-22 18:39:01 -03:00
1 parent 1b39c6892d
commit 9f6a1a0eb0
8 files changed
+774 -84

No files matched your search

+118 -18
View File
@@ -13,6 +13,7 @@
#include <tb_graphics.h>
#include <tb_events.h>
#include <tb_control.h>
#include <tb_errors.h>
#include <tb_operators.h>
#include <tb_variables.h>
#include <tb_appearance.h>
@@ -91,6 +92,20 @@ static bool find(const char* s, int region, int nth, float* x, float* y) {
return false;
}
/* Bloco "+" na area de trabalho (nao o "+" da barra de abas) */
static bool block_plus(float* x, float* y, float min_y) {
for (int k = 0; find("+", R_WS, k, x, y); k++) if (*y > min_y) return true;
printf("FAIL não achei o bloco \"+\"\n"); fails++;
return false;
}
/* Texto s na mesma linha de (ax, ay), a direita de ax */
static bool find_near(const char* s, float ax, float ay, float* x, float* y) {
for (int k = 0; find(s, R_WS, k, x, y); k++)
if (*y > ay - 6 && *y < ay + 6 && *x > ax) return true;
return false;
}
static bool stage_has(const char* s) {
for (uint32_t i = 0; i < ncmd; i++)
if (cmds[i].kind == TB_DRAW_TEXT && cmds[i].x >= 920 && strstr(cmds[i].text, s)) return true;
@@ -204,6 +219,7 @@ int main(void) {
tb_graphics_set_backend(tb_graphics_null_backend());
tb_library_register(rt, tb_events_get_library());
tb_library_register(rt, tb_control_get_library());
tb_library_register(rt, tb_errors_get_library());
tb_library_register(rt, tb_operators_get_library());
tb_library_register(rt, tb_variables_get_library());
tb_library_register(rt, tb_appearance_get_library());
@@ -300,50 +316,56 @@ int main(void) {
if (need("numérica ou texto", R_WS, 0, &x, &y)) click(x, y);
type("n");
key(TB_KEY_RETURN, 0, 0); /* OK no diálogo */
CHECK(ws_has("parâmetro"), "diálogo cria o bloco composto e o corpo com o parâmetro");
/* 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("quando a bandeira for clicado"), "sem abrir aba nova: os scripts continuam ali");
hx = 0; hy = 0;
need("defina", R_WS, 0, &hx, &hy);
if (need("Meus Blocos", R_CAT, 0, &x, &y)) click(x, y);
if (need("devolva", R_FLY, 0, &x, &y)) drag(x, y, 450, 300);
if (need("devolva", R_FLY, 0, &x, &y)) drag(x, y, hx, hy + 44); /* embaixo do "defina" */
if (need("Operadores", R_CAT, 0, &x, &y)) click(x, y);
if (need("+", R_FLY, 0, &x, &y)) drag(x, y, 470, 450);
if (need("+", R_FLY, 0, &x, &y)) drag(x, y, hx + 300, hy + 110);
/* encaixa o parâmetro n no slot esquerdo do "+" */
{
float px, py, zx, zy;
if (need("parâmetro", R_WS, 0, &px, &py) && need("+", R_WS, 0, &x, &y)) {
if (need("parâmetro", R_WS, 0, &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 +\n"); fails++; }
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);
}
}
/* digita 1 no slot direito do "+" */
if (need("+", R_WS, 0, &x, &y)) {
if (block_plus(&x, &y, hy + 20)) {
float zx, zy; int k = 0;
while (find("0", R_WS, k++, &zx, &zy))
if (zy > y - 4 && zy < y + 4 && zx > x) {
click(zx, zy); type("1"); key(TB_KEY_RETURN, 0, 0); break;
}
}
if (need("+", R_WS, 0, &x, &y) && need("=", R_WS, 0, &x2, &y2)) {
if (block_plus(&x, &y, hy + 20) && need("=", R_WS, 0, &x2, &y2)) {
float rep_left = x - 3 - 10;
{ float px, py; if (find("parâmetro", R_WS, 0, &px, &py)) rep_left = px - 3 - 10 - 10; }
drag(x, y, x + ((x2 + 12) - rep_left), y + (y2 - y));
}
if (need("Salvar", R_WS, 0, &x, &y)) click(x, y);
CHECK(find("dobro", R_ANY, 0, &x, &y), "bloco composto salvo");
if (need("Código", R_ANY, 0, &x, &y)) click(x, y);
if (need("Meus Blocos", R_CAT, 0, &x, &y)) click(x, y);
if (need("dobro", R_FLY, 0, &x, &y)) drag(x, y, 650, 640);
CHECK(find("dobro", R_FLY, 0, &x, &y), "bloco dobro aparece em Meus Blocos");
if (need("dobro", R_FLY, 0, &x, &y)) drag(x, y, hx + 330, hy - 30);
{
float sx, sy;
if (find("n", R_WS, 0, &sx, &sy)) {
float dx2 = 0, dy2 = 0;
for (int k = 0; find("dobro", R_WS, k, &x, &y); k++) if (y < hy) { dx2 = x; dy2 = y; }
if (dx2 > 0 && find_near("n", dx2, dy2, &sx, &sy)) {
click(sx, sy);
key(TB_KEY_BACKSPACE, 0, 0);
type("21"); key(TB_KEY_RETURN, 0, 0);
}
}
if (need("dobro", R_WS, 0, &x, &y)) { click(x, y); click(x, y); }
{ int k = 0; float bx = 0, by = 0;
while (find("dobro", R_WS, k, &x, &y)) { if (y < hy) { bx = x; by = y; } k++; } /* a chamada, não o "defina" */
if (bx > 0) { click(bx, by); click(bx, by); } }
CHECK(ws_has("22"), "bloco composto dobro(21) = 22");
if (!ws_has("22")) { for (uint32_t i = 0; i < ncmd; i++) if (cmds[i].kind==TB_DRAW_TEXT && in_region(cmds[i].x,R_WS)) printf(" ws: %s (%.0f,%.0f)\n", cmds[i].text, cmds[i].x, cmds[i].y); }
@@ -772,9 +794,10 @@ int main(void) {
float px = 0, py = 0;
if (find_tabbar("+", &px, &py)) click(px, py); else { CHECK(false, "achei o botao +"); }
frame(0, 0, 0, 0, 0, 0, NULL);
CHECK(find("Editor de Codigo", R_ANY, 0, &x, &y), "menu '+' lista as opcoes de nova aba");
CHECK(find("Edicao de Imagem", R_ANY, 0, &x, &y), "menu '+' oferece Edicao de Imagem");
if (need("Editor de Codigo", R_ANY, 0, &x, &y)) click(x, y);
CHECK(find("Editor de Código", R_ANY, 0, &x, &y) && find("Novo bloco", R_ANY, 0, &x, &y) &&
find("Blueprint", R_ANY, 0, &x, &y), "menu '+' lista Blueprint, Novo bloco, Imagem e Código");
CHECK(find("Edição de Imagem", R_ANY, 0, &x, &y), "menu '+' oferece Edição de Imagem");
if (need("Editor de Código", R_ANY, 0, &x, &y)) click(x, y);
frame(0, 0, 0, 0, 0, 0, NULL);
type("int x = 42;");
frame(0, 0, 0, 0, 0, 0, NULL);
@@ -784,14 +807,14 @@ int main(void) {
CHECK(!any_has("</code>"), "aba de codigo nao mostra a marcacao </code>");
}
/* 23. Botao "+" -> nova aba: Edicao de Imagem */
/* 23. Botao "+" -> nova aba: Edição de Imagem */
key(TB_KEY_ESCAPE, 0, 0);
frame(0, 0, 0, 0, 0, 0, NULL);
{
float px = 0, py = 0;
if (find_tabbar("+", &px, &py)) click(px, py); else { CHECK(false, "achei o botao +"); }
frame(0, 0, 0, 0, 0, 0, NULL);
if (need("Edicao de Imagem", R_ANY, 0, &x, &y)) click(x, y);
if (need("Edição de Imagem", R_ANY, 0, &x, &y)) click(x, y);
frame(0, 0, 0, 0, 0, 0, NULL);
CHECK(find("Camadas", R_WS, 0, &x, &y), "aba de imagem mostra a lista de camadas");
CHECK(find("Salvar", R_WS, 0, &x, &y), "aba de imagem mostra o botao Salvar");
@@ -1004,6 +1027,83 @@ int main(void) {
}
printf("comandos de desenho no último frame: %u\n", ncmd);
/* 26. Erros: tente / se der erro / finalmente, resultados e erro sem tratamento */
{
const char* pj = "/tmp/tb_harness_try.json";
FILE* f = fopen(pj, "wb");
if (f) {
fputs("{\"format\":\"tinyblocks-project\",\"version\":2,\"cur_tab\":0,\"tabs\":[{\"name\":\"Erros\",\"composite\":false,\"comp_id\":-1,\"color\":0,\"blocks\":["
"{\"i\":0,\"type\":\"Events.WhenRun\",\"x\":40,\"y\":40,\"next\":1},"
"{\"i\":1,\"type\":\"Errors.Try\",\"parent\":0,\"prel\":0,\"next\":7,\"sub\":[2,3,5]},"
"{\"i\":2,\"type\":\"Errors.Raise\",\"parent\":1,\"prel\":1,\"next\":6,\"in_text\":[\"deu ruim\"]},"
"{\"i\":6,\"type\":\"IO.Print\",\"parent\":2,\"prel\":0,\"in_text\":[\"nao roda\",\"\"]},"
"{\"i\":3,\"type\":\"IO.Print\",\"parent\":1,\"prel\":2,\"args\":[-1,4],\"in_text\":[\"pegou\",\"\"]},"
"{\"i\":4,\"type\":\"Errors.Caught\",\"parent\":3,\"prel\":5},"
"{\"i\":5,\"type\":\"IO.Print\",\"parent\":1,\"prel\":3,\"in_text\":[\"no finalmente\",\"\"]},"
"{\"i\":7,\"type\":\"IO.Print\",\"parent\":1,\"prel\":0,\"next\":8,\"args\":[-1,9],\"in_text\":[\"resultado\",\"\"]},"
"{\"i\":9,\"type\":\"Errors.UnwrapOr\",\"parent\":7,\"prel\":5,\"args\":[10,-1],\"in_text\":[\"\",\"77\"]},"
"{\"i\":10,\"type\":\"Errors.Attempt\",\"parent\":9,\"prel\":4,\"args\":[11]},"
"{\"i\":11,\"type\":\"Errors.Unwrap\",\"parent\":10,\"prel\":4,\"args\":[12]},"
"{\"i\":12,\"type\":\"Errors.Err\",\"parent\":11,\"prel\":4,\"in_text\":[\"x\"]},"
"{\"i\":8,\"type\":\"Errors.Raise\",\"parent\":7,\"prel\":0,\"next\":13,\"in_text\":[\"sem tente\"]},"
"{\"i\":13,\"type\":\"IO.Print\",\"parent\":8,\"prel\":0,\"in_text\":[\"nunca\",\"\"]}"
"],\"top\":[0]}],\"user_blocks\":[],\"variables\":[]}", f);
fclose(f);
}
if (need("Abrir", R_FLY, 0, &x, &y)) click(x, y);
for (int i = 0; i < 40; i++) key(TB_KEY_BACKSPACE, 0, 0);
type(pj);
key(TB_KEY_RETURN, 0, 0);
if (find("Descartar", R_ANY, 0, &x, &y)) click(x, y);
frame(0, 0, 0, 0, 0, 0, NULL);
CHECK(ws_has("tente") && ws_has("se der erro") && ws_has("finalmente"),
"bloco tente / se der erro / finalmente com três bocas");
flag();
for (int i = 0; i < 3; i++) frame(0, 0, 0, 0, 0, 0, NULL);
CHECK(stage_has("deu ruim") && !stage_has("nao roda"), "erro pula o resto do tente e vai para o se der erro");
CHECK(stage_has("no finalmente"), "finalmente roda depois");
CHECK(stage_has("77"), "tente obter + desembrulhe + valor de ... ou: erro vira valor (Result)");
CHECK(stage_has("[ERRO] sem tente") && !stage_has("nunca"), "erro sem tente para o script e aparece no palco");
remove(pj);
}
/* 25. Grupos (como no Unreal): agrupar a pilha e mover tudo pelo título */
{
float hx0, hy0;
frame(0, 0, 0, 0, 0, 0, NULL);
if (need("quando a bandeira for clicado", R_WS, 0, &hx0, &hy0)) {
drag(hx0, hy0, hx0 + 40, hy0 + 160); /* longe da barra de abas */
need("quando a bandeira for clicado", R_WS, 0, &hx0, &hy0);
frame(hx0, hy0, 0, 0, 1, 0, NULL); /* botão direito na pilha */
frame(hx0, hy0, 0, 0, 0, 0, NULL);
if (need("Agrupar esta pilha", R_ANY, 0, &x, &y)) click(x, y);
for (int i = 0; i < 12; i++) key(TB_KEY_BACKSPACE, 0, 0);
type("Principal");
key(TB_KEY_RETURN, 0, 0);
CHECK(find("Principal", R_WS, 0, &x, &y), "Agrupar esta pilha cria o grupo com título");
float tx = x, ty = y;
drag(tx, ty, tx + 120, ty + 70); /* arrasta pelo título */
float nx, ny;
CHECK(find("quando a bandeira for clicado", R_WS, 0, &nx, &ny) &&
fabsf(nx - (hx0 + 120)) < 2 && fabsf(ny - (hy0 + 70)) < 2,
"arrastar o título do grupo move a pilha junto");
CHECK(find("Principal", R_WS, 0, &x, &y) && fabsf(x - (tx + 120)) < 2,
"o grupo vai junto com a pilha");
/* salvar e abrir de novo mantém o grupo */
const char* gp = "/tmp/tb_harness_grp.json";
remove(gp);
if (need("Salvar", R_FLY, 0, &x, &y)) click(x, y);
for (int i = 0; i < 40; i++) key(TB_KEY_BACKSPACE, 0, 0);
type(gp);
key(TB_KEY_RETURN, 0, 0);
FILE* f = fopen(gp, "rb");
char buf[65536] = "";
if (f) { size_t n = fread(buf, 1, sizeof(buf) - 1, f); buf[n] = 0; fclose(f); }
CHECK(strstr(buf, "\"groups\"") && strstr(buf, "Principal"), "projeto salvo guarda os grupos");
}
}
tinyblocks_deinit(&rt);
printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails);
return fails != 0;