From 8e4b35eb0707a3755d76b3af512e6709001b987e Mon Sep 17 00:00:00 2001 From: DeltaNedas <39013340+DeltaNedas@users.noreply.github.com> Date: Sat, 26 Dec 2020 16:22:11 +0000 Subject: [PATCH] SearchBar class - add search bars to mods (#3267) * Create SearchBar and make ModsDialog use it * add search bar to servers * Update core/src/mindustry/ui/SearchBar.java Co-authored-by: Antsiferov Andrew * a * fix mods menu not rowing * remove search bar for remote servers * h * hide searchbar on landscape mobile * anukes feedback * Update core/src/mindustry/ui/SearchBar.java Co-authored-by: Anuken * java dumb Co-authored-by: Antsiferov Andrew Co-authored-by: Anuken --- core/src/mindustry/ui/SearchBar.java | 57 +++++++++++++++++++ core/src/mindustry/ui/dialogs/ModsDialog.java | 30 +++++----- 2 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 core/src/mindustry/ui/SearchBar.java diff --git a/core/src/mindustry/ui/SearchBar.java b/core/src/mindustry/ui/SearchBar.java new file mode 100644 index 0000000000..162c18ac54 --- /dev/null +++ b/core/src/mindustry/ui/SearchBar.java @@ -0,0 +1,57 @@ +package mindustry.ui; + +import arc.func.*; +import arc.scene.ui.layout.*; +import arc.struct.*; +import mindustry.gen.*; + +public class SearchBar{ + public static Table add(Table parent, Seq list, Func queryf, + Func namef, Cons2 itemc, Cons> barc){ + Cons[] rebuild = new Cons[]{null}; + Table[] pane = {null}; + + barc.get(parent.table(search -> { + rebuild[0] = str -> { + String query = queryf.get(str); + + pane[0].clear(); + list.each(item -> { + if(query.isEmpty() || matches(query, namef.get(item))){ + itemc.get(pane[0], item); + } + }); + }; + + search.image(Icon.zoom).padRight(8f); + search.field("", rebuild[0]).growX(); + }).fillX().padBottom(4)); + + parent.row(); + parent.pane(table -> { + pane[0] = table; + rebuild[0].get(""); + }); + return pane[0]; + } + + public static Table add(Table parent, Seq list, Func queryf, Func namef, Cons2 itemc){ + return add(parent, list, queryf, namef, itemc, c -> {}); + } + + public static Table add(Table parent, Seq list, Func namef, Cons2 itemc, Cons> barc){ + return add(parent, list, String::toLowerCase, namef, itemc, barc); + } + + public static Table add(Table parent, Seq list, Func namef, Cons2 itemc){ + return add(parent, list, String::toLowerCase, namef, itemc); + } + + /** Match a list item with the search query, case insensitive */ + public static boolean matches(String query, String name){ + if(name == null || name.isEmpty()){ + return false; + } + return name.toLowerCase().contains(query); + } +} diff --git a/core/src/mindustry/ui/dialogs/ModsDialog.java b/core/src/mindustry/ui/dialogs/ModsDialog.java index 5e35e0659b..f73ada4959 100644 --- a/core/src/mindustry/ui/dialogs/ModsDialog.java +++ b/core/src/mindustry/ui/dialogs/ModsDialog.java @@ -37,6 +37,9 @@ public class ModsDialog extends BaseDialog{ buttons.button("@mods.guide", Icon.link, () -> Core.app.openURI(modGuideURL)).size(210, 64f); shown(this::setup); + if(mobile){ + onResize(this::setup); + } hidden(() -> { if(mods.requiresReload()){ @@ -228,14 +231,12 @@ public class ModsDialog extends BaseDialog{ cont.row(); if(!mods.list().isEmpty()){ - cont.pane(table -> { - table.margin(10f).top(); - - boolean anyDisabled = false; - for(LoadedMod mod : mods.list()){ - - if(!mod.enabled() && !anyDisabled && mods.list().size > 0){ - anyDisabled = true; + boolean[] anyDisabled = {false}; + SearchBar.add(cont, mods.list(), + mod -> mod.meta.displayName(), + (table, mod) -> { + if(!mod.enabled() && !anyDisabled[0] && mods.list().size > 0){ + anyDisabled[0] = true; table.row(); table.image().growX().height(4f).pad(6f).color(Pal.gray); table.row(); @@ -309,20 +310,19 @@ public class ModsDialog extends BaseDialog{ }).size(50f); } }).growX().right().padRight(-8f).padTop(-8f); - - }, Styles.clearPartialt, () -> showMod(mod)).size(w, h).growX().pad(4f); table.row(); - } - }); - + }, bar -> { + if(mobile && !Core.graphics.isPortrait()){ + //hide search bar on mobile, takes up too much space + bar.get().clear(); + } + }).margin(10f).top(); }else{ cont.table(Styles.black6, t -> t.add("@mods.none")).height(80f); } cont.row(); - - } private void reload(){