Move more logic into fj-viewer

This commit is contained in:
Hanno Braun 2024-11-15 19:26:05 +01:00
parent 532187d22b
commit 466efa2975
2 changed files with 11 additions and 13 deletions

View File

@ -11,6 +11,7 @@ use crate::{
/// The Fornjot model viewer
pub struct Viewer {
current_screen_size: ScreenSize,
new_screen_size: Option<ScreenSize>,
camera: Camera,
cursor: Option<NormalizedScreenPosition>,
@ -27,6 +28,7 @@ impl Viewer {
Ok(Self {
current_screen_size: screen.size(),
new_screen_size: None,
camera: Camera::default(),
cursor: None,
draw_config: DrawConfig::default(),
@ -84,10 +86,7 @@ impl Viewer {
/// Handle the screen being resized
pub fn on_screen_resize(&mut self, new_size: ScreenSize) {
self.current_screen_size = new_size;
if new_size.is_valid() {
// We should only supply valid screen sizes to the renderer.
self.renderer.handle_resize(new_size);
}
self.new_screen_size = Some(new_size);
}
/// Compute and store a focus point, unless one is already stored
@ -111,6 +110,13 @@ impl Viewer {
return;
}
if let Some(new_size) = self.new_screen_size.take() {
// We should only supply valid screen sizes to the renderer. But
// `self.current_screen_size` has already been updated, and we're
// checking if that's valid above. No need to check again.
self.renderer.handle_resize(new_size);
}
let aabb = self
.model
.as_ref()

View File

@ -28,7 +28,6 @@ pub fn display(model: Model, invert_zoom: bool) -> Result<(), Error> {
window: None,
viewer: None,
held_mouse_button: None,
new_size: None,
};
event_loop.run_app(&mut display_state)?;
@ -58,7 +57,6 @@ struct DisplayState {
window: Option<Window>,
viewer: Option<Viewer>,
held_mouse_button: Option<MouseButton>,
new_size: Option<ScreenSize>,
}
impl ApplicationHandler for DisplayState {
@ -127,7 +125,7 @@ impl ApplicationHandler for DisplayState {
width: size.width,
height: size.height,
};
self.new_size = Some(size);
viewer.on_screen_resize(size);
}
WindowEvent::MouseInput { state, button, .. } => match state {
ElementState::Pressed => {
@ -141,12 +139,6 @@ impl ApplicationHandler for DisplayState {
},
WindowEvent::MouseWheel { .. } => viewer.add_focus_point(),
WindowEvent::RedrawRequested => {
// Only do a screen resize once per frame. This protects against
// spurious resize events that cause issues with the renderer.
if let Some(size) = self.new_size.take() {
viewer.on_screen_resize(size);
}
viewer.draw();
}
_ => {}