/* * 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 #include #include #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). */ /* 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 */