From 4eaa1c89aefb4ebb0cc5aa40613a555e6d250ed0 Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Sat, 16 Feb 2019 17:54:22 +0000 Subject: Add customisable image resizing behaviour Two new flags: -w and -W enable resizing and recentering, respectively. Also add an 'autoresize' config variable to control this behaviour. Fixes #122 --- src/imv.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/imv.c b/src/imv.c index 2af5878..29e3a20 100644 --- a/src/imv.c +++ b/src/imv.c @@ -53,6 +53,13 @@ enum background_type { BACKGROUND_TYPE_COUNT }; +/* window behaviour on image change */ +enum resize_mode { + RESIZE_NONE, /* do nothing */ + RESIZE_ONLY, /* resize to fit the new image */ + RESIZE_CENTER /* resize to fit the new image and recenter */ +}; + struct backend_chain { const struct imv_backend *backend; struct backend_chain *next; @@ -84,6 +91,10 @@ struct imv { /* dirty state flags */ bool need_redraw; bool need_rescale; + bool need_resize; + + /* mode for resizing the window on image change */ + enum resize_mode resize_mode; /* traverse sub-directories for more images */ bool recursive_load; @@ -555,6 +566,26 @@ static bool parse_upscaling_method(struct imv *imv, const char *method) return false; } +static bool parse_resizing_mode(struct imv *imv, const char *method) +{ + if (!strcmp(method, "none")) { + imv->upscaling_method = RESIZE_NONE; + return true; + } + + if (!strcmp(method, "resize")) { + imv->upscaling_method = RESIZE_ONLY; + return true; + } + + if (!strcmp(method, "recenter")) { + imv->upscaling_method = RESIZE_CENTER; + return true; + } + + return false; +} + static int load_paths_from_stdin(void *data) { struct imv *imv = data; @@ -613,11 +644,13 @@ bool imv_parse_args(struct imv *imv, int argc, char **argv) int o; - while((o = getopt(argc, argv, "frdxhlu:s:n:b:t:")) != -1) { + while((o = getopt(argc, argv, "frdwWxhlu:s:n:b:t:")) != -1) { switch(o) { case 'f': imv->fullscreen = true; break; case 'r': imv->recursive_load = true; break; case 'd': imv->overlay_enabled = true; break; + case 'w': imv->resize_mode = RESIZE_ONLY; break; + case 'W': imv->resize_mode = RESIZE_CENTER; break; case 'x': imv->loop_input = false; break; case 'l': imv->list_files_at_exit = true; break; case 'n': imv->starting_path = optarg; break; @@ -986,6 +1019,17 @@ static void handle_new_image(struct imv *imv, struct imv_bitmap *bitmap, int fra imv_bitmap_free(bitmap); imv->need_redraw = true; imv->need_rescale = true; + /* If autoresizing on every image is enabled, make sure we do so */ + if (imv->resize_mode != RESIZE_NONE) { + imv->need_resize = true; + } + if (imv->need_resize) { + imv->need_resize = false; + SDL_SetWindowSize(imv->window, imv->current_image.width, imv->current_image.height); + if (imv->resize_mode == RESIZE_CENTER) { + SDL_SetWindowPosition(imv->window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); + } + } imv->loading = false; imv->next_frame_due = frametime ? SDL_GetTicks() + frametime : 0; imv->next_frame_duration = 0; @@ -1282,6 +1326,10 @@ static int handle_ini_value(void *user, const char *section, const char *name, return 1; } + if(!strcmp(name, "autoresize")) { + return parse_resizing_mode(imv, value); + } + if(!strcmp(name, "upscaling_method")) { return parse_upscaling_method(imv, value); } -- cgit v1.2.3