Adiciona tipo imagem (core) + tinyblocks-ppm e previa de imagem no balao

This commit is contained in:
mguinhos committed 2026-10-20 21:04:41 -03:00
1 parent f794d8b8c7
commit 959a4027c3
16 files changed
+721 -10

No files matched your search

+36 -1
View File
@@ -250,6 +250,14 @@ static const BlockSpec s_specs[] = {
/* Base64 */
{"Base64.Encode", "codifique em base64 %1", NULL},
{"Base64.Decode", "decodifique base64 %1", NULL},
/* Imagens PPM (tipo imagem do core) */
{"PPM.Load", "carregue a imagem %1", "imagem.ppm"},
{"PPM.Save", "grave a imagem %1 em %2", "|saida.ppm"},
{"PPM.New", "nova imagem(%1 x %2) cor r %3 g %4 b %5", "32|32|255|0|0"},
{"PPM.Width", "largura de %1", NULL},
{"PPM.Height", "altura de %1", NULL},
{"PPM.SetPixel", "pinte %1 em (%2,%3) de r %4 g %5 b %6", "|0|0|0|0|0"},
{"PPM.GetPixel", "cor de %1 em (%2,%3)", "|0|0"},
};
/* ===
@@ -401,6 +409,7 @@ typedef struct {
int bubble_bi;
char bubble_text[128];
TinyBlocks_JsonValue* bubble_json; /* lista de pontos (retida) ou NULL */
TinyBlocks_Image* bubble_img; /* imagem (retida) ou NULL */
int last_click_bi;
unsigned last_click_ms;
@@ -576,6 +585,8 @@ static void value_to_str(const TinyBlocks_Value* v, char* buf, size_t cap) {
case TB_VALUE_BLOCK: snprintf(buf, cap, "[bloco]"); break;
case TB_VALUE_VECTOR: snprintf(buf, cap, "[vetor]"); break;
case TB_VALUE_JSON: snprintf(buf, cap, "[json]"); break;
case TB_VALUE_IMAGE: snprintf(buf, cap, "[imagem %dx%d]",
tb_image_width(v->as.image), tb_image_height(v->as.image)); break;
default: buf[0] = 0; break;
}
}
@@ -1578,6 +1589,7 @@ static void run_flag(Ed* ed) {
/* Libera o balão de resultado (texto + eventual lista de pontos) */
static void bubble_clear(Ed* ed) {
if (ed->bubble_json) { tb_json_release(ed->bubble_json); ed->bubble_json = NULL; }
if (ed->bubble_img) { tb_image_release(ed->bubble_img); ed->bubble_img = NULL; }
ed->bubble_bi = -1;
ed->bubble_text[0] = 0;
}
@@ -1624,6 +1636,13 @@ static void bubble_set(Ed* ed, int root, TinyBlocks_Value* v) {
(unsigned)tb_json_array_length(v->as.json));
return;
}
if (v->kind == TB_VALUE_IMAGE && v->as.image) {
tb_image_retain(v->as.image);
ed->bubble_img = v->as.image;
snprintf(ed->bubble_text, sizeof(ed->bubble_text), "%dx%d",
tb_image_width(v->as.image), tb_image_height(v->as.image));
return;
}
value_to_str(v, ed->bubble_text, sizeof(ed->bubble_text));
if (!ed->bubble_text[0]) strcpy(ed->bubble_text, "(vazio)");
}
@@ -2795,7 +2814,23 @@ static void render_workspace(Ed* ed, TinyBlocks_RenderableBlock* rb) {
if (ed->bubble_bi >= 0 && CT.ws[ed->bubble_bi].used) {
EdBlock* b = &CT.ws[ed->bubble_bi];
if (ed->bubble_json) {
if (ed->bubble_img) {
/* Balão com prévia da imagem (escalada, mantendo proporção) */
const float BW = 184, BH = 160, PAD = 10;
float bx = b->ax + ox + 8, by = b->ay + oy - (BH + 6);
rb_fill_rrect(rb, bx, by, BW, BH, 10, COL_BORDER);
rb_fill_rrect(rb, bx+1, by+1, BW-2, BH-2, 9, COL_WHITE);
rb_fill_rect(rb, bx + 16, by + BH - 2, 10, 5, COL_WHITE);
int iw = tb_image_width(ed->bubble_img);
int ih = tb_image_height(ed->bubble_img);
if (iw > 0 && ih > 0) {
float aw = BW - 2*PAD, ah = BH - 2*PAD - 12;
float sc = aw/iw < ah/ih ? aw/iw : ah/ih;
float dw = iw*sc, dh = ih*sc;
tb_draw_image(rb, bx + (BW-dw)*0.5f, by + (BH-12-dh)*0.5f, dw, dh, ed->bubble_img);
}
rb_text_c(rb, bx + 10, by + BH - 4, ed->bubble_text, COL_TEXT_DIM, 11.f);
} else if (ed->bubble_json) {
/* Balão com prévia gráfica de uma lista de pontos (curva) */
const float BW = 180, BH = 140, PAD = 16;
float bx = b->ax + ox + 8, by = b->ay + oy - (BH + 6);