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
@@ -2,6 +2,7 @@
|
||||
* TinyBlocks - tb_image.c
|
||||
* TinyBlocks_Image: buffer de pixels ref-counted com 1, 3 ou 4 canais.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.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;
|
||||
}
|
||||
|
||||
/* == 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 v = { .kind = TB_VALUE_IMAGE, .as.image = img };
|
||||
if (img) tb_image_retain(img);
|
||||
|
||||
+27
-1
@@ -13,13 +13,14 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "tinyblocks/tb_stage.h"
|
||||
#include "tinyblocks/tb_image.h"
|
||||
|
||||
#define STAGE_W 480.f
|
||||
#define STAGE_H 360.f
|
||||
#define STAGE_MAX_CMDS 4000
|
||||
#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 {
|
||||
StageCmdKind kind;
|
||||
@@ -27,6 +28,7 @@ typedef struct {
|
||||
float size; /* espessura da caneta */
|
||||
float col[3];
|
||||
char text[96];
|
||||
const TinyBlocks_Image* img; /* SC_IMAGE (vem do armazem de imagens) */
|
||||
} StageCmd;
|
||||
|
||||
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) {
|
||||
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),
|
||||
c->size*sc, r, g, b, 1.f);
|
||||
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: {
|
||||
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);
|
||||
|
||||
Reference in new issue
Block a user