Move more event handling to the render context

This commit is contained in:
Jarrod Doyle 2023-10-20 11:37:24 +01:00
parent 9a4bd5d1a0
commit 2a6bee18a6
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
2 changed files with 67 additions and 51 deletions

View File

@ -84,24 +84,15 @@ impl App {
let mut cumulative_dt = 0.0; let mut cumulative_dt = 0.0;
let mut frames_accumulated = 0.0; let mut frames_accumulated = 0.0;
let mut last_render_time = Instant::now(); let mut last_render_time = Instant::now();
self.event_loop self.event_loop.run(move |event, _, control_flow| {
.run(move |event, _, control_flow| match event { if !self.render_ctx.handle_window_event(&event, control_flow) {
match event {
Event::WindowEvent { Event::WindowEvent {
ref event, ref event,
window_id, window_id,
} if window_id == self.render_ctx.window.id() } if window_id == self.render_ctx.window.id() => {
&& !self.render_ctx.handle_window_event(event) =>
{
match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
_ => {
camera_controller.process_events(event); camera_controller.process_events(event);
} }
}
}
Event::MainEventsCleared => {
self.render_ctx.window.request_redraw();
}
Event::RedrawRequested(_) => { Event::RedrawRequested(_) => {
let now = Instant::now(); let now = Instant::now();
let dt = now - last_render_time; let dt = now - last_render_time;
@ -129,6 +120,8 @@ impl App {
} }
} }
_ => {} _ => {}
}
}
}); });
} }
} }

View File

@ -1,4 +1,9 @@
use winit::{dpi::PhysicalSize, event::WindowEvent, window::Window}; use winit::{
dpi::PhysicalSize,
event::{Event, WindowEvent},
event_loop::ControlFlow,
window::Window,
};
pub struct Context { pub struct Context {
pub window: Window, pub window: Window,
@ -78,8 +83,20 @@ impl Context {
} }
} }
pub fn handle_window_event(&mut self, event: &WindowEvent) -> bool { pub fn handle_window_event(
&mut self,
event: &Event<()>,
control_flow: &mut ControlFlow,
) -> bool {
match event { match event {
Event::WindowEvent {
ref event,
window_id,
} if *window_id == self.window.id() => match event {
WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit;
true
}
WindowEvent::Resized(physical_size) => { WindowEvent::Resized(physical_size) => {
self.resize(*physical_size); self.resize(*physical_size);
true true
@ -89,6 +106,12 @@ impl Context {
true true
} }
_ => false, _ => false,
},
Event::MainEventsCleared => {
self.window.request_redraw();
true
}
_ => false,
} }
} }
} }