diff --git a/crates/horizon-client/src/options_horizon.rs b/crates/horizon-client/src/options_horizon.rs index 634bb8d75..7e2f7e9f1 100644 --- a/crates/horizon-client/src/options_horizon.rs +++ b/crates/horizon-client/src/options_horizon.rs @@ -207,8 +207,15 @@ const DONE: u64 = id("options.done", 0); const ROW: u64 = id("options.row", 0); const STYLE: u64 = id("options.style", 0); const LEVEL: u64 = id("options.level", 0); +const SEARCH: u64 = id("options.search", 0); const ROW_H: f32 = 12.0 + CONTROL + 12.0; const HEADER_H: f32 = 40.0; +pub(crate) fn searchable_section(section: usize) -> bool { + matches!( + SECTIONS.get(section).map(|s| s.name), + Some("Video" | "Controls") + ) +} /// Whether an option is an on/off switch, and its value. fn switch_value(opt: Opt, s: &Settings) -> Option { @@ -258,12 +265,29 @@ fn caption_value(opt: Opt, s: &Settings) -> (String, String) { enum Target { Nav(usize), Done, + Search, Row(usize), Slider(Item, f32), Style(usize), Level(usize), } impl OptionsUi { + fn horizon_rows(&self, s: &Settings, section: usize) -> Vec { + let rows = (SECTIONS[section].rows)(s); + let query = self.settings_search.value.to_lowercase(); + if query.is_empty() || !searchable_section(section) { + return rows; + } + rows.into_iter() + .filter(|row| match row { + HRow::Opt(opt) => caption_value(*opt, s).0.to_lowercase().contains(&query), + HRow::Graphics(item, ..) => item.caption_value(s).0.to_lowercase().contains(&query), + HRow::Page(label, ..) => label.to_lowercase().contains(&query), + HRow::Level => true, + _ => false, + }) + .collect() + } /// Lay out (and draw, on a real frame) the Horizon options shell. /// Returns the sliders' tracks (option, x, width) for dragging. fn horizon_layout( @@ -307,8 +331,17 @@ impl OptionsUi { let sec = &SECTIONS[section]; let mut y = 36.0; y += ui.page_header(x, y, sec.name, sec.blurb) + 24.0; + if searchable_section(section) { + ui.text_field( + SEARCH, + [x + cw - 240.0, y, 240.0, CONTROL], + &self.settings_search.value, + self.settings_search.focused, + ); + y += CONTROL + 12.0; + } let top = y; - let rows = (sec.rows)(s); + let rows = self.horizon_rows(s, section); let content: f32 = rows .iter() .map(|r| { @@ -535,6 +568,9 @@ impl OptionsUi { let mut ui = Ui::dry(atlas, &mut batch, (x, y)); let sliders = self.horizon_layout(s, &mut ui, 1.0); let hit = ui.hit((x, y))?; + if hit == SEARCH { + return Some(Target::Search); + } let section = self.section.min(SECTIONS.len() - 1); if (NAV..NAV + SECTIONS.len() as u64).contains(&hit) { return Some(Target::Nav((hit - NAV) as usize)); @@ -549,7 +585,7 @@ impl OptionsUi { return Some(Target::Level((hit - LEVEL) as usize)); } let i = hit.checked_sub(ROW)? as usize; - let rows = (SECTIONS[section].rows)(s); + let rows = self.horizon_rows(s, section); let item = match rows.get(i) { Some(HRow::Opt(opt)) => Some(Item::Vanilla(*opt)), Some(HRow::Graphics(item, ..)) => Some(*item), @@ -598,11 +634,20 @@ impl OptionsUi { }, ); } - match self.horizon_target(s, atlas, x, y) { + let target = self.horizon_target(s, atlas, x, y); + self.settings_search.focused = false; + match target { Some(Target::Nav(i)) => { + if searchable_section(self.section) || searchable_section(i) { + self.settings_search.set_value(""); + } self.section = i; self.hscroll = 0.0; } + Some(Target::Search) => { + self.settings_search.focused = true; + self.settings_search.move_cursor_to_end(false); + } Some(Target::Done) => events.extend(self.back(s)), Some(Target::Style(i)) => { t::set_style(if i == 1 { @@ -626,7 +671,7 @@ impl OptionsUi { } Some(Target::Row(i)) => { let section = self.section.min(SECTIONS.len() - 1); - match (SECTIONS[section].rows)(s).get(i) { + match self.horizon_rows(s, section).get(i) { Some(HRow::Opt(opt)) => { opt.cycle(s, right, framebuffer); if *opt == Opt::Preset { diff --git a/crates/horizon-client/src/options_ui.rs b/crates/horizon-client/src/options_ui.rs index fab36defa..994cc4793 100644 --- a/crates/horizon-client/src/options_ui.rs +++ b/crates/horizon-client/src/options_ui.rs @@ -823,6 +823,7 @@ pub struct OptionsUi { pub languages: Vec, language_choice: Option, language_search: ui::EditBox, + pub(crate) settings_search: ui::EditBox, /// Packs: the packs found and the selection the screen opens with. pub pack_infos: Vec, pub pack_selection: Vec, @@ -841,6 +842,8 @@ impl Default for OptionsUi { fn default() -> Self { let mut search = ui::EditBox::new(0.0, 0.0, 200.0, 15.0); search.max_length = 32; + let mut settings_search = ui::EditBox::new(0.0, 0.0, 200.0, 15.0); + settings_search.focused = false; Self { stack: vec![Page::Main], scroll: 0.0, @@ -857,6 +860,7 @@ impl Default for OptionsUi { languages: Vec::new(), language_choice: None, language_search: search, + settings_search, pack_infos: Vec::new(), pack_selection: Vec::new(), pack_screen: None, @@ -918,6 +922,13 @@ fn pairs(opts: &[Opt]) -> Vec { .collect() } impl OptionsUi { + fn settings_search_active(&self) -> bool { + self.settings_search.focused + && (matches!(self.page(), Page::Video | Page::Controls | Page::KeyBinds) + || (crate::ui_theme::horizon() + && self.page() == Page::Main + && crate::options_horizon::searchable_section(self.section))) + } pub fn page(&self) -> Page { *self.stack.last().unwrap_or(&Page::Main) } @@ -945,6 +956,10 @@ impl OptionsUi { self.stack.push(page); self.scroll = 0.0; self.scrollbar_drag_y = None; + if matches!(page, Page::Video | Page::Controls | Page::KeyBinds) { + self.settings_search.set_value(""); + self.settings_search.focused = false; + } if page == Page::Language { self.language_choice = None; self.language_search.set_value(""); @@ -993,6 +1008,33 @@ impl OptionsUi { _ => 163.0, } } + fn filter_option_rows(&self, rows: Vec) -> Vec { + let query = self.settings_search.value.to_lowercase(); + if query.is_empty() { + return rows; + } + rows.into_iter() + .flat_map(|row| match row { + Row::Big(opt) if lang::get(opt.caption()).to_lowercase().contains(&query) => { + vec![Row::Big(opt)] + } + Row::Pair(a, b) => { + let matches = + |opt: Opt| lang::get(opt.caption()).to_lowercase().contains(&query); + pairs( + &[Some(a), b] + .into_iter() + .flatten() + .filter(|opt| matches(*opt)) + .collect::>(), + ) + } + // Keep navigation available and omit section headers with no visible options. + Row::Buttons(a, b) => vec![Row::Buttons(a, b)], + _ => Vec::new(), + }) + .collect() + } fn list_rows(&self, s: &Settings) -> Vec { match self.page() { Page::Extras => crate::extras::rows() @@ -1080,7 +1122,7 @@ impl OptionsUi { Opt::AttackIndicator, Opt::ChunkFade, ])); - rows + self.filter_option_rows(rows) } Page::Sound => { let mut rows = vec![Row::Big(Opt::Volume(0))]; @@ -1122,7 +1164,7 @@ impl OptionsUi { ), None, )); - rows + self.filter_option_rows(rows) } Page::Controller => { let mut rows = vec![Row::Buttons( @@ -1227,6 +1269,10 @@ impl OptionsUi { let mut out = Vec::new(); let page = self.page(); let cx = (w / 2.0).floor(); + if matches!(page, Page::Video | Page::Controls | Page::KeyBinds) { + self.settings_search.x = cx - 100.0; + self.settings_search.y = 17.0; + } let done = |y: f32| button(cx - 100.0, y, 200.0, tr("gui.done"), Act::Done); // (list top, list bottom, content height) let (mut top, mut bottom, mut content) = (33.0, h - 33.0, 0.0); @@ -1276,9 +1322,10 @@ impl OptionsUi { bottom = 0.0; } Page::KeyBinds => { + top = 38.0; out.push(Widget { x: 0.0, - y: 12.0, + y: 4.0, w, h: 9.0, kind: WKind::Text { @@ -1290,6 +1337,10 @@ impl OptionsUi { }); let mut mappings: Vec<(&str, &str)> = MAPPINGS.iter().map(|(n, _, c)| (*n, *c)).collect(); + let query = self.settings_search.value.to_lowercase(); + mappings.retain(|(name, _)| { + query.is_empty() || lang::get(name).to_lowercase().contains(&query) + }); mappings.sort_by(|a, b| { let ca = CATEGORIES.iter().position(|c| *c == a.1).unwrap_or(99); let cb = CATEGORIES.iter().position(|c| *c == b.1).unwrap_or(99); @@ -1558,9 +1609,16 @@ impl OptionsUi { // Drawn by `pack_screen`. Page::Packs => {} _ => { + if matches!(page, Page::Video | Page::Controls) { + top = 38.0; + } out.push(Widget { x: 0.0, - y: 12.0, + y: if matches!(page, Page::Video | Page::Controls) { + 4.0 + } else { + 12.0 + }, w, h: 9.0, kind: WKind::Text { @@ -1881,6 +1939,23 @@ impl OptionsUi { } self.language_search = search; } + if matches!(self.page(), Page::Video | Page::Controls | Page::KeyBinds) { + let mut search = self.settings_search.clone(); + search.draw(batch, atlas, None); + if search.value.is_empty() && !search.focused { + batch.text_styled( + atlas, + &Text::literal("Search settings...") + .with_color(text::GRAY) + .italic(), + search.text_x(), + search.text_y(), + u32::MAX, + true, + ); + } + self.settings_search = search; + } if let Some(lines) = tooltip { ui::tooltip(batch, atlas, &lines, cursor); } @@ -1964,6 +2039,16 @@ impl OptionsUi { } let (widgets, top, bottom, content) = self.layout(s, size.0, size.1); let mut events = Vec::new(); + if !drag && matches!(self.page(), Page::Video | Page::Controls | Page::KeyBinds) { + let search = &mut self.settings_search; + search.focused = ui::hovered((x, y), search.x, search.y, search.width, search.height); + if search.focused { + search.click(atlas, x, false, false); + self.scroll = 0.0; + events.push(Event::ButtonClick); + return events; + } + } if drag { if let Some(previous_y) = self.scrollbar_drag_y { if let Some((_, thumb)) = ui::scrollbar_thumb(top, bottom, content, self.scroll) { @@ -2238,6 +2323,9 @@ impl OptionsUi { if self.page() == Page::Language { self.language_search.insert_text(text); self.scroll = 0.0; + } else if self.settings_search_active() { + self.settings_search.insert_text(text); + self.scroll = 0.0; } } /// Ctrl+A/C/V/X in the language or pack search box; false when neither @@ -2263,6 +2351,12 @@ impl OptionsUi { } return true; } + if self.settings_search_active() { + if self.settings_search.shortcut(shortcut, clipboard) { + self.scroll = 0.0; + } + return true; + } false } fn bind_key(&mut self, s: &mut Settings, value: String) -> Vec { @@ -2346,6 +2440,16 @@ impl OptionsUi { } } self.scroll = 0.0; + } else if self.settings_search_active() { + if !self.settings_search.key(key, false, false) + && let Some(t) = text + { + let typed: String = t.chars().filter(|c| !c.is_control()).collect(); + if !typed.is_empty() { + self.settings_search.insert_text(&typed); + } + } + self.scroll = 0.0; } Vec::new() } diff --git a/crates/horizon-client/src/ui_theme.rs b/crates/horizon-client/src/ui_theme.rs index 2cb3b5315..7f99e9830 100644 --- a/crates/horizon-client/src/ui_theme.rs +++ b/crates/horizon-client/src/ui_theme.rs @@ -1042,6 +1042,49 @@ impl<'a> Ui<'a> { self.opacity = saved; hover } + /// A search field styled like the launcher's rounded text inputs. + pub fn text_field(&mut self, id: u64, rect: [f32; 4], value: &str, focused: bool) { + let [x, y, w, h] = rect; + self.record_label(value, rect, true, crate::widgets::Kind::Button); + let hover = self.interact(id, x, y, w, h, true); + self.rounded( + x, + y, + w, + h, + 8.0, + if focused { color::HOVER } else { color::RAISED }, + ); + self.rounded( + x, + y, + w, + h, + 8.0, + color::with_alpha(color::HOVER, hover * 0.5), + ); + let display = if value.is_empty() { + "Search settings".to_owned() + } else if focused { + format!("{value}|") + } else { + value.to_owned() + }; + let display = self.fit(Face::Regular, BODY, &display, w - 24.0); + self.text_line( + Face::Regular, + BODY, + h, + x + 12.0, + y, + if value.is_empty() { + color::FAINT + } else { + color::TEXT + }, + &display, + ); + } /// The width the launcher's button gives a label (padding 16, or 24 large). pub fn button_width(&self, label: &str, large: bool) -> f32 { let (size, pad) = if large { (LARGE, 24.0) } else { (BODY, 16.0) };