Divide as seções da paleta em libraries de categoria

Cada seção (Eventos, Controle, Operadores, Variáveis, Aparência, Caneta)
virou uma library própria, com seus blocos e eval. Foi adicionado um
metadado opcional de categoria (TinyBlocks_Category) para a UI agrupar a
paleta a partir das libraries registradas, sem hardcode no editor.

- núcleo: tb_library_set_category() e campo category em LibraryBlock
- editor: cats_rebuild/cat_of usam a library dona de cada TypeBlock;
  "Meus Blocos" (User./Port.) continua no editor
- variáveis: armazenamento movido para tinyblocks-variables, com API de
  enumeração (tb_variables_count/name/value/reset_blocks)
- palco: Editor.Stage continua no editor; a API de desenho (tb_stage_*)
  é usada pela library tinyblocks-pen
- host/tests/Makefile: registram e linkam as novas libraries
This commit is contained in:
mguinhos committed 2026-10-20 19:27:28 -03:00
1 parent 278d3f4473
commit 1cc0ff9d02
20 files changed
+1063 -597

No files matched your search

+22 -125
View File
@@ -1,11 +1,16 @@
/*
* TinyBlocks — exemplo editor
* TinyBlocks - exemplo editor
*
* Abre o editor visual com as libraries de categoria registradas:
* Eventos, Controle, Operadores, Variáveis, Aparência, Caneta, Meus Blocos.
*
* Abre o editor visual com os blocos Math.* e IO.Print pré-registrados.
* A janela vem da interface tinyblocks-graphics; o backend padrão é SDL2 e
* pode ser trocado por outro .so:
*
* ./editor [--backend libbackend.so] [library.so ...]
*
* O editor não conhece essas libraries: elas apenas registram seus blocos e
* declaram uma categoria, e o editor monta a paleta a partir disso.
*/
#include <stdio.h>
#include <stdlib.h>
@@ -13,97 +18,12 @@
#include <tinyblocks/tinyblocks.h>
#include <tb_editor.h>
#include <tb_graphics.h>
/* ── TypeBlocks de demonstração ─────────────────────────────────────────── */
static void eval_math_add(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Value a = tb_block_get_input(self, rt, 0);
TinyBlocks_Value b = tb_block_get_input(self, rt, 1);
int64_t va = a.kind==TB_VALUE_INTEGER ? a.as.integer : 0;
int64_t vb = b.kind==TB_VALUE_INTEGER ? b.as.integer : 0;
tb_block_set_output(self, 0, tb_value_integer(va + vb));
}
static void eval_math_sub(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Value a = tb_block_get_input(self, rt, 0);
TinyBlocks_Value b = tb_block_get_input(self, rt, 1);
int64_t va = a.kind==TB_VALUE_INTEGER ? a.as.integer : 0;
int64_t vb = b.kind==TB_VALUE_INTEGER ? b.as.integer : 0;
tb_block_set_output(self, 0, tb_value_integer(va - vb));
}
static void eval_math_mul(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Value a = tb_block_get_input(self, rt, 0);
TinyBlocks_Value b = tb_block_get_input(self, rt, 1);
int64_t va = a.kind==TB_VALUE_INTEGER ? a.as.integer : 0;
int64_t vb = b.kind==TB_VALUE_INTEGER ? b.as.integer : 0;
tb_block_set_output(self, 0, tb_value_integer(va * vb));
}
static void eval_math_div(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Value a = tb_block_get_input(self, rt, 0);
TinyBlocks_Value b = tb_block_get_input(self, rt, 1);
int64_t va = a.kind==TB_VALUE_INTEGER ? a.as.integer : 0;
int64_t vb = b.kind==TB_VALUE_INTEGER ? b.as.integer : 1;
tb_block_set_output(self, 0, tb_value_integer(vb ? va/vb : 0));
}
static void eval_math_mod(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Value a = tb_block_get_input(self, rt, 0);
TinyBlocks_Value b = tb_block_get_input(self, rt, 1);
int64_t va = a.kind==TB_VALUE_INTEGER ? a.as.integer : 0;
int64_t vb = b.kind==TB_VALUE_INTEGER ? b.as.integer : 1;
tb_block_set_output(self, 0, tb_value_integer(vb ? va%vb : 0));
}
static void eval_math_abs(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Value a = tb_block_get_input(self, rt, 0);
int64_t v = a.kind==TB_VALUE_INTEGER ? a.as.integer : 0;
tb_block_set_output(self, 0, tb_value_integer(v<0 ? -v : v));
}
static void eval_math_max(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Value a = tb_block_get_input(self, rt, 0);
TinyBlocks_Value b = tb_block_get_input(self, rt, 1);
int64_t va = a.kind==TB_VALUE_INTEGER ? a.as.integer : 0;
int64_t vb = b.kind==TB_VALUE_INTEGER ? b.as.integer : 0;
tb_block_set_output(self, 0, tb_value_integer(va>vb ? va : vb));
}
static void eval_math_min(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Value a = tb_block_get_input(self, rt, 0);
TinyBlocks_Value b = tb_block_get_input(self, rt, 1);
int64_t va = a.kind==TB_VALUE_INTEGER ? a.as.integer : 0;
int64_t vb = b.kind==TB_VALUE_INTEGER ? b.as.integer : 0;
tb_block_set_output(self, 0, tb_value_integer(va<vb ? va : vb));
}
static void eval_io_print(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Value label = tb_block_get_input(self, rt, 0);
TinyBlocks_Value value = tb_block_get_input(self, rt, 1);
const char* lbl = label.kind==TB_VALUE_TEXT ? label.as.text : "resultado";
if (value.kind==TB_VALUE_INTEGER)
printf("%s = %lld\n", lbl, (long long)value.as.integer);
else if (value.kind==TB_VALUE_REAL)
printf("%s = %g\n", lbl, value.as.real);
else if (value.kind==TB_VALUE_TEXT)
printf("%s = %s\n", lbl, value.as.text ? value.as.text : "");
else printf("%s\n", lbl);
}
static void eval_io_print_int(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Value v = tb_block_get_input(self, rt, 0);
int64_t n = v.kind==TB_VALUE_INTEGER ? v.as.integer : 0;
printf("%lld\n", (long long)n);
}
static void eval_text_concat(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Value a = tb_block_get_input(self, rt, 0);
TinyBlocks_Value b = tb_block_get_input(self, rt, 1);
const char* sa = a.kind==TB_VALUE_TEXT ? a.as.text : "";
const char* sb = b.kind==TB_VALUE_TEXT ? b.as.text : "";
size_t n = strlen(sa)+strlen(sb)+1;
char* buf = (char*)malloc(n);
if (!buf) return;
memcpy(buf, sa, strlen(sa));
memcpy(buf+strlen(sa), sb, strlen(sb)+1);
TinyBlocks_Value v; v.kind=TB_VALUE_TEXT; v.as.text=buf;
tb_block_set_output(self, 0, v);
}
static void eval_text_len(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Value a = tb_block_get_input(self, rt, 0);
int64_t n = a.kind==TB_VALUE_TEXT && a.as.text ? (int64_t)strlen(a.as.text) : 0;
tb_block_set_output(self, 0, tb_value_integer(n));
}
#include <tb_events.h>
#include <tb_control.h>
#include <tb_operators.h>
#include <tb_variables.h>
#include <tb_appearance.h>
#include <tb_pen.h>
int main(int argc, char** argv) {
TinyBlocks* rt = NULL;
@@ -118,42 +38,19 @@ int main(int argc, char** argv) {
}
}
/* Slots reutilizáveis */
static const TinyBlocks_SlotDesc two_int[] = {
{"a", TB_VALUE_INTEGER}, {"b", TB_VALUE_INTEGER} };
static const TinyBlocks_SlotDesc one_int[] = {
{"n", TB_VALUE_INTEGER} };
static const TinyBlocks_SlotDesc int_out[] = { {"result", TB_VALUE_INTEGER} };
static const TinyBlocks_SlotDesc print_in[] = {
{"label", TB_VALUE_TEXT}, {"value", TB_VALUE_NONE} };
static const TinyBlocks_SlotDesc two_text[] = {
{"a", TB_VALUE_TEXT}, {"b", TB_VALUE_TEXT} };
static const TinyBlocks_SlotDesc text_out[] = { {"result", TB_VALUE_TEXT} };
static const TinyBlocks_SlotDesc text_in[] = { {"text", TB_VALUE_TEXT} };
/* Libraries de categoria, na ordem em que aparecem na paleta */
tb_library_register(rt, tb_events_get_library());
tb_library_register(rt, tb_control_get_library());
tb_library_register(rt, tb_operators_get_library());
tb_library_register(rt, tb_variables_get_library());
tb_library_register(rt, tb_appearance_get_library());
tb_library_register(rt, tb_pen_get_library());
/* Math */
tb_type_block_create(rt,"Math.Add", two_int,2, int_out,1, eval_math_add);
tb_type_block_create(rt,"Math.Sub", two_int,2, int_out,1, eval_math_sub);
tb_type_block_create(rt,"Math.Mul", two_int,2, int_out,1, eval_math_mul);
tb_type_block_create(rt,"Math.Div", two_int,2, int_out,1, eval_math_div);
tb_type_block_create(rt,"Math.Mod", two_int,2, int_out,1, eval_math_mod);
tb_type_block_create(rt,"Math.Abs", one_int,1, int_out,1, eval_math_abs);
tb_type_block_create(rt,"Math.Max", two_int,2, int_out,1, eval_math_max);
tb_type_block_create(rt,"Math.Min", two_int,2, int_out,1, eval_math_min);
/* IO */
tb_type_block_create(rt,"IO.Print", print_in,2, NULL,0, eval_io_print);
tb_type_block_create(rt,"IO.PrintInt", one_int,1, NULL,0, eval_io_print_int);
/* Text */
tb_type_block_create(rt,"Text.Concat", two_text,2, text_out,1, eval_text_concat);
tb_type_block_create(rt,"Text.Length", text_in, 1, int_out, 1, eval_text_len);
/* Interface gráfica primeiro: o editor mede texto e cria o palco ao nascer */
/* Interface gráfica: o editor mede texto e cria o palco ao nascer */
tb_library_register(rt, tb_graphics_get_library());
tb_library_register(rt, tb_editor_get_library());
/* Graphics.Input → Editor.Scene → Graphics.Window */
/* Graphics.Input - Editor.Scene - Graphics.Window */
TinyBlocks_TypeBlock* t_input = tb_type_block_find(rt, "Graphics.Input");
TinyBlocks_TypeBlock* t_scene = tb_type_block_find(rt, "Editor.Scene");
TinyBlocks_TypeBlock* t_win = tb_type_block_find(rt, "Graphics.Window");