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/scenery.c | 6 ++--- src/scenes/world_map.c | 63 ++++++++++++++++++++++++++++++++------------------ src/world.c | 8 +++++-- 3 files changed, 49 insertions(+), 28 deletions(-) diff --git a/src/scenery.c b/src/scenery.c index 9e647d6..bf8afe6 100644 --- a/src/scenery.c +++ b/src/scenery.c @@ -10,8 +10,8 @@ static void update_render_path(world* world, boat_route *route) float percentage = route->current_point_duration/time_between_points; - vec2f start_pos = {area.x + (prev_point.x*area.w), area.y + (prev_point.y*area.h)}; - vec2f end_pos = {area.x + (next_point.x*area.w), area.y + (next_point.y*area.h)}; + vec2f start_pos = {area.x/zoom + (prev_point.x*area.w), area.y/zoom + (prev_point.y*area.h)}; + vec2f end_pos = {area.x/zoom + (next_point.x*area.w), area.y/zoom + (next_point.y*area.h)}; float currx = start_pos.x - (start_pos.x - end_pos.x)*percentage; float curry = start_pos.y - (start_pos.y - end_pos.y)*percentage; @@ -21,7 +21,7 @@ static void update_render_path(world* world, boat_route *route) float rad = atan2(end_pos.y-start_pos.y, end_pos.x-start_pos.x); gl_render_set_rotation(-rad); - renderer->render_image(img_boat, map_pos.x-3, map_pos.y-3, 6, 6); + renderer->render_image(img_boat, map_pos.x-3, map_pos.y-3, 3*zoom, 3*zoom); gl_render_set_rotation(0.0f); route->current_point_duration += (frame_delta*1000.0f)*world->simulation_speed; 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); } diff --git a/src/world.c b/src/world.c index bcff4e8..88ef7c6 100644 --- a/src/world.c +++ b/src/world.c @@ -589,8 +589,8 @@ world* world_create_new() static vec2f coords_to_px(platform_window* window, double lon, double lat) { vec2f extra = {9 * scale, 4 * scale}; - vec2f map_pos = {area.x + (float)(area.w * (180.0f + lon) / 360.0f), - area.y + (float)(area.h * (90.0f - lat) / 180.0f)}; + vec2f map_pos = {area.x/zoom + (float)(area.w * (180.0f + lon) / 360.0f), + area.y/zoom + (float)(area.h * (90.0f - lat) / 180.0f)}; map_pos.x += extra.x; map_pos.y += extra.y; return map_pos; @@ -1298,6 +1298,10 @@ static vec2f get_world_location_for_job(platform_window* window, world* world, a double lat = source->latitude + (dest->latitude - source->latitude) * progress_between_points; vec2f map_pos = coords_to_px(window, lon, lat); + map_pos.x *= zoom; + map_pos.y *= zoom; + map_pos.x += camera_x; + map_pos.y += camera_y; return map_pos; } else { -- cgit v1.2.3-70-g09d2