Better item selection UI

This commit is contained in:
Anuken 2022-09-01 17:49:29 -04:00
parent 61295bc89c
commit b854b0ca22
11 changed files with 79 additions and 41 deletions

View File

@ -36,6 +36,8 @@ public abstract class UnlockableContent extends MappableContent{
public boolean generateIcons = true;
/** Special logic icon ID. */
public int iconId = 0;
/** How big the content appears in certain selection menus */
public float selectionSize = 24f;
/** Icon of the content to use in UI. */
public TextureRegion uiIcon;
/** Icon of the full content. Unscaled.*/

View File

@ -423,6 +423,7 @@ public class UnitType extends UnlockableContent{
super(name);
constructor = EntityMapping.map(this.name);
selectionSize = 30f;
}
public UnitController createController(Unit unit){

View File

@ -211,6 +211,8 @@ public class Block extends UnlockableContent implements Senseable{
public boolean commandable;
/** If true, the building inventory can be shown with the config. */
public boolean allowConfigInventory = true;
/** Defines how large selection menus, such as that of sorters, should be. */
public int selectionRows = 5, selectionColumns = 4;
/** If true, this block can be configured by logic. */
public boolean logicConfigurable = false;
/** Whether this block consumes touchDown events when tapped. */
@ -365,6 +367,7 @@ public class Block extends UnlockableContent implements Senseable{
public Block(String name){
super(name);
initBuilding();
selectionSize = 28f;
}
public void drawBase(Tile tile){

View File

@ -1,6 +1,7 @@
package mindustry.world.blocks;
import arc.func.*;
import arc.math.*;
import arc.scene.style.*;
import arc.scene.ui.*;
import arc.scene.ui.layout.*;
@ -15,49 +16,78 @@ import mindustry.world.*;
import static mindustry.Vars.*;
public class ItemSelection{
private static TextField search;
private static int rowCount;
public static <T extends UnlockableContent> void buildTable(Table table, Seq<T> items, Prov<T> holder, Cons<T> consumer){
buildTable(table, items, holder, consumer, true);
}
public static <T extends UnlockableContent> void buildTable(Block block, Table table, Seq<T> items, Prov<T> holder, Cons<T> consumer){
buildTable(block, table, items, holder, consumer, true);
}
public static <T extends UnlockableContent> void buildTable(Table table, Seq<T> items, Prov<T> holder, Cons<T> consumer, boolean closeSelect){
buildTable(null, table, items, holder, consumer, closeSelect);
buildTable(null, table, items, holder, consumer, closeSelect, 5, 4);
}
public static <T extends UnlockableContent> void buildTable(@Nullable Block block, Table table, Seq<T> items, Prov<T> holder, Cons<T> consumer, boolean closeSelect){
public static <T extends UnlockableContent> void buildTable(Table table, Seq<T> items, Prov<T> holder, Cons<T> consumer, int columns){
buildTable(null, table, items, holder, consumer, true, 5, columns);
}
public static <T extends UnlockableContent> void buildTable(Block block, Table table, Seq<T> items, Prov<T> holder, Cons<T> consumer){
buildTable(block, table, items, holder, consumer, true, 5, 4);
}
public static <T extends UnlockableContent> void buildTable(Block block, Table table, Seq<T> items, Prov<T> holder, Cons<T> consumer, boolean closeSelect){
buildTable(block, table, items, holder, consumer, closeSelect, 5 ,4);
}
public static <T extends UnlockableContent> void buildTable(Block block, Table table, Seq<T> items, Prov<T> holder, Cons<T> consumer, int rows, int columns){
buildTable(block, table, items, holder, consumer, true, rows, columns);
}
public static <T extends UnlockableContent> void buildTable(Table table, Seq<T> items, Prov<T> holder, Cons<T> consumer, int rows, int columns){
buildTable(null, table, items, holder, consumer, true, rows, columns);
}
public static <T extends UnlockableContent> void buildTable(@Nullable Block block, Table table, Seq<T> items, Prov<T> holder, Cons<T> consumer, boolean closeSelect, int rows, int columns){
ButtonGroup<ImageButton> group = new ButtonGroup<>();
group.setMinCheckCount(0);
Table cont = new Table();
Table cont = new Table().top();
cont.defaults().size(40);
int i = 0;
if(search != null) search.clearText();
for(T item : items){
if(!item.unlockedNow() || (item instanceof Item checkVisible && state.rules.hiddenBuildItems.contains(checkVisible)) || item.isHidden()) continue;
Runnable rebuild = () -> {
group.clear();
cont.clearChildren();
ImageButton button = cont.button(Tex.whiteui, Styles.clearTogglei, 24, () -> {
if(closeSelect) control.input.config.hideConfig();
}).group(group).tooltip(item.localizedName).get();
button.changed(() -> consumer.get(button.isChecked() ? item : null));
button.getStyle().imageUp = new TextureRegionDrawable(item.uiIcon);
button.update(() -> button.setChecked(holder.get() == item));
var text = search != null ? search.getText() : "";
int i = 0;
rowCount = 0;
if(i++ % 4 == 3){
cont.row();
Seq<T> list = items.select(u -> (text.isEmpty() || u.localizedName.toLowerCase().contains(text.toLowerCase())));
for(T item : list){
if(!item.unlockedNow() || (item instanceof Item checkVisible && state.rules.hiddenBuildItems.contains(checkVisible)) || item.isHidden()) continue;
ImageButton button = cont.button(Tex.whiteui, Styles.clearNoneTogglei, Mathf.clamp(item.selectionSize, 0f, 40f), () -> {
if(closeSelect) control.input.config.hideConfig();
}).tooltip(item.localizedName).group(group).get();
button.changed(() -> consumer.get(button.isChecked() ? item : null));
button.getStyle().imageUp = new TextureRegionDrawable(item.uiIcon);
button.update(() -> button.setChecked(holder.get() == item));
if(i++ % columns == (columns - 1)){
cont.row();
rowCount++;
}
}
}
};
//add extra blank spaces so it looks nice
if(i % 4 != 0){
int remaining = 4 - (i % 4);
for(int j = 0; j < remaining; j++){
cont.image(Styles.black6);
}
rebuild.run();
Table main = new Table().background(Styles.black6);
if(rowCount > rows * 1.5f){
search = main.field(null, text -> rebuild.run()).width(40 * columns).padBottom(4).left().growX().get();
search.setMessageText("@players.search");
main.row();
}
ScrollPane pane = new ScrollPane(cont, Styles.smallPane);
@ -71,6 +101,7 @@ public class ItemSelection{
}
pane.setOverscroll(false, false);
table.add(pane).maxHeight(Scl.scl(40 * 5));
main.add(pane).maxHeight(40 * rows);
table.top().add(main);
}
}
}

View File

@ -126,7 +126,7 @@ public class Sorter extends Block{
@Override
public void buildConfiguration(Table table){
ItemSelection.buildTable(Sorter.this, table, content.items(), () -> sortItem, this::configure);
ItemSelection.buildTable(Sorter.this, table, content.items(), () -> sortItem, this::configure, selectionRows, selectionColumns);
}
@Override
@ -155,4 +155,4 @@ public class Sorter extends Block{
}
}
}
}
}

View File

@ -58,7 +58,7 @@ public class Constructor extends BlockProducer{
@Override
public void buildConfiguration(Table table){
ItemSelection.buildTable(Constructor.this, table, filter.isEmpty() ? content.blocks().select(Constructor.this::canProduce) : filter, () -> recipe, this::configure);
ItemSelection.buildTable(Constructor.this, table, filter.isEmpty() ? content.blocks().select(Constructor.this::canProduce) : filter, () -> recipe, this::configure, selectionRows, selectionColumns);
}
@Override
@ -91,4 +91,4 @@ public class Constructor extends BlockProducer{
recipe = Vars.content.block(read.s());
}
}
}
}

View File

@ -29,6 +29,7 @@ public class PayloadSource extends PayloadBlock{
hasPower = false;
rotate = true;
configurable = true;
selectionRows = selectionColumns = 8;
//make sure to display large units.
clipSize = 120;
noUpdateDisabled = true;
@ -103,7 +104,7 @@ public class PayloadSource extends PayloadBlock{
ItemSelection.buildTable(PayloadSource.this, table,
content.blocks().select(PayloadSource.this::canProduce).<UnlockableContent>as()
.add(content.units().select(PayloadSource.this::canProduce).as()),
() -> (UnlockableContent)config(), this::configure);
() -> (UnlockableContent)config(), this::configure, selectionRows, selectionColumns);
}
@Override

View File

@ -89,7 +89,7 @@ public class ItemSource extends Block{
@Override
public void buildConfiguration(Table table){
ItemSelection.buildTable(ItemSource.this, table, content.items(), () -> outputItem, this::configure);
ItemSelection.buildTable(ItemSource.this, table, content.items(), () -> outputItem, this::configure, selectionRows, selectionColumns);
}
@Override
@ -114,4 +114,4 @@ public class ItemSource extends Block{
outputItem = content.item(read.s());
}
}
}
}

