editor: roda do mouse da zoom na aba de imagem e nao pinta mais

- Aba de imagem: roda do mouse = zoom centrado no ponteiro (como no Krita),
  com deslocamento (pan) para nao perder o ponto de referencia; com zoom <=
  100% a pagina volta ao centro. O scroll nao pinta nem move blocos.
- tinyblocks-editor-image exporta tb_editor_image_zoom e a tabela da API
  (tb_ed_img.zoom) faz a ponte, entao o editor so chama por ponteiro.
- Removida a barra cinza decorativa no fim da tela (parecia uma barra de
  rolagem falsa).
- harness: scroll() e testes 'roda do mouse aproxima/afasta/nao pinta'.
This commit is contained in:
mguinhos committed 2026-10-20 23:41:19 -03:00
1 parent 0143d3efd5
commit 2b6bc65062
7 files changed
+77 -7

No files matched your search

@@ -430,6 +430,8 @@ typedef struct Tab {
float img_hue, img_sat, img_val; /* HSV da cor atual (roda de cor) */
int img_cdrag; /* arrasto na roda: 0=nada 1=matiz 2=SV */
float img_zoom; /* escala de exibicao (1.0 = 1:1) */
float img_pan_x; /* deslocamento da pagina (zoom no cursor) */
float img_pan_y;
int img_drag; /* traco em andamento */
int img_drag_x, img_drag_y;
TinyBlocks_Image* img_undo[IMG_UNDO_MAX];
@@ -681,6 +683,7 @@ TB_ED_API void start_path(Ed* ed, int mode, const char* def);
# define img_ensure tb_editor_image_ensure
# define img_sel_reset tb_editor_image_sel_reset
# define img_sel_commit tb_editor_image_sel_commit
# define image_zoom tb_editor_image_zoom
#else
# define render_image_tab tb_ed_img.view
# define image_click tb_ed_img.click
@@ -691,6 +694,7 @@ TB_ED_API void start_path(Ed* ed, int mode, const char* def);
# define img_ensure tb_ed_img.ensure
# define img_sel_reset tb_ed_img.sel_reset
# define img_sel_commit tb_ed_img.sel_commit
# define image_zoom tb_ed_img.zoom
#endif
/* Configuracoes (settings) - UI desenhada pelo editor */
@@ -54,6 +54,7 @@ typedef struct {
void (*ensure) (Ed*, Tab*);
void (*sel_reset) (Tab*);
void (*sel_commit)(Tab*);
void (*zoom) (Ed*, float, float, int); /* roda do mouse = zoom */
} TbEdImageApi;
typedef struct {
@@ -39,6 +39,10 @@ void tb_editor_image_ensure (Ed* ed, Tab* T);
void tb_editor_image_sel_reset (Tab* T);
void tb_editor_image_sel_commit(Tab* T);
/* Zoom centrado no ponteiro (roda do mouse): steps > 0 aproxima, < 0 afasta.
Fora da area de desenho nao faz nada (o scroll nao pinta nem move blocos). */
void tb_editor_image_zoom(Ed* ed, float mx, float my, int steps);
#ifdef __cplusplus
}
#endif
@@ -122,8 +122,15 @@ static void img_view(Ed* ed, Tab* T, float* dx, float* dy, float* dw, float* dh)
img_area(ed, &ax, &ay, &aw, &ah);
*dw = T->img_w * T->img_zoom;
*dh = T->img_h * T->img_zoom;
*dx = ax + (aw - *dw) * 0.5f;
*dy = ay + (ah - *dh) * 0.5f;
*dx = ax + (aw - *dw) * 0.5f + T->img_pan_x;
*dy = ay + (ah - *dh) * 0.5f + T->img_pan_y;
}
/* Canto da pagina sem o deslocamento (usado para reposicionar no zoom) */
static void img_base(Ed* ed, Tab* T, float zoom, float* bx, float* by) {
float ax, ay, aw, ah;
img_area(ed, &ax, &ay, &aw, &ah);
*bx = ax + (aw - T->img_w * zoom) * 0.5f;
*by = ay + (ah - T->img_h * zoom) * 0.5f;
}
static void img_tool_rect(Ed* ed, int i, float* x, float* y) {
*x = WS_X + (IMG_TOOL_W - IMG_TH) * 0.5f;
@@ -742,7 +749,6 @@ void tb_editor_image_view(Ed* ed, TinyBlocks_RenderableBlock* rb) {
}
}
clip_clear(ed, rb);
rb_fill_rrect(rb, ax + 40, ay + ah - 10, aw - 80, 6, 3, COL_PAINT_SHAD);
}
/* --- rodape: controles do desenho --- */
@@ -1002,8 +1008,8 @@ bool tb_editor_image_click(Ed* ed, float mx, float my) {
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) { T->img_zoom *= 0.8f; if (T->img_zoom < 0.1f) T->img_zoom = 0.1f; return true; }
if (i == 3) { T->img_zoom *= 1.25f; if (T->img_zoom > 8.f) T->img_zoom = 8.f; 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;
}
@@ -1069,6 +1075,39 @@ bool tb_editor_image_click(Ed* ed, float mx, float my) {
return true;
}
/* Zoom centrado no cursor (roda do mouse), como no Krita: o ponto que esta
embaixo do ponteiro continua no mesmo lugar; com zoom <= 100% a pagina
cabe na tela e volta ao centro. */
void tb_editor_image_zoom(Ed* ed, float mx, float my, int steps) {
Tab* T = &CT;
if (steps == 0) return;
float ax, ay, aw, ah;
img_area(ed, &ax, &ay, &aw, &ah);
if (!in_rect(mx, my, ax, ay, aw, ah)) return; /* so na area de desenho */
float old = T->img_zoom;
float nz = old * powf(1.18f, (float)steps);
if (nz < 0.1f) nz = 0.1f;
if (nz > 8.f) nz = 8.f;
if (fabsf(nz - old) < 1e-4f) return;
float dx, dy, dw, dh;
img_view(ed, T, &dx, &dy, &dw, &dh);
(void)dw; (void)dh;
float ix = (mx - dx) / old, iy = (my - dy) / old; /* ponto na pagina */
T->img_zoom = nz;
if (nz <= 1.f) {
T->img_pan_x = T->img_pan_y = 0.f; /* cabe: centraliza */
} else {
float bx, by;
img_base(ed, T, nz, &bx, &by);
T->img_pan_x = (mx - ix * nz) - bx;
T->img_pan_y = (my - iy * nz) - by;
}
char msg[48];
snprintf(msg, sizeof(msg), "Zoom %d%%", (int)(nz * 100.f + 0.5f));
snprintf(ed->hot_status, sizeof(ed->hot_status), "%s", msg);
ed->hot_status_ok = true;
}
void tb_editor_image_drag(Ed* ed, float mx, float my) {
Tab* T = &CT;
if (!T->img_drag) return;
@@ -1171,6 +1210,7 @@ void tb_editor_image_load_file(Ed* ed, const char* path, int kind) {
scopy(T->img_path, sizeof(T->img_path), path);
scopy(T->name, sizeof(T->name), base);
T->img_zoom = 1.f;
T->img_pan_x = 0.f; T->img_pan_y = 0.f;
snprintf(ed->hot_status, sizeof(ed->hot_status), "Imagem aberta: %.200s", path);
ed->hot_status_ok = true;
return;
@@ -4332,6 +4332,9 @@ static void editor_eval(TinyBlocks_Block* self, TinyBlocks* rt) {
} else if (CT.kind == TK_CODE && mx >= WS_X && mx < WIN_W - STAGE_W && my >= WS_Y) {
CT.code_scroll -= (int)sdy * 3;
if (CT.code_scroll < 0) CT.code_scroll = 0;
} else if (CT.kind == TK_IMAGE && mx >= WS_X && mx < WIN_W - STAGE_W && my >= WS_Y) {
/* roda do mouse na aba de imagem = zoom no cursor (nao pinta) */
image_zoom(ed, mx, my, (int)(sdy > 0 ? 1 : sdy < 0 ? -1 : 0));
} else if (mx >= WS_X && mx < WIN_W - STAGE_W && my >= WS_Y) {
CT.ws_sx -= (float)sdx * 40.f;
CT.ws_sy -= (float)sdy * 40.f;
@@ -131,8 +131,9 @@ static bool resolve_image(void) {
sym_fn(&a.ensure, "tinyblocks-editor-image", "tb_editor_image_ensure");
sym_fn(&a.sel_reset, "tinyblocks-editor-image", "tb_editor_image_sel_reset");
sym_fn(&a.sel_commit, "tinyblocks-editor-image", "tb_editor_image_sel_commit");
sym_fn(&a.zoom, "tinyblocks-editor-image", "tb_editor_image_zoom");
bool ok = a.view && a.click && a.drag && a.release && a.load_file && a.save &&
a.ensure && a.sel_reset && a.sel_commit;
a.ensure && a.sel_reset && a.sel_commit && a.zoom;
if (ok) tb_ed_img = a;
return ok;
}