ui: click TextBox to focus, fixes #415 (#475)

Previously text boxes could only be focused by cycling through
focus with the tab key, now you can click with the mouse as well.
This commit is contained in:
iceiix 2021-01-15 16:42:20 -08:00 committed by GitHub
parent 2f08e14b1c
commit 4dd25de709
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 0 deletions

View File

@ -379,10 +379,19 @@ impl Container {
let mx = (x / width) * SCALED_WIDTH;
let my = (y / height) * SCALED_HEIGHT;
let mut clicked_element: Option<&Element> = None;
for e in &self.elements {
let r = Self::compute_draw_region(e, sw, sh, &SCREEN);
if mx >= r.x && mx <= r.x + r.w && my >= r.y && my <= r.y + r.h {
e.click_at(&r, game, mx, my, sw, sh);
clicked_element = Some(e);
}
}
if let Some(e) = clicked_element {
if let Element::TextBox(_) = e {
self.clear_focus();
e.set_focused(true);
}
}
}
@ -391,6 +400,12 @@ impl Container {
self.focusable_elements.push(el);
}
fn clear_focus(&self) {
for e in &self.elements {
e.set_focused(false);
}
}
pub fn cycle_focus(&mut self) {
if self.focusable_elements.is_empty() {
return;