Nicer zooming

This commit is contained in:
Bert 2011-01-21 10:29:31 +01:00
parent bb12b8c0fb
commit 9dc46c1d81
2 changed files with 46 additions and 27 deletions

View File

@ -2,12 +2,14 @@
#define WIN_WIDTH 800 #define WIN_WIDTH 800
#define WIN_HEIGHT 600 #define WIN_HEIGHT 600
/* */
#define BG_COLOR "#888888"
/* */ /* */
#define SCALE_MODE SCALE_DOWN #define SCALE_MODE SCALE_DOWN
/* */ /* */
#define ZOOM_MIN 12.5 static const float zoom_levels[] = {
#define ZOOM_MAX 400 12.5, 25.0, 50.0, 75.0,
100.0, 150.0, 200.0, 400.0, 800.0
/* */ };
#define BG_COLOR "#888888"

61
image.c
View File

@ -24,6 +24,10 @@
#include "sxiv.h" #include "sxiv.h"
#include "image.h" #include "image.h"
int zl_cnt;
float zoom_min;
float zoom_max;
void imlib_init(win_t *win) { void imlib_init(win_t *win) {
if (!win) if (!win)
return; return;
@ -31,6 +35,10 @@ void imlib_init(win_t *win) {
imlib_context_set_display(win->env.dpy); imlib_context_set_display(win->env.dpy);
imlib_context_set_visual(win->env.vis); imlib_context_set_visual(win->env.vis);
imlib_context_set_colormap(win->env.cmap); imlib_context_set_colormap(win->env.cmap);
zl_cnt = sizeof(zoom_levels) / sizeof(zoom_levels[0]);
zoom_min = zoom_levels[0] / 100.0;
zoom_max = zoom_levels[zl_cnt - 1] / 100.0;
} }
void imlib_destroy() { void imlib_destroy() {
@ -72,10 +80,10 @@ void img_display(img_t *img, win_t *win) {
zh = (float) win->h / (float) img->h; zh = (float) win->h / (float) img->h;
img->zoom = MIN(zw, zh); img->zoom = MIN(zw, zh);
if (img->zoom * 100.0 < ZOOM_MIN) if (img->zoom < zoom_min)
img->zoom = ZOOM_MIN / 100.0; img->zoom = zoom_min;
else if (img->zoom * 100.0 > ZOOM_MAX) else if (img->zoom > zoom_max)
img->zoom = ZOOM_MAX / 100.0; img->zoom = zoom_max;
if (img->scalemode == SCALE_DOWN && img->zoom > 1.0) if (img->scalemode == SCALE_DOWN && img->zoom > 1.0)
img->zoom = 1.0; img->zoom = 1.0;
@ -150,25 +158,14 @@ void img_render(img_t *img, win_t *win) {
win_draw(win); win_draw(win);
} }
int img_zoom(img_t *img, int d) { int img_zoom(img_t *img, float z) {
int ad, iz;
float z;
if (!img) if (!img)
return 0; return 0;
ad = ABS(d); if (z < zoom_min)
iz = (int) (img->zoom * 1000.0) + d; z = zoom_min;
if (iz % ad > ad / 2) else if (z > zoom_max)
iz += ad - iz % ad; z = zoom_max;
else
iz -= iz % ad;
z = (float) iz / 1000.0;
if (z * 100.0 < ZOOM_MIN)
z = ZOOM_MIN / 100.0;
else if (z * 100.0 > ZOOM_MAX)
z = ZOOM_MAX / 100.0;
if (z != img->zoom) { if (z != img->zoom) {
img->x -= (img->w * z - img->w * img->zoom) / 2; img->x -= (img->w * z - img->w * img->zoom) / 2;
@ -181,9 +178,29 @@ int img_zoom(img_t *img, int d) {
} }
int img_zoom_in(img_t *img) { int img_zoom_in(img_t *img) {
return img_zoom(img, 125); int i;
if (!img)
return 0;
for (i = 1; i < zl_cnt; ++i) {
if (zoom_levels[i] > img->zoom * 100.0)
return img_zoom(img, zoom_levels[i] / 100.0);
}
return 0;
} }
int img_zoom_out(img_t *img) { int img_zoom_out(img_t *img) {
return img_zoom(img, -125); int i;
if (!img)
return 0;
for (i = zl_cnt - 2; i >= 0; --i) {
if (zoom_levels[i] < img->zoom * 100.0)
return img_zoom(img, zoom_levels[i] / 100.0);
}
return 0;
} }