Skip to content

Commit

Permalink
Only enable mouse movement when pressed in scene_viewer (#4405)
Browse files Browse the repository at this point in the history
# Objective

- Only move the camera when explicitly wanted, otherwise the camera goes crazy if the cursor isn't already in the middle of the window when it opens.

## Solution

- Check if the Left mouse button is pressed before updating the mouse delta
- Input is configurable
  • Loading branch information
IceSentry committed Apr 3, 2022
1 parent ac29cbe commit 28d0a40
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions examples/tools/scene_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ fn main() {
println!(
"
Controls:
MOUSE - Move camera orientation
LClick - Enable mouse movement
WSAD - forward/back/strafe left/right
LShift - 'run'
E - up
Expand Down Expand Up @@ -356,6 +358,7 @@ struct CameraController {
pub key_up: KeyCode,
pub key_down: KeyCode,
pub key_run: KeyCode,
pub key_enable_mouse: MouseButton,
pub walk_speed: f32,
pub run_speed: f32,
pub friction: f32,
Expand All @@ -377,6 +380,7 @@ impl Default for CameraController {
key_up: KeyCode::E,
key_down: KeyCode::Q,
key_run: KeyCode::LShift,
key_enable_mouse: MouseButton::Left,
walk_speed: 5.0,
run_speed: 15.0,
friction: 0.5,
Expand All @@ -390,17 +394,12 @@ impl Default for CameraController {
fn camera_controller(
time: Res<Time>,
mut mouse_events: EventReader<MouseMotion>,
mouse_button_input: Res<Input<MouseButton>>,
key_input: Res<Input<KeyCode>>,
mut query: Query<(&mut Transform, &mut CameraController), With<Camera>>,
) {
let dt = time.delta_seconds();

// Handle mouse input
let mut mouse_delta = Vec2::ZERO;
for mouse_event in mouse_events.iter() {
mouse_delta += mouse_event.delta;
}

if let Ok((mut transform, mut options)) = query.get_single_mut() {
if !options.initialized {
let (_roll, yaw, pitch) = transform.rotation.to_euler(EulerRot::ZYX);
Expand Down Expand Up @@ -454,6 +453,14 @@ fn camera_controller(
+ options.velocity.y * dt * Vec3::Y
+ options.velocity.z * dt * forward;

// Handle mouse input
let mut mouse_delta = Vec2::ZERO;
if mouse_button_input.pressed(options.key_enable_mouse) {
for mouse_event in mouse_events.iter() {
mouse_delta += mouse_event.delta;
}
}

if mouse_delta != Vec2::ZERO {
// Apply look update
let (pitch, yaw) = (
Expand Down

0 comments on commit 28d0a40

Please sign in to comment.