From 63eb8682d2b85536c2dda812790d39bfd1162d24 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Sun, 29 Jan 2023 02:15:12 +0100 Subject: [PATCH] exit on esc and cursor lock --- src/control_flow.rs | 21 +++++++++++++++++++++ src/cursor_lock.rs | 8 +++++++- src/main.rs | 20 ++++++++++++++------ 3 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 src/control_flow.rs diff --git a/src/control_flow.rs b/src/control_flow.rs new file mode 100644 index 0000000..253dbdb --- /dev/null +++ b/src/control_flow.rs @@ -0,0 +1,21 @@ +use shipyard::{UniqueView, UniqueViewMut, Unique, AllStoragesView}; +use glium::glutin::{event::VirtualKeyCode, event_loop::ControlFlow}; +use crate::input::RawInputState; + +#[derive(Unique)] +pub struct SetControlFlow(pub Option); + +pub fn exit_on_esc( + raw_inputs: UniqueView, + mut control_flow: UniqueViewMut +) { + if raw_inputs.keyboard_state.contains(&VirtualKeyCode::Escape) { + control_flow.0 = Some(ControlFlow::Exit); + } +} + +pub fn insert_control_flow_unique( + storages: AllStoragesView +) { + storages.add_unique(SetControlFlow(None)) +} diff --git a/src/cursor_lock.rs b/src/cursor_lock.rs index 200d9e8..2c3303b 100644 --- a/src/cursor_lock.rs +++ b/src/cursor_lock.rs @@ -1,4 +1,4 @@ -use shipyard::{AllStoragesView, Unique, NonSendSync, UniqueView}; +use shipyard::{AllStoragesView, Unique, NonSendSync, UniqueView, UniqueViewMut}; use crate::rendering::Renderer; use glium::glutin::window::CursorGrabMode; @@ -26,3 +26,9 @@ pub fn insert_lock_state( ) { storages.add_unique(CursorLock(false)) } + +pub fn lock_cursor_now( + mut lock: UniqueViewMut +) { + lock.0 = true +} diff --git a/src/main.rs b/src/main.rs index a962b05..0dff5a4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,7 @@ pub(crate) mod fly_controller; pub(crate) mod block_placement; pub(crate) mod delta_time; pub(crate) mod cursor_lock; +pub(crate) mod control_flow; use rendering::{ Renderer, @@ -52,7 +53,8 @@ use rendering::{ }; use block_placement::block_placement_system; use delta_time::{DeltaTime, init_delta_time}; -use cursor_lock::{insert_lock_state, update_cursor_lock_state}; +use cursor_lock::{insert_lock_state, update_cursor_lock_state, lock_cursor_now}; +use control_flow::{exit_on_esc, insert_control_flow_unique, SetControlFlow}; fn startup() -> Workload { ( @@ -60,9 +62,11 @@ fn startup() -> Workload { load_prefabs, init_selection_box_buffers, insert_lock_state, + lock_cursor_now, init_input, init_game_world, spawn_player, + insert_control_flow_unique, init_delta_time, ).into_workload() } @@ -74,7 +78,8 @@ fn update() -> Workload { update_raycasts, block_placement_system, update_cursor_lock_state, - compute_cameras + compute_cameras, + exit_on_esc, ).into_workload() } fn render() -> Workload { @@ -117,11 +122,9 @@ fn main() { event_loop.run(move |event, _, control_flow| { *control_flow = ControlFlow::Poll; process_glutin_events(&mut world, &event); + #[allow(clippy::collapsible_match, clippy::single_match)] match event { Event::WindowEvent { event, .. } => match event { - WindowEvent::Resized(_size) => { - // todo ... - } WindowEvent::CloseRequested => { log::info!("exit requested"); *control_flow = ControlFlow::Exit; @@ -154,8 +157,13 @@ fn main() { let target = world.remove_unique::().unwrap(); target.0.finish().unwrap(); - //FrameEnd + //After frame end world.run_workload(after_frame_end).unwrap(); + + //Process control flow changes + if let Some(flow) = world.borrow::>().unwrap().0 { + *control_flow = flow; + } }, _ => (), };