aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.c10
-rw-r--r--src/viewport.c53
-rw-r--r--src/viewport.h4
3 files changed, 37 insertions, 30 deletions
diff --git a/src/main.c b/src/main.c
index 34100aa..7f4b415 100644
--- a/src/main.c
+++ b/src/main.c
@@ -250,10 +250,14 @@ int main(int argc, char** argv)
case SDLK_RIGHT: imv_navigator_next_path(&nav); break;
case SDLK_EQUALS:
case SDLK_i:
- case SDLK_UP: imv_viewport_zoom(&view, &img, KBD, 1); break;
+ case SDLK_UP:
+ imv_viewport_zoom(&view, &img, IMV_ZOOM_KEYBOARD, 1);
+ break;
case SDLK_MINUS:
case SDLK_o:
- case SDLK_DOWN: imv_viewport_zoom(&view, &img, KBD, -1); break;
+ case SDLK_DOWN:
+ imv_viewport_zoom(&view, &img, IMV_ZOOM_KEYBOARD, -1);
+ break;
case SDLK_a: imv_viewport_scale_to_actual(&view, &img);break;
case SDLK_r: imv_viewport_scale_to_window(&view, &img);break;
case SDLK_c: imv_viewport_center(&view, &img); break;
@@ -269,7 +273,7 @@ int main(int argc, char** argv)
}
break;
case SDL_MOUSEWHEEL:
- imv_viewport_zoom(&view, &img, MOUSE, e.wheel.y);
+ imv_viewport_zoom(&view, &img, IMV_ZOOM_MOUSE, e.wheel.y);
break;
case SDL_MOUSEMOTION:
if(e.motion.state & SDL_BUTTON_LMASK) {
diff --git a/src/viewport.c b/src/viewport.c
index 3c965d9..0cb3c14 100644
--- a/src/viewport.c
+++ b/src/viewport.c
@@ -71,53 +71,56 @@ void imv_viewport_move(struct imv_viewport *view, int x, int y)
void imv_viewport_zoom(struct imv_viewport *view, const struct imv_image *img, enum imv_zoom_source src, int amount)
{
- double prevScale = view->scale;
+ double prev_scale = view->scale;
int x, y, ww, wh;
SDL_GetWindowSize(view->window, &ww, &wh);
- if(src == MOUSE) {
+ /* x and y cordinates are relative to the image */
+ if(src == IMV_ZOOM_MOUSE) {
SDL_GetMouseState(&x, &y);
- /* Translate mouse coordinates to projected coordinates */
x -= view->x;
y -= view->y;
} else {
- x = 0;
- y = 0;
+ x = view->scale * img->width / 2;
+ y = view->scale * img->height / 2;
}
- const int scaledWidth = img->width * view->scale;
- const int scaledHeight = img->height * view->scale;
- const int ic_x = view->x + scaledWidth/2;
- const int ic_y = view->y + scaledHeight/2;
+ const int scaled_width = img->width * view->scale;
+ const int scaled_height = img->height * view->scale;
+ const int ic_x = view->x + scaled_width/2;
+ const int ic_y = view->y + scaled_height/2;
const int wc_x = ww/2;
const int wc_y = wh/2;
- view->scale += (view->scale / img->width) * amount * 20;
- const int min_scale = 0.01;
- const int max_scale = 100;
- if(view->scale > max_scale)
+ double delta_scale = 0.04 * ww * amount / img->width;
+ view->scale += delta_scale;
+
+ const double min_scale = 0.1;
+ const double max_scale = 100;
+ if(view->scale > max_scale) {
view->scale = max_scale;
- else if (view->scale < min_scale)
+ } else if (view->scale < min_scale) {
view->scale = min_scale;
+ }
- if(view->scale < prevScale) {
- if(scaledWidth < ww) {
- x = scaledWidth/2 - (ic_x - wc_x)*2;
+ if(view->scale < prev_scale) {
+ if(scaled_width < ww) {
+ x = scaled_width/2 - (ic_x - wc_x)*2;
}
- if(scaledHeight < wh) {
- y = scaledHeight/2 - (ic_y - wc_y)*2;
+ if(scaled_height < wh) {
+ y = scaled_height/2 - (ic_y - wc_y)*2;
}
} else {
- if(scaledWidth < ww) {
- x = scaledWidth/2;
+ if(scaled_width < ww) {
+ x = scaled_width/2;
}
- if(scaledHeight < wh) {
- y = scaledHeight/2;
+ if(scaled_height < wh) {
+ y = scaled_height/2;
}
}
- const double changeX = x - (x * (view->scale / prevScale));
- const double changeY = y - (y * (view->scale / prevScale));
+ const double changeX = x - (x * (view->scale / prev_scale));
+ const double changeY = y - (y * (view->scale / prev_scale));
view->x += changeX;
view->y += changeY;
diff --git a/src/viewport.h b/src/viewport.h
index 462e4e3..096f17a 100644
--- a/src/viewport.h
+++ b/src/viewport.h
@@ -32,8 +32,8 @@ struct imv_viewport {
};
enum imv_zoom_source {
- MOUSE,
- KBD
+ IMV_ZOOM_MOUSE,
+ IMV_ZOOM_KEYBOARD
};
void imv_init_viewport(struct imv_viewport *view, SDL_Window *window);