nsxiv9dc46c1
Nicer zooming
+45 −26
2 files changed
| @@ -3,11 +3,13 @@ | |||
| 3 | 3 | #define WIN_HEIGHT 600 | |
| 4 | 4 | ||
| 5 | 5 | /* */ | |
| 6 | − | #define SCALE_MODE SCALE_DOWN | |
| 6 | + | #define BG_COLOR "#888888" | |
| 7 | 7 | ||
| 8 | 8 | /* */ | |
| 9 | − | #define ZOOM_MIN 12.5 | |
| 10 | − | #define ZOOM_MAX 400 | |
| 9 | + | #define SCALE_MODE SCALE_DOWN | |
| 11 | 10 | ||
| 12 | 11 | /* */ | |
| 13 | − | #define BG_COLOR "#888888" | |
| 12 | + | static const float zoom_levels[] = { | |
| 13 | + | 12.5, 25.0, 50.0, 75.0, | |
| 14 | + | 100.0, 150.0, 200.0, 400.0, 800.0 | |
| 15 | + | }; | |
| @@ -24,6 +24,10 @@ | |||
| 24 | 24 | #include "sxiv.h" | |
| 25 | 25 | #include "image.h" | |
| 26 | 26 | ||
| 27 | + | int zl_cnt; | |
| 28 | + | float zoom_min; | |
| 29 | + | float zoom_max; | |
| 30 | + | ||
| 27 | 31 | void imlib_init(win_t *win) { | |
| 28 | 32 | if (!win) | |
| 29 | 33 | return; | |
| @@ -31,6 +35,10 @@ void imlib_init(win_t *win) { | |||
| 31 | 35 | imlib_context_set_display(win->env.dpy); | |
| 32 | 36 | imlib_context_set_visual(win->env.vis); | |
| 33 | 37 | imlib_context_set_colormap(win->env.cmap); | |
| 38 | + | ||
| 39 | + | zl_cnt = sizeof(zoom_levels) / sizeof(zoom_levels[0]); | |
| 40 | + | zoom_min = zoom_levels[0] / 100.0; | |
| 41 | + | zoom_max = zoom_levels[zl_cnt - 1] / 100.0; | |
| 34 | 42 | } | |
| 35 | 43 | ||
| 36 | 44 | void imlib_destroy() { | |
| @@ -72,10 +80,10 @@ void img_display(img_t *img, win_t *win) { | |||
| 72 | 80 | zh = (float) win->h / (float) img->h; | |
| 73 | 81 | img->zoom = MIN(zw, zh); | |
| 74 | 82 | ||
| 75 | − | if (img->zoom * 100.0 < ZOOM_MIN) | |
| 76 | − | img->zoom = ZOOM_MIN / 100.0; | |
| 77 | − | else if (img->zoom * 100.0 > ZOOM_MAX) | |
| 78 | − | img->zoom = ZOOM_MAX / 100.0; | |
| 83 | + | if (img->zoom < zoom_min) | |
| 84 | + | img->zoom = zoom_min; | |
| 85 | + | else if (img->zoom > zoom_max) | |
| 86 | + | img->zoom = zoom_max; | |
| 79 | 87 | ||
| 80 | 88 | if (img->scalemode == SCALE_DOWN && img->zoom > 1.0) | |
| 81 | 89 | img->zoom = 1.0; | |
| @@ -150,25 +158,14 @@ void img_render(img_t *img, win_t *win) { | |||
| 150 | 158 | win_draw(win); | |
| 151 | 159 | } | |
| 152 | 160 | ||
| 153 | − | int img_zoom(img_t *img, int d) { | |
| 154 | − | int ad, iz; | |
| 155 | − | float z; | |
| 156 | − | ||
| 161 | + | int img_zoom(img_t *img, float z) { | |
| 157 | 162 | if (!img) | |
| 158 | 163 | return 0; | |
| 159 | 164 | ||
| 160 | − | ad = ABS(d); | |
| 161 | − | iz = (int) (img->zoom * 1000.0) + d; | |
| 162 | − | if (iz % ad > ad / 2) | |
| 163 | − | iz += ad - iz % ad; | |
| 164 | − | else | |
| 165 | − | iz -= iz % ad; | |
| 166 | − | z = (float) iz / 1000.0; | |
| 167 | − | ||
| 168 | − | if (z * 100.0 < ZOOM_MIN) | |
| 169 | − | z = ZOOM_MIN / 100.0; | |
| 170 | − | else if (z * 100.0 > ZOOM_MAX) | |
| 171 | − | z = ZOOM_MAX / 100.0; | |
| 165 | + | if (z < zoom_min) | |
| 166 | + | z = zoom_min; | |
| 167 | + | else if (z > zoom_max) | |
| 168 | + | z = zoom_max; | |
| 172 | 169 | ||
| 173 | 170 | if (z != img->zoom) { | |
| 174 | 171 | img->x -= (img->w * z - img->w * img->zoom) / 2; | |
| @@ -181,9 +178,29 @@ int img_zoom(img_t *img, int d) { | |||
| 181 | 178 | } | |
| 182 | 179 | ||
| 183 | 180 | int img_zoom_in(img_t *img) { | |
| 184 | − | return img_zoom(img, 125); | |
| 181 | + | int i; | |
| 182 | + | ||
| 183 | + | if (!img) | |
| 184 | + | return 0; | |
| 185 | + | ||
| 186 | + | for (i = 1; i < zl_cnt; ++i) { | |
| 187 | + | if (zoom_levels[i] > img->zoom * 100.0) | |
| 188 | + | return img_zoom(img, zoom_levels[i] / 100.0); | |
| 189 | + | } | |
| 190 | + | ||
| 191 | + | return 0; | |
| 185 | 192 | } | |
| 186 | 193 | ||
| 187 | 194 | int img_zoom_out(img_t *img) { | |
| 188 | − | return img_zoom(img, -125); | |
| 195 | + | int i; | |
| 196 | + | ||
| 197 | + | if (!img) | |
| 198 | + | return 0; | |
| 199 | + | ||
| 200 | + | for (i = zl_cnt - 2; i >= 0; --i) { | |
| 201 | + | if (zoom_levels[i] < img->zoom * 100.0) | |
| 202 | + | return img_zoom(img, zoom_levels[i] / 100.0); | |
| 203 | + | } | |
| 204 | + | ||
| 205 | + | return 0; | |
| 189 | 206 | } | |