From bc621ff9c1a8f96d1c3996a667f67c33fbffef35 Mon Sep 17 00:00:00 2001 From: Arkitu <85173315+Arkitu@users.noreply.github.com> Date: Fri, 27 Dec 2024 19:03:41 +0100 Subject: [PATCH 1/6] mouse scroll + comments --- src/camera.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index d7a264c..381ec6d 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -1,4 +1,4 @@ -use bevy::{math::NormedVectorSpace, picking::{focus::HoverMap, pointer::PointerId}, prelude::*, utils::HashMap, window::PrimaryWindow}; +use bevy::{input::mouse::MouseWheel, math::NormedVectorSpace, picking::{focus::HoverMap, pointer::PointerId}, prelude::*, utils::{dbg, HashMap}, window::PrimaryWindow}; use crate::ui; @@ -15,7 +15,7 @@ fn setup(mut cmds: Commands, window: Query<&Window>) { let zoom = 2./window.single().width().min(window.single().height()); cmds.spawn(( Camera2d, - Transform::from_scale(Vec3::new(zoom, zoom, 1.)) + Transform::from_scale(Vec3::new(zoom, zoom, zoom)) )); } @@ -26,6 +26,7 @@ fn move_cam( mut cam: Query<&mut Transform, With>, map_ui_entity: Query>, mouse_buttons: Res>, + mut ev_scroll: EventReader, touches: Res, window: Query<&Window, With>, mut pointers: ResMut, @@ -41,14 +42,6 @@ fn move_cam( }.map(|(pressed,new_pos, old_pos)| (pressed,new_pos,old_pos,id,hit_map)) ).collect::>(); let pressed_on_map = ps.iter().filter(|p| p.0 && p.4.contains_key(&map_ui_entity)).collect::>(); - // let mut movement = Vec2::ZERO; - // for (pressed, new_pos, old_pos, id, hit_map) in pressed_on_map.iter() { - // let delta = new_pos - old_pos; - // cam.scale += delta.dot(movement)/delta.norm(); - // movement.x -= delta.x*cam.scale.x/pressed_num as f32; - // movement.y += delta.y*cam.scale.y/pressed_num as f32; - // pointers.0.insert(**id, *new_pos); - // } let old_midpoint = pressed_on_map.iter().fold(Vec2::ZERO, |acc, (_, _, old_pos, _, _)| { acc + (old_pos/pressed_on_map.len() as f32) @@ -56,9 +49,12 @@ fn move_cam( let new_midpoint = pressed_on_map.iter().fold(Vec2::ZERO, |acc, (_, new_pos, _, _, _)| { acc + (new_pos/pressed_on_map.len() as f32) }); + + // 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; + // multiple fingers zoom if pressed_on_map.len() > 1 { let old_d_to_midpoint = pressed_on_map.iter().fold(0., |acc, (_, _, old_pos, _, _)| { acc + (old_midpoint-old_pos).norm() @@ -68,10 +64,17 @@ fn move_cam( }); let zoom = new_d_to_midpoint/old_d_to_midpoint; dbg!(zoom); - cam.scale.x /= zoom; - cam.scale.y /= zoom; + cam.scale /= zoom; } + // mouse scroll zoom + 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); + dbg!(cam.scale); + } + + // update cached pointer positions for (_, new_pos, _, id, _) in ps { pointers.0.insert(*id, new_pos); } From 62dd03795708e43059af232febd6ab0ec063124f Mon Sep 17 00:00:00 2001 From: Arkitu <85173315+Arkitu@users.noreply.github.com> Date: Thu, 9 Jan 2025 20:56:50 +0100 Subject: [PATCH 2/6] custom picking for map --- Cargo.lock | 6 +++--- Cargo.toml | 2 +- src/camera.rs | 33 ++++++++++++++++++++++----------- src/map.rs | 25 +++++++++++++++++-------- src/map/picking.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/ui.rs | 27 ++++++++++++++++++--------- 6 files changed, 99 insertions(+), 32 deletions(-) create mode 100644 src/map/picking.rs diff --git a/Cargo.lock b/Cargo.lock index 4fccc48..616a3a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -338,9 +338,9 @@ dependencies = [ [[package]] name = "bevy-inspector-egui" -version = "0.28.0" +version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd64580f4496ed987c6231c6a7d833068914331a9084bf5a3dd9dcbc66fd8a73" +checksum = "36172627eb6fd8586600972bcbba2880ed6f59e4e243dcf2ed7ff68d987577ce" dependencies = [ "bevy-inspector-egui-derive", "bevy_app", @@ -2402,7 +2402,7 @@ dependencies = [ "vec_map", "wasm-bindgen", "web-sys", - "windows 0.52.0", + "windows 0.58.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 1c7bdcf..691bbc5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,7 @@ bevy = { version = "0.15", default-features = false, features = [ "bevy_gltf", "bevy_mesh_picking_backend", "bevy_pbr", - "bevy_picking", + # "bevy_picking", "bevy_render", "bevy_scene", "bevy_sprite", diff --git a/src/camera.rs b/src/camera.rs index 381ec6d..2c12fa2 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -1,4 +1,6 @@ -use bevy::{input::mouse::MouseWheel, math::NormedVectorSpace, picking::{focus::HoverMap, pointer::PointerId}, prelude::*, utils::{dbg, HashMap}, window::PrimaryWindow}; +use std::time::Duration; + +use bevy::{input::mouse::MouseWheel, math::NormedVectorSpace, picking::{focus::HoverMap, pointer::PointerId}, prelude::*, utils::HashMap, window::PrimaryWindow}; use crate::ui; @@ -20,7 +22,7 @@ fn setup(mut cmds: Commands, window: Query<&Window>) { } #[derive(Resource, Default)] -struct Pointers(HashMap); +struct Pointers(HashMap)>); fn move_cam( mut cam: Query<&mut Transform, With>, @@ -30,18 +32,24 @@ fn move_cam( touches: Res, window: Query<&Window, With>, mut pointers: ResMut, - hover_map: Res + hover_map: Res, + time: Res