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:
1 parent
2b6bc65062
commit
adf5d2cd12
11 files changed
+378
-32
No files matched your search
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user