From c22409bd54511a01b075c9c47e6d708c8be4ef4c Mon Sep 17 00:00:00 2001 From: Vijfhoek Date: Wed, 2 Jun 2021 17:21:03 +0200 Subject: [PATCH] Clean up main.rs event handling a little --- src/main.rs | 105 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 64 insertions(+), 41 deletions(-) diff --git a/src/main.rs b/src/main.rs index ec68421..05b921d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,11 +18,70 @@ use winit::{ dpi::{PhysicalSize, Size}, event::{ElementState, Event, KeyboardInput, MouseButton, VirtualKeyCode, WindowEvent}, event_loop::{ControlFlow, EventLoop}, - window::WindowBuilder, + window::{Window, WindowBuilder}, }; use crate::state::State; +fn handle_window_event( + event: &WindowEvent, + state: &mut State, + window: &Window, +) -> Option { + match event { + WindowEvent::CloseRequested => Some(ControlFlow::Exit), + WindowEvent::KeyboardInput { + input: + KeyboardInput { + state: ElementState::Pressed, + virtual_keycode: Some(VirtualKeyCode::Escape), + .. + }, + .. + } => { + let _ = window.set_cursor_grab(false); + window.set_cursor_visible(true); + state.mouse_grabbed = false; + None + } + WindowEvent::Resized(physical_size) => { + state.resize(*physical_size); + None + } + WindowEvent::ScaleFactorChanged { new_inner_size, .. } => { + state.resize(**new_inner_size); + None + } + WindowEvent::MouseInput { + state: mouse_state, + button, + .. + } => { + if !state.mouse_grabbed + && *button == MouseButton::Left + && *mouse_state == ElementState::Pressed + { + let _ = window.set_cursor_grab(true); + window.set_cursor_visible(false); + state.mouse_grabbed = true; + } else { + state.window_event(event); + } + None + } + WindowEvent::Focused(false) => { + let _ = window.set_cursor_grab(false); + window.set_cursor_visible(true); + state.mouse_grabbed = false; + None + } + event => { + state.window_event(event); + None + } + } +} + fn main() { env_logger::init(); let event_loop = EventLoop::new(); @@ -53,47 +112,11 @@ fn main() { Event::WindowEvent { ref event, window_id, - } if window_id == window.id() => match event { - WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, - WindowEvent::KeyboardInput { - input: - KeyboardInput { - state: ElementState::Pressed, - virtual_keycode: Some(VirtualKeyCode::Escape), - .. - }, - .. - } => { - let _ = window.set_cursor_grab(false); - window.set_cursor_visible(true); - state.mouse_grabbed = false; + } if window_id == window.id() => { + if let Some(cf) = handle_window_event(event, &mut state, &window) { + *control_flow = cf } - WindowEvent::Resized(physical_size) => { - state.resize(*physical_size); - } - WindowEvent::ScaleFactorChanged { new_inner_size, .. } => { - state.resize(**new_inner_size); - } - WindowEvent::MouseInput { - state: mouse_state, - button, - .. - } => { - if !state.mouse_grabbed && *button == MouseButton::Left && *mouse_state == ElementState::Pressed { - let _ = window.set_cursor_grab(true); - window.set_cursor_visible(false); - state.mouse_grabbed = true; - } else { - state.window_event(event); - } - } - WindowEvent::Focused(false) => { - let _ = window.set_cursor_grab(false); - window.set_cursor_visible(true); - state.mouse_grabbed = false; - } - event => { state.window_event(event); } - }, + } Event::RedrawRequested(_) => { let frame_elapsed = frame_instant.elapsed(); frame_instant = Instant::now();