Skip to content

Commit

Permalink
Adjust the conversion from mouse events to viewport change
Browse files Browse the repository at this point in the history
  • Loading branch information
skywhale committed Sep 9, 2023
1 parent c74eeff commit e4d5e01
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions crates/viewer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ mod camera;
mod edge_drawer;
mod surface_drawer;

// Multipliers to convert mouse position deltas to a more sane camera perspective change.
// Multipliers to convert mouse position deltas to a more intuitve camera perspective change.
const ZOOM_MULTIPLIER: f32 = 5.0;
const TOUCHPAD_ZOOM_MULTIPLIER: f32 = 2.0;
const ROTATE_MULTIPLIER: f32 = 5.0;
const TOUCHPAD_ROTATE_MULTIPLIER: f32 = 0.05;
const PAN_MULTIPLIER: f32 = 100.0;
const TOUCHPAD_ZOOM_MULTIPLIER: f32 = 0.5;
const ROTATE_MULTIPLIER: f32 = -5.0;
const TOUCHPAD_ROTATE_MULTIPLIER: f32 = -0.05;
const PAN_MULTIPLIER: f32 = 150.0;
const TOUCHPAD_PAN_MULTIPLIER: f32 = 100.0;

#[derive(Default)]
struct MouseState {
Expand Down Expand Up @@ -233,9 +234,13 @@ impl GameApp for ViewerApp {
self.mouse_state.input(*button, *state)
},
WindowEvent::MouseWheel { delta: PixelDelta(delta), .. } => {
// winit can not distinguish mouse wheel and touchpad pan events unfortunately.
// Because of that, we assign pan operation to MouseWheel events. For mice, you
// need to instead use mouse move while holding down the right button.
let delta_x = delta.x as f32 / self.client_rect.x;
let delta_y = delta.y as f32 / self.client_rect.y;
self.camera.pan(delta_x * PAN_MULTIPLIER, delta_y * PAN_MULTIPLIER);
self.camera
.pan(delta_x * TOUCHPAD_PAN_MULTIPLIER, delta_y * TOUCHPAD_PAN_MULTIPLIER);
},
WindowEvent::TouchpadMagnify { delta, .. } => {
self.camera.zoom(*delta as f32 * TOUCHPAD_ZOOM_MULTIPLIER);
Expand Down

0 comments on commit e4d5e01

Please sign in to comment.