simplify chat keybaord capture

Former-commit-id: 3c5c5c241fe0c0d97059c7b87c0c08104eb5c5fe
This commit is contained in:
Imbris 2019-04-14 20:45:54 -04:00
parent b7439b9107
commit e755afce91
2 changed files with 26 additions and 46 deletions

View File

@ -402,7 +402,6 @@ pub struct Hud {
ids: Ids, ids: Ids,
imgs: Imgs, imgs: Imgs,
chat: chat::Chat, chat: chat::Chat,
typing: bool,
cursor_grabbed: bool, cursor_grabbed: bool,
font_metamorph: FontId, font_metamorph: FontId,
font_opensans: FontId, font_opensans: FontId,
@ -456,7 +455,6 @@ impl Hud {
imgs, imgs,
ids, ids,
chat, chat,
typing: false,
cursor_grabbed: true, cursor_grabbed: true,
settings_tab: SettingsTab::Interface, settings_tab: SettingsTab::Interface,
show_help: true, show_help: true,
@ -1443,14 +1441,6 @@ impl Hud {
}; };
} }
// update whether keyboard is captured
self.typing =
if let Some(widget_id) = ui_widgets.global_input().current.widget_capturing_keyboard {
widget_id == self.chat.input_box_id()
} else {
false
};
events events
} }
@ -1530,10 +1520,17 @@ impl Hud {
self.cursor_grabbed = cursor_grabbed; self.cursor_grabbed = cursor_grabbed;
} }
fn typing(&self) -> bool {
match self.ui.widget_capturing_keyboard() {
Some(id) if id == self.chat.input_box_id() => true,
_ => false,
}
}
pub fn handle_event(&mut self, event: WinEvent) -> bool { pub fn handle_event(&mut self, event: WinEvent) -> bool {
match event { match event {
WinEvent::Ui(event) => { WinEvent::Ui(event) => {
if (self.typing && event.is_keyboard()) if (self.typing() && event.is_keyboard())
|| !(self.cursor_grabbed && event.is_keyboard_or_mouse()) || !(self.cursor_grabbed && event.is_keyboard_or_mouse())
{ {
self.ui.handle_event(event); self.ui.handle_event(event);
@ -1542,18 +1539,15 @@ impl Hud {
} }
WinEvent::Zoom(_) => !self.ui.no_widget_capturing_mouse(), WinEvent::Zoom(_) => !self.ui.no_widget_capturing_mouse(),
WinEvent::KeyDown(Key::Enter) => { WinEvent::KeyDown(Key::Enter) => {
if self.typing { self.ui.focus_widget(if self.typing() {
self.ui.focus_widget(None); None
self.typing = false;
} else { } else {
self.ui.focus_widget(Some(self.chat.input_box_id())); Some(self.chat.input_box_id())
self.typing = true; });
};
true true
} }
WinEvent::KeyDown(Key::Escape) => { WinEvent::KeyDown(Key::Escape) => {
if self.typing { if self.typing() {
self.typing = false;
self.ui.focus_widget(None); self.ui.focus_widget(None);
} else { } else {
// Close windows on esc // Close windows on esc
@ -1561,7 +1555,7 @@ impl Hud {
} }
true true
} }
WinEvent::KeyDown(key) if !self.typing => match key { WinEvent::KeyDown(key) if !self.typing() => match key {
Key::Map => { Key::Map => {
self.toggle_map(); self.toggle_map();
true true
@ -1602,9 +1596,9 @@ impl Hud {
}, },
WinEvent::KeyDown(key) | WinEvent::KeyUp(key) => match key { WinEvent::KeyDown(key) | WinEvent::KeyUp(key) => match key {
Key::ToggleCursor => false, Key::ToggleCursor => false,
_ => self.typing, _ => self.typing(),
}, },
WinEvent::Char(_) => self.typing, WinEvent::Char(_) => self.typing(),
_ => false, _ => false,
} }
} }

View File

@ -246,31 +246,17 @@ impl Ui {
self.ui.set_widgets() self.ui.set_widgets()
} }
// Workaround because conrod currently gives us no way to programmatically set which widget is capturing key input // Accepts option so widget can be unfocused
// Note the widget must be visible and not covered by other widgets at its center for this to work
// Accepts option so widget can be "unfocused"
// TODO: try https://docs.rs/conrod_core/0.63.0/conrod_core/struct.Ui.html#method.keyboard_capture
pub fn focus_widget(&mut self, id: Option<WidgId>) { pub fn focus_widget(&mut self, id: Option<WidgId>) {
let (x, y) = match id { self.ui.keyboard_capture(match id {
// get position of widget Some(id) => id,
Some(id) => if let Some([x, y]) = self.ui.xy_of(id) { None => self.ui.window,
(x, y) });
} else { }
return;
}, // Get id of current widget capturing keyboard
// outside window (origin is center) pub fn widget_capturing_keyboard(&self) -> Option<WidgId> {
None => (self.ui.win_h, self.ui.win_w) self.ui.global_input().current.widget_capturing_keyboard
};
// things to consider: does cursor need to be moved back?, should we check if the mouse if already pressed and then trigger a release?
self.ui.handle_event(
Input::Motion(Motion::MouseCursor { x, y })
);
self.ui.handle_event(
Input::Press(Button::Mouse(MouseButton::Left))
);
self.ui.handle_event(
Input::Release(Button::Mouse(MouseButton::Left))
);
} }
// Get whether the a widget besides the window is capturing the mouse // Get whether the a widget besides the window is capturing the mouse