Files
tinyblocks/examples/editor/main.c
T
mguinhosandClaude 4950ad6006 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>
2026-10-20 19:10:43 -03:00

186 lines
8.6 KiB
C

/*
* TinyBlocks — exemplo editor
*
* 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 ...]
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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));
}
int main(int argc, char** argv) {
TinyBlocks* rt = NULL;
if (!tinyblocks_init(&rt, NULL)) return 1;
/* --backend troca o backend gráfico; os demais .so são pré-carregados */
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--backend") == 0 && i + 1 < argc) {
if (!tb_graphics_load_backend(argv[++i])) return 1;
} else {
tb_editor_preload(argv[i]);
}
}
/* 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} };
/* 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 */
tb_library_register(rt, tb_graphics_get_library());
tb_library_register(rt, tb_editor_get_library());
/* 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");
if (!t_input || !t_scene || !t_win) {
fprintf(stderr, "blocos Graphics.Input / Editor.Scene / Graphics.Window não encontrados\n");
return 1;
}
TinyBlocks_Block* input = tb_block_create(rt, t_input);
TinyBlocks_Block* scene = tb_block_create(rt, t_scene);
TinyBlocks_Block* win = tb_block_create(rt, t_win);
tb_block_set_default(scene, 0, tb_value_block(input));
TinyBlocks_Value title = tb_value_text("TinyBlocks Editor");
tb_block_set_default(win, 0, title); /* set_default clona */
tb_value_free(&title);
tb_block_set_default(win, 1, tb_value_integer(1280));
tb_block_set_default(win, 2, tb_value_integer(800));
tb_block_set_default(win, 3, tb_value_block(scene));
tb_block_set_default(win, 4, tb_value_block(input));
tb_block_eval(win, rt); /* abre a janela */
printf("Backend gráfico: %s\n", tb_graphics_backend()->name);
tb_graphics_run(rt); /* loop até a janela principal fechar */
tinyblocks_deinit(&rt);
return 0;
}