Files
tinyblocks/libraries/tinyblocks-blueprint/include/blueprint.h
T
mguinhosandClaude Opus 5.5 9f6a1a0eb0 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>
2026-10-22 18:39:01 -03:00

143 lines
4.6 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 256
#define TB_BP_MAX_INPUTS 16
#define TB_BP_MAX_TABS 8
#define TB_BP_MAX_UBLOCKS 16
#define TB_BP_MAX_UPARTS 16
#define TB_BP_MAX_USLOTS 8
#define TB_BP_MAX_VARS 64
#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 {
TbBpBlock blocks[TB_BP_MAX_WS];
int top[TB_BP_MAX_WS];
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 } 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);
/* Aloca um bloco no pool da aba; devolve o indice ou -1. */
int tb_blueprint_alloc_block(TbBpTab* t);
/* 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). */
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 */