Merge branch 'huntertparks/fix-cursor-not-locking' into 'master'

Fixes #520 and #840 - locks cursor to screen when menu is not open

Closes #840 and #520

See merge request veloren/veloren!2069
This commit is contained in:
Marcel 2021-04-05 21:40:24 +00:00
commit 4dde9f84b0
3 changed files with 108 additions and 61 deletions

View File

@ -44,6 +44,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) - 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 - Added a raycast check to beams to prevent their effect applying through walls
- Flying agents raycast more angles to check for obstacles. - Flying agents raycast more angles to check for obstacles.
- Mouse Cursor now locks to the center of the screen when menu is not open
## [0.9.0] - 2021-03-20 ## [0.9.0] - 2021-03-20

View File

@ -731,6 +731,24 @@ impl Show {
fn selected_crafting_tab(&mut self, sel_cat: SelectedCraftingTab) { fn selected_crafting_tab(&mut self, sel_cat: SelectedCraftingTab) {
self.crafting_tab = sel_cat; 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 { pub struct PromptDialogSettings {
@ -3340,7 +3358,8 @@ impl Hud {
}, },
// Press key while not typing // 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 => { GameInput::Command if state => {
self.force_chat_input = Some("/".to_owned()); self.force_chat_input = Some("/".to_owned());
self.force_chat_cursor = Some(Index { line: 0, char: 1 }); self.force_chat_cursor = Some(Index { line: 0, char: 1 });
@ -3401,6 +3420,12 @@ impl Hud {
false 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 // Else the player is typing in chat
WinEvent::InputUpdate(_key, _) => self.typing(), WinEvent::InputUpdate(_key, _) => self.typing(),

View File

@ -1036,7 +1036,11 @@ impl Window {
self.events.push(Event::Focused(state)); self.events.push(Event::Focused(state));
}, },
WindowEvent::CursorMoved { position, .. } => { WindowEvent::CursorMoved { position, .. } => {
if self.cursor_grabbed {
self.center_cursor();
} else {
self.cursor_position = position; self.cursor_position = position;
}
}, },
WindowEvent::MouseWheel { delta, .. } if self.cursor_grabbed && self.focused => { WindowEvent::MouseWheel { delta, .. } if self.cursor_grabbed && self.focused => {
const DIFFERENCE_FROM_DEVICE_EVENT_ON_X11: f32 = -15.0; const DIFFERENCE_FROM_DEVICE_EVENT_ON_X11: f32 = -15.0;
@ -1090,6 +1094,23 @@ impl Window {
let _ = self.window.window().set_cursor_grab(grab); 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) { pub fn toggle_fullscreen(&mut self, settings: &mut Settings) {
let fullscreen = FullScreenSettings { let fullscreen = FullScreenSettings {
enabled: !self.is_fullscreen(), enabled: !self.is_fullscreen(),