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
@@ -0,0 +1,210 @@
|
||||
/*
|
||||
* vector.c — Library de vetor 2D para o TinyBlocks Editor.
|
||||
*
|
||||
* Blocos incluídos:
|
||||
* Vector.BeginPath — inicia um novo path (sem inputs)
|
||||
* Vector.MoveTo — move caneta para (x, y)
|
||||
* Vector.LineTo — linha até (x, y)
|
||||
* Vector.ClosePath — fecha sub-path atual
|
||||
* Vector.Stroke — aplica traçado com cor/espessura RGBA
|
||||
* Vector.Fill — aplica preenchimento RGBA ao path
|
||||
* Vector.Rect — retângulo preenchido (usa Fill por cima)
|
||||
* Vector.Circle — círculo preenchido
|
||||
* Vector.Render — recebe vector e desenha no corpo do bloco (on_draw)
|
||||
*
|
||||
* Todos os blocos encadeiam: cada um recebe o vetor do anterior e devolve
|
||||
* um novo com o comando acrescentado. Combine-os com Vector.Render no final.
|
||||
*
|
||||
* Para compilar:
|
||||
* make vector
|
||||
*
|
||||
* Para carregar no editor:
|
||||
* Ctrl+O → build/vector.so ou arraste build/vector.so para a janela
|
||||
*/
|
||||
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include <tinyblocks/tb_renderable_block.h>
|
||||
|
||||
/* ── Helper: lê float de qualquer slot numérico ──────────────────────────── */
|
||||
static float gf(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t idx) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, idx);
|
||||
if (v.kind == TB_VALUE_REAL) return (float)v.as.real;
|
||||
if (v.kind == TB_VALUE_INTEGER) return (float)v.as.integer;
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
/* Helper: obtém VectorData do slot 0 e clona (ou cria novo se não conectado) */
|
||||
static TinyBlocks_VectorData* clone_vec(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, 0);
|
||||
TinyBlocks_VectorData* src = tb_value_as_vector(v);
|
||||
return src ? tb_vector_data_clone(src) : tb_vector_data_create();
|
||||
}
|
||||
|
||||
/* Macro: produz output vector e libera a ref local */
|
||||
#define EMIT_VEC(self, v) do { \
|
||||
tb_block_set_output((self), 0, tb_value_vector(v)); \
|
||||
tb_vector_data_release(v); \
|
||||
(self)->dirty = false; \
|
||||
} while(0)
|
||||
|
||||
/* ══════════════════════════════════════════════════════════════════════════
|
||||
* Implementações dos blocos
|
||||
* ══════════════════════════════════════════════════════════════════════════ */
|
||||
|
||||
static void eval_begin_path(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
TinyBlocks_VectorData* v = tb_vector_data_create();
|
||||
if (!v) return;
|
||||
tb_vector_begin_path(v);
|
||||
EMIT_VEC(self, v);
|
||||
}
|
||||
|
||||
static void eval_move_to(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_VectorData* v = clone_vec(self, rt);
|
||||
if (!v) return;
|
||||
tb_vector_move_to(v, gf(self, rt, 1), gf(self, rt, 2));
|
||||
EMIT_VEC(self, v);
|
||||
}
|
||||
|
||||
static void eval_line_to(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_VectorData* v = clone_vec(self, rt);
|
||||
if (!v) return;
|
||||
tb_vector_line_to(v, gf(self, rt, 1), gf(self, rt, 2));
|
||||
EMIT_VEC(self, v);
|
||||
}
|
||||
|
||||
static void eval_close_path(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_VectorData* v = clone_vec(self, rt);
|
||||
if (!v) return;
|
||||
tb_vector_close_path(v);
|
||||
EMIT_VEC(self, v);
|
||||
}
|
||||
|
||||
static void eval_stroke(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_VectorData* v = clone_vec(self, rt);
|
||||
if (!v) return;
|
||||
tb_vector_stroke(v, gf(self,rt,1), gf(self,rt,2),
|
||||
gf(self,rt,3), gf(self,rt,4), gf(self,rt,5));
|
||||
EMIT_VEC(self, v);
|
||||
}
|
||||
|
||||
static void eval_fill(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_VectorData* v = clone_vec(self, rt);
|
||||
if (!v) return;
|
||||
tb_vector_fill(v, gf(self,rt,1), gf(self,rt,2),
|
||||
gf(self,rt,3), gf(self,rt,4));
|
||||
EMIT_VEC(self, v);
|
||||
}
|
||||
|
||||
static void eval_rect(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_VectorData* v = clone_vec(self, rt);
|
||||
if (!v) return;
|
||||
tb_vector_rect(v, gf(self,rt,1), gf(self,rt,2),
|
||||
gf(self,rt,3), gf(self,rt,4));
|
||||
EMIT_VEC(self, v);
|
||||
}
|
||||
|
||||
static void eval_circle(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_VectorData* v = clone_vec(self, rt);
|
||||
if (!v) return;
|
||||
tb_vector_circle(v, gf(self,rt,1), gf(self,rt,2), gf(self,rt,3));
|
||||
EMIT_VEC(self, v);
|
||||
}
|
||||
|
||||
/* Vector.Render — passa o vetor adiante e tem on_render para preview */
|
||||
static void eval_render(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, 0);
|
||||
/* Clona para manter o vector vivo no output (get_input é não-owning) */
|
||||
if (v.kind == TB_VALUE_VECTOR && v.as.vector) {
|
||||
tb_vector_data_retain(v.as.vector);
|
||||
tb_block_set_output(self, 0, v); /* set_output toma ownership */
|
||||
}
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static bool on_draw_vector(TinyBlocks_Block* self, TinyBlocks* rt,
|
||||
TinyBlocks_RenderableBlock** dst) {
|
||||
(void)rt;
|
||||
if (!self) return false;
|
||||
TinyBlocks_RenderableBlock* rb = tb_renderable_acquire();
|
||||
if (!rb) return false;
|
||||
TinyBlocks_VectorData* vd = tb_value_as_vector(self->outputs[0]);
|
||||
if (vd) tb_draw_ctx_play(rb, vd);
|
||||
*dst = rb;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ══════════════════════════════════════════════════════════════════════════
|
||||
* Descritores de slots
|
||||
* ══════════════════════════════════════════════════════════════════════════ */
|
||||
static const TinyBlocks_SlotDesc out_vec[] = {{"vec", TB_VALUE_VECTOR}};
|
||||
static const TinyBlocks_SlotDesc in_vec[] = {{"vec", TB_VALUE_VECTOR}};
|
||||
static const TinyBlocks_SlotDesc in_vec_xy[] = {{"vec", TB_VALUE_VECTOR},
|
||||
{"x", TB_VALUE_REAL},
|
||||
{"y", TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc in_vec_rgba[] = {{"vec", TB_VALUE_VECTOR},
|
||||
{"r", TB_VALUE_REAL},
|
||||
{"g", TB_VALUE_REAL},
|
||||
{"b", TB_VALUE_REAL},
|
||||
{"a", TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc in_vec_rgbaw[] = {{"vec", TB_VALUE_VECTOR},
|
||||
{"r", TB_VALUE_REAL},
|
||||
{"g", TB_VALUE_REAL},
|
||||
{"b", TB_VALUE_REAL},
|
||||
{"a", TB_VALUE_REAL},
|
||||
{"w", TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc in_vec_xywh[] = {{"vec", TB_VALUE_VECTOR},
|
||||
{"x", TB_VALUE_REAL},
|
||||
{"y", TB_VALUE_REAL},
|
||||
{"w", TB_VALUE_REAL},
|
||||
{"h", TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc in_vec_cyr[] = {{"vec", TB_VALUE_VECTOR},
|
||||
{"cx", TB_VALUE_REAL},
|
||||
{"cy", TB_VALUE_REAL},
|
||||
{"r", TB_VALUE_REAL}};
|
||||
|
||||
/* ══════════════════════════════════════════════════════════════════════════
|
||||
* Registro da library
|
||||
* ══════════════════════════════════════════════════════════════════════════ */
|
||||
static bool vector_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
TinyBlocks_TypeBlock* t;
|
||||
|
||||
#define REG(name, ins, ni, fn) \
|
||||
t = tb_type_block_create(rt, name, ins, ni, out_vec, 1, fn); \
|
||||
if (t) tb_library_add(lib, &t->base)
|
||||
|
||||
REG("Vector.BeginPath", NULL, 0, eval_begin_path);
|
||||
REG("Vector.MoveTo", in_vec_xy, 3, eval_move_to);
|
||||
REG("Vector.LineTo", in_vec_xy, 3, eval_line_to);
|
||||
REG("Vector.ClosePath", in_vec, 1, eval_close_path);
|
||||
REG("Vector.Stroke", in_vec_rgbaw, 6, eval_stroke);
|
||||
REG("Vector.Fill", in_vec_rgba, 5, eval_fill);
|
||||
REG("Vector.Rect", in_vec_xywh, 5, eval_rect);
|
||||
REG("Vector.Circle", in_vec_cyr, 4, eval_circle);
|
||||
|
||||
#undef REG
|
||||
|
||||
/* Vector.Render tem on_draw — preview gráfico no corpo do bloco */
|
||||
t = tb_type_block_create(rt, "Vector.Render",
|
||||
in_vec, 1, out_vec, 1, eval_render);
|
||||
if (t) {
|
||||
t->on_draw = on_draw_vector;
|
||||
tb_library_add(lib, &t->base);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void vector_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
(void)rt; (void)lib;
|
||||
}
|
||||
|
||||
TINYBLOCKS_DEFINE_LIBRARY(vector,
|
||||
"tinyblocks-vector",
|
||||
"1.0.0",
|
||||
vector_init,
|
||||
vector_deinit)
|
||||
Reference in new issue
Block a user