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:
1 parent
278d3f4473
commit
1cc0ff9d02
20 files changed
+1063
-597
No files matched your search
@@ -32,13 +32,13 @@ OBJS_DBG := $(patsubst src/%.c,$(BUILD)/%-dbg.o,$(SRCS))
|
||||
all: static example ## Compila a biblioteca estática e o exemplo hello (padrão)
|
||||
|
||||
help: ## Lista os alvos disponíveis
|
||||
@echo "TinyBlocks — alvos do make:"
|
||||
@echo "TinyBlocks - alvos do make:"
|
||||
@echo
|
||||
@awk 'BEGIN {FS = ":.*## "} /^[a-zA-Z0-9_-]+:.*## / { printf " \033[36m%-18s\033[0m %s\n", $$1, $$2 }' $(MAKEFILE_LIST)
|
||||
@echo
|
||||
@echo "Exemplo: make run-editor (libraries extras: ./build/editor build/mylib.so)"
|
||||
|
||||
# ── Biblioteca ────────────────────────────────────────────────────────────
|
||||
# -- Biblioteca ---
|
||||
static: $(LIB_A) ## Compila build/libtinyblocks.a
|
||||
shared: $(LIB_SO) ## Compila build/libtinyblocks.so
|
||||
|
||||
@@ -58,7 +58,7 @@ $(LIB_A): $(OBJS)
|
||||
$(LIB_SO): $(OBJS)
|
||||
$(CC) -shared -o $@ $^
|
||||
|
||||
# ── Exemplo hello ─────────────────────────────────────────────────────────
|
||||
# -- Exemplo hello ---
|
||||
$(EXAMPLE): examples/hello/main.c $(LIB_A)
|
||||
$(CC) $(CFLAGS_REL) $< -o $@ -L$(BUILD) -ltinyblocks
|
||||
|
||||
@@ -67,7 +67,7 @@ example: $(EXAMPLE) ## Compila o exemplo hello
|
||||
run: example ## Compila e roda o exemplo hello
|
||||
./$(EXAMPLE)
|
||||
|
||||
# ── Build de debug com ASan/UBSan ────────────────────────────────────────
|
||||
# -- Build de debug com ASan/UBSan ---
|
||||
$(BUILD)/libtinyblocks-dbg.a: $(OBJS_DBG)
|
||||
ar rcs $@ $^
|
||||
|
||||
@@ -77,7 +77,7 @@ $(BUILD)/hello-dbg: examples/hello/main.c $(BUILD)/libtinyblocks-dbg.a
|
||||
debug: $(BUILD)/hello-dbg ## Compila e roda o hello com ASan/UBSan
|
||||
./$(BUILD)/hello-dbg
|
||||
|
||||
# ── Library SDL2 ──────────────────────────────────────────────────────────
|
||||
# -- Library SDL2 ---
|
||||
SDL2_CFLAGS := $(shell sdl2-config --cflags)
|
||||
SDL2_LIBS := $(shell sdl2-config --libs)
|
||||
|
||||
@@ -107,7 +107,7 @@ $(BUILD)/libtinyblocks-sdl2.so: libraries/tinyblocks-sdl2/src/tb_sdl2.c $(GFX_HD
|
||||
sdl2-backend: static $(BUILD)/libtinyblocks-sdl2.so ## Backend SDL2 como .so (./build/editor --backend build/libtinyblocks-sdl2.so)
|
||||
@echo "Build backend OK: $(BUILD)/libtinyblocks-sdl2.so"
|
||||
|
||||
# ── Library Graphics (interface; backend padrão: SDL2) ───────────────────
|
||||
# -- Library Graphics (interface; backend padrão: SDL2) ---
|
||||
$(BUILD)/tb_graphics.o: libraries/tinyblocks-graphics/src/tb_graphics.c $(GFX_HDR) $(HDRS)
|
||||
@mkdir -p $(BUILD)
|
||||
$(CC) $(CFLAGS_REL) -DTB_GRAPHICS_DEFAULT_SDL2 \
|
||||
@@ -122,7 +122,33 @@ graphics: static $(BUILD)/libtinyblocks-graphics.a $(BUILD)/libtinyblocks-sdl2.a
|
||||
run-sdl2: sdl2 ## Compila e roda o exemplo SDL2
|
||||
LD_LIBRARY_PATH=$(BUILD) ./$(BUILD)/sdl2-example
|
||||
|
||||
# ── Library Editor ────────────────────────────────────────────────────────
|
||||
# -- Libraries de categoria (uma por seção da paleta) --
|
||||
CAT_LIBS := events control operators variables appearance pen
|
||||
CAT_A := $(patsubst %,$(BUILD)/libtinyblocks-%.a,$(CAT_LIBS))
|
||||
CAT_SO := $(patsubst %,$(BUILD)/tinyblocks-%.so,$(CAT_LIBS))
|
||||
CAT_LDFLAGS := $(patsubst %,-ltinyblocks-%,$(CAT_LIBS))
|
||||
|
||||
define CAT_LIB_RULE
|
||||
$(BUILD)/tb_$(1).o: libraries/tinyblocks-$(1)/src/tb_$(1).c libraries/tinyblocks-$(1)/include/tb_$(1).h $(HDRS)
|
||||
@mkdir -p $(BUILD)
|
||||
$(CC) $(CFLAGS_REL) -Ilibraries/tinyblocks-$(1)/include -Ilibraries/tinyblocks-editor/src -fPIC -c $$< -o $$@
|
||||
|
||||
$(BUILD)/libtinyblocks-$(1).a: $(BUILD)/tb_$(1).o
|
||||
ar rcs $$@ $$^
|
||||
|
||||
$(BUILD)/tinyblocks-$(1).so: libraries/tinyblocks-$(1)/src/tb_$(1).c libraries/tinyblocks-$(1)/include/tb_$(1).h $(LIB_A)
|
||||
$(CC) $(CFLAGS_REL) -Ilibraries/tinyblocks-$(1)/include -Ilibraries/tinyblocks-editor/src -fPIC -shared $$< -o $$@ -L$(BUILD) -ltinyblocks '-Wl,-rpath,$$$$ORIGIN'
|
||||
endef
|
||||
|
||||
$(foreach l,$(CAT_LIBS),$(eval $(call CAT_LIB_RULE,$(l))))
|
||||
|
||||
category-libs: static $(CAT_A) ## Compila as libraries de categoria (.a)
|
||||
@echo "Build category libs OK: $(CAT_A)"
|
||||
|
||||
category-sos: static $(CAT_SO) ## Compila as libraries de categoria (.so, hot reload)
|
||||
@echo "Build category .so OK: $(CAT_SO)"
|
||||
|
||||
# -- Library Editor ---
|
||||
SDL2TTF_CFLAGS := $(shell pkg-config --cflags SDL2_ttf 2>/dev/null)
|
||||
SDL2TTF_LIBS := $(shell pkg-config --libs SDL2_ttf 2>/dev/null)
|
||||
|
||||
@@ -130,7 +156,8 @@ EDITOR_SRC := libraries/tinyblocks-editor/src
|
||||
EDITOR_HDRS := $(EDITOR_SRC)/tb_stage.h libraries/tinyblocks-editor/include/tb_editor.h $(GFX_HDR) $(HDRS)
|
||||
|
||||
$(BUILD)/tb_editor.o: $(EDITOR_SRC)/tb_editor.c $(EDITOR_HDRS)
|
||||
$(CC) $(CFLAGS_REL) -Ilibraries/tinyblocks-editor/include -I$(GFX_INC) -fPIC -c $< -o $@
|
||||
$(CC) $(CFLAGS_REL) -Ilibraries/tinyblocks-editor/include -I$(GFX_INC) \
|
||||
$(patsubst %,-Ilibraries/tinyblocks-%/include,$(CAT_LIBS)) -fPIC -c $< -o $@
|
||||
|
||||
$(BUILD)/tb_stage.o: $(EDITOR_SRC)/tb_stage.c $(EDITOR_HDRS)
|
||||
$(CC) $(CFLAGS_REL) -I$(GFX_INC) -fPIC -c $< -o $@
|
||||
@@ -139,52 +166,55 @@ $(BUILD)/libtinyblocks-editor.a: $(BUILD)/tb_editor.o $(BUILD)/tb_stage.o
|
||||
ar rcs $@ $^
|
||||
|
||||
$(BUILD)/editor: examples/editor/main.c $(LIB_A) $(BUILD)/libtinyblocks-editor.a \
|
||||
$(BUILD)/libtinyblocks-graphics.a $(BUILD)/libtinyblocks-sdl2.a
|
||||
$(CC) $(CFLAGS_REL) -Ilibraries/tinyblocks-editor/include -I$(GFX_INC) \
|
||||
$(BUILD)/libtinyblocks-graphics.a $(BUILD)/libtinyblocks-sdl2.a $(CAT_A)
|
||||
$(CC) $(CFLAGS_REL) \
|
||||
-Ilibraries/tinyblocks-editor/include -I$(GFX_INC) \
|
||||
$(patsubst %,-Ilibraries/tinyblocks-%/include,$(CAT_LIBS)) \
|
||||
$< -o $@ \
|
||||
-L$(BUILD) -ltinyblocks-editor -ltinyblocks-graphics -ltinyblocks-sdl2 -ltinyblocks \
|
||||
-L$(BUILD) -Wl,--start-group -ltinyblocks-editor -ltinyblocks-graphics \
|
||||
-ltinyblocks-sdl2 $(CAT_LDFLAGS) -ltinyblocks -Wl,--end-group \
|
||||
$(SDL2_LIBS) $(SDL2TTF_LIBS) -ldl -lm \
|
||||
'-Wl,-rpath,$$ORIGIN'
|
||||
|
||||
editor: static graphics $(BUILD)/libtinyblocks-editor.a $(BUILD)/editor ## Compila o editor visual (estilo Scratch)
|
||||
editor: static graphics $(BUILD)/libtinyblocks-editor.a $(CAT_A) $(BUILD)/editor ## Compila o editor visual (estilo Scratch)
|
||||
@echo "Build editor OK: $(BUILD)/editor"
|
||||
|
||||
run-editor: editor ## Compila e abre o editor visual
|
||||
./$(BUILD)/editor
|
||||
|
||||
# ── Exemplo de library externa (hot reload) ──────────────────────────────
|
||||
# -- Exemplo de library externa (hot reload) ---
|
||||
$(BUILD)/mylib.so: examples/mylib/mylib.c $(LIB_A)
|
||||
$(CC) $(CFLAGS_REL) -Iinclude -fPIC -shared \
|
||||
$< -o $@ -L$(BUILD) -ltinyblocks '-Wl,-rpath,$$ORIGIN'
|
||||
|
||||
mylib: static $(BUILD)/mylib.so ## Library de exemplo (hot reload) → build/mylib.so
|
||||
@echo "Build mylib OK: $(BUILD)/mylib.so → Carregue no editor com Ctrl+O"
|
||||
mylib: static $(BUILD)/mylib.so ## Library de exemplo (hot reload) -> build/mylib.so
|
||||
@echo "Build mylib OK: $(BUILD)/mylib.so -> Carregue no editor com Ctrl+O"
|
||||
|
||||
# ── Library de vetor 2D ───────────────────────────────────────────────────
|
||||
# -- Library de vetor 2D ---
|
||||
$(BUILD)/vector.so: examples/vector/vector.c $(LIB_A)
|
||||
$(CC) $(CFLAGS_REL) -Iinclude -fPIC -shared \
|
||||
$< -o $@ -L$(BUILD) -ltinyblocks '-Wl,-rpath,$$ORIGIN'
|
||||
|
||||
vector: static $(BUILD)/vector.so ## Library de vetor 2D → build/vector.so
|
||||
@echo "Build vector OK: $(BUILD)/vector.so → Carregue no editor com Ctrl+O"
|
||||
vector: static $(BUILD)/vector.so ## Library de vetor 2D -> build/vector.so
|
||||
@echo "Build vector OK: $(BUILD)/vector.so -> Carregue no editor com Ctrl+O"
|
||||
|
||||
# ── Library de JSON ────────────────────────────────────────────────────────
|
||||
# -- Library de JSON ---
|
||||
$(BUILD)/json.so: examples/json/json.c $(LIB_A)
|
||||
$(CC) $(CFLAGS_REL) -Iinclude -fPIC -shared \
|
||||
$< -o $@ -L$(BUILD) -ltinyblocks '-Wl,-rpath,$$ORIGIN'
|
||||
|
||||
json: static $(BUILD)/json.so ## Library de JSON → build/json.so
|
||||
@echo "Build json OK: $(BUILD)/json.so → Carregue no editor com Ctrl+O"
|
||||
json: static $(BUILD)/json.so ## Library de JSON -> build/json.so
|
||||
@echo "Build json OK: $(BUILD)/json.so -> Carregue no editor com Ctrl+O"
|
||||
|
||||
# ── Library de aparência personalizada (on_render demo) ───────────────────
|
||||
# -- Library de aparência personalizada (on_render demo) ---
|
||||
$(BUILD)/customappearance.so: examples/customappearance/customappearance.c $(LIB_A)
|
||||
$(CC) $(CFLAGS_REL) -Iinclude -fPIC -shared \
|
||||
$< -o $@ -L$(BUILD) -ltinyblocks -lm '-Wl,-rpath,$$ORIGIN'
|
||||
|
||||
customappearance: static $(BUILD)/customappearance.so ## Library de demo do on_draw → build/customappearance.so
|
||||
customappearance: static $(BUILD)/customappearance.so ## Library de demo do on_draw -> build/customappearance.so
|
||||
@echo "Build customappearance OK: $(BUILD)/customappearance.so"
|
||||
|
||||
# ── Library Python (embedding CPython) ───────────────────────────────────
|
||||
# -- Library Python (embedding CPython) ---
|
||||
PYTHON_CFLAGS := $(shell python3-config --cflags --embed 2>/dev/null || python3-config --cflags)
|
||||
PYTHON_LIBS := $(shell python3-config --ldflags --embed 2>/dev/null || python3-config --ldflags)
|
||||
|
||||
@@ -193,7 +223,7 @@ $(BUILD)/tb_python_lib.o: libraries/tinyblocks-python/src/tb_python.c
|
||||
$(CC) $(CFLAGS_REL) $(PYTHON_CFLAGS) \
|
||||
-Ilibraries/tinyblocks-python/include -fPIC -c $< -o $@
|
||||
|
||||
# Shared lib — garante um único interpretador Python no processo
|
||||
# Shared lib - garante um único interpretador Python no processo
|
||||
$(BUILD)/libtinyblocks-python.so: $(BUILD)/tb_python_lib.o
|
||||
$(CC) -shared -Wl,-soname,libtinyblocks-python.so -o $@ $^ $(PYTHON_LIBS)
|
||||
|
||||
@@ -203,10 +233,10 @@ $(BUILD)/python.so: examples/python/python.c $(LIB_A) $(BUILD)/libtinyblocks-pyt
|
||||
$< -o $@ -L$(BUILD) -ltinyblocks -ltinyblocks-python \
|
||||
'-Wl,-rpath,$$ORIGIN'
|
||||
|
||||
python-lib: static $(BUILD)/libtinyblocks-python.so $(BUILD)/python.so ## Library Python (CPython embutido) → build/python.so
|
||||
python-lib: static $(BUILD)/libtinyblocks-python.so $(BUILD)/python.so ## Library Python (CPython embutido) -> build/python.so
|
||||
@echo "Build python OK: $(BUILD)/python.so"
|
||||
|
||||
# ── Library MCP (usa tinyblocks-python) ──────────────────────────────────
|
||||
# -- Library MCP (usa tinyblocks-python) ---
|
||||
# Gera header com o código Python embutido (unsigned char tb_mcp_py[])
|
||||
libraries/tinyblocks-mcp/src/tb_mcp_py.h: libraries/tinyblocks-mcp/src/tb_mcp.py
|
||||
xxd -i -n tb_mcp_py $< $@
|
||||
@@ -235,11 +265,11 @@ $(BUILD)/mcp.so: examples/mcp/mcp.c $(LIB_A) \
|
||||
$< -o $@ -L$(BUILD) -ltinyblocks -ltinyblocks-mcp -ltinyblocks-python \
|
||||
'-Wl,-rpath,$$ORIGIN'
|
||||
|
||||
mcp-lib: static $(BUILD)/libtinyblocks-python.so $(BUILD)/libtinyblocks-mcp.so $(BUILD)/mcp.so ## Library cliente MCP → build/mcp.so
|
||||
mcp-lib: static $(BUILD)/libtinyblocks-python.so $(BUILD)/libtinyblocks-mcp.so $(BUILD)/mcp.so ## Library cliente MCP -> build/mcp.so
|
||||
@echo "Build mcp OK: $(BUILD)/mcp.so"
|
||||
|
||||
# ── Limpeza ───────────────────────────────────────────────────────────────
|
||||
# ── Testes (harness headless com ASan/UBSan) ────────────────────────────
|
||||
# -- Limpeza ---
|
||||
# -- Testes (harness headless com ASan/UBSan) ---
|
||||
$(BUILD)/editor_harness: tests/editor_harness.c $(SRCS) \
|
||||
$(EDITOR_SRC)/tb_editor.c $(EDITOR_SRC)/tb_stage.c \
|
||||
libraries/tinyblocks-graphics/src/tb_graphics.c
|
||||
@@ -247,9 +277,11 @@ $(BUILD)/editor_harness: tests/editor_harness.c $(SRCS) \
|
||||
$(CC) $(CFLAGS_DBG) \
|
||||
-Ilibraries/tinyblocks-editor/include -Ilibraries/tinyblocks-editor/src \
|
||||
-I$(GFX_INC) \
|
||||
$(patsubst %,-Ilibraries/tinyblocks-%/include,$(CAT_LIBS)) \
|
||||
$(SRCS) \
|
||||
$(EDITOR_SRC)/tb_editor.c $(EDITOR_SRC)/tb_stage.c \
|
||||
libraries/tinyblocks-graphics/src/tb_graphics.c \
|
||||
$(foreach l,$(CAT_LIBS),libraries/tinyblocks-$(l)/src/tb_$(l).c) \
|
||||
tests/editor_harness.c -o $@ -ldl -lm
|
||||
|
||||
test-editor: $(BUILD)/editor_harness ## Roda o harness headless do editor (ASan/UBSan)
|
||||
|
||||
+22
-125
@@ -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");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* TinyBlocks — tb_library_block.h
|
||||
* TinyBlocks - tb_library_block.h
|
||||
* TinyBlocks_LibraryBlock: contêiner que agrupa um conjunto de blocos.
|
||||
*
|
||||
* Libraries são o mecanismo de modularidade da linguagem. Uma library contém
|
||||
@@ -15,6 +15,13 @@
|
||||
|
||||
#include "tb_block.h"
|
||||
|
||||
/* == Metadados opcionais de categoria (usados por UIs, ex.: o editor) === */
|
||||
typedef struct TinyBlocks_Category {
|
||||
const char* name; /* nome exibido, ex.: "Controle" */
|
||||
float color[4]; /* r,g,b,a (0..1) */
|
||||
float dark[4]; /* variante escura para contorno/sombra */
|
||||
} TinyBlocks_Category;
|
||||
|
||||
struct TinyBlocks_LibraryBlock {
|
||||
TinyBlocks_Block base; /* DEVE ser o primeiro campo */
|
||||
|
||||
@@ -27,9 +34,13 @@ struct TinyBlocks_LibraryBlock {
|
||||
void (*on_unload)(TinyBlocks_LibraryBlock* self, TinyBlocks* rt);
|
||||
|
||||
void* lib_userdata; /* dados privados da library */
|
||||
|
||||
/* Categoria opcional: quando definida, todos os TypeBlocks desta library
|
||||
* pertencem a ela (a UI, como o editor, agrupa a paleta por categoria). */
|
||||
const TinyBlocks_Category* category;
|
||||
};
|
||||
|
||||
/* ── Descritor exportado por cada library .so/.dll ───────────────────────── */
|
||||
/* == Descritor exportado por cada library .so/.dll === */
|
||||
typedef struct TinyBlocks_LibraryDescriptor {
|
||||
const char* name;
|
||||
const char* version;
|
||||
@@ -53,11 +64,15 @@ typedef struct TinyBlocks_LibraryDescriptor {
|
||||
return &_tb_lib_desc_##sym; \
|
||||
}
|
||||
|
||||
/* ── Operações em LibraryBlock ───────────────────────────────────────────── */
|
||||
/* == Operações em LibraryBlock === */
|
||||
TB_API TinyBlocks_LibraryBlock* tb_library_block_create(TinyBlocks* rt,
|
||||
const char* name);
|
||||
TB_API void tb_library_block_destroy(TinyBlocks_LibraryBlock* lib, TinyBlocks* rt);
|
||||
|
||||
/* Define a categoria de uma library (todos os seus TypeBlocks vão para ela) */
|
||||
TB_API void tb_library_set_category(TinyBlocks_LibraryBlock* lib,
|
||||
const TinyBlocks_Category* category);
|
||||
|
||||
/* Adiciona um bloco à library (a library não toma ownership do bloco) */
|
||||
TB_API bool tb_library_add(TinyBlocks_LibraryBlock* lib, TinyBlocks_Block* block);
|
||||
|
||||
@@ -68,7 +83,7 @@ TB_API bool tb_library_remove_at(TinyBlocks_LibraryBlock* lib, uint32_t index);
|
||||
TB_API TinyBlocks_Block* tb_library_find(TinyBlocks_LibraryBlock* lib,
|
||||
const char* name);
|
||||
|
||||
/* ── Carregamento dinâmico de libraries ──────────────────────────────────── */
|
||||
/* == Carregamento dinâmico de libraries === */
|
||||
/* Carrega uma library a partir de um .so/.dll; retorna NULL em erro */
|
||||
TB_API TinyBlocks_LibraryBlock* tb_library_load(TinyBlocks* rt, const char* path);
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
* TinyBlocks - tb_appearance.h
|
||||
* Library da categoria "Aparência": saída/escrita (IO.Print, IO.PrintInt).
|
||||
*/
|
||||
#ifndef TINYBLOCKS_APPEARANCE_H
|
||||
#define TINYBLOCKS_APPEARANCE_H
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
|
||||
TB_API const TinyBlocks_LibraryDescriptor* tb_appearance_get_library(void);
|
||||
|
||||
#endif /* TINYBLOCKS_APPEARANCE_H */
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* TinyBlocks - tb_appearance.c
|
||||
* Categoria "Aparência": blocos que produzem saída visível (console do editor).
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include "tb_appearance.h"
|
||||
|
||||
static void eval_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);
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_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);
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static bool appearance_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_Category cat = {
|
||||
"Aparência",
|
||||
{153/255.f, 102/255.f, 255/255.f, 1.f},
|
||||
{119/255.f, 77/255.f, 203/255.f, 1.f},
|
||||
};
|
||||
tb_library_set_category(lib, &cat);
|
||||
|
||||
static const TinyBlocks_SlotDesc print_in[] = {{"label", TB_VALUE_TEXT}, {"value", TB_VALUE_NONE}};
|
||||
static const TinyBlocks_SlotDesc one_int[] = {{"n", TB_VALUE_INTEGER}};
|
||||
|
||||
TinyBlocks_TypeBlock* t;
|
||||
t = tb_type_block_create(rt, "IO.Print", print_in, 2, NULL, 0, eval_print);
|
||||
if (!t || !tb_library_add(lib, &t->base)) return false;
|
||||
t = tb_type_block_create(rt, "IO.PrintInt", one_int, 1, NULL, 0, eval_print_int);
|
||||
if (!t || !tb_library_add(lib, &t->base)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void appearance_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
(void)rt; (void)lib;
|
||||
}
|
||||
|
||||
TINYBLOCKS_DEFINE_LIBRARY(appearance,
|
||||
"tinyblocks-appearance",
|
||||
"1.0.0",
|
||||
appearance_init,
|
||||
appearance_deinit)
|
||||
|
||||
const TinyBlocks_LibraryDescriptor* tb_appearance_get_library(void) {
|
||||
return &_tb_lib_desc_appearance;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
* TinyBlocks - tb_control.h
|
||||
* Library da categoria "Controle" (repita, se, se-senão, repita até que).
|
||||
*/
|
||||
#ifndef TINYBLOCKS_CONTROL_H
|
||||
#define TINYBLOCKS_CONTROL_H
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
|
||||
TB_API const TinyBlocks_LibraryDescriptor* tb_control_get_library(void);
|
||||
|
||||
#endif /* TINYBLOCKS_CONTROL_H */
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* TinyBlocks - tb_control.c
|
||||
* Categoria "Controle": repita / repita até que / se / se-senão.
|
||||
* A semântica (bocas e sequência) é interpretada pelo editor; aqui só
|
||||
* registramos os tipos e a categoria.
|
||||
*/
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include "tb_control.h"
|
||||
|
||||
static void eval_noop(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
|
||||
const TinyBlocks_SlotDesc* in, uint32_t ni) {
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, NULL, 0, eval_noop);
|
||||
return t && tb_library_add(lib, &t->base);
|
||||
}
|
||||
|
||||
static bool control_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_Category cat = {
|
||||
"Controle",
|
||||
{255/255.f, 171/255.f, 25/255.f, 1.f},
|
||||
{207/255.f, 139/255.f, 23/255.f, 1.f},
|
||||
};
|
||||
tb_library_set_category(lib, &cat);
|
||||
|
||||
static const TinyBlocks_SlotDesc times_in[] = {{"vezes", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc cond_in[] = {{"condição", TB_VALUE_INTEGER}};
|
||||
return reg(rt, lib, "Control.Repeat", times_in, 1)
|
||||
&& reg(rt, lib, "Control.RepeatUntil", cond_in, 1)
|
||||
&& reg(rt, lib, "Control.If", cond_in, 1)
|
||||
&& reg(rt, lib, "Control.IfElse", cond_in, 1);
|
||||
}
|
||||
|
||||
static void control_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
(void)rt; (void)lib;
|
||||
}
|
||||
|
||||
TINYBLOCKS_DEFINE_LIBRARY(control,
|
||||
"tinyblocks-control",
|
||||
"1.0.0",
|
||||
control_init,
|
||||
control_deinit)
|
||||
|
||||
const TinyBlocks_LibraryDescriptor* tb_control_get_library(void) {
|
||||
return &_tb_lib_desc_control;
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* TinyBlocks — tb_stage.c
|
||||
* TinyBlocks - tb_stage.c
|
||||
* O Palco do editor: uma tela lógica de 480x360 (origem no centro, y para
|
||||
* cima, como no Scratch) onde os blocos Stage.* desenham. É um componente de
|
||||
* UI: o editor o embute no painel direito (chamando seu on_draw) ou o entrega
|
||||
@@ -42,6 +42,7 @@ typedef struct {
|
||||
} Stage;
|
||||
|
||||
static TinyBlocks_Block* s_current = NULL;
|
||||
static TinyBlocks* s_rt = NULL; /* para mensagens de erro */
|
||||
|
||||
static Stage* stage_of(TinyBlocks_Block* b) { return b ? *(Stage**)b->userdata : NULL; }
|
||||
|
||||
@@ -52,9 +53,9 @@ void tb_stage_set_view(TinyBlocks_Block* stage, float w, float h) {
|
||||
if (s && w > 0 && h > 0) { s->view_w = w; s->view_h = h; }
|
||||
}
|
||||
|
||||
/* ══════════════════════════════════════════════════════════════════════════
|
||||
/* ===
|
||||
* Editor.Stage
|
||||
* ══════════════════════════════════════════════════════════════════════════ */
|
||||
* === */
|
||||
static bool stage_instance_init(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
Stage* s = (Stage*)calloc(1, sizeof(Stage));
|
||||
@@ -76,7 +77,7 @@ static void stage_instance_deinit(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
if (s_current == self) s_current = NULL;
|
||||
}
|
||||
|
||||
/* input 0 (opcional): Graphics.Input da janela que mostra o palco — dá o tamanho da vista */
|
||||
/* input 0 (opcional): Graphics.Input da janela que mostra o palco - dá o tamanho da vista */
|
||||
static void eval_stage(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
self->dirty = false;
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, 0);
|
||||
@@ -130,24 +131,16 @@ static bool stage_on_draw(TinyBlocks_Block* self, TinyBlocks* rt, TinyBlocks_Ren
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ══════════════════════════════════════════════════════════════════════════
|
||||
* Blocos Stage.* — desenham no palco atual
|
||||
* ══════════════════════════════════════════════════════════════════════════ */
|
||||
static float num(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||
if (v.kind == TB_VALUE_REAL) return (float)v.as.real;
|
||||
if (v.kind == TB_VALUE_INTEGER) return (float)v.as.integer;
|
||||
if (v.kind == TB_VALUE_TEXT && v.as.text) return (float)strtod(v.as.text, NULL);
|
||||
return 0.f;
|
||||
}
|
||||
|
||||
/* ===
|
||||
* API de desenho - usada pela library tinyblocks-pen (categoria "Caneta")
|
||||
* === */
|
||||
static float chan(float v) { return v < 0 ? 0.f : v > 255 ? 1.f : v / 255.f; }
|
||||
|
||||
static StageCmd* push(TinyBlocks* rt, StageCmdKind kind) {
|
||||
Stage* s = stage_of(s_current);
|
||||
if (!s) { tinyblocks_set_error(rt, "nenhum palco (Editor.Stage) criado"); return NULL; }
|
||||
if (s->n >= STAGE_MAX_CMDS) {
|
||||
if (!s->full_warned) tinyblocks_set_error(rt, "palco cheio (%d desenhos) — use \"apague tudo\"", STAGE_MAX_CMDS);
|
||||
if (!s->full_warned) tinyblocks_set_error(rt, "palco cheio (%d desenhos) = use \"apague tudo\"", STAGE_MAX_CMDS);
|
||||
s->full_warned = true;
|
||||
return NULL;
|
||||
}
|
||||
@@ -165,95 +158,58 @@ static StageCmd* push(TinyBlocks* rt, StageCmdKind kind) {
|
||||
return c;
|
||||
}
|
||||
|
||||
static void eval_clear(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
void tb_stage_clear(void) {
|
||||
Stage* s = stage_of(s_current);
|
||||
if (s) { s->n = 0; s->full_warned = false; }
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_background(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
void tb_stage_set_background(float r, float g, float b) {
|
||||
Stage* s = stage_of(s_current);
|
||||
if (s) { s->bg[0] = chan(num(self, rt, 0)); s->bg[1] = chan(num(self, rt, 1)); s->bg[2] = chan(num(self, rt, 2)); }
|
||||
self->dirty = false;
|
||||
if (s) { s->bg[0] = chan(r); s->bg[1] = chan(g); s->bg[2] = chan(b); }
|
||||
}
|
||||
|
||||
static void eval_pen_color(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
void tb_stage_set_pen(float r, float g, float b) {
|
||||
Stage* s = stage_of(s_current);
|
||||
if (s) { s->pen[0] = chan(num(self, rt, 0)); s->pen[1] = chan(num(self, rt, 1)); s->pen[2] = chan(num(self, rt, 2)); }
|
||||
self->dirty = false;
|
||||
if (s) { s->pen[0] = chan(r); s->pen[1] = chan(g); s->pen[2] = chan(b); }
|
||||
}
|
||||
|
||||
static void eval_pen_size(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
void tb_stage_set_pen_size(float size) {
|
||||
Stage* s = stage_of(s_current);
|
||||
float v = num(self, rt, 0);
|
||||
if (s) s->pen_size = v < 1.f ? 1.f : v;
|
||||
self->dirty = false;
|
||||
if (s) s->pen_size = size < 1.f ? 1.f : size;
|
||||
}
|
||||
|
||||
static void eval_line(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
StageCmd* c = push(rt, SC_LINE);
|
||||
if (c) { c->a = num(self, rt, 0); c->b = num(self, rt, 1); c->c = num(self, rt, 2); c->d = num(self, rt, 3); }
|
||||
self->dirty = false;
|
||||
void tb_stage_line(float x1, float y1, float x2, float y2) {
|
||||
StageCmd* c = push(s_rt, SC_LINE);
|
||||
if (c) { c->a = x1; c->b = y1; c->c = x2; c->d = y2; }
|
||||
}
|
||||
|
||||
static void eval_rect(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
StageCmd* c = push(rt, SC_RECT);
|
||||
if (c) { c->a = num(self, rt, 0); c->b = num(self, rt, 1); c->c = num(self, rt, 2); c->d = num(self, rt, 3); }
|
||||
self->dirty = false;
|
||||
void tb_stage_rect(float x, float y, float w, float h) {
|
||||
StageCmd* c = push(s_rt, SC_RECT);
|
||||
if (c) { c->a = x; c->b = y; c->c = w; c->d = h; }
|
||||
}
|
||||
|
||||
static void eval_circle(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
StageCmd* c = push(rt, SC_CIRCLE);
|
||||
if (c) { c->a = num(self, rt, 0); c->b = num(self, rt, 1); c->c = num(self, rt, 2); }
|
||||
self->dirty = false;
|
||||
void tb_stage_circle(float x, float y, float radius) {
|
||||
StageCmd* c = push(s_rt, SC_CIRCLE);
|
||||
if (c) { c->a = x; c->b = y; c->c = radius; }
|
||||
}
|
||||
|
||||
static void eval_text(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, 0);
|
||||
char buf[96] = "";
|
||||
if (v.kind == TB_VALUE_TEXT && v.as.text) snprintf(buf, sizeof(buf), "%s", v.as.text);
|
||||
else if (v.kind == TB_VALUE_INTEGER) snprintf(buf, sizeof(buf), "%lld", (long long)v.as.integer);
|
||||
else if (v.kind == TB_VALUE_REAL) snprintf(buf, sizeof(buf), "%g", v.as.real);
|
||||
StageCmd* c = push(rt, SC_TEXT);
|
||||
if (c) { memcpy(c->text, buf, sizeof(c->text)); c->a = num(self, rt, 1); c->b = num(self, rt, 2); }
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
/* ══════════════════════════════════════════════════════════════════════════
|
||||
* Registro
|
||||
* ══════════════════════════════════════════════════════════════════════════ */
|
||||
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
|
||||
const TinyBlocks_SlotDesc* in, uint32_t ni,
|
||||
void (*fn)(TinyBlocks_Block*, TinyBlocks*)) {
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, NULL, 0, fn);
|
||||
if (!t) return false;
|
||||
tb_library_add(lib, &t->base);
|
||||
return true;
|
||||
void tb_stage_text(const char* text, float x, float y) {
|
||||
StageCmd* c = push(s_rt, SC_TEXT);
|
||||
if (c) {
|
||||
snprintf(c->text, sizeof(c->text), "%s", text ? text : "");
|
||||
c->a = x; c->b = y;
|
||||
}
|
||||
}
|
||||
|
||||
/* ===
|
||||
* Registro do widget Editor.Stage
|
||||
* === */
|
||||
bool tb_stage_register(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
s_rt = rt;
|
||||
static const TinyBlocks_SlotDesc stage_in[] = {{"entrada", TB_VALUE_BLOCK}};
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, "Editor.Stage", stage_in, 1, NULL, 0, eval_stage);
|
||||
if (!t) return false;
|
||||
tb_type_block_set_instance_hooks(t, sizeof(Stage*), stage_instance_init, stage_instance_deinit);
|
||||
t->on_draw = stage_on_draw;
|
||||
tb_library_add(lib, &t->base);
|
||||
|
||||
static const TinyBlocks_SlotDesc rgb[] = {{"r", TB_VALUE_REAL}, {"g", TB_VALUE_REAL}, {"b", TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc size[] = {{"tamanho", TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc line[] = {{"x1", TB_VALUE_REAL}, {"y1", TB_VALUE_REAL},
|
||||
{"x2", TB_VALUE_REAL}, {"y2", TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc rect[] = {{"x", TB_VALUE_REAL}, {"y", TB_VALUE_REAL},
|
||||
{"largura", TB_VALUE_REAL}, {"altura", TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc circ[] = {{"x", TB_VALUE_REAL}, {"y", TB_VALUE_REAL}, {"raio", TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc text[] = {{"texto", TB_VALUE_TEXT}, {"x", TB_VALUE_REAL}, {"y", TB_VALUE_REAL}};
|
||||
return reg(rt, lib, "Stage.Clear", NULL, 0, eval_clear)
|
||||
&& reg(rt, lib, "Stage.Background", rgb, 3, eval_background)
|
||||
&& reg(rt, lib, "Stage.PenColor", rgb, 3, eval_pen_color)
|
||||
&& reg(rt, lib, "Stage.PenSize", size, 1, eval_pen_size)
|
||||
&& reg(rt, lib, "Stage.Line", line, 4, eval_line)
|
||||
&& reg(rt, lib, "Stage.Rect", rect, 4, eval_rect)
|
||||
&& reg(rt, lib, "Stage.Circle", circ, 3, eval_circle)
|
||||
&& reg(rt, lib, "Stage.Text", text, 3, eval_text);
|
||||
return tb_library_add(lib, &t->base);
|
||||
}
|
||||
@@ -1,19 +1,29 @@
|
||||
/*
|
||||
* TinyBlocks — tb_stage.h
|
||||
* Palco do editor (Editor.Stage) e blocos de desenho Stage.*.
|
||||
* TinyBlocks - tb_stage.h
|
||||
* Palco do editor (Editor.Stage) e API de desenho usada pela library Caneta.
|
||||
*/
|
||||
#ifndef TINYBLOCKS_EDITOR_STAGE_H
|
||||
#define TINYBLOCKS_EDITOR_STAGE_H
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
|
||||
/* Registra Editor.Stage e os blocos Stage.* na library do editor */
|
||||
/* Registra apenas o widget Editor.Stage na library do editor */
|
||||
bool tb_stage_register(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib);
|
||||
|
||||
/* Tamanho da área onde o palco será desenhado (on_draw usa 0..w × 0..h) */
|
||||
/* Tamanho da área onde o palco será desenhado (on_draw usa 0..w - 0..h) */
|
||||
void tb_stage_set_view(TinyBlocks_Block* stage, float w, float h);
|
||||
|
||||
/* Palco onde os blocos Stage.* desenham (o último criado) */
|
||||
/* Palco onde o desenho acontece (o último Editor.Stage criado) */
|
||||
TinyBlocks_Block* tb_stage_current(void);
|
||||
|
||||
/* == API de desenho (usada pela library tinyblocks-pen) === */
|
||||
void tb_stage_clear(void);
|
||||
void tb_stage_set_background(float r, float g, float b); /* canais 0..255 */
|
||||
void tb_stage_set_pen(float r, float g, float b); /* canais 0..255 */
|
||||
void tb_stage_set_pen_size(float size);
|
||||
void tb_stage_line(float x1, float y1, float x2, float y2);
|
||||
void tb_stage_rect(float x, float y, float w, float h);
|
||||
void tb_stage_circle(float x, float y, float radius);
|
||||
void tb_stage_text(const char* text, float x, float y);
|
||||
|
||||
#endif /* TINYBLOCKS_EDITOR_STAGE_H */
|
||||
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
* TinyBlocks - tb_events.h
|
||||
* Library da categoria "Eventos" (blocos que iniciam scripts).
|
||||
*/
|
||||
#ifndef TINYBLOCKS_EVENTS_H
|
||||
#define TINYBLOCKS_EVENTS_H
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
|
||||
TB_API const TinyBlocks_LibraryDescriptor* tb_events_get_library(void);
|
||||
|
||||
#endif /* TINYBLOCKS_EVENTS_H */
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* TinyBlocks - tb_events.c
|
||||
* Categoria "Eventos": blocos que iniciam scripts. A execução é conduzida
|
||||
* pelo editor (run_chain); aqui só registramos os tipos e a categoria.
|
||||
*/
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include "tb_events.h"
|
||||
|
||||
static void eval_noop(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static bool events_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_Category cat = {
|
||||
"Eventos",
|
||||
{255/255.f, 191/255.f, 0/255.f, 1.f},
|
||||
{204/255.f, 153/255.f, 0/255.f, 1.f},
|
||||
};
|
||||
tb_library_set_category(lib, &cat);
|
||||
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, "Events.WhenRun",
|
||||
NULL, 0, NULL, 0, eval_noop);
|
||||
if (!t) return false;
|
||||
return tb_library_add(lib, &t->base);
|
||||
}
|
||||
|
||||
static void events_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
(void)rt; (void)lib;
|
||||
}
|
||||
|
||||
TINYBLOCKS_DEFINE_LIBRARY(events,
|
||||
"tinyblocks-events",
|
||||
"1.0.0",
|
||||
events_init,
|
||||
events_deinit)
|
||||
|
||||
const TinyBlocks_LibraryDescriptor* tb_events_get_library(void) {
|
||||
return &_tb_lib_desc_events;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
/*
|
||||
* TinyBlocks - tb_operators.h
|
||||
* Library da categoria "Operadores": lógica, comparação, matemática, texto
|
||||
* e literais.
|
||||
*/
|
||||
#ifndef TINYBLOCKS_OPERATORS_H
|
||||
#define TINYBLOCKS_OPERATORS_H
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
|
||||
TB_API const TinyBlocks_LibraryDescriptor* tb_operators_get_library(void);
|
||||
|
||||
#endif /* TINYBLOCKS_OPERATORS_H */
|
||||
@@ -0,0 +1,237 @@
|
||||
/*
|
||||
* TinyBlocks - tb_operators.c
|
||||
* Categoria "Operadores": comparações, lógica booleana, número aleatório,
|
||||
* aritmética, texto e literais.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include "tb_operators.h"
|
||||
|
||||
/* == Conversões === */
|
||||
static bool value_num(const TinyBlocks_Value* v, double* d) {
|
||||
if (v->kind == TB_VALUE_INTEGER) { *d = (double)v->as.integer; return true; }
|
||||
if (v->kind == TB_VALUE_REAL) { *d = v->as.real; return true; }
|
||||
if (v->kind == TB_VALUE_TEXT && v->as.text && v->as.text[0]) {
|
||||
char* e; *d = strtod(v->as.text, &e);
|
||||
return *e == 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool value_truthy(const TinyBlocks_Value* v) {
|
||||
switch (v->kind) {
|
||||
case TB_VALUE_INTEGER: return v->as.integer != 0;
|
||||
case TB_VALUE_REAL: return v->as.real != 0.0;
|
||||
case TB_VALUE_TEXT: {
|
||||
const char* t = v->as.text ? v->as.text : "";
|
||||
if (!t[0] || strcmp(t, "0") == 0) return false;
|
||||
const char* f = "false";
|
||||
for (; *t && *f; t++, f++)
|
||||
if ((*t | 32) != *f) return true;
|
||||
return *t || *f;
|
||||
}
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
|
||||
static void value_to_str(const TinyBlocks_Value* v, char* buf, size_t cap) {
|
||||
switch (v->kind) {
|
||||
case TB_VALUE_INTEGER: snprintf(buf, cap, "%lld", (long long)v->as.integer); break;
|
||||
case TB_VALUE_REAL: snprintf(buf, cap, "%g", v->as.real); break;
|
||||
case TB_VALUE_TEXT: snprintf(buf, cap, "%s", v->as.text ? v->as.text : ""); break;
|
||||
default: buf[0] = 0; break;
|
||||
}
|
||||
}
|
||||
|
||||
static int str_icmp(const char* a, const char* b) {
|
||||
for (;; a++, b++) {
|
||||
int ca = (*a | 32), cb = (*b | 32);
|
||||
if (ca != cb || !ca) return (ca > cb) - (ca < cb);
|
||||
}
|
||||
}
|
||||
|
||||
static int cmp_inputs(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);
|
||||
double x, y;
|
||||
if (value_num(&a, &x) && value_num(&b, &y)) return (x > y) - (x < y);
|
||||
char sa[256], sb[256];
|
||||
value_to_str(&a, sa, sizeof(sa));
|
||||
value_to_str(&b, sb, sizeof(sb));
|
||||
return str_icmp(sa, sb);
|
||||
}
|
||||
|
||||
/* == Lógica e comparação === */
|
||||
static void eval_op_lt(TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{ tb_block_set_output(self, 0, tb_value_integer(cmp_inputs(self, rt) < 0)); self->dirty = false; }
|
||||
static void eval_op_eq(TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{ tb_block_set_output(self, 0, tb_value_integer(cmp_inputs(self, rt) == 0)); self->dirty = false; }
|
||||
static void eval_op_gt(TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{ tb_block_set_output(self, 0, tb_value_integer(cmp_inputs(self, rt) > 0)); self->dirty = false; }
|
||||
|
||||
static void eval_op_and(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);
|
||||
tb_block_set_output(self, 0, tb_value_integer(value_truthy(&a) && value_truthy(&b)));
|
||||
self->dirty = false;
|
||||
}
|
||||
static void eval_op_or(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);
|
||||
tb_block_set_output(self, 0, tb_value_integer(value_truthy(&a) || value_truthy(&b)));
|
||||
self->dirty = false;
|
||||
}
|
||||
static void eval_op_not(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Value a = tb_block_get_input(self, rt, 0);
|
||||
tb_block_set_output(self, 0, tb_value_integer(!value_truthy(&a)));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_op_random(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);
|
||||
double lo = 0, hi = 0;
|
||||
value_num(&a, &lo); value_num(&b, &hi);
|
||||
if (lo > hi) { double t = lo; lo = hi; hi = t; }
|
||||
if (lo == floor(lo) && hi == floor(hi)) {
|
||||
long long span = (long long)(hi - lo) + 1;
|
||||
tb_block_set_output(self, 0, tb_value_integer((long long)lo + (span > 0 ? rand() % span : 0)));
|
||||
} else {
|
||||
tb_block_set_output(self, 0, tb_value_real(lo + (hi - lo) * rand() / (double)RAND_MAX));
|
||||
}
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
/* == Aritmética (inteiros) === */
|
||||
static int64_t ai(TinyBlocks_Block* s, TinyBlocks* rt, uint32_t i) {
|
||||
TinyBlocks_Value v = tb_block_get_input(s, rt, i);
|
||||
return v.kind == TB_VALUE_INTEGER ? v.as.integer : 0;
|
||||
}
|
||||
static void eval_add(TinyBlocks_Block* s, TinyBlocks* rt)
|
||||
{ tb_block_set_output(s, 0, tb_value_integer(ai(s,rt,0) + ai(s,rt,1))); }
|
||||
static void eval_sub(TinyBlocks_Block* s, TinyBlocks* rt)
|
||||
{ tb_block_set_output(s, 0, tb_value_integer(ai(s,rt,0) - ai(s,rt,1))); }
|
||||
static void eval_mul(TinyBlocks_Block* s, TinyBlocks* rt)
|
||||
{ tb_block_set_output(s, 0, tb_value_integer(ai(s,rt,0) * ai(s,rt,1))); }
|
||||
static void eval_div(TinyBlocks_Block* s, TinyBlocks* rt)
|
||||
{ int64_t b = ai(s,rt,1); tb_block_set_output(s, 0, tb_value_integer(b ? ai(s,rt,0)/b : 0)); }
|
||||
static void eval_mod(TinyBlocks_Block* s, TinyBlocks* rt)
|
||||
{ int64_t b = ai(s,rt,1); tb_block_set_output(s, 0, tb_value_integer(b ? ai(s,rt,0)%b : 0)); }
|
||||
static void eval_abs(TinyBlocks_Block* s, TinyBlocks* rt)
|
||||
{ int64_t a = ai(s,rt,0); tb_block_set_output(s, 0, tb_value_integer(a < 0 ? -a : a)); }
|
||||
static void eval_max(TinyBlocks_Block* s, TinyBlocks* rt)
|
||||
{ int64_t a = ai(s,rt,0), b = ai(s,rt,1); tb_block_set_output(s, 0, tb_value_integer(a > b ? a : b)); }
|
||||
static void eval_min(TinyBlocks_Block* s, TinyBlocks* rt)
|
||||
{ int64_t a = ai(s,rt,0), b = ai(s,rt,1); tb_block_set_output(s, 0, tb_value_integer(a < b ? a : b)); }
|
||||
|
||||
/* == Texto === */
|
||||
static void eval_concat(TinyBlocks_Block* s, TinyBlocks* rt) {
|
||||
TinyBlocks_Value a = tb_block_get_input(s, rt, 0);
|
||||
TinyBlocks_Value b = tb_block_get_input(s, rt, 1);
|
||||
const char* sa = a.kind == TB_VALUE_TEXT ? a.as.text : "";
|
||||
const char* sb = b.kind == TB_VALUE_TEXT ? b.as.text : "";
|
||||
if (!sa) sa = "";
|
||||
if (!sb) sb = "";
|
||||
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(s, 0, v); /* set_output toma ownership */
|
||||
}
|
||||
static void eval_len(TinyBlocks_Block* s, TinyBlocks* rt) {
|
||||
TinyBlocks_Value a = tb_block_get_input(s, rt, 0);
|
||||
int64_t n = (a.kind == TB_VALUE_TEXT && a.as.text) ? (int64_t)strlen(a.as.text) : 0;
|
||||
tb_block_set_output(s, 0, tb_value_integer(n));
|
||||
}
|
||||
|
||||
/* == Literais === */
|
||||
static void eval_lit_int(TinyBlocks_Block* s, TinyBlocks* rt)
|
||||
{ tb_block_set_output(s, 0, tb_value_clone(tb_block_get_input(s, rt, 0))); }
|
||||
static void eval_lit_real(TinyBlocks_Block* s, TinyBlocks* rt)
|
||||
{ tb_block_set_output(s, 0, tb_value_clone(tb_block_get_input(s, rt, 0))); }
|
||||
static void eval_lit_text(TinyBlocks_Block* s, TinyBlocks* rt) {
|
||||
TinyBlocks_Value v = tb_block_get_input(s, rt, 0);
|
||||
const char* t = (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : "";
|
||||
tb_block_set_output(s, 0, tb_value_text(t));
|
||||
}
|
||||
|
||||
/* == Registro === */
|
||||
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
|
||||
const TinyBlocks_SlotDesc* in, uint32_t ni,
|
||||
const TinyBlocks_SlotDesc* out, uint32_t no,
|
||||
void (*fn)(TinyBlocks_Block*, TinyBlocks*)) {
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, out, no, fn);
|
||||
return t && tb_library_add(lib, &t->base);
|
||||
}
|
||||
|
||||
static bool operators_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
srand((unsigned)time(NULL));
|
||||
static const TinyBlocks_Category cat = {
|
||||
"Operadores",
|
||||
{ 89/255.f, 192/255.f, 89/255.f, 1.f},
|
||||
{ 56/255.f, 148/255.f, 56/255.f, 1.f},
|
||||
};
|
||||
tb_library_set_category(lib, &cat);
|
||||
|
||||
static const TinyBlocks_SlotDesc any2[] = {{"a", TB_VALUE_NONE}, {"b", TB_VALUE_NONE}};
|
||||
static const TinyBlocks_SlotDesc bool2[] = {{"a", TB_VALUE_INTEGER}, {"b", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc bool1[] = {{"a", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc bool_out[] = {{"resultado", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc rnd_in[] = {{"de", TB_VALUE_NONE}, {"até", TB_VALUE_NONE}};
|
||||
static const TinyBlocks_SlotDesc any_out[] = {{"valor", TB_VALUE_NONE}};
|
||||
|
||||
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 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}};
|
||||
|
||||
static const TinyBlocks_SlotDesc lit_int_in[] = {{"value", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc lit_int_out[] = {{"out", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc lit_real_in[] = {{"value", TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc lit_real_out[]= {{"out", TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc lit_txt_in[] = {{"value", TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc lit_txt_out[] = {{"out", TB_VALUE_TEXT}};
|
||||
|
||||
return reg(rt, lib, "Operators.Lt", any2, 2, bool_out, 1, eval_op_lt)
|
||||
&& reg(rt, lib, "Operators.Eq", any2, 2, bool_out, 1, eval_op_eq)
|
||||
&& reg(rt, lib, "Operators.Gt", any2, 2, bool_out, 1, eval_op_gt)
|
||||
&& reg(rt, lib, "Operators.And", bool2, 2, bool_out, 1, eval_op_and)
|
||||
&& reg(rt, lib, "Operators.Or", bool2, 2, bool_out, 1, eval_op_or)
|
||||
&& reg(rt, lib, "Operators.Not", bool1, 1, bool_out, 1, eval_op_not)
|
||||
&& reg(rt, lib, "Operators.Random", rnd_in, 2, any_out, 1, eval_op_random)
|
||||
&& reg(rt, lib, "Math.Add", two_int, 2, int_out, 1, eval_add)
|
||||
&& reg(rt, lib, "Math.Sub", two_int, 2, int_out, 1, eval_sub)
|
||||
&& reg(rt, lib, "Math.Mul", two_int, 2, int_out, 1, eval_mul)
|
||||
&& reg(rt, lib, "Math.Div", two_int, 2, int_out, 1, eval_div)
|
||||
&& reg(rt, lib, "Math.Mod", two_int, 2, int_out, 1, eval_mod)
|
||||
&& reg(rt, lib, "Math.Abs", one_int, 1, int_out, 1, eval_abs)
|
||||
&& reg(rt, lib, "Math.Max", two_int, 2, int_out, 1, eval_max)
|
||||
&& reg(rt, lib, "Math.Min", two_int, 2, int_out, 1, eval_min)
|
||||
&& reg(rt, lib, "Text.Concat", two_text, 2, text_out, 1, eval_concat)
|
||||
&& reg(rt, lib, "Text.Length", text_in, 1, int_out, 1, eval_len)
|
||||
&& reg(rt, lib, "Literal.Integer", lit_int_in, 1, lit_int_out, 1, eval_lit_int)
|
||||
&& reg(rt, lib, "Literal.Real", lit_real_in, 1, lit_real_out, 1, eval_lit_real)
|
||||
&& reg(rt, lib, "Literal.Text", lit_txt_in, 1, lit_txt_out, 1, eval_lit_text);
|
||||
}
|
||||
|
||||
static void operators_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
(void)rt; (void)lib;
|
||||
}
|
||||
|
||||
TINYBLOCKS_DEFINE_LIBRARY(operators,
|
||||
"tinyblocks-operators",
|
||||
"1.0.0",
|
||||
operators_init,
|
||||
operators_deinit)
|
||||
|
||||
const TinyBlocks_LibraryDescriptor* tb_operators_get_library(void) {
|
||||
return &_tb_lib_desc_operators;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
* TinyBlocks - tb_pen.h
|
||||
* Library da categoria "Caneta": desenha no palco (Editor.Stage).
|
||||
*/
|
||||
#ifndef TINYBLOCKS_PEN_H
|
||||
#define TINYBLOCKS_PEN_H
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
|
||||
TB_API const TinyBlocks_LibraryDescriptor* tb_pen_get_library(void);
|
||||
|
||||
#endif /* TINYBLOCKS_PEN_H */
|
||||
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* TinyBlocks - tb_pen.c
|
||||
* Categoria "Caneta": apague / mude fundo / cor / espessura / desenhe linhas,
|
||||
* retângulos, círculos e texto. Delega o desenho à API do palco do editor.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include "tb_stage.h"
|
||||
#include "tb_pen.h"
|
||||
|
||||
static float num(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||
if (v.kind == TB_VALUE_REAL) return (float)v.as.real;
|
||||
if (v.kind == TB_VALUE_INTEGER) return (float)v.as.integer;
|
||||
if (v.kind == TB_VALUE_TEXT && v.as.text) return (float)strtod(v.as.text, NULL);
|
||||
return 0.f;
|
||||
}
|
||||
|
||||
static void eval_clear(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt; tb_stage_clear(); self->dirty = false;
|
||||
}
|
||||
static void eval_background(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
tb_stage_set_background(num(self, rt, 0), num(self, rt, 1), num(self, rt, 2));
|
||||
self->dirty = false;
|
||||
}
|
||||
static void eval_pen_color(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
tb_stage_set_pen(num(self, rt, 0), num(self, rt, 1), num(self, rt, 2));
|
||||
self->dirty = false;
|
||||
}
|
||||
static void eval_pen_size(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
tb_stage_set_pen_size(num(self, rt, 0));
|
||||
self->dirty = false;
|
||||
}
|
||||
static void eval_line(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
tb_stage_line(num(self, rt, 0), num(self, rt, 1), num(self, rt, 2), num(self, rt, 3));
|
||||
self->dirty = false;
|
||||
}
|
||||
static void eval_rect(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
tb_stage_rect(num(self, rt, 0), num(self, rt, 1), num(self, rt, 2), num(self, rt, 3));
|
||||
self->dirty = false;
|
||||
}
|
||||
static void eval_circle(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
tb_stage_circle(num(self, rt, 0), num(self, rt, 1), num(self, rt, 2));
|
||||
self->dirty = false;
|
||||
}
|
||||
static void eval_text(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, 0);
|
||||
char buf[96] = "";
|
||||
if (v.kind == TB_VALUE_TEXT && v.as.text) snprintf(buf, sizeof(buf), "%s", v.as.text);
|
||||
else if (v.kind == TB_VALUE_INTEGER) snprintf(buf, sizeof(buf), "%lld", (long long)v.as.integer);
|
||||
else if (v.kind == TB_VALUE_REAL) snprintf(buf, sizeof(buf), "%g", v.as.real);
|
||||
tb_stage_text(buf, num(self, rt, 1), num(self, rt, 2));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
|
||||
const TinyBlocks_SlotDesc* in, uint32_t ni,
|
||||
void (*fn)(TinyBlocks_Block*, TinyBlocks*)) {
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, NULL, 0, fn);
|
||||
return t && tb_library_add(lib, &t->base);
|
||||
}
|
||||
|
||||
static bool pen_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_Category cat = {
|
||||
"Caneta",
|
||||
{ 15/255.f, 189/255.f, 140/255.f, 1.f},
|
||||
{ 11/255.f, 142/255.f, 105/255.f, 1.f},
|
||||
};
|
||||
tb_library_set_category(lib, &cat);
|
||||
|
||||
static const TinyBlocks_SlotDesc rgb[] = {{"r", TB_VALUE_REAL}, {"g", TB_VALUE_REAL}, {"b", TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc size[] = {{"tamanho", TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc line[] = {{"x1", TB_VALUE_REAL}, {"y1", TB_VALUE_REAL},
|
||||
{"x2", TB_VALUE_REAL}, {"y2", TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc rect[] = {{"x", TB_VALUE_REAL}, {"y", TB_VALUE_REAL},
|
||||
{"largura", TB_VALUE_REAL}, {"altura", TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc circ[] = {{"x", TB_VALUE_REAL}, {"y", TB_VALUE_REAL}, {"raio", TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc text[] = {{"texto", TB_VALUE_TEXT}, {"x", TB_VALUE_REAL}, {"y", TB_VALUE_REAL}};
|
||||
|
||||
return reg(rt, lib, "Stage.Clear", NULL, 0, eval_clear)
|
||||
&& reg(rt, lib, "Stage.Background", rgb, 3, eval_background)
|
||||
&& reg(rt, lib, "Stage.PenColor", rgb, 3, eval_pen_color)
|
||||
&& reg(rt, lib, "Stage.PenSize", size, 1, eval_pen_size)
|
||||
&& reg(rt, lib, "Stage.Line", line, 4, eval_line)
|
||||
&& reg(rt, lib, "Stage.Rect", rect, 4, eval_rect)
|
||||
&& reg(rt, lib, "Stage.Circle", circ, 3, eval_circle)
|
||||
&& reg(rt, lib, "Stage.Text", text, 3, eval_text);
|
||||
}
|
||||
|
||||
static void pen_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
(void)rt; (void)lib;
|
||||
}
|
||||
|
||||
TINYBLOCKS_DEFINE_LIBRARY(pen,
|
||||
"tinyblocks-pen",
|
||||
"1.0.0",
|
||||
pen_init,
|
||||
pen_deinit)
|
||||
|
||||
const TinyBlocks_LibraryDescriptor* tb_pen_get_library(void) {
|
||||
return &_tb_lib_desc_pen;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* TinyBlocks - tb_variables.h
|
||||
* Library da categoria "Variáveis". Ela é dona do armazenamento (nome=valor);
|
||||
* UIs, como o editor, consultam a lista para desenhar monitores.
|
||||
*/
|
||||
#ifndef TINYBLOCKS_VARIABLES_H
|
||||
#define TINYBLOCKS_VARIABLES_H
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
|
||||
TB_API const TinyBlocks_LibraryDescriptor* tb_variables_get_library(void);
|
||||
|
||||
/* Enumeração do armazenamento (para monitores etc.) */
|
||||
TB_API int tb_variables_count(void);
|
||||
TB_API const char* tb_variables_name(int i);
|
||||
TB_API const TinyBlocks_Value* tb_variables_value(int i); /* emprestado */
|
||||
|
||||
/* Zera variáveis que guardam blocos (chamado antes de uma nova execução) */
|
||||
TB_API void tb_variables_reset_blocks(void);
|
||||
|
||||
#endif /* TINYBLOCKS_VARIABLES_H */
|
||||
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* TinyBlocks - tb_variables.c
|
||||
* Categoria "Variáveis": mude / adicione / valor de <variável>.
|
||||
* A library é dona do armazenamento de variáveis e o expõe para monitores.
|
||||
*/
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include "tb_variables.h"
|
||||
|
||||
#define MAX_VARS 64
|
||||
|
||||
typedef struct { char name[32]; TinyBlocks_Value val; } Var;
|
||||
|
||||
static Var s_vars[MAX_VARS];
|
||||
static int s_n = 0;
|
||||
|
||||
static int var_find(const char* n, bool create) {
|
||||
for (int i = 0; i < s_n; i++)
|
||||
if (strcmp(s_vars[i].name, n) == 0) return i;
|
||||
if (!create || s_n >= MAX_VARS) return -1;
|
||||
int i = s_n++;
|
||||
strncpy(s_vars[i].name, n, 31); s_vars[i].name[31] = 0;
|
||||
s_vars[i].val = tb_value_integer(0);
|
||||
return i;
|
||||
}
|
||||
|
||||
static void input_name(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i,
|
||||
char* out, size_t cap) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||
/* value_to_str reduzido */
|
||||
switch (v.kind) {
|
||||
case TB_VALUE_INTEGER: snprintf(out, cap, "%lld", (long long)v.as.integer); break;
|
||||
case TB_VALUE_REAL: snprintf(out, cap, "%g", v.as.real); break;
|
||||
case TB_VALUE_TEXT: snprintf(out, cap, "%s", v.as.text ? v.as.text : ""); break;
|
||||
default: out[0] = 0; break;
|
||||
}
|
||||
}
|
||||
|
||||
static void eval_var_get(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char name[64]; input_name(self, rt, 0, name, sizeof(name));
|
||||
int i = var_find(name, false);
|
||||
tb_block_set_output(self, 0, i >= 0 ? tb_value_clone(s_vars[i].val)
|
||||
: tb_value_integer(0));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_var_set(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char name[64]; input_name(self, rt, 0, name, sizeof(name));
|
||||
int i = var_find(name, true);
|
||||
if (i >= 0) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, 1);
|
||||
tb_value_free(&s_vars[i].val);
|
||||
s_vars[i].val = tb_value_clone(v);
|
||||
}
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_var_change(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char name[64]; input_name(self, rt, 0, name, sizeof(name));
|
||||
int i = var_find(name, true);
|
||||
if (i >= 0) {
|
||||
TinyBlocks_Value by = tb_block_get_input(self, rt, 1);
|
||||
TinyBlocks_Value* cur = &s_vars[i].val;
|
||||
double a = 0, d = 0;
|
||||
if (cur->kind == TB_VALUE_INTEGER) a = (double)cur->as.integer;
|
||||
else if (cur->kind == TB_VALUE_REAL) a = cur->as.real;
|
||||
else if (cur->kind == TB_VALUE_TEXT && cur->as.text) a = strtod(cur->as.text, NULL);
|
||||
if (by.kind == TB_VALUE_INTEGER) d = (double)by.as.integer;
|
||||
else if (by.kind == TB_VALUE_REAL) d = by.as.real;
|
||||
else if (by.kind == TB_VALUE_TEXT && by.as.text) d = strtod(by.as.text, NULL);
|
||||
TinyBlocks_Value nv = (a == floor(a) && d == floor(d))
|
||||
? tb_value_integer((long long)llround(a + d))
|
||||
: tb_value_real(a + d);
|
||||
tb_value_free(cur);
|
||||
*cur = nv;
|
||||
}
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
/* == API de enumeração === */
|
||||
int tb_variables_count(void) { return s_n; }
|
||||
|
||||
const char* tb_variables_name(int i) {
|
||||
return (i >= 0 && i < s_n) ? s_vars[i].name : "";
|
||||
}
|
||||
|
||||
const TinyBlocks_Value* tb_variables_value(int i) {
|
||||
return (i >= 0 && i < s_n) ? &s_vars[i].val : NULL;
|
||||
}
|
||||
|
||||
void tb_variables_reset_blocks(void) {
|
||||
for (int v = 0; v < s_n; v++)
|
||||
if (s_vars[v].val.kind == TB_VALUE_BLOCK) {
|
||||
tb_value_free(&s_vars[v].val);
|
||||
s_vars[v].val = tb_value_integer(0);
|
||||
}
|
||||
}
|
||||
|
||||
/* == Registro === */
|
||||
static bool variables_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_Category cat = {
|
||||
"Variáveis",
|
||||
{255/255.f, 140/255.f, 26/255.f, 1.f},
|
||||
{219/255.f, 110/255.f, 0/255.f, 1.f},
|
||||
};
|
||||
tb_library_set_category(lib, &cat);
|
||||
|
||||
static const TinyBlocks_SlotDesc var_in[] = {{"variável", TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc var_set[] = {{"variável", TB_VALUE_TEXT}, {"valor", TB_VALUE_NONE}};
|
||||
static const TinyBlocks_SlotDesc any_out[] = {{"valor", TB_VALUE_NONE}};
|
||||
|
||||
TinyBlocks_TypeBlock* t;
|
||||
t = tb_type_block_create(rt, "Variables.Set", var_set, 2, NULL, 0, eval_var_set);
|
||||
if (!t || !tb_library_add(lib, &t->base)) return false;
|
||||
t = tb_type_block_create(rt, "Variables.Change", var_set, 2, NULL, 0, eval_var_change);
|
||||
if (!t || !tb_library_add(lib, &t->base)) return false;
|
||||
t = tb_type_block_create(rt, "Variables.Get", var_in, 1, any_out, 1, eval_var_get);
|
||||
if (!t || !tb_library_add(lib, &t->base)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void variables_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
(void)rt; (void)lib;
|
||||
for (int i = 0; i < s_n; i++) tb_value_free(&s_vars[i].val);
|
||||
s_n = 0;
|
||||
}
|
||||
|
||||
TINYBLOCKS_DEFINE_LIBRARY(variables,
|
||||
"tinyblocks-variables",
|
||||
"1.0.0",
|
||||
variables_init,
|
||||
variables_deinit)
|
||||
|
||||
const TinyBlocks_LibraryDescriptor* tb_variables_get_library(void) {
|
||||
return &_tb_lib_desc_variables;
|
||||
}
|
||||
+13
-5
@@ -7,7 +7,7 @@
|
||||
# include <dlfcn.h>
|
||||
#endif
|
||||
|
||||
/* ── Vtable de LibraryBlock ──────────────────────────────────────────────── */
|
||||
/* == Vtable de LibraryBlock === */
|
||||
static void lib_eval(TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{
|
||||
/* Libraries não avaliam diretamente; servem como namespaces */
|
||||
@@ -47,7 +47,7 @@ static const TinyBlocks_BlockVTable s_lib_vtable = {
|
||||
.clone = lib_clone,
|
||||
};
|
||||
|
||||
/* ── Criação ─────────────────────────────────────────────────────────────── */
|
||||
/* == Criação === */
|
||||
TinyBlocks_LibraryBlock* tb_library_block_create(TinyBlocks* rt, const char* name)
|
||||
{
|
||||
TinyBlocks_LibraryBlock* lib = (TinyBlocks_LibraryBlock*)tb_block_alloc_base(
|
||||
@@ -57,6 +57,7 @@ TinyBlocks_LibraryBlock* tb_library_block_create(TinyBlocks* rt, const char* nam
|
||||
|
||||
lib->base.name = tb_strdup(rt, name);
|
||||
lib->block_capacity = 16;
|
||||
lib->category = NULL;
|
||||
lib->blocks = tb_alloc(rt, lib->block_capacity * sizeof(TinyBlocks_Block*));
|
||||
if (!lib->blocks) {
|
||||
tb_block_destroy(&lib->base, rt);
|
||||
@@ -72,13 +73,13 @@ void tb_library_block_destroy(TinyBlocks_LibraryBlock* lib, TinyBlocks* rt)
|
||||
tb_block_destroy(&lib->base, rt);
|
||||
}
|
||||
|
||||
/* ── Gerenciamento de blocos contidos ────────────────────────────────────── */
|
||||
/* == Gerenciamento de blocos contidos === */
|
||||
bool tb_library_add(TinyBlocks_LibraryBlock* lib, TinyBlocks_Block* block)
|
||||
{
|
||||
if (!lib || !block) return false;
|
||||
if (lib->block_count >= lib->block_capacity) {
|
||||
uint32_t new_cap = lib->block_capacity * 2;
|
||||
/* A library pertence a um runtime — mas não temos rt aqui.
|
||||
/* A library pertence a um runtime - mas não temos rt aqui.
|
||||
Solução: guardar rt no lib_userdata NÃO é a API pública.
|
||||
Usamos realloc direto pois este array não usa o alocador customizado
|
||||
(trade-off documentado: arrays de library usam realloc). */
|
||||
@@ -93,6 +94,13 @@ bool tb_library_add(TinyBlocks_LibraryBlock* lib, TinyBlocks_Block* block)
|
||||
return true;
|
||||
}
|
||||
|
||||
/* == Categoria === */
|
||||
void tb_library_set_category(TinyBlocks_LibraryBlock* lib,
|
||||
const TinyBlocks_Category* category)
|
||||
{
|
||||
if (lib) lib->category = category;
|
||||
}
|
||||
|
||||
bool tb_library_remove_at(TinyBlocks_LibraryBlock* lib, uint32_t index)
|
||||
{
|
||||
if (!lib || index >= lib->block_count) return false;
|
||||
@@ -116,7 +124,7 @@ TinyBlocks_Block* tb_library_find(TinyBlocks_LibraryBlock* lib, const char* name
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* ── Carregamento dinâmico ───────────────────────────────────────────────── */
|
||||
/* == Carregamento dinâmico === */
|
||||
TinyBlocks_LibraryBlock* tb_library_load(TinyBlocks* rt, const char* path)
|
||||
{
|
||||
#ifdef __linux__
|
||||
|
||||
+15
-21
@@ -8,6 +8,12 @@
|
||||
#include <tinyblocks/tb_renderable_block.h>
|
||||
#include <tb_editor.h>
|
||||
#include <tb_graphics.h>
|
||||
#include <tb_events.h>
|
||||
#include <tb_control.h>
|
||||
#include <tb_operators.h>
|
||||
#include <tb_variables.h>
|
||||
#include <tb_appearance.h>
|
||||
#include <tb_pen.h>
|
||||
|
||||
static TinyBlocks* rt;
|
||||
static TinyBlocks_Block *ev, *scene;
|
||||
@@ -15,19 +21,6 @@ static TinyBlocks_DrawCmd cmds[TB_DRAW_CMD_CAP];
|
||||
static uint32_t ncmd;
|
||||
static int fails = 0;
|
||||
|
||||
static void eval_print(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Value l = tb_block_get_input(self, rt, 0), v = tb_block_get_input(self, rt, 1);
|
||||
const char* lbl = l.kind==TB_VALUE_TEXT ? l.as.text : "resultado";
|
||||
if (v.kind==TB_VALUE_INTEGER) printf("%s = %lld\n", lbl, (long long)v.as.integer);
|
||||
else if (v.kind==TB_VALUE_REAL) printf("%s = %g\n", lbl, v.as.real);
|
||||
else if (v.kind==TB_VALUE_TEXT) printf("%s = %s\n", lbl, v.as.text);
|
||||
else printf("%s\n", lbl);
|
||||
}
|
||||
static void eval_add(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Value a = tb_block_get_input(self, rt, 0), b = tb_block_get_input(self, rt, 1);
|
||||
long long x = a.kind==TB_VALUE_INTEGER ? a.as.integer : 0, y = b.kind==TB_VALUE_INTEGER ? b.as.integer : 0;
|
||||
tb_block_set_output(self, 0, tb_value_integer(x + y));
|
||||
}
|
||||
static void eval_ev(TinyBlocks_Block* self, TinyBlocks* rt) { (void)rt; self->dirty = false; }
|
||||
|
||||
static void draw(void) {
|
||||
@@ -122,12 +115,13 @@ static bool need(const char* s, int region, int nth, float* x, float* y) {
|
||||
|
||||
int main(void) {
|
||||
tinyblocks_init(&rt, NULL);
|
||||
static const TinyBlocks_SlotDesc print_in[] = {{"label", TB_VALUE_TEXT}, {"value", TB_VALUE_NONE}};
|
||||
static const TinyBlocks_SlotDesc two_int[] = {{"a", TB_VALUE_INTEGER}, {"b", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc int_out[] = {{"result", TB_VALUE_INTEGER}};
|
||||
tb_type_block_create(rt, "Math.Add", two_int, 2, int_out, 1, eval_add);
|
||||
tb_type_block_create(rt, "IO.Print", print_in, 2, NULL, 0, eval_print);
|
||||
tb_graphics_set_backend(tb_graphics_null_backend());
|
||||
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());
|
||||
tb_library_register(rt, tb_graphics_get_library());
|
||||
tb_library_register(rt, tb_editor_get_library());
|
||||
(void)eval_ev;
|
||||
@@ -154,8 +148,8 @@ int main(void) {
|
||||
|
||||
/* 3. Encaixar o "escreva Olá!" logo abaixo do chapéu */
|
||||
float hx, hy;
|
||||
if (need("quando ⚑ for clicado", R_WS, 0, &hx, &hy) && need("escreva", R_WS, 1, &x, &y)) {
|
||||
/* hat: rótulo em (bx+10, by+18+18); fundo em by+54 → alvo: topo do bloco em by+54 */
|
||||
if (need("quando a bandeira for clicado", R_WS, 0, &hx, &hy) && need("escreva", R_WS, 1, &x, &y)) {
|
||||
/* hat: rótulo em (bx+10, by+18+18); fundo em by+54 - alvo: topo do bloco em by+54 */
|
||||
float bx = hx - 3 - 10, by = hy - 18 - 18;
|
||||
float gx = x - 3 - 10, gy = y - 18; /* canto do bloco arrastado */
|
||||
drag(x, y, x + (bx - gx), y + (by + 54 - gy) + 6);
|
||||
@@ -169,7 +163,7 @@ int main(void) {
|
||||
}
|
||||
CHECK(oy1 > 0 && oy2 > oy1, "ordem de execução: Olá! antes de x vale");
|
||||
|
||||
/* 4. Repórter "+": solto, editar campos, clicar → balão */
|
||||
/* 4. Repórter "+": solto, editar campos, clicar - balão */
|
||||
if (need("Operadores", R_CAT, 0, &x, &y)) click(x, y);
|
||||
if (need("+", R_FLY, 0, &x, &y)) drag(x, y, 640, 470);
|
||||
if (need("+", R_WS, 0, &x, &y)) {
|
||||
|
||||
Reference in new issue
Block a user