Files
tinyblocks/libraries/tinyblocks-blueprint/include/blueprint.h
T
mguinhosandClaude Opus 5.5 f180eaf75a Editor: zoom (Ctrl+roda, pinca), selecao de texto e partes do bloco moveis
- Area de scripts com zoom: Ctrl+roda no ponteiro, roda rola na vertical e
  Shift+roda na horizontal; pinca com dois dedos (toque) e pinca do
  touchpad (XInput 2.4 no X11, GFX_IN_ZOOM). Desenho em escala 1 escalado
  depois (rb_scale_from); a arvore acessivel acompanha (tb_a11y_scale_from).
- SDL2: cache de fontes com 64 tamanhos e descarte do menos usado (antes
  vazava uma fonte por chamada depois de 8 tamanhos).
- Campos de texto e editor de codigo: cursor em qualquer posicao, selecao
  (mouse, Shift+setas, clique duplo), Ctrl+A/C/X/V; no toque, segurar
  seleciona a palavra com alcas e barra Recortar/Copiar/Colar/Tudo.
- "Criar um Bloco": o nome e uma parte movel (PARAM_NAME) e pode ser
  apagado quando um rotulo logo depois assume; hexagono booleano sem corte.
- Harness: cenarios 32-34 (zoom, pinca, dialogo, selecao e clipboard).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 21:04:26 -03:00

167 lines
6.1 KiB
C

