From f414ecce34a507069b955f974f423eede6b2006a Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Mon, 25 Nov 2024 20:00:26 +0100 Subject: fix zooming and added map panning --- src/scenes/world_map.c | 63 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 23 deletions(-) (limited to 'src/scenes') diff --git a/src/scenes/world_map.c b/src/scenes/world_map.c index 4f1e20e..ac0d3b2 100644 --- a/src/scenes/world_map.c +++ b/src/scenes/world_map.c @@ -993,8 +993,11 @@ static void world_handle_scroll(platform_window* window) zoom-=0.1f; changed = true; } - if (zoom < 1.0f) zoom = 1.0f; - else if (zoom > 2.0f) zoom = 2.0f; + if (zoom < 1.5f) zoom = 1.5f; + else if (zoom > 3.0f) zoom = 3.0f; + + float center_y = -((area.h * zoom) - area.h) / 8; + float center_x = -((area.w * zoom) - area.w) / 2; if (changed && prev_zoom != zoom) { @@ -1002,19 +1005,11 @@ static void world_handle_scroll(platform_window* window) float cursor_offset_y = (((_global_mouse.y) - (float)area.h / 2) / area.h); if (zoom < prev_zoom) { - float center_y = -((area.h * zoom) - area.h) / 2; - float center_x = -((area.w * zoom) - area.w) / 2; - - float diffy = center_y - camera_y; - float diffx = center_x - camera_x; - - //camera_x -= (camera_x - center_x)/150; - //camera_y -= (camera_y - center_y)/150; + int space_lost_x = ( (area.w * prev_zoom) - (area.w * zoom) ); + camera_x += space_lost_x/2; - //camera_x = center_x; - //camera_y = center_y; - - printf("%f %f\n", diffx, diffy); + int space_lost_y = ( (area.h * prev_zoom) - (area.h * zoom) ); + camera_y += space_lost_y/2; } else { int space_lost_x = ( (area.w * zoom) - (area.w * prev_zoom) ); @@ -1023,18 +1018,40 @@ static void world_handle_scroll(platform_window* window) int space_lost_y = ( (area.h * zoom) - (area.h * prev_zoom) ); camera_y -= space_lost_y/2 + (cursor_offset_y*150); } + } + + static bool is_dragging = false; + static s32 drag_startx = 0; + static s32 drag_starty = 0; + static s32 camera_start_x = 0; + static s32 camera_start_y = 0; + if (is_right_down() && window->has_focus) + { + if (!is_dragging) { + is_dragging = true; + drag_startx = _global_mouse.x; + drag_starty = _global_mouse.y; + camera_start_x = camera_x; + camera_start_y = camera_y; + } - vec4 area = camera_get_target_rectangle(window); + s32 diffx = _global_mouse.x - drag_startx; + s32 diffy = _global_mouse.y - drag_starty; + camera_x = camera_start_x + diffx; + camera_y = camera_start_y + diffy; + } + else { + is_dragging = false; + } - if (camera_x > 0.0f) camera_x = 0.0f; - if (camera_y > 0.0f) camera_y = 0.0f; + if (camera_x > 0.0f) camera_x = 0.0f; + if (camera_y > 0.0f) camera_y = 0.0f; - float max_camera_offset_y = -((area.h * zoom) - area.h); - float max_camera_offset_x = -((area.w * zoom) - area.w); + float max_camera_offset_y = -((area.h * zoom) - area.h); + float max_camera_offset_x = -((area.w * zoom) - area.w); - if (camera_y < max_camera_offset_y) camera_y = max_camera_offset_y; - if (camera_x < max_camera_offset_x) camera_x = max_camera_offset_x; - } + if (camera_y < max_camera_offset_y) camera_y = max_camera_offset_y; + if (camera_x < max_camera_offset_x) camera_x = max_camera_offset_x; } void world_map_scene_render(platform_window* window) @@ -1087,7 +1104,7 @@ void world_map_scene_render(platform_window* window) renderer->set_render_depth(0); vec4 area = camera_get_target_rectangle(window); - renderer->render_rectangle(area.x, area.y, area.w, area.h, COLOR_WORLD_MAP_BACKGROUND); + renderer->render_rectangle(area.x + camera_x, area.y + camera_y, area.w*zoom, area.h*zoom, COLOR_WORLD_MAP_BACKGROUND); renderer->render_image(img_world_map, area.x + camera_x, area.y + camera_y, area.w*zoom, area.h*zoom); } -- cgit v1.2.3-70-g09d2