Skip to content

Commit

Permalink
Lift event dispatch out of Window
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralith committed Jul 29, 2024
1 parent 70f304a commit 5625bf1
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 147 deletions.
272 changes: 129 additions & 143 deletions client/src/graphics/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use winit::event_loop::EventLoopWindowTarget;
use winit::keyboard::{KeyCode, PhysicalKey};
use winit::{
dpi::PhysicalSize,
event::{DeviceEvent, ElementState, Event, MouseButton, WindowEvent},
event::{DeviceEvent, ElementState, MouseButton, WindowEvent},
event_loop::EventLoop,
window::{CursorGrabMode, Window as WinitWindow, WindowBuilder},
};
Expand Down Expand Up @@ -51,9 +51,8 @@ impl EarlyWindow {
/// OS window + rendering handles
pub struct Window {
_core: Arc<Core>,
window: WinitWindow,
pub window: WinitWindow,
config: Arc<Config>,
metrics: Arc<crate::metrics::Recorder>,
surface_fn: khr::surface::Instance,
surface: vk::SurfaceKHR,
swapchain: Option<SwapchainMgr>,
Expand All @@ -69,13 +68,7 @@ pub struct Window {

impl Window {
/// Finish constructing a window
pub fn new(
early: EarlyWindow,
core: Arc<Core>,
config: Arc<Config>,
metrics: Arc<crate::metrics::Recorder>,
net: Net,
) -> Self {
pub fn new(early: EarlyWindow, core: Arc<Core>, config: Arc<Config>, net: Net) -> Self {
let surface = unsafe {
ash_window::create_surface(
&core.entry,
Expand All @@ -92,7 +85,6 @@ impl Window {
_core: core,
window: early.window,
config,
metrics,
surface,
surface_fn,
swapchain: None,
Expand Down Expand Up @@ -127,158 +119,152 @@ impl Window {
self.draw = Some(Draw::new(gfx, self.config.clone()));
}

pub fn handle_event(&mut self, event: Event<()>, window_target: &EventLoopWindowTarget<()>) {
pub fn handle_device_event(&mut self, event: DeviceEvent) {
match event {
Event::AboutToWait => {
self.window.request_redraw();
DeviceEvent::MouseMotion { delta } if self.input.mouse_captured => {
if let Some(sim) = self.sim.as_mut() {
const SENSITIVITY: f32 = 2e-3;
sim.look(
-delta.0 as f32 * SENSITIVITY,
-delta.1 as f32 * SENSITIVITY,
0.0,
);
}
}
Event::DeviceEvent { event, .. } => match event {
DeviceEvent::MouseMotion { delta } if self.input.mouse_captured => {
if let Some(sim) = self.sim.as_mut() {
const SENSITIVITY: f32 = 2e-3;
sim.look(
-delta.0 as f32 * SENSITIVITY,
-delta.1 as f32 * SENSITIVITY,
0.0,
);
}
_ => {}
}
}

pub fn handle_event(&mut self, event: WindowEvent, window_target: &EventLoopWindowTarget<()>) {
match event {
WindowEvent::RedrawRequested => {
while let Ok(msg) = self.net.incoming.try_recv() {
self.handle_net(msg);
}
_ => {}
},
Event::WindowEvent { event, .. } => match event {
WindowEvent::RedrawRequested => {
while let Ok(msg) = self.net.incoming.try_recv() {
self.handle_net(msg);
}

if let Some(sim) = self.sim.as_mut() {
let this_frame = Instant::now();
let dt = this_frame - self.last_frame.unwrap_or(this_frame);
sim.set_movement_input(self.input.movement());
sim.set_jump_held(self.input.jump);
if let Some(sim) = self.sim.as_mut() {
let this_frame = Instant::now();
let dt = this_frame - self.last_frame.unwrap_or(this_frame);
sim.set_movement_input(self.input.movement());
sim.set_jump_held(self.input.jump);

sim.look(0.0, 0.0, 2.0 * self.input.roll() * dt.as_secs_f32());
sim.look(0.0, 0.0, 2.0 * self.input.roll() * dt.as_secs_f32());

sim.step(dt, &mut self.net);
self.last_frame = Some(this_frame);
}
sim.step(dt, &mut self.net);
self.last_frame = Some(this_frame);
}

self.draw();
self.draw();
}
WindowEvent::Resized(_) => {
// Some environments may not emit the vulkan signals that recommend or
// require surface reconstruction, so we need to check for messages from the
// windowing system too. We defer actually performing the update until
// drawing to avoid doing unnecessary work between frames.
self.swapchain_needs_update = true;
}
WindowEvent::CloseRequested => {
info!("exiting due to closed window");
window_target.exit();
}
WindowEvent::MouseInput {
button: MouseButton::Left,
state: ElementState::Pressed,
..
} => {
if self.input.mouse_captured {
if let Some(sim) = self.sim.as_mut() {
sim.set_break_block_pressed_true();
}
}
WindowEvent::Resized(_) => {
// Some environments may not emit the vulkan signals that recommend or
// require surface reconstruction, so we need to check for messages from the
// windowing system too. We defer actually performing the update until
// drawing to avoid doing unnecessary work between frames.
self.swapchain_needs_update = true;
let _ = self
.window
.set_cursor_grab(CursorGrabMode::Confined)
.or_else(|_e| self.window.set_cursor_grab(CursorGrabMode::Locked));
self.window.set_cursor_visible(false);
self.input.mouse_captured = true;
}
WindowEvent::MouseInput {
button: MouseButton::Right,
state: ElementState::Pressed,
..
} => {
if self.input.mouse_captured {
if let Some(sim) = self.sim.as_mut() {
sim.set_place_block_pressed_true();
}
}
WindowEvent::CloseRequested => {
info!("exiting due to closed window");
window_target.exit();
}
WindowEvent::KeyboardInput {
event:
KeyEvent {
state,
physical_key: PhysicalKey::Code(key),
..
},
..
} => match key {
KeyCode::KeyW => {
self.input.forward = state == ElementState::Pressed;
}
WindowEvent::MouseInput {
button: MouseButton::Left,
state: ElementState::Pressed,
..
} => {
if self.input.mouse_captured {
if let Some(sim) = self.sim.as_mut() {
sim.set_break_block_pressed_true();
}
}
let _ = self
.window
.set_cursor_grab(CursorGrabMode::Confined)
.or_else(|_e| self.window.set_cursor_grab(CursorGrabMode::Locked));
self.window.set_cursor_visible(false);
self.input.mouse_captured = true;
KeyCode::KeyA => {
self.input.left = state == ElementState::Pressed;
}
WindowEvent::MouseInput {
button: MouseButton::Right,
state: ElementState::Pressed,
..
} => {
if self.input.mouse_captured {
if let Some(sim) = self.sim.as_mut() {
sim.set_place_block_pressed_true();
}
}
KeyCode::KeyS => {
self.input.back = state == ElementState::Pressed;
}
WindowEvent::KeyboardInput {
event:
KeyEvent {
state,
physical_key: PhysicalKey::Code(key),
..
},
..
} => match key {
KeyCode::KeyW => {
self.input.forward = state == ElementState::Pressed;
}
KeyCode::KeyA => {
self.input.left = state == ElementState::Pressed;
}
KeyCode::KeyS => {
self.input.back = state == ElementState::Pressed;
}
KeyCode::KeyD => {
self.input.right = state == ElementState::Pressed;
}
KeyCode::KeyQ => {
self.input.anticlockwise = state == ElementState::Pressed;
}
KeyCode::KeyE => {
self.input.clockwise = state == ElementState::Pressed;
}
KeyCode::KeyR => {
self.input.up = state == ElementState::Pressed;
}
KeyCode::KeyF => {
self.input.down = state == ElementState::Pressed;
}
KeyCode::Space => {
if let Some(sim) = self.sim.as_mut() {
if !self.input.jump && state == ElementState::Pressed {
sim.set_jump_pressed_true();
}
self.input.jump = state == ElementState::Pressed;
}
}
KeyCode::KeyV if state == ElementState::Pressed => {
if let Some(sim) = self.sim.as_mut() {
sim.toggle_no_clip();
KeyCode::KeyD => {
self.input.right = state == ElementState::Pressed;
}
KeyCode::KeyQ => {
self.input.anticlockwise = state == ElementState::Pressed;
}
KeyCode::KeyE => {
self.input.clockwise = state == ElementState::Pressed;
}
KeyCode::KeyR => {
self.input.up = state == ElementState::Pressed;
}
KeyCode::KeyF => {
self.input.down = state == ElementState::Pressed;
}
KeyCode::Space => {
if let Some(sim) = self.sim.as_mut() {
if !self.input.jump && state == ElementState::Pressed {
sim.set_jump_pressed_true();
}
self.input.jump = state == ElementState::Pressed;
}
KeyCode::F1 if state == ElementState::Pressed => {
self.gui_state.toggle_gui();
}
KeyCode::Escape => {
let _ = self.window.set_cursor_grab(CursorGrabMode::None);
self.window.set_cursor_visible(true);
self.input.mouse_captured = false;
}
KeyCode::KeyV if state == ElementState::Pressed => {
if let Some(sim) = self.sim.as_mut() {
sim.toggle_no_clip();
}
_ => {
if let Some(material_idx) = number_key_to_index(key) {
if state == ElementState::Pressed {
if let Some(sim) = self.sim.as_mut() {
sim.select_material(material_idx);
}
}
KeyCode::F1 if state == ElementState::Pressed => {
self.gui_state.toggle_gui();
}
KeyCode::Escape => {
let _ = self.window.set_cursor_grab(CursorGrabMode::None);
self.window.set_cursor_visible(true);
self.input.mouse_captured = false;
}
_ => {
if let Some(material_idx) = number_key_to_index(key) {
if state == ElementState::Pressed {
if let Some(sim) = self.sim.as_mut() {
sim.select_material(material_idx);
}
}
}
},
WindowEvent::Focused(focused) => {
if !focused {
let _ = self.window.set_cursor_grab(CursorGrabMode::None);
self.window.set_cursor_visible(true);
self.input.mouse_captured = false;
}
}
_ => {}
},
Event::LoopExiting => {
self.metrics.report();
WindowEvent::Focused(focused) => {
if !focused {
let _ = self.window.set_cursor_grab(CursorGrabMode::None);
self.window.set_cursor_visible(true);
self.input.mouse_captured = false;
}
}
_ => {}
}
Expand Down
20 changes: 16 additions & 4 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use save::Save;

use ash::khr;
use tracing::{error, error_span, info};
use winit::event_loop::EventLoop;
use winit::{event::Event, event_loop::EventLoop};

fn main() {
// Set up logging
Expand Down Expand Up @@ -70,7 +70,7 @@ fn main() {
let net = net::spawn(config.clone());

// Finish creating the window, including the Vulkan resources used to render to it
let mut window = graphics::Window::new(window, core.clone(), config, metrics, net);
let mut window = graphics::Window::new(window, core.clone(), config, net);

// Initialize widely-shared graphics resources
let gfx = Arc::new(
Expand All @@ -87,8 +87,20 @@ fn main() {

// Run the window's event loop
event_loop
.run(|event, window_target| {
window.handle_event(event, window_target);
.run(|event, window_target| match event {
Event::AboutToWait => {
window.window.request_redraw();
}
Event::DeviceEvent { event, .. } => {
window.handle_device_event(event);
}
Event::WindowEvent { event, .. } => {
window.handle_event(event, window_target);
}
Event::LoopExiting => {
metrics.report();
}
_ => {}
})
.unwrap();
}

0 comments on commit 5625bf1

Please sign in to comment.