zoom with bevy_picking
This commit is contained in:
parent
c1250bbb7f
commit
3e5e4d5254
@ -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
|
||||
|
@ -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::<Pointers>();
|
||||
// .init_resource::<Pointers>();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,7 +10,7 @@ pub fn main() {
|
||||
App::new()
|
||||
.add_plugins((
|
||||
DefaultPlugins,
|
||||
WorldInspectorPlugin::default(),
|
||||
// WorldInspectorPlugin::default(),
|
||||
camera::Plugin,
|
||||
map::Plugin,
|
||||
ui::Plugin,
|
||||
|
30
src/ui.rs
30
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<Camera2d>>,
|
||||
mut ev_scroll: EventReader<MouseWheel>,
|
||||
hover_map: Res<HoverMap>,
|
||||
window: Query<&Window, With<PrimaryWindow>>,
|
||||
map_ui_id: Query<Entity, With<MapUIComponent>>
|
||||
) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user