Estado atual do TinyBlocks: editor estilo Scratch + graphics como interface
Snapshot do projeto antes de implementar o diálogo "Criar um Bloco": - runtime C block-based (src/, include/) - libraries: graphics (interface), sdl2 (backend), editor (estilo Scratch), python, mcp - exemplos (hello, sdl2, editor, vector, json, customappearance, python, mcp) - Makefile com alvo help e build do editor Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
commit
4950ad6006
49 files changed
+10596
No files matched your search
+252
@@ -0,0 +1,252 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "tinyblocks/tinyblocks.h"
|
||||
|
||||
/* ── Alocação da estrutura base ──────────────────────────────────────────── */
|
||||
TinyBlocks_Block* tb_block_alloc_base(TinyBlocks* rt,
|
||||
TinyBlocks_BlockKind kind,
|
||||
size_t struct_size,
|
||||
uint32_t input_count,
|
||||
uint32_t output_count,
|
||||
const TinyBlocks_BlockVTable* vtable)
|
||||
{
|
||||
TinyBlocks_Block* b = tb_alloc(rt, struct_size);
|
||||
if (!b) return NULL;
|
||||
|
||||
b->kind = kind;
|
||||
b->vtable = vtable;
|
||||
b->id = tb_rt_next_id(rt);
|
||||
b->input_count = input_count;
|
||||
b->output_count = output_count;
|
||||
b->dirty = true;
|
||||
|
||||
if (input_count > 0) {
|
||||
b->inputs = tb_alloc(rt, input_count * sizeof(TinyBlocks_Connection));
|
||||
if (!b->inputs) { tb_free(rt, b); return NULL; }
|
||||
}
|
||||
if (output_count > 0) {
|
||||
b->outputs = tb_alloc(rt, output_count * sizeof(TinyBlocks_Value));
|
||||
if (!b->outputs) { tb_free(rt, b->inputs); tb_free(rt, b); return NULL; }
|
||||
}
|
||||
|
||||
tb_rt_register_block(rt, b);
|
||||
return b;
|
||||
}
|
||||
|
||||
/* ── Vtable padrão para blocos definidos por TypeBlock ───────────────────── */
|
||||
static void default_eval(TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{
|
||||
if (self->type && self->type->eval_fn)
|
||||
self->type->eval_fn(self, rt);
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void default_destroy(TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{
|
||||
if (self->type && self->type->instance_deinit)
|
||||
self->type->instance_deinit(self, rt);
|
||||
tb_free(rt, self->userdata);
|
||||
}
|
||||
|
||||
static size_t default_to_string(TinyBlocks_Block* self, TinyBlocks* rt,
|
||||
char* buf, size_t buf_size)
|
||||
{
|
||||
(void)rt;
|
||||
const char* tname = self->type ? self->type->type_name : "Block";
|
||||
const char* bname = self->name ? self->name : "";
|
||||
return (size_t)snprintf(buf, buf_size,
|
||||
"[%s#%llu%s%s]",
|
||||
tname, (unsigned long long)self->id,
|
||||
bname[0] ? " " : "", bname);
|
||||
}
|
||||
|
||||
static TinyBlocks_Block* default_clone(const TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{
|
||||
TinyBlocks_Block* c = tb_block_create(rt, self->type);
|
||||
if (!c) return NULL;
|
||||
if (self->name) c->name = tb_strdup(rt, self->name);
|
||||
/* copia conexões */
|
||||
memcpy(c->inputs, self->inputs, self->input_count * sizeof(TinyBlocks_Connection));
|
||||
/* copia outputs */
|
||||
for (uint32_t i = 0; i < self->output_count; ++i)
|
||||
c->outputs[i] = tb_value_clone(self->outputs[i]);
|
||||
return c;
|
||||
}
|
||||
|
||||
static const TinyBlocks_BlockVTable s_default_vtable = {
|
||||
.eval = default_eval,
|
||||
.destroy = default_destroy,
|
||||
.to_string = default_to_string,
|
||||
.clone = default_clone,
|
||||
};
|
||||
|
||||
/* ── Criação via TypeBlock ───────────────────────────────────────────────── */
|
||||
TinyBlocks_Block* tb_block_create(TinyBlocks* rt, TinyBlocks_TypeBlock* type)
|
||||
{
|
||||
if (!type) {
|
||||
tinyblocks_set_error(rt, "tb_block_create: type is NULL");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TinyBlocks_Block* b = tb_block_alloc_base(rt,
|
||||
TB_KIND_BLOCK,
|
||||
sizeof(TinyBlocks_Block),
|
||||
type->input_desc_count,
|
||||
type->output_desc_count,
|
||||
&s_default_vtable);
|
||||
if (!b) return NULL;
|
||||
|
||||
b->type = type;
|
||||
|
||||
/* Inicializa userdata por instância */
|
||||
if (type->instance_userdata_size > 0) {
|
||||
b->userdata = tb_alloc(rt, type->instance_userdata_size);
|
||||
if (!b->userdata) {
|
||||
tb_block_destroy(b, rt);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (type->instance_init && !type->instance_init(b, rt)) {
|
||||
tb_block_destroy(b, rt);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
/* ── Destruição ──────────────────────────────────────────────────────────── */
|
||||
void tb_block_destroy(TinyBlocks_Block* block, TinyBlocks* rt)
|
||||
{
|
||||
if (!block) return;
|
||||
tb_rt_unregister_block(rt, block);
|
||||
|
||||
if (block->vtable && block->vtable->destroy)
|
||||
block->vtable->destroy(block, rt);
|
||||
|
||||
tb_free(rt, block->name);
|
||||
|
||||
/* Libera valores de default dos inputs */
|
||||
for (uint32_t i = 0; i < block->input_count; ++i)
|
||||
tb_value_free(&block->inputs[i].default_value);
|
||||
tb_free(rt, block->inputs);
|
||||
|
||||
for (uint32_t i = 0; i < block->output_count; ++i)
|
||||
tb_value_free(&block->outputs[i]);
|
||||
tb_free(rt, block->outputs);
|
||||
|
||||
tb_free(rt, block);
|
||||
}
|
||||
|
||||
/* ── Avaliação ───────────────────────────────────────────────────────────── */
|
||||
void tb_block_eval(TinyBlocks_Block* block, TinyBlocks* rt)
|
||||
{
|
||||
if (!block || !block->vtable || !block->vtable->eval) return;
|
||||
|
||||
/* Detecta ciclo: bloco já está sendo avaliado nesta chamada recursiva */
|
||||
if (block->in_eval) {
|
||||
tinyblocks_set_error(rt, "ciclo detectado em '%s' — conexão ignorada",
|
||||
block->type ? block->type->type_name : "?");
|
||||
return;
|
||||
}
|
||||
block->in_eval = true;
|
||||
|
||||
/* Avalia fontes conectadas primeiro (lazy) */
|
||||
for (uint32_t i = 0; i < block->input_count; ++i) {
|
||||
TinyBlocks_Connection* conn = &block->inputs[i];
|
||||
if (conn->source && conn->source->dirty)
|
||||
tb_block_eval(conn->source, rt);
|
||||
}
|
||||
|
||||
block->vtable->eval(block, rt);
|
||||
block->in_eval = false;
|
||||
}
|
||||
|
||||
/* ── Clone ───────────────────────────────────────────────────────────────── */
|
||||
TinyBlocks_Block* tb_block_clone(const TinyBlocks_Block* block, TinyBlocks* rt)
|
||||
{
|
||||
if (!block || !block->vtable || !block->vtable->clone) return NULL;
|
||||
return block->vtable->clone(block, rt);
|
||||
}
|
||||
|
||||
/* ── Representação textual ───────────────────────────────────────────────── */
|
||||
size_t tb_block_to_string(TinyBlocks_Block* block, TinyBlocks* rt,
|
||||
char* buf, size_t buf_size)
|
||||
{
|
||||
if (!block || !buf || buf_size == 0) return 0;
|
||||
if (block->vtable && block->vtable->to_string)
|
||||
return block->vtable->to_string(block, rt, buf, buf_size);
|
||||
return (size_t)snprintf(buf, buf_size, "[Block#%llu]",
|
||||
(unsigned long long)block->id);
|
||||
}
|
||||
|
||||
/* ── Conexões ────────────────────────────────────────────────────────────── */
|
||||
bool tb_block_connect(TinyBlocks_Block* target, uint32_t input_idx,
|
||||
TinyBlocks_Block* source, uint32_t output_idx)
|
||||
{
|
||||
if (!target || input_idx >= target->input_count) return false;
|
||||
if (!source || output_idx >= source->output_count) return false;
|
||||
|
||||
TinyBlocks_Connection* conn = &target->inputs[input_idx];
|
||||
conn->source = source;
|
||||
conn->output_idx = output_idx;
|
||||
target->dirty = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void tb_block_disconnect(TinyBlocks_Block* target, uint32_t input_idx)
|
||||
{
|
||||
if (!target || input_idx >= target->input_count) return;
|
||||
target->inputs[input_idx].source = NULL;
|
||||
target->dirty = true;
|
||||
}
|
||||
|
||||
bool tb_block_set_default(TinyBlocks_Block* target, uint32_t input_idx,
|
||||
TinyBlocks_Value default_value)
|
||||
{
|
||||
if (!target || input_idx >= target->input_count) return false;
|
||||
tb_value_free(&target->inputs[input_idx].default_value);
|
||||
target->inputs[input_idx].default_value = tb_value_clone(default_value);
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ── Acesso a valores em eval_fn ─────────────────────────────────────────── */
|
||||
TinyBlocks_Value tb_block_get_input(TinyBlocks_Block* self, TinyBlocks* rt,
|
||||
uint32_t input_idx)
|
||||
{
|
||||
if (!self || input_idx >= self->input_count) return tb_value_none();
|
||||
|
||||
TinyBlocks_Connection* conn = &self->inputs[input_idx];
|
||||
if (conn->source) {
|
||||
if (conn->source->dirty) tb_block_eval(conn->source, rt);
|
||||
if (conn->output_idx < conn->source->output_count)
|
||||
return conn->source->outputs[conn->output_idx];
|
||||
}
|
||||
return conn->default_value;
|
||||
}
|
||||
|
||||
/* Toma ownership de v — o caller NÃO deve liberar v depois desta chamada.
|
||||
Se precisar manter v, passe tb_value_clone(v) em vez de v. */
|
||||
void tb_block_set_output(TinyBlocks_Block* self, uint32_t output_idx,
|
||||
TinyBlocks_Value v)
|
||||
{
|
||||
if (!self || output_idx >= self->output_count) return;
|
||||
tb_value_free(&self->outputs[output_idx]);
|
||||
self->outputs[output_idx] = v;
|
||||
}
|
||||
|
||||
/* ── Downcasting seguro ──────────────────────────────────────────────────── */
|
||||
TinyBlocks_TypeBlock* tb_block_as_type (TinyBlocks_Block* b)
|
||||
{ return b && b->kind == TB_KIND_TYPE ? (TinyBlocks_TypeBlock*)b : NULL; }
|
||||
|
||||
TinyBlocks_LibraryBlock* tb_block_as_library(TinyBlocks_Block* b)
|
||||
{ return b && b->kind == TB_KIND_LIBRARY ? (TinyBlocks_LibraryBlock*)b : NULL; }
|
||||
|
||||
TinyBlocks_TextBlock* tb_block_as_text (TinyBlocks_Block* b)
|
||||
{ return b && b->kind == TB_KIND_TEXT ? (TinyBlocks_TextBlock*)b : NULL; }
|
||||
|
||||
TinyBlocks_IntegerBlock* tb_block_as_integer(TinyBlocks_Block* b)
|
||||
{ return b && b->kind == TB_KIND_INTEGER ? (TinyBlocks_IntegerBlock*)b : NULL; }
|
||||
|
||||
TinyBlocks_RealBlock* tb_block_as_real (TinyBlocks_Block* b)
|
||||
{ return b && b->kind == TB_KIND_REAL ? (TinyBlocks_RealBlock*)b : NULL; }
|
||||
Reference in new issue
Block a user