From 6af9662b03c43dd8cc85d41d60d6b8e9def58fda Mon Sep 17 00:00:00 2001 From: Hunter Parks Date: Sun, 28 Mar 2021 14:24:02 -0700 Subject: [PATCH] Change cursor lock location to be center of screen Changing the cursor coordinates to the middle of the screen had some side effects. When a user would close a menu and re-open it back up without moving the mouse, the cursor position would not change. This commit takes care of that by changing how the HUD handles closing menus via keyboard inputs. --- CHANGELOG.md | 2 +- voxygen/src/hud/mod.rs | 145 ++++++++++++++++++++++++----------------- voxygen/src/window.rs | 25 +++++-- 3 files changed, 104 insertions(+), 68 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6395a379d2..3d967dd624 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,7 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Server kicks old client when a user is trying to log in again (often the case when a user's original connection gets dropped) - Added a raycast check to beams to prevent their effect applying through walls - Flying agents raycast more angles to check for obstacles. -- Mouse Cursor now stays in the same position when menu is not open +- Mouse Cursor now locks to the center of the screen when menu is not open ## [0.9.0] - 2021-03-20 diff --git a/voxygen/src/hud/mod.rs b/voxygen/src/hud/mod.rs index 61f79ba9f5..06cc442f38 100644 --- a/voxygen/src/hud/mod.rs +++ b/voxygen/src/hud/mod.rs @@ -731,6 +731,24 @@ impl Show { fn selected_crafting_tab(&mut self, sel_cat: SelectedCraftingTab) { self.crafting_tab = sel_cat; } + + /// If all of the menus are closed, adjusts coordinates of cursor to center + /// of screen + fn toggle_cursor_on_menu_close(&self, global_state: &mut GlobalState) { + if !self.bag + && !self.trade + && !self.esc_menu + && !self.map + && !self.social + && !self.crafting + && !self.diary + && !self.help + && !self.intro + && global_state.window.is_cursor_grabbed() + { + global_state.window.center_cursor(); + } + } } pub struct PromptDialogSettings { @@ -3340,67 +3358,74 @@ impl Hud { }, // Press key while not typing - WinEvent::InputUpdate(key, state) if !self.typing() => match key { - GameInput::Command if state => { - self.force_chat_input = Some("/".to_owned()); - self.force_chat_cursor = Some(Index { line: 0, char: 1 }); - self.ui.focus_widget(Some(self.ids.chat)); - true - }, - GameInput::Map if state => { - self.show.toggle_map(); - true - }, - GameInput::Bag if state => { - self.show.toggle_bag(); - true - }, - GameInput::Social if state => { - self.show.toggle_social(); - true - }, - GameInput::Crafting if state => { - self.show.toggle_crafting(); - true - }, - GameInput::Spellbook if state => { - self.show.toggle_spell(); - true - }, - GameInput::Settings if state => { - self.show.toggle_settings(global_state); - true - }, - GameInput::Help if state => { - self.show.toggle_settings(global_state); - self.show.settings_tab = SettingsTab::Controls; - true - }, - GameInput::ToggleDebug if state => { - global_state.settings.interface.toggle_debug = - !global_state.settings.interface.toggle_debug; - self.show.debug = global_state.settings.interface.toggle_debug; - true - }, - GameInput::ToggleIngameUi if state => { - self.show.ingame = !self.show.ingame; - true - }, - // Skillbar - input => { - if let Some(slot) = try_hotbar_slot_from_input(input) { - handle_slot( - slot, - state, - &mut self.events, - &mut self.slot_manager, - &mut self.hotbar, - ); + WinEvent::InputUpdate(key, state) if !self.typing() => { + let matching_key = match key { + GameInput::Command if state => { + self.force_chat_input = Some("/".to_owned()); + self.force_chat_cursor = Some(Index { line: 0, char: 1 }); + self.ui.focus_widget(Some(self.ids.chat)); true - } else { - false - } - }, + }, + GameInput::Map if state => { + self.show.toggle_map(); + true + }, + GameInput::Bag if state => { + self.show.toggle_bag(); + true + }, + GameInput::Social if state => { + self.show.toggle_social(); + true + }, + GameInput::Crafting if state => { + self.show.toggle_crafting(); + true + }, + GameInput::Spellbook if state => { + self.show.toggle_spell(); + true + }, + GameInput::Settings if state => { + self.show.toggle_settings(global_state); + true + }, + GameInput::Help if state => { + self.show.toggle_settings(global_state); + self.show.settings_tab = SettingsTab::Controls; + true + }, + GameInput::ToggleDebug if state => { + global_state.settings.interface.toggle_debug = + !global_state.settings.interface.toggle_debug; + self.show.debug = global_state.settings.interface.toggle_debug; + true + }, + GameInput::ToggleIngameUi if state => { + self.show.ingame = !self.show.ingame; + true + }, + // Skillbar + input => { + if let Some(slot) = try_hotbar_slot_from_input(input) { + handle_slot( + slot, + state, + &mut self.events, + &mut self.slot_manager, + &mut self.hotbar, + ); + true + } else { + false + } + }, + }; + + // When a player closes all menus, resets the cursor + // to the center of the screen + self.show.toggle_cursor_on_menu_close(global_state); + matching_key }, // Else the player is typing in chat WinEvent::InputUpdate(_key, _) => self.typing(), diff --git a/voxygen/src/window.rs b/voxygen/src/window.rs index 10eb09fc10..b8afc8b27b 100644 --- a/voxygen/src/window.rs +++ b/voxygen/src/window.rs @@ -1037,13 +1037,7 @@ impl Window { }, WindowEvent::CursorMoved { position, .. } => { if self.cursor_grabbed { - if let Err(err) = self - .window - .window() - .set_cursor_position(self.cursor_position) - { - error!("Error setting cursor position: {:?}", err); - } + self.center_cursor(); } else { self.cursor_position = position; } @@ -1100,6 +1094,23 @@ impl Window { let _ = self.window.window().set_cursor_grab(grab); } + /// Moves mouse cursor to center of screen + /// based on the window dimensions + pub fn center_cursor(&self) { + let dimensions: Vec2 = self.logical_size(); + + if let Err(err) = + self.window + .window() + .set_cursor_position(winit::dpi::PhysicalPosition::new( + dimensions[0] / (2_f64), + dimensions[1] / (2_f64), + )) + { + error!("Error centering cursor position: {:?}", err); + } + } + pub fn toggle_fullscreen(&mut self, settings: &mut Settings) { let fullscreen = FullScreenSettings { enabled: !self.is_fullscreen(),