exit on esc and cursor lock

This commit is contained in:
griffi-gh 2023-01-29 02:15:12 +01:00
parent 3b08f3e13d
commit 63eb8682d2
3 changed files with 42 additions and 7 deletions

21
src/control_flow.rs Normal file
View file

@ -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<ControlFlow>);
pub fn exit_on_esc(
raw_inputs: UniqueView<RawInputState>,
mut control_flow: UniqueViewMut<SetControlFlow>
) {
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))
}

View file

@ -1,4 +1,4 @@
use shipyard::{AllStoragesView, Unique, NonSendSync, UniqueView}; use shipyard::{AllStoragesView, Unique, NonSendSync, UniqueView, UniqueViewMut};
use crate::rendering::Renderer; use crate::rendering::Renderer;
use glium::glutin::window::CursorGrabMode; use glium::glutin::window::CursorGrabMode;
@ -26,3 +26,9 @@ pub fn insert_lock_state(
) { ) {
storages.add_unique(CursorLock(false)) storages.add_unique(CursorLock(false))
} }
pub fn lock_cursor_now(
mut lock: UniqueViewMut<CursorLock>
) {
lock.0 = true
}

View file

@ -27,6 +27,7 @@ pub(crate) mod fly_controller;
pub(crate) mod block_placement; pub(crate) mod block_placement;
pub(crate) mod delta_time; pub(crate) mod delta_time;
pub(crate) mod cursor_lock; pub(crate) mod cursor_lock;
pub(crate) mod control_flow;
use rendering::{ use rendering::{
Renderer, Renderer,
@ -52,7 +53,8 @@ use rendering::{
}; };
use block_placement::block_placement_system; use block_placement::block_placement_system;
use delta_time::{DeltaTime, init_delta_time}; 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 { fn startup() -> Workload {
( (
@ -60,9 +62,11 @@ fn startup() -> Workload {
load_prefabs, load_prefabs,
init_selection_box_buffers, init_selection_box_buffers,
insert_lock_state, insert_lock_state,
lock_cursor_now,
init_input, init_input,
init_game_world, init_game_world,
spawn_player, spawn_player,
insert_control_flow_unique,
init_delta_time, init_delta_time,
).into_workload() ).into_workload()
} }
@ -74,7 +78,8 @@ fn update() -> Workload {
update_raycasts, update_raycasts,
block_placement_system, block_placement_system,
update_cursor_lock_state, update_cursor_lock_state,
compute_cameras compute_cameras,
exit_on_esc,
).into_workload() ).into_workload()
} }
fn render() -> Workload { fn render() -> Workload {
@ -117,11 +122,9 @@ fn main() {
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll; *control_flow = ControlFlow::Poll;
process_glutin_events(&mut world, &event); process_glutin_events(&mut world, &event);
#[allow(clippy::collapsible_match, clippy::single_match)]
match event { match event {
Event::WindowEvent { event, .. } => match event { Event::WindowEvent { event, .. } => match event {
WindowEvent::Resized(_size) => {
// todo ...
}
WindowEvent::CloseRequested => { WindowEvent::CloseRequested => {
log::info!("exit requested"); log::info!("exit requested");
*control_flow = ControlFlow::Exit; *control_flow = ControlFlow::Exit;
@ -154,8 +157,13 @@ fn main() {
let target = world.remove_unique::<RenderTarget>().unwrap(); let target = world.remove_unique::<RenderTarget>().unwrap();
target.0.finish().unwrap(); target.0.finish().unwrap();
//FrameEnd //After frame end
world.run_workload(after_frame_end).unwrap(); world.run_workload(after_frame_end).unwrap();
//Process control flow changes
if let Some(flow) = world.borrow::<UniqueView<SetControlFlow>>().unwrap().0 {
*control_flow = flow;
}
}, },
_ => (), _ => (),
}; };