Adiciona tinyblocks-blueprint e libraries json/xml/csv; abrir/salvar/exportar projeto

- tinyblocks-blueprint: dona do estado de edicao (abas, pool de blocos,
  vinculos parent/prel/next/sub/args, blocos compostos e variaveis) e da
  serializacao. JSON nativo (to/from) usando a AST JSON do core e export
  JSON/XML/CSV. Testado com round-trip (test-blueprint).
- tinyblocks-json: lexer + parser + emissor JSON independentes.
- tinyblocks-xml: lexer + parser de XML -> AST JSON do core + emissor.
- tinyblocks-csv: divisor de linhas/campos + parser/emissor sobre a AST JSON.
- Editor: botoes Abrir / Salvar / Exportar JSON XML CSV no menu; converte
  Ed <-> TbBlueprint; barra de caminho com modo; .json pode ser arrastado.
- Variaveis: tb_variables_clear e tb_variables_set_text.
- Harness: teste de salvar/abrir projeto; tests/blueprint_test.c.
This commit is contained in:
mguinhos committed 2026-10-20 19:51:48 -03:00
1 parent dc23b92d15
commit 1199d2d7c7
39 files changed
+3462 -24

No files matched your search

+4 -2
View File
@@ -9,7 +9,8 @@ CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \
-D_POSIX_C_SOURCE=200809L \
-I../../include -Iinclude -Isrc \
-I../tinyblocks-graphics/include \
-I../tinyblocks-variables/include
-I../tinyblocks-variables/include \
-I../tinyblocks-blueprint/include
OBJ := $(BUILD)/tb_editor.o $(BUILD)/tb_stage_widget.o
LIB_A := $(BUILD)/libtinyblocks-editor.a
@@ -22,7 +23,8 @@ shared: $(LIB_SO)
$(BUILD)/tb_editor.o: src/tb_editor.c include/tb_editor.h src/tb_stage_widget.h \
../tinyblocks-graphics/include/tb_graphics.h \
../tinyblocks-variables/include/tb_variables.h
../tinyblocks-variables/include/tb_variables.h \
../tinyblocks-blueprint/include/tb_blueprint.h
@mkdir -p $(BUILD)
$(CC) $(CFLAGS) -fPIC -c $< -o $@
+288 -15
View File
@@ -34,9 +34,11 @@
#include <tinyblocks/tinyblocks.h>
#include <tinyblocks/tb_renderable_block.h>
#include <tinyblocks/tb_json_block.h>
#include <tb_graphics.h>
#include <tinyblocks/tb_stage.h>
#include <tb_variables.h>
#include <tb_blueprint.h>
#include "tb_stage_widget.h"
#include "../include/tb_editor.h"
@@ -81,8 +83,22 @@ TB_API void tb_editor_preload(const char* path) {
#define WS_H (ed->win_h - WS_Y)
#define TAB_W 150
#define PLUS_W 28
#define LIB_BTN_X 150
#define LIB_BTN_W 170
/* Botoes de projeto no menu (geometria) */
#define OPEN_BTN_X 150
#define OPEN_BTN_W 64
#define SAVE_BTN_X 218
#define SAVE_BTN_W 70
#define EXP_LBL_X 296
#define EXP_LBL_W 60
#define EXP_JSON_X 360
#define EXP_JSON_W 54
#define EXP_XML_X 418
#define EXP_XML_W 44
#define EXP_CSV_X 466
#define EXP_CSV_W 44
#define LIB_BTN_X 522
#define LIB_BTN_W 168
/* == Geometria dos blocos === */
#define FS 12 /* texto dos blocos */
@@ -309,6 +325,12 @@ typedef enum { ES_IDLE, ES_PRESS, ES_DRAG, ES_PAN } EdSt;
typedef enum { SN_NONE, SN_NEXT, SN_SUB, SN_ABOVE, SN_ARG } SnapKind;
enum { CM_DUPLICATE, CM_DELETE, CM_EDIT_DEF, CM_CLEANUP, CM_NEW_COMPOSITE };
/* Modos da barra de caminho do menu (path_input) */
enum { PM_NONE, PM_LIB, PM_LOAD, PM_SAVE, PM_EXP_JSON, PM_EXP_XML, PM_EXP_CSV };
/* Formatos de exportacao */
enum { EX_JSON, EX_XML, EX_CSV };
typedef struct {
TinyBlocks* rt;
@@ -399,6 +421,7 @@ typedef struct {
bool path_input_active;
char path_input_buf[512];
int path_input_len;
int path_mode; /* PM_* - o que fazer no Enter */
int win_w, win_h;
} Ed;
@@ -1895,6 +1918,202 @@ static void save_composite_from_tab(Ed* ed) {
ed->hot_status_ok = true;
}
/* ===
* Blueprint: conversao Ed <-> TbBlueprint e I/O de projeto
* === */
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);
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];
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 < 2; 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);
}
}
for (int u = 0; u < ed->user_block_n && bp->user_block_n < TB_BP_MAX_UBLOCKS; u++) {
UserBlockDef* d = &ed->user_blocks[u];
TbBpUserBlock* o = &bp->user_blocks[bp->user_block_n++];
memset(o, 0, sizeof(*o));
snprintf(o->name, sizeof(o->name), "%s", d->name);
o->input_count = d->input_count;
o->output_count = d->output_count;
for (int i = 0; i < d->input_count && i < TB_BP_MAX_USLOTS; i++)
snprintf(o->input_names[i], 32, "%s", d->input_names[i]);
for (int i = 0; i < d->output_count && i < TB_BP_MAX_USLOTS; i++)
snprintf(o->output_names[i], 32, "%s", d->output_names[i]);
o->part_n = d->part_n;
for (int i = 0; i < d->part_n && i < TB_BP_MAX_UPARTS; i++) {
o->parts[i].kind = (TbBpParamKind)d->parts[i].kind;
snprintf(o->parts[i].name, 32, "%s", d->parts[i].name);
}
o->color_idx = d->color_idx;
o->tab_idx = d->tab_idx;
}
for (int i = 0; i < tb_variables_count() && bp->var_n < TB_BP_MAX_VARS; i++) {
const TinyBlocks_Value* v = tb_variables_value(i);
TbBpVar* o = &bp->vars[bp->var_n++];
memset(o, 0, sizeof(*o));
snprintf(o->name, sizeof(o->name), "%s", tb_variables_name(i));
if (v && v->kind == TB_VALUE_INTEGER) { o->kind = TB_BP_VAL_INT; o->num = (double)v->as.integer; }
else if (v && v->kind == TB_VALUE_REAL) { o->kind = TB_BP_VAL_REAL; o->num = v->as.real; }
else if (v && v->kind == TB_VALUE_TEXT && v->as.text) { o->kind = TB_BP_VAL_TEXT; snprintf(o->text, sizeof(o->text), "%s", v->as.text); }
else o->kind = TB_BP_VAL_INT;
}
return bp;
}
/* Descarta tudo do editor e reconstroi a partir da blueprint. */
static void blueprint_apply(Ed* ed, const TbBlueprint* bp) {
run_reset(ed);
invalidate_types(ed);
for (int u = 0; u < ed->user_block_n; u++) {
UserBlockDef* d = &ed->user_blocks[u];
if (d->reg_type) { tb_block_destroy(&d->reg_type->base, ed->rt); d->reg_type = NULL; }
free(d->snap); d->snap = NULL;
}
ed->user_block_n = 0;
for (int t = 0; t < ed->tab_n; t++) memset(&ed->tabs[t], 0, sizeof(Tab));
ed->tab_n = 0;
tb_variables_clear();
for (int u = 0; u < bp->user_block_n && ed->user_block_n < MAX_USER_BLOCKS; u++) {
const TbBpUserBlock* o = &bp->user_blocks[u];
UserBlockDef* d = &ed->user_blocks[ed->user_block_n++];
memset(d, 0, sizeof(*d));
snprintf(d->name, sizeof(d->name), "%s", o->name);
d->input_count = o->input_count;
d->output_count = o->output_count;
for (int i = 0; i < o->input_count && i < MAX_UBLK_SLOTS; i++)
snprintf(d->input_names[i], 32, "%s", o->input_names[i]);
for (int i = 0; i < o->output_count && i < MAX_UBLK_SLOTS; i++)
snprintf(d->output_names[i], 32, "%s", o->output_names[i]);
d->part_n = o->part_n;
for (int i = 0; i < o->part_n && i < MAX_UBLK_PARTS; i++) {
d->parts[i].kind = (ParamKind)o->parts[i].kind;
snprintf(d->parts[i].name, 32, "%s", o->parts[i].name);
}
d->color_idx = o->color_idx;
d->tab_idx = o->tab_idx;
}
for (int ti = 0; ti < bp->tab_count && ed->tab_n < MAX_TABS; ti++) {
const TbBpTab* B = &bp->tabs[ti];
int idx = ed->tab_n++;
Tab* T = &ed->tabs[idx];
memset(T, 0, sizeof(*T));
snprintf(T->name, sizeof(T->name), "%s", B->name);
T->is_composite = B->composite;
T->comp_id = B->comp_id;
T->color_idx = B->color_idx;
T->ws_sx = B->ws_sx; T->ws_sy = B->ws_sy;
for (int i = 0; i < MAX_WS; i++) {
T->ws[i].parent = -1; T->ws[i].prel = REL_NONE; T->ws[i].next = -1;
T->ws[i].sub[0] = T->ws[i].sub[1] = -1;
for (int s = 0; s < MAX_INPUTS; s++) T->ws[i].args[s] = -1;
}
for (int i = 0; i < MAX_WS; i++) {
const TbBpBlock* o = &B->blocks[i];
if (!o->used) continue;
EdBlock* b = &T->ws[i];
b->used = true;
snprintf(b->type_name, sizeof(b->type_name), "%s", o->type);
b->type = b->type_name[0] ? tb_type_block_find(ed->rt, b->type_name) : NULL;
b->x = o->x; b->y = o->y;
b->parent = o->parent; b->prel = o->prel; b->next = o->next;
for (int k = 0; k < 2; k++) b->sub[k] = o->sub[k];
for (int s = 0; s < MAX_INPUTS; s++) {
b->args[s] = o->args[s];
snprintf(b->in_text[s], sizeof(b->in_text[s]), "%s", o->in_text[s]);
}
snprintf(b->lit_text, sizeof(b->lit_text), "%s", o->lit);
}
T->top_n = B->top_n < MAX_WS ? B->top_n : MAX_WS;
for (int i = 0; i < T->top_n; i++) T->top[i] = B->top[i];
}
for (int u = 0; u < ed->user_block_n; u++) {
UserBlockDef* d = &ed->user_blocks[u];
int ti = -1;
for (int t = 0; t < ed->tab_n; t++)
if (ed->tabs[t].comp_id == u && ed->tabs[t].is_composite) { ti = t; break; }
if (ti >= 0) comp_register(ed, d, &ed->tabs[ti]);
}
for (int i = 0; i < bp->var_n; i++) {
const TbBpVar* v = &bp->vars[i];
char buf[160];
if (v->kind == TB_BP_VAL_INT) snprintf(buf, sizeof(buf), "%lld", (long long)v->num);
else if (v->kind == TB_BP_VAL_REAL) snprintf(buf, sizeof(buf), "%g", v->num);
else snprintf(buf, sizeof(buf), "%s", v->text);
tb_variables_set_text(v->name, buf);
}
ed->cur_tab = (bp->cur_tab >= 0 && bp->cur_tab < ed->tab_n) ? bp->cur_tab : 0;
palette_rebuild(ed);
tab_select(ed, ed->cur_tab);
ed->stage_buf[0] = 0;
}
static bool project_load(Ed* ed, const char* path) {
char err[160] = "";
TbBlueprint* bp = tb_blueprint_load_file(path, err, sizeof(err));
if (!bp) {
snprintf(ed->hot_status, sizeof(ed->hot_status), "Falha ao abrir: %.200s", err[0] ? err : path);
ed->hot_status_ok = false;
return false;
}
blueprint_apply(ed, bp);
tb_blueprint_free(bp);
snprintf(ed->hot_status, sizeof(ed->hot_status), "Projeto aberto: %.200s", path);
ed->hot_status_ok = true;
return true;
}
static 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);
snprintf(ed->hot_status, sizeof(ed->hot_status),
ok ? "Projeto salvo: %.200s" : "Falha ao salvar: %.200s", path);
ed->hot_status_ok = ok;
return ok;
}
static bool project_export(Ed* ed, const char* path, int fmt) {
TbBlueprint* bp = ed_to_blueprint(ed);
bool ok = bp && tb_blueprint_save_file(bp, path, fmt, true);
tb_blueprint_free(bp);
snprintf(ed->hot_status, sizeof(ed->hot_status),
ok ? "Exportado %s: %.200s" : "Falha ao exportar %s: %.200s",
tb_blueprint_format_name(fmt), path);
ed->hot_status_ok = ok;
return ok;
}
/* ===
* Diálogo modal "Criar um Bloco" (estilo Scratch)
* === */
@@ -2339,18 +2558,43 @@ static void hotlib_check(Ed* ed) {
/* ===
* Renderização da interface
* === */
static bool hit_open_btn(float mx, float my);
static bool hit_save_btn(float mx, float my);
static bool hit_exp_btn(float mx, float my, int fmt);
static void menu_button(TinyBlocks_RenderableBlock* rb, float x, float w, const char* label, bool active) {
rb_fill_rrect(rb, x, 7, w, MENU_H - 14, 6, active ? COL_MENU_DARK : COLA(255, 255, 255, 34));
rb_text_c(rb, x + (w - tw(label, FS_UI)) * 0.5f, MENU_H * 0.5f, label, COL_WHITE, FS_UI);
}
static void render_menu(Ed* ed, TinyBlocks_RenderableBlock* rb) {
rb_fill_rect(rb, 0, 0, WIN_W, MENU_H, COL_MENU);
rb_text_c(rb, 18, MENU_H*0.5f, "TinyBlocks", COL_WHITE, 20);
float mx = ed->mouse_x, my = ed->mouse_y;
bool active = ed->path_input_active;
if (ed->lib_hover || ed->path_input_active)
menu_button(rb, OPEN_BTN_X, OPEN_BTN_W, "Abrir", (active && ed->path_mode == PM_LOAD) || hit_open_btn(mx, my));
menu_button(rb, SAVE_BTN_X, SAVE_BTN_W, "Salvar", (active && ed->path_mode == PM_SAVE) || hit_save_btn(mx, my));
rb_text_c(rb, EXP_LBL_X, MENU_H*0.5f, "Exportar:", COL_WHITE, FS_UI);
menu_button(rb, EXP_JSON_X, EXP_JSON_W, "JSON", (active && ed->path_mode == PM_EXP_JSON) || hit_exp_btn(mx, my, EX_JSON));
menu_button(rb, EXP_XML_X, EXP_XML_W, "XML", (active && ed->path_mode == PM_EXP_XML) || hit_exp_btn(mx, my, EX_XML));
menu_button(rb, EXP_CSV_X, EXP_CSV_W, "CSV", (active && ed->path_mode == PM_EXP_CSV) || hit_exp_btn(mx, my, EX_CSV));
if (ed->lib_hover || (active && ed->path_mode == PM_LIB))
rb_fill_rect(rb, LIB_BTN_X, 0, LIB_BTN_W, MENU_H, COL_MENU_DARK);
rb_text_c(rb, LIB_BTN_X + 14, MENU_H*0.5f, "Carregar library=", COL_WHITE, FS_UI);
if (ed->path_input_active) {
float bx = LIB_BTN_X + LIB_BTN_W + 10, bw = WIN_W - bx - 16;
rb_fill_rrect(rb, bx, 7, bw, MENU_H-14, 6, COL_WHITE);
const char* t = ed->path_input_buf[0] ? ed->path_input_buf : "caminho do .so (Enter carrega, Esc cancela)";
const char* prompt =
ed->path_mode == PM_LOAD ? "caminho do projeto .json (Enter abre, Esc cancela)" :
ed->path_mode == PM_SAVE ? "caminho para salvar o projeto .json" :
ed->path_mode == PM_EXP_JSON ? "caminho para exportar .json" :
ed->path_mode == PM_EXP_XML ? "caminho para exportar .xml" :
ed->path_mode == PM_EXP_CSV ? "caminho para exportar .csv" :
"caminho do .so (Enter carrega, Esc cancela)";
const char* t = ed->path_input_buf[0] ? ed->path_input_buf : prompt;
rb_text_c(rb, bx + 10, MENU_H*0.5f, t, ed->path_input_buf[0] ? COL_TEXT : COL_TEXT_DIM, FS_UI);
float cw = tw(ed->path_input_buf, FS_UI);
rb_fill_rect(rb, bx + 11 + cw, MENU_H*0.5f - 8, 1.5f, 16, COL_TEXT);
@@ -2795,6 +3039,22 @@ static bool hit_detach(Ed* ed, float mx, float my) {
return ed->stage && in_rect(mx, my, WIN_W-50, MENU_H+6, 40, 32);
}
static bool hit_lib_btn(float mx, float my) { return in_rect(mx, my, LIB_BTN_X, 0, LIB_BTN_W, MENU_H); }
static bool hit_open_btn(float mx, float my) { return in_rect(mx, my, OPEN_BTN_X, 0, OPEN_BTN_W, MENU_H); }
static bool hit_save_btn(float mx, float my) { return in_rect(mx, my, SAVE_BTN_X, 0, SAVE_BTN_W, MENU_H); }
static bool hit_exp_btn(float mx, float my, int fmt) {
switch (fmt) {
case EX_JSON: return in_rect(mx, my, EXP_JSON_X, 0, EXP_JSON_W, MENU_H);
case EX_XML: return in_rect(mx, my, EXP_XML_X, 0, EXP_XML_W, MENU_H);
default: return in_rect(mx, my, EXP_CSV_X, 0, EXP_CSV_W, MENU_H);
}
}
static void start_path(Ed* ed, int mode, const char* def) {
ed->path_input_active = true;
ed->path_mode = mode;
snprintf(ed->path_input_buf, sizeof(ed->path_input_buf), "%s", def ? def : "");
ed->path_input_len = (int)strlen(ed->path_input_buf);
}
static int hit_fly(Ed* ed, float mx, float my) {
float fx = mx - CAT_W, fy = my - PAL_Y + ed->fly_scroll;
@@ -2890,12 +3150,13 @@ static void on_lmb_down(Ed* ed, float mx, float my) {
/* Barra de menu */
if (my < MENU_H) {
if (hit_lib_btn(mx, my)) {
ed->path_input_active = true;
ed->path_input_buf[0] = 0; ed->path_input_len = 0;
} else if (mx < LIB_BTN_X + LIB_BTN_W) {
ed->path_input_active = false;
}
if (hit_lib_btn(mx, my)) start_path(ed, PM_LIB, "");
else if (hit_open_btn(mx, my)) start_path(ed, PM_LOAD, "projeto.json");
else if (hit_save_btn(mx, my)) start_path(ed, PM_SAVE, "projeto.json");
else if (hit_exp_btn(mx, my, EX_JSON)) start_path(ed, PM_EXP_JSON, "projeto.json");
else if (hit_exp_btn(mx, my, EX_XML)) start_path(ed, PM_EXP_XML, "projeto.xml");
else if (hit_exp_btn(mx, my, EX_CSV)) start_path(ed, PM_EXP_CSV, "projeto.csv");
else ed->path_input_active = false;
return;
}
ed->path_input_active = false;
@@ -3021,10 +3282,21 @@ static void on_key(Ed* ed, int sc, bool ctrl) {
if (ed->ctx_menu_open) { if (sc == TB_KEY_ESCAPE) ed->ctx_menu_open = false; return; }
if (ed->path_input_active) {
if (sc == TB_KEY_ESCAPE) ed->path_input_active = false;
if (sc == TB_KEY_ESCAPE) { ed->path_input_active = false; ed->path_mode = PM_NONE; }
else if (enter) {
if (ed->path_input_buf[0]) hotlib_load(ed, ed->path_input_buf);
const char* p = ed->path_input_buf;
if (p[0]) {
switch (ed->path_mode) {
case PM_LOAD: project_load(ed, p); break;
case PM_SAVE: project_save(ed, p); break;
case PM_EXP_JSON: project_export(ed, p, EX_JSON); break;
case PM_EXP_XML: project_export(ed, p, EX_XML); break;
case PM_EXP_CSV: project_export(ed, p, EX_CSV); break;
default: hotlib_load(ed, p); break;
}
}
ed->path_input_active = false;
ed->path_mode = PM_NONE;
} else if (sc == TB_KEY_BACKSPACE) {
utf8_pop(ed->path_input_buf);
ed->path_input_len = (int)strlen(ed->path_input_buf);
@@ -3040,8 +3312,7 @@ static void on_key(Ed* ed, int sc, bool ctrl) {
return;
}
if (ctrl && sc == (TB_KEY_A + ('o' - 'a'))) {
ed->path_input_active = true;
ed->path_input_buf[0] = 0; ed->path_input_len = 0;
start_path(ed, PM_LIB, "");
return;
}
if (sc == TB_KEY_DELETE && ed->state == ES_IDLE) {
@@ -3132,6 +3403,7 @@ static void editor_eval(TinyBlocks_Block* self, TinyBlocks* rt) {
if (dropped[0]) {
size_t len = strlen(dropped);
if (len > 3 && strcmp(dropped + len - 3, ".so") == 0) hotlib_load(ed, dropped);
else if (len > 5 && strcmp(dropped + len - 5, ".json") == 0) project_load(ed, dropped);
}
if (ev_int(evb, GFX_IN_MOUSE_L_DOWN)) on_lmb_down(ed, mx, my);
@@ -3232,6 +3504,7 @@ static bool editor_instance_init(TinyBlocks_Block* self, TinyBlocks* rt) {
ed->edit_bi = -1;
ed->bubble_bi = -1;
ed->ctx_menu_bi = -1;
ed->path_mode = PM_NONE;
ed->tab_n = 1;
ed->cur_tab = 0;
@@ -3240,7 +3513,7 @@ static bool editor_instance_init(TinyBlocks_Block* self, TinyBlocks* rt) {
strcpy(ed->stage_buf,
"Clique na bandeira verde (ou F5) para executar.\n"
"Clique num bloco para executá-lo sozinho.\n");
strcpy(ed->hot_status, "Ctrl+O ou arraste um .so para carregar uma library");
strcpy(ed->hot_status, "Abrir/Salvar projeto no menu; Ctrl+O ou arraste um .so");
ed->hot_status_ok = true;
*(Ed**)self->userdata = ed;