Imagens no palco por nome/número, paleta recarregável e slider de zoom

- 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>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-21 11:03:01 -03:00
1 parent 2b6bc65062
commit adf5d2cd12
11 files changed
+378 -32

No files matched your search

+7 -1
View File
@@ -15,6 +15,7 @@
#define TINYBLOCKS_API_H
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
#include <tinyblocks/tinyblocks.h>
@@ -34,7 +35,7 @@ typedef struct TbApiModule {
TinyBlocks_LibraryBlock* lib;
const TinyBlocks_Category* category; /* pode ser NULL */
unsigned block_count;
time_t mtime; /* mtime do .so (hot reload) */
int64_t mtime; /* mtime do .so (ns; hot reload) */
} TbApiModule;
/* -- Ciclo de vida --- */
@@ -53,6 +54,11 @@ TB_API const TbApiModule* tb_api_module_find(const char* name);
/* -- Carregamento dinamico --- */
/* Carrega um .so; retorna o indice (>=0) ou -1 (erro em tb_api_last_error). */
TB_API int tb_api_load(const char* path);
/* Procura "lib<name>.so" nos diretorios usuais (repo, ao lado do executavel,
* AppImage, $TINYBLOCKS_LIBS) e carrega. Serve para o app abrir as libraries
* de categoria como .so (e entao poder recarrega-las ao vivo). */
TB_API bool tb_api_find_library(const char* name, char* out, size_t cap);
TB_API int tb_api_load_named(const char* name); /* indice ou -1 */
TB_API bool tb_api_unload(int idx);
TB_API bool tb_api_reload(int idx);
TB_API bool tb_api_is_loaded(const char* name);
+100 -12
View File
@@ -25,9 +25,14 @@ static int s_n = 0;
static char s_err[256];
/* -- Helpers --- */
static time_t file_mtime(const char* p) {
static int64_t file_mtime(const char* p) {
struct stat st;
return (stat(p, &st) == 0) ? st.st_mtime : 0;
if (stat(p, &st) != 0) return 0;
#if defined(__linux__)
return (int64_t)st.st_mtim.tv_sec * 1000000000LL + (int64_t)st.st_mtim.tv_nsec;
#else
return (int64_t)st.st_mtime * 1000000000LL;
#endif
}
static void set_err(const char* fmt, const char* a);
@@ -44,7 +49,7 @@ typedef struct {
char name[64];
char path[TB_API_PATH_LEN];
void* handle;
time_t mtime;
int64_t mtime;
bool dirty;
} TbApiCode;
@@ -68,6 +73,7 @@ typedef struct {
static TbWatch s_watch[TB_API_WATCH_MAX];
static int s_wn = 0;
static int s_inot = -1;
static bool s_lib_dirty = false; /* algum .so de library mudou */
/* Vigia o DIRETORIO do arquivo: cobre tanto escrita no lugar quanto
"make" (escreve num temp e renomeia por cima). */
@@ -75,13 +81,19 @@ static int watch_add(const char* path) {
if (s_inot < 0) return -1;
char dir[TB_API_PATH_LEN];
path_dirname(path, dir, sizeof(dir));
if (s_wn > 0 && strcmp(s_watch[s_wn - 1].dir, dir) == 0) {
/* mesmo diretorio: reaproveita o wd */
snprintf(s_watch[s_wn - 1].base, sizeof(s_watch[s_wn-1].base), "%s", path_basename(path));
return s_watch[s_wn - 1].wd;
}
const char* base = path_basename(path);
/* ja vigia esse arquivo? */
for (int i = 0; i < s_wn; i++)
if (strcmp(s_watch[i].dir, dir) == 0 && strcmp(s_watch[i].base, base) == 0) {
s_watch[i].idx = s_cn - 1; /* (re)aponta para o dono atual */
return s_watch[i].wd;
}
if (s_wn >= TB_API_WATCH_MAX) return -1;
int wd = inotify_add_watch(s_inot, dir,
int wd = -1;
for (int i = 0; i < s_wn; i++)
if (strcmp(s_watch[i].dir, dir) == 0) { wd = s_watch[i].wd; break; }
if (wd < 0)
wd = inotify_add_watch(s_inot, dir,
IN_CLOSE_WRITE | IN_MOVED_TO | IN_CREATE | IN_ATTRIB);
if (wd < 0) return -1;
TbWatch* w = &s_watch[s_wn++];
@@ -118,6 +130,18 @@ int tb_api_poll_watch(void) {
struct inotify_event* ev = (struct inotify_event*)p;
p += sizeof(*ev) + ev->len;
if (!ev->len) continue;
/* libraries: basta saber que o arquivo mudou */
for (int i = 0; i < s_n; i++) {
if (!s_mods[i].path[0]) continue;
if (strcmp(ev->name, path_basename(s_mods[i].path)) != 0) continue;
char d[TB_API_PATH_LEN];
path_dirname(s_mods[i].path, d, sizeof(d));
for (int k = 0; k < s_wn; k++)
if (s_watch[k].wd == ev->wd && strcmp(s_watch[k].dir, d) == 0) {
s_lib_dirty = true;
changed++;
}
}
for (int i = 0; i < s_cn; i++) {
if (strcmp(ev->name, path_basename(s_code[i].path)) != 0) continue;
char d[TB_API_PATH_LEN];
@@ -145,7 +169,7 @@ static void path_dirname(const char* p, char* out, size_t cap) {
static bool code_changed_mtime(void) {
bool any = false;
for (int i = 0; i < s_cn; i++) {
time_t mt = file_mtime(s_code[i].path);
int64_t mt = file_mtime(s_code[i].path);
if (mt && mt != s_code[i].mtime) { s_code[i].dirty = true; any = true; }
}
return any;
@@ -359,6 +383,9 @@ int tb_api_load(const char* path) {
m->category = tb_library_category(lib);
m->block_count = tb_library_block_count(lib);
m->mtime = file_mtime(path);
#ifdef __linux__
tb_api_watch(path);
#endif
s_err[0] = 0;
return s_n++;
#else
@@ -389,11 +416,72 @@ bool tb_api_reload(int idx) {
return tb_api_load(path) >= 0;
}
/* -- Descoberta de libraries no disco ---------------------------------- */
static bool try_path(const char* dir, const char* file, char* out, size_t cap) {
if (!dir || !dir[0]) return false;
int n = snprintf(out, cap, "%s/%s", dir, file);
if (n <= 0 || (size_t)n >= cap) return false;
return access(out, R_OK) == 0;
}
static bool exe_dir(char* out, size_t cap) {
char exe[TB_API_PATH_LEN];
ssize_t n = readlink("/proc/self/exe", exe, sizeof(exe) - 1);
if (n <= 0) return false;
exe[n] = 0;
path_dirname(exe, out, cap);
return out[0] != 0;
}
bool tb_api_find_library(const char* name, char* out, size_t cap) {
if (!name || !name[0] || !out || cap == 0) return false;
char file[128], rel[192], exe[TB_API_PATH_LEN];
snprintf(file, sizeof(file), "lib%s.so", name);
const char* env = getenv("TINYBLOCKS_LIBS");
bool have_exe = exe_dir(exe, sizeof(exe));
/* 1) arvore do repositorio (rodando de build/editor) */
snprintf(rel, sizeof(rel), "libraries/%s/build/%s", name, file);
if (try_path(".", rel, out, cap)) return true;
if (have_exe) {
/* 2) <exe>/../libraries/<lib>/build/<file> (repo) */
char buf[TB_API_PATH_LEN + 256];
snprintf(buf, sizeof(buf), "%s/../libraries/%s/build/%s", exe, name, file);
if (access(buf, R_OK) == 0) { snprintf(out, cap, "%s", buf); return true; }
/* 3) AppImage: <exe>/../lib/tinyblocks/<file> */
snprintf(buf, sizeof(buf), "%s/../lib/tinyblocks/%s", exe, file);
if (access(buf, R_OK) == 0) { snprintf(out, cap, "%s", buf); return true; }
/* 4) ao lado do executavel */
snprintf(buf, sizeof(buf), "%s/%s", exe, file);
if (access(buf, R_OK) == 0) { snprintf(out, cap, "%s", buf); return true; }
}
/* 5) $TINYBLOCKS_LIBS */
if (env && try_path(env, file, out, cap)) return true;
if (try_path("build", file, out, cap)) return true;
return false;
}
int tb_api_load_named(const char* name) {
char path[TB_API_PATH_LEN];
if (!tb_api_find_library(name, path, sizeof(path))) {
static char msg[160];
snprintf(msg, sizeof(msg), "'%s' nao encontrado (lib%s.so)", name, name);
set_err("%s", msg);
return -1;
}
return tb_api_load(path);
}
bool tb_api_any_changed(void) {
#ifdef __linux__
tb_api_poll_watch();
if (s_lib_dirty) { s_lib_dirty = false; return true; }
#endif
for (int i = 0; i < s_n; i++) {
TbApiModule* m = &s_mods[i];
if (!m->path[0]) continue;
time_t mt = file_mtime(m->path);
int64_t mt = file_mtime(m->path);
if (mt && mt != m->mtime) return true;
}
return false;
@@ -407,7 +495,7 @@ int tb_api_poll_reload(void) {
for (int i = 0; i < s_n; i++) {
TbApiModule* m = &s_mods[i];
if (!m->path[0]) continue;
time_t mt = file_mtime(m->path);
int64_t mt = file_mtime(m->path);
if (!mt || mt == m->mtime) continue;
char path[TB_API_PATH_LEN];
snprintf(path, sizeof(path), "%s", m->path);
@@ -298,6 +298,8 @@ static const BlockSpec s_specs[] = {
{"Stage.Line", "desenhe linha de x %1 y %2 até x %3 y %4", "-100|0|100|0"},
{"Stage.Rect", "desenhe retângulo em x %1 y %2 largura %3 altura %4", "0|0|120|80"},
{"Stage.Circle", "desenhe círculo em x %1 y %2 raio %3", "0|0|50"},
{"Stage.ImageByName", "desenhe imagem nome %1 em x %2 y %3", "|0|0"},
{"Stage.ImageByNumber", "desenhe imagem número %1 em x %2 y %3", "1|0|0"},
{"Stage.Text", "escreva %1 no palco em x %2 y %3", "Olá!|-40|0"},
/* Curvas (lista de pontos [x,y]) */
{"Curves.Line", "reta de x %1 y %2 até x %3 y %4 com %5 passos", "0|0|100|0|20"},
@@ -165,6 +165,42 @@ static float img_pal_y(Ed* ed) {
(void)cx;
return cy + ro + 24;
}
/* Slider de zoom do rodape: trilha + botao redondo (mostra a porcentagem).
E um widget novo do modulo: nasceu por hot reload, sem mudar o struct Tab
(usa o img_zoom que ja existia). */
#define IMG_SLIDER_H 6
static void img_zoom_slider(Ed* ed, float* x, float* y, float* w, float* h) {
float ax, ay, aw, ah;
img_area(ed, &ax, &ay, &aw, &ah);
*x = ax + 176;
*w = aw - 176 - 14;
*h = IMG_SLIDER_H;
*y = img_foot_y(ed) + 22.f;
(void)h; (void)ah;
*h = IMG_SLIDER_H;
}
/* posicao (0..1) do botao na trilha, em escala logaritmica 10%..800% */
static float img_zoom_t(float zoom) {
if (zoom < 0.1f) zoom = 0.1f;
if (zoom > 8.f) zoom = 8.f;
return logf(zoom / 0.1f) / logf(80.f);
}
static float img_zoom_of_t(float t) {
if (t < 0.f) t = 0.f;
if (t > 1.f) t = 1.f;
return 0.1f * powf(80.f, t);
}
/* aplica zoom "de verdade" (sem ancorar no ponteiro) */
static void img_zoom_set(Ed* ed, float nz) {
Tab* T = &CT;
if (nz < 0.1f) nz = 0.1f;
if (nz > 8.f) nz = 8.f;
T->img_zoom = nz;
if (nz <= 1.f) { T->img_pan_x = 0.f; T->img_pan_y = 0.f; }
snprintf(ed->hot_status, sizeof(ed->hot_status), "Zoom %d%%", (int)(nz * 100.f + 0.5f));
ed->hot_status_ok = true;
}
/* Rodape da area de desenho: Desfazer / Limpar / zoom */
static void img_foot_btn(Ed* ed, int which, float* x, float* y, float* w, float* h) {
float ax, ay, aw, ah, fy;
@@ -174,8 +210,7 @@ static void img_foot_btn(Ed* ed, int which, float* x, float* y, float* w, float*
switch (which) {
case 0: *w = 78; *x = ax + 10; break; /* Desfazer */
case 1: *w = 68; *x = ax + 96; break; /* Limpar */
case 2: *w = 30; *x = ax + aw - 40; break; /* zoom - */
case 3: *w = 30; *x = ax + aw - 78; break; /* zoom + */
default: *w = 30; *x = ax + aw - 40; break;
}
}
@@ -758,8 +793,8 @@ void tb_editor_image_view(Ed* ed, TinyBlocks_RenderableBlock* rb) {
fy = img_foot_y(ed);
rb_fill_rect(rb, ax, fy, aw, IMG_FOOT_H, COL_PANEL);
rb_line(rb, ax, fy, WS_X + WS_W, fy, 1.f, COL_PAINT_LINE);
const char* labels[4] = { "Desfazer", "Limpar", "-", "+" };
for (int i = 0; i < 4; i++) {
const char* labels[2] = { "Desfazer", "Limpar" };
for (int i = 0; i < 2; i++) {
float bx, by, bw, bh;
img_foot_btn(ed, i, &bx, &by, &bw, &bh);
bool enabled = !(i == 0 && T->img_undo_n == 0);
@@ -769,6 +804,37 @@ void tb_editor_image_view(Ed* ed, TinyBlocks_RenderableBlock* rb) {
rb_text_c(rb, bx + (bw - tw(labels[i], FS_UI-1)) * 0.5f, by + bh * 0.5f,
labels[i], enabled ? COL_TEXT : COL_TEXT_DIM, FS_UI - 1);
}
/* ---- widget novo: slider de zoom ---- */
{
float sx, sy, sw, sh;
img_zoom_slider(ed, &sx, &sy, &sw, &sh);
float t = img_zoom_t(T->img_zoom);
float kx = sx + t * (sw - 14.f) + 7.f;
bool hov = in_rect(ed->mouse_x, ed->mouse_y, sx - 6, sy - 10, sw + 12, sh + 20);
/* trilha */
rb_fill_rrect(rb, sx, sy, sw, sh, 3, COL_PAINT_BTN);
/* parte preenchida ate o botao */
rb_fill_rrect(rb, sx, sy, kx - sx, sh, 3, hov ? COL_MENU : COLA(0, 122, 216, 255));
rb_stroke_rect(rb, sx, sy, sw, sh, 1.f, COL_PAINT_LINE);
/* marcas de 100% */
{
float mxr = sx + img_zoom_t(1.f) * (sw - 14.f) + 7.f;
rb_fill_rect(rb, mxr - 0.5f, sy - 3, 1.f, sh + 6, COL_TEXT_DIM);
}
/* botao */
rb_fill_circle(rb, kx, sy + sh * 0.5f, 7.f, COL_WHITE);
rb_stroke_circle(rb, kx, sy + sh * 0.5f, 7.f, 2.f, hov ? COL_MENU : COLA(0, 122, 216, 255));
/* porcentagem acima do botao */
{
char z[16];
snprintf(z, sizeof(z), "%d%%", (int)(T->img_zoom * 100.f + 0.5f));
float zx = kx - tw(z, 8.5f) * 0.5f;
if (zx < sx - 2) zx = sx - 2;
if (zx + tw(z, 8.5f) > sx + sw + 2) zx = sx + sw + 2 - tw(z, 8.5f);
rb_text_c(rb, zx, sy - 7, z, COL_TEXT, 8.5f);
}
}
}
/* --- coluna direita: cores (roda de matiz + saturacao/valor + paleta) --- */
@@ -1002,14 +1068,24 @@ bool tb_editor_image_click(Ed* ed, float mx, float my) {
/* rodape: botoes do desenho */
float fy = img_foot_y(ed);
if (my >= fy) {
for (int i = 0; i < 4; i++) {
/* widget novo: clique/arrasto no slider de zoom */
{
float sx, sy, sw, sh;
img_zoom_slider(ed, &sx, &sy, &sw, &sh);
if (in_rect(mx, my, sx - 6, fy, sw + 12, img_foot_y(ed) + IMG_FOOT_H - fy)) {
img_zoom_set(ed, img_zoom_of_t((mx - (sx + 7.f)) / (sw - 14.f)));
T->img_cdrag = 5;
T->img_drag = 1;
ed->img_mx = mx; ed->img_my = my;
return true;
}
}
for (int i = 0; i < 2; i++) {
float rx, ry, rw, rh;
img_foot_btn(ed, i, &rx, &ry, &rw, &rh);
if (!in_rect(mx, my, rx, ry, rw, rh)) continue;
if (i == 0) { img_sel_reset(T); img_undo_do(T); return true; }
if (i == 1) { img_sel_reset(T); img_undo_push(T); img_clear_layer(T); return true; }
if (i == 2) { tb_editor_image_zoom(ed, (rx + rw * 0.5f), WS_Y + WS_H * 0.5f, -1); T->img_pan_x = T->img_pan_y = 0.f; return true; }
if (i == 3) { tb_editor_image_zoom(ed, (rx + rw * 0.5f), WS_Y + WS_H * 0.5f, +1); T->img_pan_x = T->img_pan_y = 0.f; return true; }
}
return true;
}
@@ -1112,6 +1188,13 @@ void tb_editor_image_drag(Ed* ed, float mx, float my) {
Tab* T = &CT;
if (!T->img_drag) return;
ed->img_mx = mx; ed->img_my = my;
/* arrasto no slider de zoom (widget novo) */
if (T->img_cdrag == 5) {
float sx, sy, sw, sh;
img_zoom_slider(ed, &sx, &sy, &sw, &sh);
img_zoom_set(ed, img_zoom_of_t((mx - (sx + 7.f)) / (sw - 14.f)));
return;
}
/* arrasto na roda de cor / quadrado saturacao-valor */
if (T->img_cdrag) {
float cx_, cy_, ro, ri, sx, sy, sw2, sh2;
+8 -2
View File
@@ -148,8 +148,14 @@ static void eval_scale(TinyBlocks_Block* self, TinyBlocks* rt) {
out_img(self, tb_imglib_scale(in_img(self,rt,0), in_int(self,rt,1), in_int(self,rt,2), true));
}
static void eval_load(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Image* img = tb_image_load(in_text(self,rt,0));
if (!img) { tinyblocks_set_error(rt, "nao carregou '%s' (formato?)", in_text(self,rt,0)); self->dirty = false; return; }
const char* path = in_text(self, rt, 0);
TinyBlocks_Image* img = tb_image_load(path);
if (!img) { tinyblocks_set_error(rt, "nao carregou '%s' (formato?)", path); self->dirty = false; return; }
/* A imagem carregada ganha nome (nome do arquivo) e numero no armazem,
para os blocos "desenhe imagem nome/numero" acharem depois. */
const char* base = strrchr(path, '/');
base = base ? base + 1 : path;
tb_image_store_put(base, img);
out_img(self, img);
}
static void eval_save(TinyBlocks_Block* self, TinyBlocks* rt) {
+23 -1
View File
@@ -55,6 +55,20 @@ static void eval_text(TinyBlocks_Block* self, TinyBlocks* rt) {
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*)) {
@@ -87,6 +101,10 @@ static bool pen_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
"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}};
@@ -96,6 +114,8 @@ static bool pen_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
{"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)
@@ -104,7 +124,9 @@ static bool pen_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
&& 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.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) {