Minor refactor, add cursor lock module

This commit is contained in:
griffi-gh 2023-01-29 01:58:52 +01:00
parent 9d2c00b204
commit a74296956e
3 changed files with 43 additions and 4 deletions

28
src/cursor_lock.rs Normal file
View file

@ -0,0 +1,28 @@
use shipyard::{AllStoragesView, Unique, NonSendSync, UniqueView};
use crate::rendering::Renderer;
use glium::glutin::window::CursorGrabMode;
#[derive(Unique)]
#[track(All)]
pub struct CursorLock(pub bool);
pub fn update_cursor_lock_state(
lock: UniqueView<CursorLock>,
display: NonSendSync<UniqueView<Renderer>>
) {
if lock.is_inserted_or_modified() {
let gl_window = display.display.gl_window();
let window = gl_window.window();
window.set_cursor_grab(match lock.0 {
true => CursorGrabMode::Confined,
false => CursorGrabMode::None,
}).expect("Failed to change cursor grab state");
window.set_cursor_visible(!lock.0);
}
}
pub fn insert_lock_state(
storages: AllStoragesView
) {
storages.add_unique(CursorLock(false))
}

View file

@ -26,6 +26,7 @@ pub(crate) mod input;
pub(crate) mod fly_controller; 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;
use rendering::{ use rendering::{
Renderer, Renderer,
@ -40,7 +41,7 @@ use world::{
}; };
use player::spawn_player; use player::spawn_player;
use prefabs::load_prefabs; use prefabs::load_prefabs;
use settings::GameSettings; use settings::load_settings;
use camera::compute_cameras; use camera::compute_cameras;
use events::{clear_events, process_glutin_events}; use events::{clear_events, process_glutin_events};
use input::{init_input, process_inputs}; use input::{init_input, process_inputs};
@ -51,15 +52,18 @@ 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};
fn startup() -> Workload { fn startup() -> Workload {
( (
init_delta_time, load_settings,
load_prefabs, load_prefabs,
init_selection_box_buffers, init_selection_box_buffers,
insert_lock_state,
init_input, init_input,
init_game_world, init_game_world,
spawn_player, spawn_player,
init_delta_time,
).into_workload() ).into_workload()
} }
fn update() -> Workload { fn update() -> Workload {
@ -69,6 +73,7 @@ fn update() -> Workload {
update_loaded_world_around_player, update_loaded_world_around_player,
update_raycasts, update_raycasts,
block_placement_system, block_placement_system,
update_cursor_lock_state,
compute_cameras compute_cameras
).into_workload() ).into_workload()
} }
@ -97,7 +102,6 @@ fn main() {
//Add systems and uniques, Init and load things //Add systems and uniques, Init and load things
world.add_unique_non_send_sync(Renderer::init(&event_loop)); world.add_unique_non_send_sync(Renderer::init(&event_loop));
world.add_unique(BackgroundColor(vec3(0.5, 0.5, 1.))); world.add_unique(BackgroundColor(vec3(0.5, 0.5, 1.)));
world.add_unique(GameSettings::default());
//Register workloads //Register workloads
world.add_workload(startup); world.add_workload(startup);

View file

@ -1,4 +1,4 @@
use shipyard::Unique; use shipyard::{Unique, AllStoragesView};
#[derive(Unique)] #[derive(Unique)]
pub struct GameSettings { pub struct GameSettings {
@ -14,3 +14,10 @@ impl Default for GameSettings {
} }
} }
} }
pub fn load_settings(
storages: AllStoragesView
) {
//todo
storages.add_unique(GameSettings::default());
}