From beb524b97fddc04ca7d52d647aab0dcd16d29cdb Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Thu, 7 May 2020 15:53:27 +0200 Subject: [PATCH] fix resize_to_fit scaling --- app/src/input_manager.c | 5 +++-- app/src/screen.c | 23 +++++++++++++---------- app/src/screen.h | 7 +++++++ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/app/src/input_manager.c b/app/src/input_manager.c index 1bb32920..e8c8d68e 100644 --- a/app/src/input_manager.c +++ b/app/src/input_manager.c @@ -527,8 +527,9 @@ input_manager_process_mouse_button(struct input_manager *im, // double-click on black borders resize to fit the device screen if (event->button == SDL_BUTTON_LEFT && event->clicks == 2) { - int x = event->x; - int y = event->y; + int32_t x = event->x; + int32_t y = event->y; + screen_hidpi_scale_coords(im->screen, &x, &y); SDL_Rect *r = &im->screen->rect; bool outside = x < r->x || x >= r->x + r->w || y < r->y || y >= r->y + r->h; diff --git a/app/src/screen.c b/app/src/screen.c index 51e7a082..efa6c9ff 100644 --- a/app/src/screen.c +++ b/app/src/screen.c @@ -611,16 +611,7 @@ screen_convert_to_frame_coords(struct screen *screen, int32_t x, int32_t y) { int32_t w = screen->content_size.width; int32_t h = screen->content_size.height; - // take the HiDPI scaling (dw/ww and dh/wh) into account - int ww, wh, dw, dh; - SDL_GetWindowSize(screen->window, &ww, &wh); - SDL_GL_GetDrawableSize(screen->window, &dw, &dh); - - LOGI("window_size = %dx%d, drawable_size = %dx%d", ww, wh, dw, dh); - - // scale for HiDPI (64 bits for intermediate multiplications) - x = (int64_t) x * dw / ww; - y = (int64_t) y * dh / wh; + screen_hidpi_scale_coords(screen, &x, &y); x = (int64_t) (x - screen->rect.x) * w / screen->rect.w; y = (int64_t) (y - screen->rect.y) * h / screen->rect.h; @@ -653,3 +644,15 @@ screen_convert_to_frame_coords(struct screen *screen, int32_t x, int32_t y) { } return result; } + +void +screen_hidpi_scale_coords(struct screen *screen, int32_t *x, int32_t *y) { + // take the HiDPI scaling (dw/ww and dh/wh) into account + int ww, wh, dw, dh; + SDL_GetWindowSize(screen->window, &ww, &wh); + SDL_GL_GetDrawableSize(screen->window, &dw, &dh); + + // scale for HiDPI (64 bits for intermediate multiplications) + *x = (int64_t) *x * dw / ww; + *y = (int64_t) *y * dh / wh; +} diff --git a/app/src/screen.h b/app/src/screen.h index 89cefd9e..17fd2477 100644 --- a/app/src/screen.h +++ b/app/src/screen.h @@ -127,4 +127,11 @@ screen_handle_window_event(struct screen *screen, const SDL_WindowEvent *event); struct point screen_convert_to_frame_coords(struct screen *screen, int32_t x, int32_t y); +// Convert coordinates from window to drawable. +// Events are expressed in window coordinates, but content is expressed in +// drawable coordinates. They are the same if HiDPI scaling is 1, but differ +// otherwise. +void +screen_hidpi_scale_coords(struct screen *screen, int32_t *x, int32_t *y); + #endif