diff --git a/src/cursor_lock.rs b/src/cursor_lock.rs new file mode 100644 index 0000000..200d9e8 --- /dev/null +++ b/src/cursor_lock.rs @@ -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, + display: NonSendSync> +) { + 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)) +} diff --git a/src/main.rs b/src/main.rs index ee70afd..a962b05 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,6 +26,7 @@ pub(crate) mod input; pub(crate) mod fly_controller; pub(crate) mod block_placement; pub(crate) mod delta_time; +pub(crate) mod cursor_lock; use rendering::{ Renderer, @@ -40,7 +41,7 @@ use world::{ }; use player::spawn_player; use prefabs::load_prefabs; -use settings::GameSettings; +use settings::load_settings; use camera::compute_cameras; use events::{clear_events, process_glutin_events}; use input::{init_input, process_inputs}; @@ -51,15 +52,18 @@ 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}; fn startup() -> Workload { ( - init_delta_time, + load_settings, load_prefabs, init_selection_box_buffers, + insert_lock_state, init_input, init_game_world, spawn_player, + init_delta_time, ).into_workload() } fn update() -> Workload { @@ -69,6 +73,7 @@ fn update() -> Workload { update_loaded_world_around_player, update_raycasts, block_placement_system, + update_cursor_lock_state, compute_cameras ).into_workload() } @@ -97,7 +102,6 @@ fn main() { //Add systems and uniques, Init and load things world.add_unique_non_send_sync(Renderer::init(&event_loop)); world.add_unique(BackgroundColor(vec3(0.5, 0.5, 1.))); - world.add_unique(GameSettings::default()); //Register workloads world.add_workload(startup); diff --git a/src/settings.rs b/src/settings.rs index 5e80b67..ceff83c 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,4 +1,4 @@ -use shipyard::Unique; +use shipyard::{Unique, AllStoragesView}; #[derive(Unique)] pub struct GameSettings { @@ -14,3 +14,10 @@ impl Default for GameSettings { } } } + +pub fn load_settings( + storages: AllStoragesView +) { + //todo + storages.add_unique(GameSettings::default()); +}