From 3e5e4d52548d29ef949d492650d67d5569629c74 Mon Sep 17 00:00:00 2001 From: Arkitu <85173315+Arkitu@users.noreply.github.com> Date: Sun, 12 Jan 2025 13:48:13 +0100 Subject: [PATCH] zoom with bevy_picking --- Cargo.toml | 3 +++ src/camera.rs | 4 ++-- src/lib.rs | 2 +- src/ui.rs | 30 ++++++++++++++++++++++++++++-- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 691bbc5..a7e54b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,6 +84,9 @@ android_logger = "0.14" [profile.dev] opt-level = 1 +[profile.release] +debug = true + # Enable a large amount of optimization in the dev profile for dependencies. [profile.dev.package."*"] opt-level = 3 diff --git a/src/camera.rs b/src/camera.rs index d437801..fae1f10 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -7,9 +7,9 @@ use crate::ui; pub struct Plugin; impl bevy::prelude::Plugin for Plugin { fn build(&self, app: &mut App) { - app.add_systems(Startup, setup) + app.add_systems(Startup, setup); // .add_systems(Update, move_cam) - .init_resource::(); + // .init_resource::(); } } diff --git a/src/lib.rs b/src/lib.rs index cc7a1f7..92b71a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,7 +10,7 @@ pub fn main() { App::new() .add_plugins(( DefaultPlugins, - WorldInspectorPlugin::default(), + // WorldInspectorPlugin::default(), camera::Plugin, map::Plugin, ui::Plugin, diff --git a/src/ui.rs b/src/ui.rs index 676a3c0..c4c5031 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,13 +1,14 @@ use std::collections::BTreeMap; -use bevy::{math::VectorSpace, picking::pointer::PointerId, prelude::*, utils::HashMap}; +use bevy::{input::mouse::MouseWheel, math::{NormedVectorSpace, VectorSpace}, picking::{focus::HoverMap, pointer::PointerId}, prelude::*, utils::HashMap, window::PrimaryWindow}; use crate::map::{self, MapMarker}; pub struct Plugin; impl bevy::prelude::Plugin for Plugin { fn build(&self, app: &mut App) { - app.add_systems(Startup, setup); + app.add_systems(Startup, setup) + .add_systems(Update, zoom_with_scroll); } } @@ -58,13 +59,38 @@ fn setup( let mut ptrs = ptrs.get_mut(event.target).unwrap(); let old_midpoint = ptrs.0.values().fold(Vec2::ZERO, |acc, pos| acc + (pos/ptrs.0.len() as f32)); + let old_d_to_midpoint = ptrs.0.values().fold(0., |acc, pos| acc + (old_midpoint-pos).norm()); ptrs.0.insert(event.pointer_id, event.pointer_location.position); let new_midpoint = ptrs.0.values().fold(Vec2::ZERO, |acc, pos| acc + (pos/ptrs.0.len() as f32)); + let new_d_to_midpoint = ptrs.0.values().fold(0., |acc, pos| acc + (new_midpoint-pos).norm()); + // move camera cam.translation.x -= (new_midpoint.x - old_midpoint.x)*cam.scale.x; cam.translation.y += (new_midpoint.y - old_midpoint.y)*cam.scale.y; + + if ptrs.0.len() > 1 { + cam.scale *= old_d_to_midpoint/new_d_to_midpoint; + } } } ); // Spawn all ui elements as children of this one +} + +fn zoom_with_scroll( + mut cam: Query<&mut Transform, With>, + mut ev_scroll: EventReader, + hover_map: Res, + window: Query<&Window, With>, + map_ui_id: Query> +) { + let map_ui_id = map_ui_id.single(); + if hover_map.get(&PointerId::Mouse).and_then(|hovered_ids| hovered_ids.get(&map_ui_id)).is_some() { + let window = window.single(); + let mut cam = cam.single_mut(); + for ev in ev_scroll.read() { + let scale = (cam.scale.x-(ev.y*0.1/window.width().min(window.height()))).clamp(0.0001, 2./window.width().min(window.height())); + cam.scale = Vec3::new(scale, scale, scale); + } + } } \ No newline at end of file