use super::*;
#[derive(Clone, Copy, Debug)]
pub struct FreeOrbitControl {
pub target: Vec3,
pub min_distance: f32,
pub max_distance: f32,
}
impl FreeOrbitControl {
pub fn new(target: Vec3, min_distance: f32, max_distance: f32) -> Self {
Self {
target,
min_distance,
max_distance,
}
}
pub fn handle_events(
&mut self,
camera: &mut three_d_asset::Camera,
events: &mut [Event],
) -> bool {
let mut change = false;
for event in events.iter_mut() {
match event {
Event::MouseMotion {
delta,
button,
handled,
..
} if !*handled && Some(MouseButton::Left) == *button => {
let speed = 0.01 * self.target.distance(camera.position()) + 0.001;
camera.rotate_around(self.target, speed * delta.0, speed * delta.1);
*handled = true;
change = true;
}
Event::MouseWheel { delta, handled, .. } if !*handled => {
let distance = self.target.distance(camera.position());
let zoom_amount = distance * (1.0 - (-delta.1 * 0.01).exp());
camera.zoom_towards(
self.target,
zoom_amount,
self.min_distance,
self.max_distance,
);
*handled = true;
change = true;
}
Event::PinchGesture { delta, handled, .. } if !*handled => {
let speed = self.target.distance(camera.position()) + 0.1;
camera.zoom_towards(
self.target,
speed * *delta,
self.min_distance,
self.max_distance,
);
*handled = true;
change = true;
}
_ => {}
}
}
change
}
}