mouse scroll + comments
This commit is contained in:
parent
f49be0b90d
commit
bc621ff9c1
@ -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;
|
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());
|
let zoom = 2./window.single().width().min(window.single().height());
|
||||||
cmds.spawn((
|
cmds.spawn((
|
||||||
Camera2d,
|
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<Camera2d>>,
|
mut cam: Query<&mut Transform, With<Camera2d>>,
|
||||||
map_ui_entity: Query<Entity, With<ui::MapUIComponent>>,
|
map_ui_entity: Query<Entity, With<ui::MapUIComponent>>,
|
||||||
mouse_buttons: Res<ButtonInput<MouseButton>>,
|
mouse_buttons: Res<ButtonInput<MouseButton>>,
|
||||||
|
mut ev_scroll: EventReader<MouseWheel>,
|
||||||
touches: Res<Touches>,
|
touches: Res<Touches>,
|
||||||
window: Query<&Window, With<PrimaryWindow>>,
|
window: Query<&Window, With<PrimaryWindow>>,
|
||||||
mut pointers: ResMut<Pointers>,
|
mut pointers: ResMut<Pointers>,
|
||||||
@ -41,14 +42,6 @@ fn move_cam(
|
|||||||
}.map(|(pressed,new_pos, old_pos)| (pressed,new_pos,old_pos,id,hit_map))
|
}.map(|(pressed,new_pos, old_pos)| (pressed,new_pos,old_pos,id,hit_map))
|
||||||
).collect::<Vec<_>>();
|
).collect::<Vec<_>>();
|
||||||
let pressed_on_map = ps.iter().filter(|p| p.0 && p.4.contains_key(&map_ui_entity)).collect::<Vec<_>>();
|
let pressed_on_map = ps.iter().filter(|p| p.0 && p.4.contains_key(&map_ui_entity)).collect::<Vec<_>>();
|
||||||
// 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, _, _)| {
|
let old_midpoint = pressed_on_map.iter().fold(Vec2::ZERO, |acc, (_, _, old_pos, _, _)| {
|
||||||
acc + (old_pos/pressed_on_map.len() as f32)
|
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, _, _, _)| {
|
let new_midpoint = pressed_on_map.iter().fold(Vec2::ZERO, |acc, (_, new_pos, _, _, _)| {
|
||||||
acc + (new_pos/pressed_on_map.len() as f32)
|
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.x -= (new_midpoint.x - old_midpoint.x)*cam.scale.x;
|
||||||
cam.translation.y += (new_midpoint.y - old_midpoint.y)*cam.scale.y;
|
cam.translation.y += (new_midpoint.y - old_midpoint.y)*cam.scale.y;
|
||||||
|
|
||||||
|
// multiple fingers zoom
|
||||||
if pressed_on_map.len() > 1 {
|
if pressed_on_map.len() > 1 {
|
||||||
let old_d_to_midpoint = pressed_on_map.iter().fold(0., |acc, (_, _, old_pos, _, _)| {
|
let old_d_to_midpoint = pressed_on_map.iter().fold(0., |acc, (_, _, old_pos, _, _)| {
|
||||||
acc + (old_midpoint-old_pos).norm()
|
acc + (old_midpoint-old_pos).norm()
|
||||||
@ -68,10 +64,17 @@ fn move_cam(
|
|||||||
});
|
});
|
||||||
let zoom = new_d_to_midpoint/old_d_to_midpoint;
|
let zoom = new_d_to_midpoint/old_d_to_midpoint;
|
||||||
dbg!(zoom);
|
dbg!(zoom);
|
||||||
cam.scale.x /= zoom;
|
cam.scale /= zoom;
|
||||||
cam.scale.y /= 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 {
|
for (_, new_pos, _, id, _) in ps {
|
||||||
pointers.0.insert(*id, new_pos);
|
pointers.0.insert(*id, new_pos);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user