diff --git a/src/ui/mod.rs b/src/ui/mod.rs index c3df9bd..4796c6d 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -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;