Support clearing the geometry in a window

This commit is contained in:
Hanno Braun 2025-08-14 11:31:12 +02:00
parent ebf44582ab
commit 04b890f594
4 changed files with 45 additions and 0 deletions

View File

@ -224,6 +224,10 @@ impl Renderer {
));
}
pub fn clear_geometry(&mut self) {
self.geometries.clear();
}
/// Resizes the render surface.
///
/// # Arguments

View File

@ -130,6 +130,12 @@ impl WindowHandle {
window_id: self.id,
});
}
/// # Clear the contents of the window
pub fn clear(&self) {
self.event_loop
.send_event(EventLoopEvent::Clear { window_id: self.id });
}
}
#[derive(Clone)]
@ -288,6 +294,22 @@ impl ApplicationHandler<EventLoopEvent> for Viewer {
window.add_displayable(displayable);
}
EventLoopEvent::Clear { window_id } => {
let Some(winit_window_id) = self.id_map.get(&window_id) else {
unreachable!(
"Mappings for all window IDs are created when handling \
the `Window` event."
);
};
let Some(window) = self.windows.get_mut(winit_window_id) else {
unreachable!(
"We never remove any windows, so it's not possible to \
have a mapping to an ID, but not a window with that ID."
);
};
window.clear();
}
}
}
}
@ -300,4 +322,7 @@ enum EventLoopEvent {
displayable: Displayable,
window_id: u64,
},
Clear {
window_id: u64,
},
}

View File

@ -194,6 +194,11 @@ impl Window {
self.should_render = true;
}
/// # Clear the geometry displayed in the window
pub fn clear(&mut self) {
self.renderer.clear_geometry();
}
/// # Draw the window
pub fn draw(&mut self) -> bool {
let size_is_invalid = {

View File

@ -56,6 +56,17 @@ impl DebugWindow {
window.display_point(point);
}
#[allow(unused)] // occasionally useful for debugging
pub fn clear(&self) {
let inner = self.inner.lock().unwrap();
let DebugWindowInner::Initialized { window } = inner.deref() else {
panic!("Debug window has not been initialized.");
};
window.clear();
}
}
enum DebugWindowInner {