From d1f07943faf735daacceb2ab0b0a9e6af10ca8d3 Mon Sep 17 00:00:00 2001 From: OMGeeky Date: Mon, 14 Apr 2025 19:50:09 +0200 Subject: [PATCH] change zoom to use a canvas resource that stores the zoom this should improve quality, since it does not scale up everything on screen and might preserve higher resolution when zoomed in? --- src/main.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7dde00c..2c12e3f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,20 +32,25 @@ mod camera { window.present_mode = bevy::window::PresentMode::AutoNoVsync; } + #[derive(Resource)] + pub struct Canvas { + pub zoom: f32, + } pub struct CameraPlugin; impl Plugin for CameraPlugin { fn build(&self, app: &mut App) { app.insert_resource(CameraSettings { zoom_speed: 0.05, - orthographic_zoom_range: 0.01..1000.0, + orthographic_zoom_range: 0.1..20.0, }) .add_systems(Startup, (change_window_mode, setup_camera)) - .add_systems(Update, draw_cursor) + .add_systems(PostUpdate, draw_cursor) .add_systems(Update, zoom) .add_systems(Update, handle_pan.run_if(input_pressed(MouseButton::Right))); } } fn setup_camera(mut commands: Commands) { + commands.insert_resource(Canvas { zoom: 1.0 }); commands.spawn(Camera2d); } fn draw_cursor( @@ -61,28 +66,26 @@ mod camera { } fn handle_pan( - mut camera: Single<(&mut Transform, &OrthographicProjection), With>, + mut camera: Single<(&mut Transform), With>, move_event: Res, ) { - let (camera, camera_projection) = &mut *camera; - camera.translation.x -= move_event.delta.x * camera_projection.scale; - camera.translation.y += move_event.delta.y * camera_projection.scale; - println!("{:?} {:?}", camera_projection.scale, move_event.delta); + camera.translation.x -= move_event.delta.x; + camera.translation.y += move_event.delta.y; } fn zoom( - mut camera: Single<&mut OrthographicProjection, With>, + mut canvas: ResMut, camera_settings: Res, mouse_wheel_input: Res, ) { // We want scrolling up to zoom in, decreasing the scale, so we negate the delta. - let delta_zoom = -mouse_wheel_input.delta.y * camera_settings.zoom_speed; + let delta_zoom = mouse_wheel_input.delta.y * camera_settings.zoom_speed; // When changing scales, logarithmic changes are more intuitive. // To get this effect, we add 1 to the delta, so that a delta of 0 // results in no multiplicative effect, positive values result in a multiplicative increase, // and negative values result in multiplicative decreases. let multiplicative_zoom = 1. + delta_zoom; - camera.scale = (camera.scale * multiplicative_zoom).clamp( + canvas.zoom = (canvas.zoom * multiplicative_zoom).clamp( camera_settings.orthographic_zoom_range.start, camera_settings.orthographic_zoom_range.end, );