Improve cursor grab

Former-commit-id: c1a8b5383bf7682245e2294e5b1d62dae0beb329
This commit is contained in:
timokoesters
2019-05-07 22:00:03 +02:00
parent 7132cfdf5b
commit a15532f81a

View File

@ -110,15 +110,19 @@ pub struct Show {
map: bool, map: bool,
inventory_test_button: bool, inventory_test_button: bool,
mini_map: bool, mini_map: bool,
want_grab: Option<bool>,
} }
impl Show { impl Show {
fn toggle_bag(&mut self) { fn toggle_bag(&mut self) {
self.bag = !self.bag self.bag = !self.bag;
self.want_grab = Some(!self.bag);
} }
fn toggle_map(&mut self) { fn toggle_map(&mut self) {
self.map = !self.map; self.map = !self.map;
self.bag = false; self.bag = false;
self.want_grab = Some(!self.map);
} }
fn toggle_mini_map(&mut self) { fn toggle_mini_map(&mut self) {
@ -155,6 +159,7 @@ impl Show {
_ => Windows::Settings, _ => Windows::Settings,
}; };
self.bag = false; self.bag = false;
self.want_grab = Some(self.open_windows != Windows::Settings);
} }
fn toggle_help(&mut self) { fn toggle_help(&mut self) {
@ -165,7 +170,7 @@ impl Show {
self.ui = !self.ui; self.ui = !self.ui;
} }
fn toggle_windows(&mut self, global_state: &mut GlobalState) { fn toggle_windows(&mut self) {
if self.bag if self.bag
|| self.esc_menu || self.esc_menu
|| self.map || self.map
@ -178,10 +183,10 @@ impl Show {
self.esc_menu = false; self.esc_menu = false;
self.map = false; self.map = false;
self.open_windows = Windows::None; self.open_windows = Windows::None;
global_state.window.grab_cursor(true); self.want_grab = Some(true);
} else { } else {
self.esc_menu = true; self.esc_menu = true;
global_state.window.grab_cursor(false); self.want_grab = Some(false);
} }
} }
} }
@ -227,6 +232,7 @@ impl Hud {
ui: true, ui: true,
inventory_test_button: false, inventory_test_button: false,
mini_map: false, mini_map: false,
want_grab: None,
}, },
to_focus: None, to_focus: None,
settings, settings,
@ -449,7 +455,7 @@ impl Hud {
pub fn handle_event(&mut self, event: WinEvent, global_state: &mut GlobalState) -> bool { pub fn handle_event(&mut self, event: WinEvent, global_state: &mut GlobalState) -> bool {
let cursor_grabbed = global_state.window.is_cursor_grabbed(); let cursor_grabbed = global_state.window.is_cursor_grabbed();
match event { let handled = match event {
WinEvent::Ui(event) => { WinEvent::Ui(event) => {
if (self.typing() && event.is_keyboard() && self.show.ui) if (self.typing() && event.is_keyboard() && self.show.ui)
|| !(cursor_grabbed && event.is_keyboard_or_mouse()) || !(cursor_grabbed && event.is_keyboard_or_mouse())
@ -477,7 +483,7 @@ impl Hud {
self.ui.focus_widget(None); self.ui.focus_widget(None);
} else { } else {
// Close windows on esc // Close windows on esc
self.show.toggle_windows(global_state); self.show.toggle_windows();
} }
true true
} }
@ -526,7 +532,13 @@ impl Hud {
true true
} }
_ => false, _ => false,
};
// Handle cursor grab
if let Some(state) = self.show.want_grab {
global_state.window.grab_cursor(state);
self.show.want_grab = None;
} }
return handled;
} }
pub fn maintain(&mut self, renderer: &mut Renderer, tps: f64) -> Vec<Event> { pub fn maintain(&mut self, renderer: &mut Renderer, tps: f64) -> Vec<Event> {