zoom with bevy_picking
This commit is contained in:
parent
c1250bbb7f
commit
3e5e4d5254
@ -84,6 +84,9 @@ android_logger = "0.14"
|
|||||||
[profile.dev]
|
[profile.dev]
|
||||||
opt-level = 1
|
opt-level = 1
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
debug = true
|
||||||
|
|
||||||
# Enable a large amount of optimization in the dev profile for dependencies.
|
# Enable a large amount of optimization in the dev profile for dependencies.
|
||||||
[profile.dev.package."*"]
|
[profile.dev.package."*"]
|
||||||
opt-level = 3
|
opt-level = 3
|
||||||
|
@ -7,9 +7,9 @@ use crate::ui;
|
|||||||
pub struct Plugin;
|
pub struct Plugin;
|
||||||
impl bevy::prelude::Plugin for Plugin {
|
impl bevy::prelude::Plugin for Plugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_systems(Startup, setup)
|
app.add_systems(Startup, setup);
|
||||||
// .add_systems(Update, move_cam)
|
// .add_systems(Update, move_cam)
|
||||||
.init_resource::<Pointers>();
|
// .init_resource::<Pointers>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ pub fn main() {
|
|||||||
App::new()
|
App::new()
|
||||||
.add_plugins((
|
.add_plugins((
|
||||||
DefaultPlugins,
|
DefaultPlugins,
|
||||||
WorldInspectorPlugin::default(),
|
// WorldInspectorPlugin::default(),
|
||||||
camera::Plugin,
|
camera::Plugin,
|
||||||
map::Plugin,
|
map::Plugin,
|
||||||
ui::Plugin,
|
ui::Plugin,
|
||||||
|
30
src/ui.rs
30
src/ui.rs
@ -1,13 +1,14 @@
|
|||||||
use std::collections::BTreeMap;
|
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};
|
use crate::map::{self, MapMarker};
|
||||||
|
|
||||||
pub struct Plugin;
|
pub struct Plugin;
|
||||||
impl bevy::prelude::Plugin for Plugin {
|
impl bevy::prelude::Plugin for Plugin {
|
||||||
fn build(&self, app: &mut App) {
|
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 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_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);
|
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_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.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;
|
||||||
|
|
||||||
|
if ptrs.0.len() > 1 {
|
||||||
|
cam.scale *= old_d_to_midpoint/new_d_to_midpoint;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
// Spawn all ui elements as children of this one
|
// 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