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
+377 -31

No files matched your search

+23 -8
View File
@@ -29,6 +29,16 @@
#include <tb_appearance.h> #include <tb_appearance.h>
#include <tb_pen.h> #include <tb_pen.h>
#include <tb_fs.h> #include <tb_fs.h>
#include <tb_api.h>
/* Registra uma library de categoria: se existir o .so dela no disco, carrega
* por ali (assim "make libs" + hot reload recarrega a lista de blocos na
* paleta); senao usa a copia embutida (linkada estaticamente). */
static void reg_core(TinyBlocks* rt, const char* so_name,
const TinyBlocks_LibraryDescriptor* (*get)(void)) {
if (tb_api_load_named(so_name) >= 0) return;
tb_library_register(rt, get());
}
int main(int argc, char** argv) { int main(int argc, char** argv) {
TinyBlocks* rt = NULL; TinyBlocks* rt = NULL;
@@ -43,14 +53,19 @@ int main(int argc, char** argv) {
} }
} }
/* Libraries de categoria, na ordem em que aparecem na paleta */ /* O tinyblocks-api faz dlopen das libraries e mantem os .so vigiados
tb_library_register(rt, tb_events_get_library()); (hot reload da paleta). */
tb_library_register(rt, tb_control_get_library()); tb_api_init(rt);
tb_library_register(rt, tb_operators_get_library());
tb_library_register(rt, tb_variables_get_library()); /* Libraries de categoria, na ordem em que aparecem na paleta.
tb_library_register(rt, tb_appearance_get_library()); Preferimos o .so: recompilar a library recarrega a paleta ao vivo. */
tb_library_register(rt, tb_pen_get_library()); reg_core(rt, "tinyblocks-events", tb_events_get_library);
tb_library_register(rt, tb_fs_get_library()); reg_core(rt, "tinyblocks-control", tb_control_get_library);
reg_core(rt, "tinyblocks-operators", tb_operators_get_library);
reg_core(rt, "tinyblocks-variables", tb_variables_get_library);
reg_core(rt, "tinyblocks-appearance", tb_appearance_get_library);
reg_core(rt, "tinyblocks-pen", tb_pen_get_library);
reg_core(rt, "tinyblocks-fs", tb_fs_get_library);
/* Interface gráfica: 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_graphics_get_library());
+12
View File
@@ -45,6 +45,18 @@ TB_API int tb_image_format_count(void);
TB_API TinyBlocks_Image* tb_image_load(const char* path); TB_API TinyBlocks_Image* tb_image_load(const char* path);
TB_API bool tb_image_save(const TinyBlocks_Image* img, const char* path); TB_API bool tb_image_save(const TinyBlocks_Image* img, const char* path);
/* -- Armazem de imagens nomeadas (usado pelos blocos "desenhe imagem") ----
* Uma imagem carregada ganha um NOME (ex: "c0bot" e "c0bot.png") e um NUMERO
* (1, 2, 3... na ordem em que entrou). O armazem mantem uma referencia, entao
* o palco pode desenhar a imagem depois sem se preocupar com a memoria. */
#define TB_IMAGE_STORE_MAX 32
TB_API int tb_image_store_put(const char* name, TinyBlocks_Image* img); /* -> numero (1..n) */
TB_API TinyBlocks_Image* tb_image_store_get(const char* name); /* nome ou arquivo */
TB_API TinyBlocks_Image* tb_image_store_at(int number); /* 1..n (0 = nada) */
TB_API int tb_image_store_count(void);
TB_API const char* tb_image_store_name(int number);
TB_API void tb_image_store_clear(void);
/* -- Helpers para TinyBlocks_Value --- */ /* -- Helpers para TinyBlocks_Value --- */
TB_API TinyBlocks_Value tb_value_image (TinyBlocks_Image* img); /* retém ref */ TB_API TinyBlocks_Value tb_value_image (TinyBlocks_Image* img); /* retém ref */
TB_API TinyBlocks_Image* tb_value_as_image(TinyBlocks_Value v); TB_API TinyBlocks_Image* tb_value_as_image(TinyBlocks_Value v);
+6
View File
@@ -20,6 +20,12 @@ TB_API void tb_stage_rect(float x, float y, float w, float h);
TB_API void tb_stage_circle(float x, float y, float radius); TB_API void tb_stage_circle(float x, float y, float radius);
TB_API void tb_stage_text(const char* text, float x, float y); TB_API void tb_stage_text(const char* text, float x, float y);
/* Imagens: desenha a imagem centrada em (x,y). Pode receber a imagem direto,
pelo NOME (ver tb_image_store_put) ou pelo NUMERO (1..n). */
TB_API bool tb_stage_image(const TinyBlocks_Image* img, float x, float y);
TB_API bool tb_stage_image_named(const char* name, float x, float y);
TB_API bool tb_stage_image_number(int number, float x, float y);
/* Vista: tamanho da area onde o palco sera desenhado (letterbox 4:3) */ /* Vista: tamanho da area onde o palco sera desenhado (letterbox 4:3) */
TB_API void tb_stage_set_view(float w, float h); TB_API void tb_stage_set_view(float w, float h);
+7 -1
View File
@@ -15,6 +15,7 @@
#define TINYBLOCKS_API_H #define TINYBLOCKS_API_H
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h>
#include <time.h> #include <time.h>
#include <tinyblocks/tinyblocks.h> #include <tinyblocks/tinyblocks.h>
@@ -34,7 +35,7 @@ typedef struct TbApiModule {
TinyBlocks_LibraryBlock* lib; TinyBlocks_LibraryBlock* lib;
const TinyBlocks_Category* category; /* pode ser NULL */ const TinyBlocks_Category* category; /* pode ser NULL */
unsigned block_count; unsigned block_count;
time_t mtime; /* mtime do .so (hot reload) */ int64_t mtime; /* mtime do .so (ns; hot reload) */
} TbApiModule; } TbApiModule;
/* -- Ciclo de vida --- */ /* -- Ciclo de vida --- */
@@ -53,6 +54,11 @@ TB_API const TbApiModule* tb_api_module_find(const char* name);
/* -- Carregamento dinamico --- */ /* -- Carregamento dinamico --- */
/* Carrega um .so; retorna o indice (>=0) ou -1 (erro em tb_api_last_error). */ /* Carrega um .so; retorna o indice (>=0) ou -1 (erro em tb_api_last_error). */
TB_API int tb_api_load(const char* path); 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_unload(int idx);
TB_API bool tb_api_reload(int idx); TB_API bool tb_api_reload(int idx);
TB_API bool tb_api_is_loaded(const char* name); TB_API bool tb_api_is_loaded(const char* name);
+99 -11
View File
@@ -25,9 +25,14 @@ static int s_n = 0;
static char s_err[256]; static char s_err[256];
/* -- Helpers --- */ /* -- Helpers --- */
static time_t file_mtime(const char* p) { static int64_t file_mtime(const char* p) {
struct stat st; 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); static void set_err(const char* fmt, const char* a);
@@ -44,7 +49,7 @@ typedef struct {
char name[64]; char name[64];
char path[TB_API_PATH_LEN]; char path[TB_API_PATH_LEN];
void* handle; void* handle;
time_t mtime; int64_t mtime;
bool dirty; bool dirty;
} TbApiCode; } TbApiCode;
@@ -68,6 +73,7 @@ typedef struct {
static TbWatch s_watch[TB_API_WATCH_MAX]; static TbWatch s_watch[TB_API_WATCH_MAX];
static int s_wn = 0; static int s_wn = 0;
static int s_inot = -1; 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 /* Vigia o DIRETORIO do arquivo: cobre tanto escrita no lugar quanto
"make" (escreve num temp e renomeia por cima). */ "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; if (s_inot < 0) return -1;
char dir[TB_API_PATH_LEN]; char dir[TB_API_PATH_LEN];
path_dirname(path, dir, sizeof(dir)); path_dirname(path, dir, sizeof(dir));
if (s_wn > 0 && strcmp(s_watch[s_wn - 1].dir, dir) == 0) { const char* base = path_basename(path);
/* mesmo diretorio: reaproveita o wd */ /* ja vigia esse arquivo? */
snprintf(s_watch[s_wn - 1].base, sizeof(s_watch[s_wn-1].base), "%s", path_basename(path)); for (int i = 0; i < s_wn; i++)
return s_watch[s_wn - 1].wd; 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; 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); IN_CLOSE_WRITE | IN_MOVED_TO | IN_CREATE | IN_ATTRIB);
if (wd < 0) return -1; if (wd < 0) return -1;
TbWatch* w = &s_watch[s_wn++]; TbWatch* w = &s_watch[s_wn++];
@@ -118,6 +130,18 @@ int tb_api_poll_watch(void) {
struct inotify_event* ev = (struct inotify_event*)p; struct inotify_event* ev = (struct inotify_event*)p;
p += sizeof(*ev) + ev->len; p += sizeof(*ev) + ev->len;
if (!ev->len) continue; 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++) { for (int i = 0; i < s_cn; i++) {
if (strcmp(ev->name, path_basename(s_code[i].path)) != 0) continue; if (strcmp(ev->name, path_basename(s_code[i].path)) != 0) continue;
char d[TB_API_PATH_LEN]; 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) { static bool code_changed_mtime(void) {
bool any = false; bool any = false;
for (int i = 0; i < s_cn; i++) { 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; } if (mt && mt != s_code[i].mtime) { s_code[i].dirty = true; any = true; }
} }
return any; return any;
@@ -359,6 +383,9 @@ int tb_api_load(const char* path) {
m->category = tb_library_category(lib); m->category = tb_library_category(lib);
m->block_count = tb_library_block_count(lib); m->block_count = tb_library_block_count(lib);
m->mtime = file_mtime(path); m->mtime = file_mtime(path);
#ifdef __linux__
tb_api_watch(path);
#endif
s_err[0] = 0; s_err[0] = 0;
return s_n++; return s_n++;
#else #else
@@ -389,11 +416,72 @@ bool tb_api_reload(int idx) {
return tb_api_load(path) >= 0; 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) { 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++) { for (int i = 0; i < s_n; i++) {
TbApiModule* m = &s_mods[i]; TbApiModule* m = &s_mods[i];
if (!m->path[0]) continue; 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; if (mt && mt != m->mtime) return true;
} }
return false; return false;
@@ -407,7 +495,7 @@ int tb_api_poll_reload(void) {
for (int i = 0; i < s_n; i++) { for (int i = 0; i < s_n; i++) {
TbApiModule* m = &s_mods[i]; TbApiModule* m = &s_mods[i];
if (!m->path[0]) continue; 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; if (!mt || mt == m->mtime) continue;
char path[TB_API_PATH_LEN]; char path[TB_API_PATH_LEN];
snprintf(path, sizeof(path), "%s", m->path); 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.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.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.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"}, {"Stage.Text", "escreva %1 no palco em x %2 y %3", "Olá!|-40|0"},
/* Curvas (lista de pontos [x,y]) */ /* 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"}, {"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; (void)cx;
return cy + ro + 24; 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 */ /* 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) { static void img_foot_btn(Ed* ed, int which, float* x, float* y, float* w, float* h) {
float ax, ay, aw, ah, fy; 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) { switch (which) {
case 0: *w = 78; *x = ax + 10; break; /* Desfazer */ case 0: *w = 78; *x = ax + 10; break; /* Desfazer */
case 1: *w = 68; *x = ax + 96; break; /* Limpar */ 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; 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); fy = img_foot_y(ed);
rb_fill_rect(rb, ax, fy, aw, IMG_FOOT_H, COL_PANEL); 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); rb_line(rb, ax, fy, WS_X + WS_W, fy, 1.f, COL_PAINT_LINE);
const char* labels[4] = { "Desfazer", "Limpar", "-", "+" }; const char* labels[2] = { "Desfazer", "Limpar" };
for (int i = 0; i < 4; i++) { for (int i = 0; i < 2; i++) {
float bx, by, bw, bh; float bx, by, bw, bh;
img_foot_btn(ed, i, &bx, &by, &bw, &bh); img_foot_btn(ed, i, &bx, &by, &bw, &bh);
bool enabled = !(i == 0 && T->img_undo_n == 0); 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, 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); 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) --- */ /* --- 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 */ /* rodape: botoes do desenho */
float fy = img_foot_y(ed); float fy = img_foot_y(ed);
if (my >= fy) { 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; float rx, ry, rw, rh;
img_foot_btn(ed, i, &rx, &ry, &rw, &rh); img_foot_btn(ed, i, &rx, &ry, &rw, &rh);
if (!in_rect(mx, my, rx, ry, rw, rh)) continue; 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 == 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 == 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; return true;
} }
@@ -1112,6 +1188,13 @@ void tb_editor_image_drag(Ed* ed, float mx, float my) {
Tab* T = &CT; Tab* T = &CT;
if (!T->img_drag) return; if (!T->img_drag) return;
ed->img_mx = mx; ed->img_my = my; 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 */ /* arrasto na roda de cor / quadrado saturacao-valor */
if (T->img_cdrag) { if (T->img_cdrag) {
float cx_, cy_, ro, ri, sx, sy, sw2, sh2; 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)); 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) { static void eval_load(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Image* img = tb_image_load(in_text(self,rt,0)); const char* path = in_text(self, rt, 0);
if (!img) { tinyblocks_set_error(rt, "nao carregou '%s' (formato?)", in_text(self,rt,0)); self->dirty = false; return; } 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); out_img(self, img);
} }
static void eval_save(TinyBlocks_Block* self, TinyBlocks* rt) { 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; 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, static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
const TinyBlocks_SlotDesc* in, uint32_t ni, const TinyBlocks_SlotDesc* in, uint32_t ni,
void (*fn)(TinyBlocks_Block*, TinyBlocks*)) { void (*fn)(TinyBlocks_Block*, TinyBlocks*)) {
@@ -87,6 +101,10 @@ static bool pen_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
"Desenha um circulo"); "Desenha um circulo");
TB_BLOCK_META("Stage.Text", "eval_text", TB_BLOCK_META("Stage.Text", "eval_text",
"Escreve um texto no palco"); "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 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 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}}; {"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 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 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) return reg(rt, lib, "Stage.Clear", NULL, 0, eval_clear)
&& reg(rt, lib, "Stage.Background", rgb, 3, eval_background) && 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.Line", line, 4, eval_line)
&& reg(rt, lib, "Stage.Rect", rect, 4, eval_rect) && reg(rt, lib, "Stage.Rect", rect, 4, eval_rect)
&& reg(rt, lib, "Stage.Circle", circ, 3, eval_circle) && 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) { static void pen_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
+80
View File
@@ -2,6 +2,7 @@
* TinyBlocks - tb_image.c * TinyBlocks - tb_image.c
* TinyBlocks_Image: buffer de pixels ref-counted com 1, 3 ou 4 canais. * TinyBlocks_Image: buffer de pixels ref-counted com 1, 3 ou 4 canais.
*/ */
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
@@ -152,6 +153,85 @@ bool tb_image_save(const TinyBlocks_Image* img, const char* path) {
return (f && f->enc) ? f->enc(img, path) : false; return (f && f->enc) ? f->enc(img, path) : false;
} }
/* == Armazem de imagens nomeadas ========================================
* Cada imagem carregada ganha um NOME (nome do arquivo, com e sem extensao)
* e um NUMERO (ordem de entrada). Os blocos "desenhe imagem nome/numero"
* usam isto para achar a imagem depois. */
typedef struct {
char name[64];
TinyBlocks_Image* img;
} TbStoredImage;
static TbStoredImage s_store[TB_IMAGE_STORE_MAX];
static int s_store_n = 0;
static const char* store_base(const char* p) {
const char* b = strrchr(p, '/');
return b ? b + 1 : p;
}
int tb_image_store_put(const char* name, TinyBlocks_Image* img) {
if (!img) return 0;
const char* nm = (name && name[0]) ? name : "imagem";
for (int i = 0; i < s_store_n; i++) {
if (strcmp(s_store[i].name, nm) == 0) {
tb_image_release(s_store[i].img);
tb_image_retain(img);
s_store[i].img = img;
return i + 1;
}
}
if (s_store_n >= TB_IMAGE_STORE_MAX) return 0;
TbStoredImage* e = &s_store[s_store_n];
memset(e, 0, sizeof(*e));
snprintf(e->name, sizeof(e->name), "%s", nm);
tb_image_retain(img);
e->img = img;
s_store_n++;
return s_store_n;
}
TinyBlocks_Image* tb_image_store_get(const char* name) {
if (!name || !name[0]) return NULL;
for (int i = 0; i < s_store_n; i++)
if (strcmp(s_store[i].name, name) == 0) return s_store[i].img;
/* aceita com/sem extensao e com caminho */
const char* base = store_base(name);
char stem[64];
snprintf(stem, sizeof(stem), "%s", base);
char* dot = strrchr(stem, '.');
if (dot && dot != stem) *dot = 0;
for (int i = 0; i < s_store_n; i++) {
char s2[64];
snprintf(s2, sizeof(s2), "%s", s_store[i].name);
char* d2 = strrchr(s2, '.');
if (d2 && d2 != s2) *d2 = 0;
if (strcmp(s_store[i].name, base) == 0 || strcmp(s2, base) == 0 ||
strcmp(s_store[i].name, stem) == 0 || strcmp(s2, stem) == 0)
return s_store[i].img;
}
return NULL;
}
TinyBlocks_Image* tb_image_store_at(int number) {
if (number < 1 || number > s_store_n) return NULL;
return s_store[number - 1].img;
}
int tb_image_store_count(void) { return s_store_n; }
const char* tb_image_store_name(int number) {
if (number < 1 || number > s_store_n) return "";
return s_store[number - 1].name;
}
void tb_image_store_clear(void) {
for (int i = 0; i < s_store_n; i++) tb_image_release(s_store[i].img);
memset(s_store, 0, sizeof(s_store));
s_store_n = 0;
}
TinyBlocks_Value tb_value_image(TinyBlocks_Image* img) { TinyBlocks_Value tb_value_image(TinyBlocks_Image* img) {
TinyBlocks_Value v = { .kind = TB_VALUE_IMAGE, .as.image = img }; TinyBlocks_Value v = { .kind = TB_VALUE_IMAGE, .as.image = img };
if (img) tb_image_retain(img); if (img) tb_image_retain(img);
+27 -1
View File
@@ -13,13 +13,14 @@
#include <string.h> #include <string.h>
#include "tinyblocks/tb_stage.h" #include "tinyblocks/tb_stage.h"
#include "tinyblocks/tb_image.h"
#define STAGE_W 480.f #define STAGE_W 480.f
#define STAGE_H 360.f #define STAGE_H 360.f
#define STAGE_MAX_CMDS 4000 #define STAGE_MAX_CMDS 4000
#define STAGE_TEXT_FS 14.f #define STAGE_TEXT_FS 14.f
typedef enum { SC_RECT, SC_CIRCLE, SC_LINE, SC_TEXT } StageCmdKind; typedef enum { SC_RECT, SC_CIRCLE, SC_LINE, SC_TEXT, SC_IMAGE } StageCmdKind;
typedef struct { typedef struct {
StageCmdKind kind; StageCmdKind kind;
@@ -27,6 +28,7 @@ typedef struct {
float size; /* espessura da caneta */ float size; /* espessura da caneta */
float col[3]; float col[3];
char text[96]; char text[96];
const TinyBlocks_Image* img; /* SC_IMAGE (vem do armazem de imagens) */
} StageCmd; } StageCmd;
typedef struct { typedef struct {
@@ -103,6 +105,21 @@ void tb_stage_text(const char* text, float x, float y) {
} }
} }
bool tb_stage_image(const TinyBlocks_Image* img, float x, float y) {
if (!img) return false;
StageCmd* c = push(SC_IMAGE);
if (!c) return false;
c->a = x; c->b = y;
c->img = img;
return true;
}
bool tb_stage_image_named(const char* name, float x, float y) {
return tb_stage_image(tb_image_store_get(name), x, y);
}
bool tb_stage_image_number(int number, float x, float y) {
return tb_stage_image(tb_image_store_at(number), x, y);
}
void tb_stage_set_view(float w, float h) { void tb_stage_set_view(float w, float h) {
if (w > 0 && h > 0) { s_stage.view_w = w; s_stage.view_h = h; } if (w > 0 && h > 0) { s_stage.view_w = w; s_stage.view_h = h; }
} }
@@ -145,6 +162,15 @@ bool tb_stage_render(TinyBlocks_RenderableBlock* rb) {
tb_draw_stroke_line(rb, SX(c->a), SY(c->b), SX(c->c), SY(c->d), tb_draw_stroke_line(rb, SX(c->a), SY(c->b), SX(c->c), SY(c->d),
c->size*sc, r, g, b, 1.f); c->size*sc, r, g, b, 1.f);
break; break;
case SC_IMAGE: {
const TinyBlocks_Image* im = c->img;
int iw = tb_image_width(im), ih = tb_image_height(im);
if (iw > 0 && ih > 0) {
float dw = iw * sc, dh = ih * sc;
tb_draw_image(rb, SX(c->a) - dw * 0.5f, SY(c->b) - dh * 0.5f, dw, dh, im);
}
break;
}
case SC_TEXT: { case SC_TEXT: {
float fs = STAGE_TEXT_FS * sc; float fs = STAGE_TEXT_FS * sc;
tb_draw_text(rb, SX(c->a), SY(c->b) + fs*0.36f, c->text, fs, r, g, b, 1.f); tb_draw_text(rb, SX(c->a), SY(c->b) + fs*0.36f, c->text, fs, r, g, b, 1.f);