Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify zoom and adapt for larger models #764

Merged
merged 6 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion crates/fj-viewer/src/input/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@ pub enum Event {
Key(Key, KeyState),

/// The user scrolled
Scroll(f64),
Scroll(MouseScrollDelta),
}

/// Describes a difference in the mouse scroll wheel state.
pub enum MouseScrollDelta {
/// Amount in lines to scroll in the horizontal direction.
///
/// Positive values indicate movement forward (away from the user).
Line(f64),
/// Amount in pixels to scroll in the vertical direction.
Pixel(f64),
Comment on lines +17 to +22
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few issues with the documentation here:

  • It says Line is horizontal, but they're both vertical.
  • I believe the sign has the same meaning for both variants, so it probably makes sense to move the comment about positive values to the struct-level doc comment.

}

/// A keyboard or mouse key
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-viewer/src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ mod rotation;
mod zoom;

pub use self::{
event::{Event, Key, KeyState},
event::{Event, Key, KeyState, MouseScrollDelta},
handler::{Actions, Handler},
};
25 changes: 17 additions & 8 deletions crates/fj-viewer/src/input/zoom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use fj_math::{Transform, Vector};

use crate::camera::{Camera, FocusPoint};

use super::event::MouseScrollDelta;

pub struct Zoom {
accumulated_delta: f64,
}
Expand All @@ -13,9 +15,12 @@ impl Zoom {
}
}

pub fn push(&mut self, delta: f64) {
pub fn push(&mut self, delta: MouseScrollDelta) {
// Accumulate all zoom inputs
self.accumulated_delta += delta;
self.accumulated_delta += match delta {
MouseScrollDelta::Line(delta) => ZOOM_FACTOR_LINE * delta,
MouseScrollDelta::Pixel(delta) => ZOOM_FACTOR_PIXEL * delta,
};
}

pub fn apply_to_camera(
Expand All @@ -28,19 +33,23 @@ impl Zoom {
Some(fp) => (fp - camera.position()).magnitude(),
None => camera.position().coords.magnitude(),
};
let displacement = self.accumulated_delta
* delta_t
* ZOOM_FACTOR
* distance.into_f64();
let displacement =
self.accumulated_delta * delta_t * distance.into_f64();
camera.translation = camera.translation
* Transform::translation(Vector::from([0.0, 0.0, -displacement]));

self.accumulated_delta = 0.;
}
}

/// Affects the speed of zoom movement.
/// Affects the speed of zoom movement given a scroll wheel input in lines.
///
/// Smaller values will move the camera less with the same input.
/// Larger values will move the camera more with the same input.
const ZOOM_FACTOR_LINE: f64 = 15.0;

/// Affects the speed of zoom movement given a scroll wheel input in pixels.
///
/// Smaller values will move the camera less with the same input.
/// Larger values will move the camera more with the same input.
const ZOOM_FACTOR: f64 = 0.1;
const ZOOM_FACTOR_PIXEL: f64 = 1.0;
18 changes: 8 additions & 10 deletions crates/fj-window/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,14 @@ pub fn run(
Event::WindowEvent {
event: WindowEvent::MouseWheel { delta, .. },
..
} => {
let delta = match delta {
MouseScrollDelta::LineDelta(_, y) => y as f64 * 10.0,
MouseScrollDelta::PixelDelta(PhysicalPosition {
y,
..
}) => y,
};
Some(input::Event::Scroll(delta))
}
} => Some(input::Event::Scroll(match delta {
MouseScrollDelta::LineDelta(_, y) => {
input::MouseScrollDelta::Line(y as f64)
}
MouseScrollDelta::PixelDelta(PhysicalPosition {
y, ..
}) => input::MouseScrollDelta::Pixel(y),
})),
Event::MainEventsCleared => {
let delta_t = now.duration_since(previous_time);
previous_time = now;
Expand Down