Reduce code verbosity.

This introduces the function `platform_get_cursor_position_scaled` as a common means of getting the cursor position compensated for window scaling.
This commit is contained in:
Aaron van Geffen 2016-07-30 21:42:21 +02:00
parent 8ac1b79799
commit 29b0b4885e
3 changed files with 23 additions and 30 deletions

View File

@ -1447,13 +1447,11 @@ void window_rotate_camera(rct_window *w, int direction)
void window_viewport_get_map_coords_by_cursor(rct_window *w, sint16 *map_x, sint16 *map_y, sint16 *offset_x, sint16 *offset_y)
{
// Get mouse position to offset against.
int mouse_x, mouse_y;
platform_get_cursor_position(&mouse_x, &mouse_y);
// Compensate for window scaling.
mouse_x = (int) ceilf(mouse_x / gConfigGeneral.window_scale);
mouse_y = (int) ceilf(mouse_y / gConfigGeneral.window_scale);
platform_get_cursor_position_scaled(&mouse_x, &mouse_y);
// Compute map coordinate by mouse position.
get_map_coordinates_from_pos(mouse_x, mouse_y, VIEWPORT_INTERACTION_MASK_NONE, map_x, map_y, NULL, NULL, NULL);
// Get viewport coordinates centring around the tile.
@ -1461,20 +1459,13 @@ void window_viewport_get_map_coords_by_cursor(rct_window *w, sint16 *map_x, sint
int dest_x, dest_y;
center_2d_coordinates(*map_x, *map_y, base_height, &dest_x, &dest_y, w->viewport);
// Rebase mouse position onto centre of window.
int rebased_x = (w->width >> 1) - mouse_x,
rebased_y = (w->height >> 1) - mouse_y;
// Rebase mouse position onto centre of window, and compensate for zoom level.
int rebased_x = ((w->width >> 1) - mouse_x) << w->viewport->zoom,
rebased_y = ((w->height >> 1) - mouse_y) << w->viewport->zoom;
// Compensate for zoom level.
rebased_x <<= w->viewport->zoom;
rebased_y <<= w->viewport->zoom;
// Apply offset to the viewport.
int new_x = dest_x + rebased_x,
new_y = dest_y + rebased_y;
*offset_x = (w->saved_view_x - new_x) << w->viewport->zoom,
*offset_y = (w->saved_view_y - new_y) << w->viewport->zoom;
// Compute cursor offset relative to tile.
*offset_x = (w->saved_view_x - (dest_x + rebased_x)) << w->viewport->zoom;
*offset_y = (w->saved_view_y - (dest_y + rebased_y)) << w->viewport->zoom;
}
void window_viewport_centre_tile_around_cursor(rct_window *w, sint16 map_x, sint16 map_y, sint16 offset_x, sint16 offset_y)
@ -1486,19 +1477,11 @@ void window_viewport_centre_tile_around_cursor(rct_window *w, sint16 map_x, sint
// Get mouse position to offset against.
int mouse_x, mouse_y;
platform_get_cursor_position(&mouse_x, &mouse_y);
platform_get_cursor_position_scaled(&mouse_x, &mouse_y);
// Compensate for window scaling.
mouse_x = (int) ceilf(mouse_x / gConfigGeneral.window_scale);
mouse_y = (int) ceilf(mouse_y / gConfigGeneral.window_scale);
// Rebase mouse position onto centre of window.
int rebased_x = (w->width >> 1) - mouse_x,
rebased_y = (w->height >> 1) - mouse_y;
// Compensate for zoom level.
rebased_x <<= w->viewport->zoom;
rebased_y <<= w->viewport->zoom;
// Rebase mouse position onto centre of window, and compensate for zoom level.
int rebased_x = ((w->width >> 1) - mouse_x) << w->viewport->zoom,
rebased_y = ((w->height >> 1) - mouse_y) << w->viewport->zoom;
// Apply offset to the viewport.
w->saved_view_x = dest_x + rebased_x + (offset_x >> w->viewport->zoom);

View File

@ -166,6 +166,7 @@ bool platform_file_delete(const utf8 *path);
void platform_hide_cursor();
void platform_show_cursor();
void platform_get_cursor_position(int *x, int *y);
void platform_get_cursor_position_scaled(int *x, int *y);
void platform_set_cursor_position(int x, int y);
unsigned int platform_get_ticks();
void platform_resolve_user_data_path();

View File

@ -783,6 +783,15 @@ void platform_get_cursor_position(int *x, int *y)
SDL_GetMouseState(x, y);
}
void platform_get_cursor_position_scaled(int *x, int *y)
{
platform_get_cursor_position(x, y);
// Compensate for window scaling.
*x = (int) ceilf(*x / gConfigGeneral.window_scale);
*y = (int) ceilf(*y / gConfigGeneral.window_scale);
}
void platform_set_cursor_position(int x, int y)
{
SDL_WarpMouseInWindow(NULL, x, y);