Editor: balao de resultado no clique duplo com previa de curvas (lista de pontos)

This commit is contained in:
mguinhos committed 2026-10-20 21:01:59 -03:00
1 parent b1b15a4c49
commit f794d8b8c7
3 files changed
+167 -20

No files matched your search

+2 -2
View File
@@ -371,7 +371,7 @@ $(BUILD)/editor_harness: tests/editor_harness.c $(SRCS) \
@mkdir -p $(BUILD)
$(CC) $(CFLAGS_DBG) \
-I$(ED_DIR)/include -I$(EDITOR_SRC) -I$(GFX_INC) $(CAT_INCS) -I$(BP_DIR)/include \
-I$(API_DIR)/include -I$(LIBM_DIR)/include -I$(EN_DIR)/include -I$(TOML_DIR)/include \
-I$(API_DIR)/include -I$(LIBM_DIR)/include -I$(EN_DIR)/include -I$(TOML_DIR)/include -I$(CV_DIR)/include -I$(B64_DIR)/include \
$(SRCS) \
$(EDITOR_SRC)/tb_editor.c $(EDITOR_SRC)/tb_stage_widget.c \
$(GX_DIR)/src/tb_graphics.c \
@@ -379,7 +379,7 @@ $(BUILD)/editor_harness: tests/editor_harness.c $(SRCS) \
$(API_DIR)/src/tb_api.c $(LIBM_DIR)/src/tb_libman.c \
$(TOML_LIB_SRCS) $(TOML_DIR)/src/tb_toml.c \
$(foreach l,$(CAT_LIBS),$(LIBDIR)/tinyblocks-$(l)/src/tb_$(l).c) \
tests/editor_harness.c -o $@ -ldl -lm -rdynamic
$(CV_DIR)/src/tb_curves.c $(B64_DIR)/src/tb_base64.c tests/editor_harness.c -o $@ -ldl -lm -rdynamic
test-editor: $(BUILD)/editor_harness ## Roda o harness headless do editor (ASan/UBSan)
./$(BUILD)/editor_harness
+143 -15
View File
@@ -238,6 +238,18 @@ static const BlockSpec s_specs[] = {
{"Stage.Rect", "desenhe retângulo em x %1 y %2 largura %3 altura %4", "0|0|120|80"},
{"Stage.Circle", "desenhe círculo em x %1 y %2 raio %3", "0|0|50"},
{"Stage.Text", "escreva %1 no palco em x %2 y %3", "Olá!|-40|0"},
/* Curvas (lista de pontos [x,y]) */
{"Curves.Line", "reta de x %1 y %2 até x %3 y %4 com %5 passos", "0|0|100|0|20"},
{"Curves.Bezier", "bezier de x %1 y %2 controles (%3,%4) (%5,%6) até x %7 y %8 com %9 passos", "0|0|0|80|100|80|100|0|20"},
{"Curves.Quadratic", "curva quadrática de x %1 y %2 controle (%3,%4) até x %5 y %6 com %7 passos", "0|0|50|80|100|0|20"},
{"Curves.Arc", "arco em (%1,%2) raio %3 de %4 a %5 graus com %6 passos", "0|0|50|0|180|24"},
{"Curves.Circle", "círculo em (%1,%2) raio %3 com %4 passos", "0|0|50|36"},
{"Curves.Count", "quantos pontos em %1", NULL},
{"Curves.PointX", "x do ponto %2 de %1", "|0"},
{"Curves.PointY", "y do ponto %2 de %1", "|0"},
/* Base64 */
{"Base64.Encode", "codifique em base64 %1", NULL},
{"Base64.Decode", "decodifique base64 %1", NULL},
};
/* ===
@@ -388,6 +400,9 @@ typedef struct {
int bubble_bi;
char bubble_text[128];
TinyBlocks_JsonValue* bubble_json; /* lista de pontos (retida) ou NULL */
int last_click_bi;
unsigned last_click_ms;
char stage_buf[STAGE_BUF];
@@ -456,6 +471,8 @@ typedef struct {
* (variáveis, compostos, portas) só recebem (self, rt). */
static Ed* g_active_ed = NULL;
static void bubble_clear(Ed* ed);
static void palette_rebuild(Ed* ed);
static void utf8_pop(char* s);
static void append_text(char* dst, size_t cap, const char* add);
@@ -932,7 +949,7 @@ static void free_tree(Ed* ed, Tab* T, int bi) {
top_remove(T, bi);
if (T == &CT) {
if (ed->editing && ed->edit_bi == bi) ed->editing = false;
if (ed->bubble_bi == bi) ed->bubble_bi = -1;
if (ed->bubble_bi == bi) bubble_clear(ed);
}
b->used = false;
b->live_rt = NULL;
@@ -1558,6 +1575,59 @@ static void run_flag(Ed* ed) {
if (!started) stage_append(ed, "Nenhum script começa com \"quando a bandeira for clicado\".\n");
}
/* Libera o balão de resultado (texto + eventual lista de pontos) */
static void bubble_clear(Ed* ed) {
if (ed->bubble_json) { tb_json_release(ed->bubble_json); ed->bubble_json = NULL; }
ed->bubble_bi = -1;
ed->bubble_text[0] = 0;
}
/* Um array JSON é "lista de pontos" quando todos os itens são [x, y]? */
static bool json_is_points(const TinyBlocks_JsonValue* j) {
if (!j || tb_json_kind(j) != TB_JSON_ARRAY) return false;
uint32_t n = tb_json_array_length(j);
if (n < 2) return false;
for (uint32_t i = 0; i < n; i++) {
TinyBlocks_JsonValue* p = tb_json_array_get(j, (uint32_t)i);
bool ok = p && tb_json_kind(p) == TB_JSON_ARRAY && tb_json_array_length(p) >= 2;
if (p) tb_json_release(p);
if (!ok) return false;
}
return true;
}
/* Lê o ponto i de uma lista [[x,y],...] */
static bool json_point_at(const TinyBlocks_JsonValue* arr, int i, double* x, double* y) {
if (!arr || tb_json_kind(arr) != TB_JSON_ARRAY) return false;
TinyBlocks_JsonValue* p = tb_json_array_get(arr, (uint32_t)i);
if (!p) return false;
bool ok = tb_json_kind(p) == TB_JSON_ARRAY && tb_json_array_length(p) >= 2;
if (ok) {
TinyBlocks_JsonValue* p0 = tb_json_array_get(p, 0);
TinyBlocks_JsonValue* p1 = tb_json_array_get(p, 1);
if (x) *x = tb_json_as_number(p0, 0.0);
if (y) *y = tb_json_as_number(p1, 0.0);
tb_json_release(p0); tb_json_release(p1);
}
tb_json_release(p);
return ok;
}
/* Mostra o resultado de um repórter no balão (estilo Scratch) */
static void bubble_set(Ed* ed, int root, TinyBlocks_Value* v) {
bubble_clear(ed);
ed->bubble_bi = root;
if (v->kind == TB_VALUE_JSON && v->as.json && json_is_points(v->as.json)) {
tb_json_retain(v->as.json);
ed->bubble_json = v->as.json;
snprintf(ed->bubble_text, sizeof(ed->bubble_text), "%u pontos",
(unsigned)tb_json_array_length(v->as.json));
return;
}
value_to_str(v, ed->bubble_text, sizeof(ed->bubble_text));
if (!ed->bubble_text[0]) strcpy(ed->bubble_text, "(vazio)");
}
/* Clique num bloco: roda a pilha inteira, ou mostra o valor de um repórter */
static void run_click(Ed* ed, int bi) {
int root = stack_root(&CT, bi);
@@ -1565,9 +1635,7 @@ static void run_click(Ed* ed, int bi) {
run_begin(ed, false);
if (shape_is_value(sh)) {
TinyBlocks_Value v = eval_reporter(ed, &CT, root);
value_to_str(&v, ed->bubble_text, sizeof(ed->bubble_text));
if (!ed->bubble_text[0]) strcpy(ed->bubble_text, "(vazio)");
ed->bubble_bi = root;
bubble_set(ed, root, &v);
tb_value_free(&v);
} else {
run_chain(ed, &CT, root);
@@ -1575,6 +1643,13 @@ static void run_click(Ed* ed, int bi) {
run_end(ed);
}
/* Relógio monotônico em milissegundos (detecção de clique duplo) */
static unsigned now_ms(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (unsigned)(ts.tv_sec * 1000u + ts.tv_nsec / 1000000u);
}
/* ===
* Portas de blocos compostos ("Meus Blocos")
* === */
@@ -1803,7 +1878,7 @@ static void tab_select(Ed* ed, int i) {
if (i < 0 || i >= ed->tab_n) return;
ed->cur_tab = i;
ed->editing = false;
ed->bubble_bi = -1;
bubble_clear(ed);
if (CT.is_composite) {
const char* nm = CT.comp_id >= 0 ? ed->user_blocks[CT.comp_id].name : "";
strncpy(ed->comp_name_buf, nm, sizeof(ed->comp_name_buf) - 1);
@@ -2720,12 +2795,54 @@ static void render_workspace(Ed* ed, TinyBlocks_RenderableBlock* rb) {
if (ed->bubble_bi >= 0 && CT.ws[ed->bubble_bi].used) {
EdBlock* b = &CT.ws[ed->bubble_bi];
float w = tw(ed->bubble_text, FS) + 20;
float bx = b->ax + ox + 8, by = b->ay + oy - 32;
rb_fill_rrect(rb, bx, by, w, 26, 8, COL_BORDER);
rb_fill_rrect(rb, bx+1, by+1, w-2, 24, 7, COL_WHITE);
rb_fill_rect(rb, bx + 12, by + 25, 8, 4, COL_WHITE);
rb_text_c(rb, bx + 10, by + 13, ed->bubble_text, COL_TEXT, FS);
if (ed->bubble_json) {
/* Balão com prévia gráfica de uma lista de pontos (curva) */
const float BW = 180, BH = 140, PAD = 16;
float bx = b->ax + ox + 8, by = b->ay + oy - (BH + 6);
rb_fill_rrect(rb, bx, by, BW, BH, 10, COL_BORDER);
rb_fill_rrect(rb, bx+1, by+1, BW-2, BH-2, 9, COL_WHITE);
rb_fill_rect(rb, bx + 16, by + BH - 2, 10, 5, COL_WHITE);
int n = (int)tb_json_array_length(ed->bubble_json);
double minx = 1e30, maxx = -1e30, miny = 1e30, maxy = -1e30;
for (int i = 0; i < n; i++) {
double px, py;
if (!json_point_at(ed->bubble_json, i, &px, &py)) continue;
if (px < minx) minx = px;
if (px > maxx) maxx = px;
if (py < miny) miny = py;
if (py > maxy) maxy = py;
}
double spanx = maxx - minx, spany = maxy - miny;
if (spanx < 1e-9) { spanx = 1; minx -= 0.5; }
if (spany < 1e-9) { spany = 1; miny -= 0.5; }
float aw = BW - 2*PAD, ah = BH - 2*PAD - 12;
float sc = (float)((aw/spanx < ah/spany) ? aw/spanx : ah/spany);
float gx = bx + PAD + (aw - (float)spanx*sc)*0.5f;
float gy = by + PAD + (ah - (float)spany*sc)*0.5f;
Col cc = COLA(233, 30, 99, 255);
float lx = 0, ly = 0, fx = 0, fy = 0, ex = 0, ey = 0;
for (int i = 0; i < n; i++) {
double px, py;
if (!json_point_at(ed->bubble_json, i, &px, &py)) continue;
float sx = gx + (float)(px - minx)*sc;
float sy = gy + (float)(maxy - py)*sc; /* y para baixo */
if (i > 0) rb_line(rb, lx, ly, sx, sy, 1.6f, cc);
lx = sx; ly = sy;
if (i == 0) { fx = sx; fy = sy; }
ex = sx; ey = sy;
}
rb_fill_circle(rb, fx, fy, 3.2f, COLA(76,175,80,255)); /* início */
rb_fill_circle(rb, ex, ey, 3.2f, COLA(229,57,53,255)); /* fim */
rb_text_c(rb, bx + 10, by + BH - 4, ed->bubble_text, COL_TEXT_DIM, 11.f);
} else {
float w = tw(ed->bubble_text, FS) + 20;
float bx = b->ax + ox + 8, by = b->ay + oy - 32;
rb_fill_rrect(rb, bx, by, w, 26, 8, COL_BORDER);
rb_fill_rrect(rb, bx+1, by+1, w-2, 24, 7, COL_WHITE);
rb_fill_rect(rb, bx + 12, by + 25, 8, 4, COL_WHITE);
rb_text_c(rb, bx + 10, by + 13, ed->bubble_text, COL_TEXT, FS);
}
}
clip_clear(ed, rb);
}
@@ -3435,7 +3552,7 @@ static void start_drag(Ed* ed) {
mark_drag(T, bi, true);
ed->state = ES_DRAG;
ed->snap_kind = SN_NONE;
ed->bubble_bi = -1;
bubble_clear(ed);
}
static void consider(Ed* ed, float* best, SnapKind k, int bi, int idx, float x0, float y0, float x1, float y1) {
@@ -3670,7 +3787,7 @@ static void on_lmb_down(Ed* ed, float mx, float my) {
return;
}
ed->editing = false;
ed->bubble_bi = -1;
bubble_clear(ed);
/* Barra de menu */
if (my < MENU_H) {
@@ -3783,7 +3900,17 @@ static void on_lmb_up(Ed* ed, float mx, float my) {
else apply_snap(ed);
} else if (ed->state == ES_PRESS) {
ed->state = ES_IDLE;
run_click(ed, ed->press_bi);
int bi = ed->press_bi;
Shape sh = shape_of(blk_type(ed, &CT.ws[bi]));
unsigned t = now_ms();
bool dbl = (bi == ed->last_click_bi) && (t - ed->last_click_ms <= 400u);
ed->last_click_bi = bi;
ed->last_click_ms = t;
if (shape_is_value(sh)) {
if (dbl) run_click(ed, bi); /* balão de resultado: clique duplo */
} else {
run_click(ed, bi);
}
}
ed->state = ES_IDLE;
ed->drag_bi = -1;
@@ -4052,7 +4179,7 @@ static bool editor_instance_init(TinyBlocks_Block* self, TinyBlocks* rt) {
ed->drag_bi = -1;
ed->press_bi = -1;
ed->edit_bi = -1;
ed->bubble_bi = -1;
bubble_clear(ed);
ed->ctx_menu_bi = -1;
ed->path_mode = PM_NONE;
ed->settings_sel = -1;
@@ -4100,6 +4227,7 @@ static void editor_instance_deinit(TinyBlocks_Block* self, TinyBlocks* rt) {
/* As instâncias em run_blocks (e o palco e sua janela) são TB_KIND_BLOCK
* registradas no runtime: a fase 1 do tinyblocks_deinit já as destrói =
* aqui só soltamos o que é do editor. */
bubble_clear(ed);
free(ed->run_blocks);
for (int u = 0; u < ed->user_block_n; u++) free(ed->user_blocks[u].snap);
tb_libman_shutdown();
+22 -3
View File
@@ -14,6 +14,8 @@
#include <tb_variables.h>
#include <tb_appearance.h>
#include <tb_pen.h>
#include <tb_curves.h>
#include <tb_base64.h>
static TinyBlocks* rt;
static TinyBlocks_Block *ev, *scene;
@@ -122,6 +124,8 @@ int main(void) {
tb_library_register(rt, tb_variables_get_library());
tb_library_register(rt, tb_appearance_get_library());
tb_library_register(rt, tb_pen_get_library());
tb_library_register(rt, tb_curves_get_library());
tb_library_register(rt, tb_base64_get_library());
tb_library_register(rt, tb_graphics_get_library());
tb_library_register(rt, tb_editor_get_library());
(void)eval_ev;
@@ -169,9 +173,9 @@ int main(void) {
if (need("+", R_WS, 0, &x, &y)) {
click(x - 22, y); type("2"); key(TB_KEY_RETURN, 0, 0);
if (need("+", R_WS, 0, &x, &y)) { click(x + 22, y); type("3"); key(TB_KEY_RETURN, 0, 0); }
if (need("+", R_WS, 0, &x, &y)) click(x, y);
if (need("+", R_WS, 0, &x, &y)) { click(x, y); click(x, y); } /* clique duplo */
}
CHECK(ws_has("5"), "repórter clicado mostra balão com 5 (2+3)");
CHECK(ws_has("5"), "clique duplo no repórter mostra balão com 5 (2+3)");
/* 5. Encaixar o repórter (2+3) no campo de "mude x para (0)" */
if (need("para", R_WS, 0, &x2, &y2) && need("+", R_WS, 0, &x, &y)) {
@@ -253,7 +257,7 @@ int main(void) {
type("21"); key(TB_KEY_RETURN, 0, 0);
}
}
if (need("dobro", R_WS, 0, &x, &y)) click(x, y);
if (need("dobro", R_WS, 0, &x, &y)) { click(x, y); click(x, y); }
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); }
@@ -419,6 +423,21 @@ int main(void) {
CHECK(!find("Configurações", R_ANY, 0, &x, &y), "Esc fecha as configuracoes");
unsetenv("TINYBLOCKS_SETTINGS");
/* 19. Previa de resultado: curva (lista de pontos) em balão, clique duplo */
if (need("Curvas", R_CAT, 0, &x, &y)) click(x, y);
frame(0, 0, 0, 0, 0, 0, NULL);
if (need("círculo em (", R_FLY, 0, &x, &y)) drag(x, y, 620, 500);
frame(0, 0, 0, 0, 0, 0, NULL);
if (need("círculo em (", R_WS, 0, &x, &y)) { click(x, y); click(x, y); }
frame(0, 0, 0, 0, 0, 0, NULL);
{
int lines = 0;
for (uint32_t i = 0; i < ncmd; i++)
if (cmds[i].kind == TB_DRAW_STROKE_LINE && in_region(cmds[i].x, R_WS)) lines++;
CHECK(lines >= 20, "clique duplo na curva desenha a polilinha no balão");
}
CHECK(ws_has("37 pontos"), "balão mostra a contagem de pontos da curva");
printf("comandos de desenho no último frame: %u\n", ncmd);
tinyblocks_deinit(&rt);
printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails);