Skip to content

Commit

Permalink
Simplify controls (#507)
Browse files Browse the repository at this point in the history
* Simplify FirstPersonControl

* Simplify FlyControl

* Simplify OrbitControl

* Simplify FreeOrbitControl

* Remove CameraControl

* Docs
  • Loading branch information
asny authored Nov 11, 2024
1 parent bf25a5e commit 7e7ebf9
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 346 deletions.
4 changes: 0 additions & 4 deletions src/renderer/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
//! A collection of controls for example to control the camera.
//!
mod camera_control;
#[doc(inline)]
pub use camera_control::*;

mod orbit_control;
#[doc(inline)]
pub use orbit_control::*;
Expand Down
242 changes: 0 additions & 242 deletions src/renderer/control/camera_control.rs

This file was deleted.

48 changes: 34 additions & 14 deletions src/renderer/control/first_person_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,49 @@ use crate::renderer::*;
///
/// A control that makes the camera move like it is a person on the ground.
///
#[derive(Clone, Copy, Debug)]
pub struct FirstPersonControl {
control: CameraControl,
/// The speed of movements.
pub speed: f32,
}

impl FirstPersonControl {
/// Creates a new first person control with the given speed of movements.
pub fn new(speed: f32) -> Self {
Self {
control: CameraControl {
left_drag_horizontal: CameraAction::Yaw {
speed: std::f32::consts::PI / 1800.0,
},
left_drag_vertical: CameraAction::Pitch {
speed: std::f32::consts::PI / 1800.0,
},
scroll_vertical: CameraAction::Forward { speed },
..Default::default()
},
}
Self { speed }
}

/// Handles the events. Must be called each frame.
pub fn handle_events(&mut self, camera: &mut Camera, events: &mut [Event]) -> bool {
self.control.handle_events(camera, events)
let mut change = false;
for event in events.iter_mut() {
match event {
Event::MouseMotion {
delta,
button,
handled,
..
} => {
if !*handled {
if Some(MouseButton::Left) == *button {
camera.yaw(radians(delta.0 * std::f32::consts::PI / 1800.0));
camera.pitch(radians(delta.1 * std::f32::consts::PI / 1800.0));
*handled = true;
change = true;
}
}
}
Event::MouseWheel { delta, handled, .. } => {
if !*handled {
let v = camera.view_direction() * self.speed * delta.1;
camera.translate(&v);
*handled = true;
change = true;
}
}
_ => {}
}
}
change
}
}
59 changes: 43 additions & 16 deletions src/renderer/control/fly_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,58 @@ use crate::renderer::*;
///
/// A control that makes the camera fly through the 3D scene.
///
#[derive(Clone, Copy, Debug)]
pub struct FlyControl {
control: CameraControl,
/// The speed of movements.
pub speed: f32,
}

impl FlyControl {
/// Creates a new fly control with the given speed of movements.
pub fn new(speed: f32) -> Self {
Self {
control: CameraControl {
left_drag_horizontal: CameraAction::Yaw {
speed: std::f32::consts::PI / 1800.0,
},
left_drag_vertical: CameraAction::Pitch {
speed: std::f32::consts::PI / 1800.0,
},
scroll_vertical: CameraAction::Forward { speed },
right_drag_horizontal: CameraAction::Left { speed },
right_drag_vertical: CameraAction::Up { speed },
..Default::default()
},
}
Self { speed }
}

/// Handles the events. Must be called each frame.
pub fn handle_events(&mut self, camera: &mut Camera, events: &mut [Event]) -> bool {
self.control.handle_events(camera, events)
let mut change = false;
for event in events.iter_mut() {
match event {
Event::MouseMotion {
delta,
button,
handled,
..
} => {
if !*handled {
if Some(MouseButton::Left) == *button {
camera.yaw(radians(delta.0 * std::f32::consts::PI / 1800.0));
camera.pitch(radians(delta.1 * std::f32::consts::PI / 1800.0));
*handled = true;
change = true;
}
if Some(MouseButton::Right) == *button {
let right = camera.right_direction();
let up = right.cross(camera.view_direction());
camera.translate(
&(-right * delta.0 * self.speed + up * delta.1 * self.speed),
);
*handled = true;
change = true;
}
}
}
Event::MouseWheel { delta, handled, .. } => {
if !*handled {
let v = camera.view_direction() * self.speed * delta.1;
camera.translate(&v);
*handled = true;
change = true;
}
}
_ => {}
}
}
change
}
}
Loading

0 comments on commit 7e7ebf9

Please sign in to comment.