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?
This commit is contained in:
OMGeeky
2025-04-14 19:50:09 +02:00
parent 9f6d5eb9fa
commit d1f07943fa

View File

@@ -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<Camera2d>>,
mut camera: Single<(&mut Transform), With<Camera2d>>,
move_event: Res<AccumulatedMouseMotion>,
) {
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<Camera2d>>,
mut canvas: ResMut<Canvas>,
camera_settings: Res<CameraSettings>,
mouse_wheel_input: Res<AccumulatedMouseScroll>,
) {
// 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,
);