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 /// The Fornjot model viewer
pub struct Viewer { pub struct Viewer {
current_screen_size: ScreenSize, current_screen_size: ScreenSize,
new_screen_size: Option<ScreenSize>,
camera: Camera, camera: Camera,
cursor: Option<NormalizedScreenPosition>, cursor: Option<NormalizedScreenPosition>,
@ -27,6 +28,7 @@ impl Viewer {
Ok(Self { Ok(Self {
current_screen_size: screen.size(), current_screen_size: screen.size(),
new_screen_size: None,
camera: Camera::default(), camera: Camera::default(),
cursor: None, cursor: None,
draw_config: DrawConfig::default(), draw_config: DrawConfig::default(),
@ -84,10 +86,7 @@ impl Viewer {
/// Handle the screen being resized /// Handle the screen being resized
pub fn on_screen_resize(&mut self, new_size: ScreenSize) { pub fn on_screen_resize(&mut self, new_size: ScreenSize) {
self.current_screen_size = new_size; self.current_screen_size = new_size;
if new_size.is_valid() { self.new_screen_size = Some(new_size);
// We should only supply valid screen sizes to the renderer.
self.renderer.handle_resize(new_size);
}
} }
/// Compute and store a focus point, unless one is already stored /// Compute and store a focus point, unless one is already stored
@ -111,6 +110,13 @@ impl Viewer {
return; 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 let aabb = self
.model .model
.as_ref() .as_ref()

View File

@ -28,7 +28,6 @@ pub fn display(model: Model, invert_zoom: bool) -> Result<(), Error> {
window: None, window: None,
viewer: None, viewer: None,
held_mouse_button: None, held_mouse_button: None,
new_size: None,
}; };
event_loop.run_app(&mut display_state)?; event_loop.run_app(&mut display_state)?;
@ -58,7 +57,6 @@ struct DisplayState {
window: Option<Window>, window: Option<Window>,
viewer: Option<Viewer>, viewer: Option<Viewer>,
held_mouse_button: Option<MouseButton>, held_mouse_button: Option<MouseButton>,
new_size: Option<ScreenSize>,
} }
impl ApplicationHandler for DisplayState { impl ApplicationHandler for DisplayState {
@ -127,7 +125,7 @@ impl ApplicationHandler for DisplayState {
width: size.width, width: size.width,
height: size.height, height: size.height,
}; };
self.new_size = Some(size); viewer.on_screen_resize(size);
} }
WindowEvent::MouseInput { state, button, .. } => match state { WindowEvent::MouseInput { state, button, .. } => match state {
ElementState::Pressed => { ElementState::Pressed => {
@ -141,12 +139,6 @@ impl ApplicationHandler for DisplayState {
}, },
WindowEvent::MouseWheel { .. } => viewer.add_focus_point(), WindowEvent::MouseWheel { .. } => viewer.add_focus_point(),
WindowEvent::RedrawRequested => { 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(); viewer.draw();
} }
_ => {} _ => {}