Files
tinyblocks/libraries/tinyblocks-pen/src/tb_pen.c
T
mguinhosandClaude Opus 5.5 bfa17eecea Linha de comando: pasta é ignorada com aviso, projeto é aberto
Todo argumento ia para o dlopen como library: abrir o editor com uma pasta
(./TinyBlocks.AppDir) dava "dlopen: ... Is a directory". Agora .so é
library, pasta é ignorada com um aviso claro e qualquer outro arquivo é
aberto como projeto (.json, ou .txt em scratchblocks).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 15:55:37 -03:00

196 lines
9.7 KiB
C

/*
* TinyBlocks - tb_pen.c
* Categoria "Caneta": apague / mude fundo / cor / espessura / desenhe linhas,
* retângulos, círculos, texto e "desenhe [imagem/pontos]" com a saida de
* outros blocos. Delega o desenho à API do palco do editor.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <tinyblocks/tinyblocks.h>
#include <tinyblocks/tb_stage.h>
#include <tinyblocks/tb_image.h>
#include <tinyblocks/tb_json_block.h>
#include "tb_pen.h"
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((float)tb_block_input_number(self, rt, 0, 0), (float)tb_block_input_number(self, rt, 1, 0), (float)tb_block_input_number(self, rt, 2, 0));
self->dirty = false;
}
static void eval_pen_color(TinyBlocks_Block* self, TinyBlocks* rt) {
tb_stage_set_pen((float)tb_block_input_number(self, rt, 0, 0), (float)tb_block_input_number(self, rt, 1, 0), (float)tb_block_input_number(self, rt, 2, 0));
self->dirty = false;
}
static void eval_pen_size(TinyBlocks_Block* self, TinyBlocks* rt) {
tb_stage_set_pen_size((float)tb_block_input_number(self, rt, 0, 0));
self->dirty = false;
}
static void eval_line(TinyBlocks_Block* self, TinyBlocks* rt) {
tb_stage_line((float)tb_block_input_number(self, rt, 0, 0), (float)tb_block_input_number(self, rt, 1, 0), (float)tb_block_input_number(self, rt, 2, 0), (float)tb_block_input_number(self, rt, 3, 0));
self->dirty = false;
}
static void eval_rect(TinyBlocks_Block* self, TinyBlocks* rt) {
tb_stage_rect((float)tb_block_input_number(self, rt, 0, 0), (float)tb_block_input_number(self, rt, 1, 0), (float)tb_block_input_number(self, rt, 2, 0), (float)tb_block_input_number(self, rt, 3, 0));
self->dirty = false;
}
static void eval_circle(TinyBlocks_Block* self, TinyBlocks* rt) {
tb_stage_circle((float)tb_block_input_number(self, rt, 0, 0), (float)tb_block_input_number(self, rt, 1, 0), (float)tb_block_input_number(self, rt, 2, 0));
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, (float)tb_block_input_number(self, rt, 1, 0), (float)tb_block_input_number(self, rt, 2, 0));
self->dirty = false;
}
static void eval_image_name(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Value v = tb_block_get_input(self, rt, 0);
const char* nm = (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : "";
if (!tb_stage_image_named(nm, (float)tb_block_input_number(self, rt, 1, 0), (float)tb_block_input_number(self, rt, 2, 0)))
tinyblocks_set_error(rt, "imagem '%s' nao encontrada", nm);
self->dirty = false;
}
static void eval_image_number(TinyBlocks_Block* self, TinyBlocks* rt) {
int n = (int)(float)tb_block_input_number(self, rt, 0, 0);
if (!tb_stage_image_number(n, (float)tb_block_input_number(self, rt, 1, 0), (float)tb_block_input_number(self, rt, 2, 0)))
tinyblocks_set_error(rt, "imagem numero %d nao existe", n);
self->dirty = false;
}
/* Ponto [x, y] numero i da lista; false se o item nao for um ponto */
static bool point_at(const TinyBlocks_JsonValue* arr, uint32_t i, float* x, float* y) {
TinyBlocks_JsonValue* p = tb_json_array_get(arr, i);
bool ok = p && tb_json_kind(p) == TB_JSON_ARRAY && tb_json_array_length(p) >= 2;
if (ok) {
TinyBlocks_JsonValue* px = tb_json_array_get(p, 0);
TinyBlocks_JsonValue* py = tb_json_array_get(p, 1);
*x = (float)tb_json_as_number(px, 0.0);
*y = (float)tb_json_as_number(py, 0.0);
if (px) tb_json_release(px);
if (py) tb_json_release(py);
}
if (p) tb_json_release(p);
return ok;
}
/* "desenhe [valor] em x y": aceita a saida de outros blocos.
imagem -> desenha centrada em (x,y); lista de pontos [[x,y],...] ->
traco com a caneta atual, deslocado por (x,y); texto -> nome de uma
imagem guardada. */
static void eval_draw(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Value v = tb_block_get_input(self, rt, 0);
float x = (float)tb_block_input_number(self, rt, 1, 0), y = (float)tb_block_input_number(self, rt, 2, 0);
float zoom = (float)tb_block_input_number(self, rt, 3, 1);
if (v.kind == TB_VALUE_IMAGE && v.as.image) {
tb_stage_image_zoom(v.as.image, x, y, zoom);
} else if (v.kind == TB_VALUE_JSON && v.as.json && tb_json_kind(v.as.json) == TB_JSON_ARRAY) {
uint32_t n = tb_json_array_length(v.as.json);
float lx = 0, ly = 0;
bool have = false;
for (uint32_t i = 0; i < n; i++) {
float px, py;
if (!point_at(v.as.json, i, &px, &py)) {
tinyblocks_set_error(rt, "desenhe: a lista precisa ter pontos [x, y]");
break;
}
if (have) tb_stage_line(lx + x, ly + y, px + x, py + y);
else if (n == 1) tb_stage_circle(px + x, py + y, 1.f); /* ponto sozinho */
lx = px; ly = py; have = true;
}
} else if (v.kind == TB_VALUE_TEXT && v.as.text && v.as.text[0]) {
if (!tb_stage_image_named(v.as.text, x, y))
tinyblocks_set_error(rt, "imagem '%s' nao encontrada", v.as.text);
} else if (v.kind != TB_VALUE_NONE) {
tinyblocks_set_error(rt, "desenhe: encaixe uma imagem ou uma lista de pontos");
}
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);
if (!t) return false;
tb_type_block_set_flags(t, TB_TYPE_REDRAW); /* desenha no palco: mostra ja */
return 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);
/* Metadados dos blocos (tooltip do editor: Alt + hover). */
TB_BLOCK_META("Stage.Clear", "eval_clear",
"Limpa o palco");
TB_BLOCK_META("Stage.Background", "eval_background",
"Define a cor de fundo do palco");
TB_BLOCK_META("Stage.PenColor", "eval_pen_color",
"Define a cor da caneta");
TB_BLOCK_META("Stage.PenSize", "eval_pen_size",
"Define a espessura da caneta");
TB_BLOCK_META("Stage.Line", "eval_line",
"Desenha uma linha entre dois pontos");
TB_BLOCK_META("Stage.Rect", "eval_rect",
"Desenha um retangulo");
TB_BLOCK_META("Stage.Circle", "eval_circle",
"Desenha um circulo");
TB_BLOCK_META("Stage.Text", "eval_text",
"Escreve um texto no palco");
TB_BLOCK_META("Stage.Draw", "eval_draw",
"Desenha a imagem (com zoom opcional) ou a lista de pontos que vier de outro bloco");
TB_BLOCK_META("Stage.ImageByName", "eval_image_name",
"Desenha no palco a imagem com esse nome");
TB_BLOCK_META("Stage.ImageByNumber", "eval_image_number",
"Desenha no palco a imagem com esse numero");
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}};
static const TinyBlocks_SlotDesc img_name[] = {{"nome", TB_VALUE_TEXT}, {"x", TB_VALUE_REAL}, {"y", TB_VALUE_REAL}};
static const TinyBlocks_SlotDesc draw[] = {{"imagem", TB_VALUE_NONE}, {"x", TB_VALUE_REAL}, {"y", TB_VALUE_REAL}, {"escala", TB_VALUE_REAL}};
static const TinyBlocks_SlotDesc img_num[] = {{"numero", TB_VALUE_INTEGER}, {"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)
&& reg(rt, lib, "Stage.Draw", draw, 4, eval_draw)
&& reg(rt, lib, "Stage.ImageByName", img_name, 3, eval_image_name)
; (void)img_num;
}
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;
}