/*
* TinyBlocks - tinyblocks-blueprint / include/blueprint.h
*
* TbBlueprint e o modelo de um "projeto" do editor (estilo Scratch):
* - abas (TbBpTab) com um pool estavel de blocos (TbBpBlock);
* - vinculos entre blocos por indice: parent/prel, next, sub[2], args[];
* - definicoes de blocos compostos (TbBpUserBlock);
* - variaveis (TbBpVar).
*
* A library e dona do estado de edicao e da logica de serializacao:
* converter para/de JSON (formato nativo) e exportar JSON/XML/CSV.
*
* A AST usada na serializacao e a do core (TinyBlocks_JsonValue).
*/
#ifndef TB_BLUEPRINT_H
#define TB_BLUEPRINT_H
#include <stddef.h>
#include <stdbool.h>
#include <tinyblocks/tb_json_block.h>
#define TB_BP_MAX_WS 65536 /* teto de blocos por aba (o vetor cresce sob demanda) */
#define TB_BP_MAX_INPUTS 16
#define TB_BP_MAX_TABS 8
#define TB_BP_MAX_UBLOCKS 64
#define TB_BP_MAX_UPARTS 16
#define TB_BP_MAX_USLOTS 8
#define TB_BP_MAX_VARS 4096
#define TB_BP_LIT_CAP 512
#define TB_BP_MAX_GROUPS 32
/* Formatos de serializacao */
enum { TB_BP_FMT_JSON, TB_BP_FMT_XML, TB_BP_FMT_CSV };
/* Relacao de um bloco com o pai (identica ao editor) */
#define TB_BP_REL_NONE (-1)
#define TB_BP_REL_NEXT 0
#define TB_BP_REL_SUB0 1
#define TB_BP_REL_ARG0 4 /* bocas 0..2 (SUB0..SUB0+2); era 3 no formato v1 */
/* -- Blocos e abas --- */
typedef struct {
bool used;
char type[64]; /* "Events.WhenRun", "User.X", ... */
float x, y;
int parent, prel;
int next;
int sub[3];
int args[TB_BP_MAX_INPUTS];
char in_text[TB_BP_MAX_INPUTS][64];
char lit[TB_BP_LIT_CAP];
} TbBpBlock;
/* Grupo: retangulo colorido com titulo que junta varias pilhas */
typedef struct {
float x, y, w, h;
int color;
char title[48];
} TbBpGroup;
typedef struct {
/* O indice do bloco e a identidade dele: o vetor pode crescer (realloc)
sem invalidar os vinculos, mas nunca compacta. */
TbBpBlock* blocks;
int blocks_cap; /* slots alocados */
int blocks_n; /* maior indice usado + 1 */
int blocks_scan; /* dica do proximo slot livre */
int* top;
int top_cap;
int top_n;
float ws_sx, ws_sy;
char name[48];
bool composite;
int comp_id; /* indice em user_blocks, ou -1 */
int color_idx;
TbBpGroup groups[TB_BP_MAX_GROUPS];
int group_n;
} TbBpTab;
/* -- Blocos compostos (protótipo) --- */
typedef enum { TB_BP_PARAM_NUM, TB_BP_PARAM_BOOL, TB_BP_PARAM_LABEL,
TB_BP_PARAM_NAME /* posicao do nome (sem ela: na frente) */ } TbBpParamKind;
typedef struct { TbBpParamKind kind; char name[32]; } TbBpParam;
typedef struct {
char name[48];
int input_count, output_count;
char input_names[TB_BP_MAX_USLOTS][32];
char output_names[TB_BP_MAX_USLOTS][32];
TbBpParam parts[TB_BP_MAX_UPARTS];
int part_n;
int color_idx;
int tab_idx;
} TbBpUserBlock;
/* -- Variaveis --- */
typedef enum { TB_BP_VAL_INT, TB_BP_VAL_REAL, TB_BP_VAL_TEXT } TbBpValueKind;
typedef struct {
char name[32];
TbBpValueKind kind;
double num;
char text[128];
} TbBpVar;
/* -- Documento --- */
typedef struct {
TbBpTab tabs[TB_BP_MAX_TABS];
int tab_count;
TbBpUserBlock user_blocks[TB_BP_MAX_UBLOCKS];
int user_block_n;
TbBpVar vars[TB_BP_MAX_VARS];
int var_n;
int cur_tab;
} TbBlueprint;
/* -- Ciclo de vida / edicao --- */
TbBlueprint* tb_blueprint_new(void);
void tb_blueprint_free(TbBlueprint* bp);
void tb_blueprint_clear(TbBlueprint* bp);
/* Cria uma aba; devolve o indice ou -1. */
int tb_blueprint_add_tab(TbBlueprint* bp, const char* name, bool composite);
TbBpTab* tb_blueprint_tab(TbBlueprint* bp, int i);
/* Solta o pool de uma TbBpTab isolada (a struct em si e de quem chamou) */
void tb_blueprint_tab_free(TbBpTab* t);
/* Aloca um bloco no pool da aba; devolve o indice ou -1. */
int tb_blueprint_alloc_block(TbBpTab* t);
/* Garante que o slot i existe (zerado); devolve o bloco ou NULL. */
TbBpBlock* tb_blueprint_tab_slot(TbBpTab* t, int i);
/* Liga child ao pai na relacao rel (child < 0 desliga). */
void tb_blueprint_link(TbBpTab* t, int parent, int rel, int child);
void tb_blueprint_top_add(TbBpTab* t, int bi);
/* Variaveis */
void tb_blueprint_var_set_text(TbBlueprint* bp, const char* name, const char* value);
/* -- Serializacao --- */
/* Formato nativo (JSON). to_json devolve ref nova; from_json nao consome 'root'. */
TinyBlocks_JsonValue* tb_blueprint_to_json(const TbBlueprint* bp);
TbBlueprint* tb_blueprint_from_json(const TinyBlocks_JsonValue* root);
/* Serializa para texto no formato pedido (malloc; libere com free). */
/* Formatos de arquivo como interface: cada um sabe escrever (e talvez ler)
um projeto. serialize/parse/save/load despacham pela tabela. */
typedef struct TbBpFormat {
int id; /* TB_BP_FMT_* */
const char* name; /* "json" */
const char* ext; /* ".json" */
char* (*write)(const TbBlueprint* bp, bool pretty); /* malloc */
TbBlueprint* (*read)(const char* text, char* err, size_t errcap); /* NULL = so exporta */
} TbBpFormat;
const TbBpFormat* tb_blueprint_format(int fmt); /* NULL se nao existe */
const TbBpFormat* tb_blueprint_format_for_path(const char* path); /* pela extensao */
char* tb_blueprint_serialize(const TbBlueprint* bp, int fmt, bool pretty);
/* Le do texto. Suporta o formato nativo (JSON). */
TbBlueprint* tb_blueprint_parse(const char* text, int fmt, char* err, size_t errcap);
/* Arquivos */
bool tb_blueprint_save_file(const TbBlueprint* bp, const char* path, int fmt, bool pretty);
TbBlueprint* tb_blueprint_load_file(const char* path, char* err, size_t errcap);
const char* tb_blueprint_format_name(int fmt); /* "json"/"xml"/"csv" */
#endif /* TB_BLUEPRINT_H */