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 <summet.dev@gmail.com> * 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 <arnukren@gmail.com> * java dumb Co-authored-by: Antsiferov Andrew <summet.dev@gmail.com> Co-authored-by: Anuken <arnukren@gmail.com>
This commit is contained in:
57
core/src/mindustry/ui/SearchBar.java
Normal file
57
core/src/mindustry/ui/SearchBar.java
Normal file
@@ -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 <T> Table add(Table parent, Seq<T> list, Func<String, String> queryf,
|
||||
Func<T, String> namef, Cons2<Table, T> itemc, Cons<Cell<Table>> barc){
|
||||
Cons<String>[] 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 <T> Table add(Table parent, Seq<T> list, Func<String, String> queryf, Func<T, String> namef, Cons2<Table, T> itemc){
|
||||
return add(parent, list, queryf, namef, itemc, c -> {});
|
||||
}
|
||||
|
||||
public static <T> Table add(Table parent, Seq<T> list, Func<T, String> namef, Cons2<Table, T> itemc, Cons<Cell<Table>> barc){
|
||||
return add(parent, list, String::toLowerCase, namef, itemc, barc);
|
||||
}
|
||||
|
||||
public static <T> Table add(Table parent, Seq<T> list, Func<T, String> namef, Cons2<Table, T> 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);
|
||||
}
|
||||
}
|
||||
@@ -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(){
|
||||
|
||||
Reference in New Issue
Block a user