- Core: armazém de imagens nomeadas (tb_image_store_*) e tb_stage_image* para desenhar imagens no palco pelo nome ou pelo número. - Caneta: blocos "desenhe imagem nome/número". - tinyblocks-api: tb_api_load_named/tb_api_find_library e mtime em ns; o editor carrega as libraries de categoria como .so quando existem, para a paleta recarregar ao vivo. - Aba de imagem: slider de zoom no rodapé no lugar dos botões -/+. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
145 lines
6.5 KiB
C
145 lines
6.5 KiB
C
/*
|
|
* 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 <tinyblocks/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 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;
|
|
}
|
|
|
|
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.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 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.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;
|
|
}
|