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.
This commit is contained in:
Hunter Parks 2021-03-28 14:24:02 -07:00 committed by Marcel Märtens
parent 7df0413be4
commit 6af9662b03
3 changed files with 104 additions and 68 deletions

View File

@ -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

View File

@ -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,7 +3358,8 @@ impl Hud {
},
// Press key while not typing
WinEvent::InputUpdate(key, state) if !self.typing() => match key {
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 });
@ -3401,6 +3420,12 @@ impl Hud {
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(),

View File

@ -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<f64> = 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(),