- Stage.Draw aceita a saída de outro bloco: imagem (desenha centrada),
lista de pontos [[x,y],...] (traço com a caneta atual, deslocado por
x/y) ou texto (nome de uma imagem guardada).
- O balão de resultado mostra o tipo do valor ("lista de pontos",
"imagem", "texto", "número inteiro"...); blocos booleanos mostram
"verdadeiro"/"falso".
- Status longo no menu mostra o fim da mensagem e não cobre mais o
botão "Carregar library=".
- Harness: desenhe (reta) no palco e tipo no balão.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
200 lines
9.1 KiB
C
200 lines
9.1 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 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 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, num(self, rt, 1), num(self, rt, 2)))
|
|
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)num(self, rt, 0);
|
|
if (!tb_stage_image_number(n, num(self, rt, 1), num(self, rt, 2)))
|
|
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 = num(self, rt, 1), y = num(self, rt, 2);
|
|
if (v.kind == TB_VALUE_IMAGE && v.as.image) {
|
|
tb_stage_image(v.as.image, x, y);
|
|
} 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);
|
|
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);
|
|
|
|
/* 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 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}};
|
|
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, 3, 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;
|
|
}
|