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)
- 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 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,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(),

View File

@ -1036,7 +1036,11 @@ impl Window {
self.events.push(Event::Focused(state));
},
WindowEvent::CursorMoved { position, .. } => {
self.cursor_position = position;
if self.cursor_grabbed {
self.center_cursor();
} else {
self.cursor_position = position;
}
},
WindowEvent::MouseWheel { delta, .. } if self.cursor_grabbed && self.focused => {
const DIFFERENCE_FROM_DEVICE_EVENT_ON_X11: f32 = -15.0;
@ -1090,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(),