View File

@ -85,7 +85,7 @@ public class LiquidSource extends Block{
@Override
public void buildConfiguration(Table table){
ItemSelection.buildTable(LiquidSource.this, table, content.liquids(), () -> source, this::configure);
ItemSelection.buildTable(LiquidSource.this, table, content.liquids(), () -> source, this::configure, selectionRows, selectionColumns);
}
@Override
@ -111,4 +111,4 @@ public class LiquidSource extends Block{
source = id == -1 ? null : content.liquid(id);
}
}
}
}

View File

@ -224,7 +224,7 @@ public class Unloader extends Block{
@Override
public void buildConfiguration(Table table){
ItemSelection.buildTable(Unloader.this, table, content.items(), () -> sortItem, this::configure);
ItemSelection.buildTable(Unloader.this, table, content.items(), () -> sortItem, this::configure, selectionRows, selectionColumns);
}
@Override
@ -250,4 +250,4 @@ public class Unloader extends Block{
sortItem = id == -1 ? null : content.item(id);
}
}
}
}

View File

@ -211,7 +211,7 @@ public class UnitFactory extends UnitBlock{
Seq<UnitType> units = Seq.with(plans).map(u -> u.unit).filter(u -> u.unlockedNow() && !u.isBanned());
if(units.any()){
ItemSelection.buildTable(UnitFactory.this, table, units, () -> currentPlan == -1 ? null : plans.get(currentPlan).unit, unit -> configure(plans.indexOf(u -> u.unit == unit)));
ItemSelection.buildTable(UnitFactory.this, table, units, () -> currentPlan == -1 ? null : plans.get(currentPlan).unit, unit -> configure(plans.indexOf(u -> u.unit == unit)), selectionRows, selectionColumns);
}else{
table.table(Styles.black3, t -> t.add("@none").color(Color.lightGray));
}
@ -357,4 +357,4 @@ public class UnitFactory extends UnitBlock{
}
}
}
}
}