From b65a73c77bf977dfd8edc450757d32642c8ebce9 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 8 May 2020 16:06:17 +0200 Subject: [PATCH] added a search bar to the item terminal --- .../terminal/containers/ItemTerminalGui.java | 70 +++++++++++++++--- .../textures/gui/item_terminal.png | Bin 1650 -> 1642 bytes 2 files changed, 59 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/ellpeck/prettypipes/terminal/containers/ItemTerminalGui.java b/src/main/java/de/ellpeck/prettypipes/terminal/containers/ItemTerminalGui.java index 02ef475..465781d 100644 --- a/src/main/java/de/ellpeck/prettypipes/terminal/containers/ItemTerminalGui.java +++ b/src/main/java/de/ellpeck/prettypipes/terminal/containers/ItemTerminalGui.java @@ -6,10 +6,13 @@ import de.ellpeck.prettypipes.misc.ItemTerminalWidget; import de.ellpeck.prettypipes.packets.PacketButton; import de.ellpeck.prettypipes.packets.PacketHandler; import de.ellpeck.prettypipes.packets.PacketRequest; +import joptsimple.internal.Strings; import net.minecraft.client.gui.screen.inventory.ContainerScreen; +import net.minecraft.client.gui.widget.TextFieldWidget; import net.minecraft.client.gui.widget.Widget; import net.minecraft.client.gui.widget.button.Button; import net.minecraft.client.resources.I18n; +import net.minecraft.client.util.InputMappings; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.item.ItemStack; import net.minecraft.util.ResourceLocation; @@ -23,11 +26,14 @@ import java.util.stream.Stream; public class ItemTerminalGui extends ContainerScreen { private static final ResourceLocation TEXTURE = new ResourceLocation(PrettyPipes.ID, "textures/gui/item_terminal.png"); private List items; + private List sortedItems; private Button minusButton; private Button plusButton; private Button requestButton; private Button orderButton; private Button ascendingButton; + private TextFieldWidget search; + private String lastSearchText; private int requestAmount = 1; private int scrollOffset; private ItemOrder order; @@ -69,13 +75,13 @@ public class ItemTerminalGui extends ContainerScreen { })); this.requestButton.active = false; this.orderButton = this.addButton(new Button(this.guiLeft - 22, this.guiTop, 20, 20, "", button -> { - if (this.items == null) + if (this.sortedItems == null) return; int order = (this.order.ordinal() + 1) % ItemOrder.values().length; PacketHandler.sendToServer(new PacketButton(this.container.tile.getPos(), PacketButton.ButtonResult.TERMINAL_ORDER, order)); })); this.ascendingButton = this.addButton(new Button(this.guiLeft - 22, this.guiTop + 22, 20, 20, "", button -> { - if (this.items == null) + if (this.sortedItems == null) return; int asc = !this.ascending ? 1 : 0; PacketHandler.sendToServer(new PacketButton(this.container.tile.getPos(), PacketButton.ButtonResult.TERMINAL_ASCENDING, asc)); @@ -84,6 +90,9 @@ public class ItemTerminalGui extends ContainerScreen { for (int x = 0; x < 9; x++) this.addButton(new ItemTerminalWidget(this.guiLeft + 8 + x * 18, this.guiTop + 18 + y * 18, x, y, this)); } + this.search = this.addButton(new TextFieldWidget(this.font, this.guiLeft + 97, this.guiTop + 6, 86, 8, "")); + this.search.setEnableBackgroundDrawing(false); + this.lastSearchText = ""; } @Override @@ -92,12 +101,30 @@ public class ItemTerminalGui extends ContainerScreen { this.requestButton.active = this.streamWidgets().anyMatch(w -> w.selected); this.plusButton.active = this.requestAmount < 384; this.minusButton.active = this.requestAmount > 1; + + this.search.tick(); + String text = this.search.getText(); + if (!this.lastSearchText.equals(text)) { + this.lastSearchText = text; + this.updateWidgets(); + } + } + + @Override + public boolean keyPressed(int x, int y, int z) { + // for some reason we have to do this to make the text field allow the inventory key to be typed + if (this.search.isFocused()) { + InputMappings.Input mouseKey = InputMappings.getInputByCode(x, y); + if (this.minecraft.gameSettings.keyBindInventory.isActiveAndMatches(mouseKey)) + return false; + } + return super.keyPressed(x, y, z); } public void updateItemList(List items, ItemOrder order, boolean ascending) { this.order = order; this.ascending = ascending; - this.items = new ArrayList<>(items); + this.items = items; this.updateWidgets(); this.ascendingButton.setMessage(this.ascending ? "^" : "v"); @@ -108,17 +135,38 @@ public class ItemTerminalGui extends ContainerScreen { Comparator comparator = this.order.comparator; if (!this.ascending) comparator = comparator.reversed(); - this.items.sort(comparator); + + this.sortedItems = new ArrayList<>(this.items); + this.sortedItems.sort(comparator); + + String searchText = this.search.getText(); + if (!Strings.isNullOrEmpty(searchText)) { + this.sortedItems.removeIf(s -> { + String search = searchText; + String toCompare; + if (search.startsWith("@")) { + toCompare = s.getItem().getRegistryName().getNamespace(); + search = search.substring(1); + } else { + // don't use formatted text here since we want to search for name + toCompare = s.getDisplayName().getString(); + } + return !toCompare.toLowerCase(Locale.ROOT).contains(search.toLowerCase(Locale.ROOT)); + }); + } + + if (this.sortedItems.size() < 9 * 4) + this.scrollOffset = 0; List widgets = this.streamWidgets().collect(Collectors.toList()); for (int i = 0; i < widgets.size(); i++) { ItemTerminalWidget widget = widgets.get(i); int index = i + this.scrollOffset * 9; - if (index >= this.items.size()) { + if (index >= this.sortedItems.size()) { widget.stack = ItemStack.EMPTY; widget.visible = false; } else { - widget.stack = this.items.get(index); + widget.stack = this.sortedItems.get(index); widget.visible = true; } } @@ -132,7 +180,7 @@ public class ItemTerminalGui extends ContainerScreen { if (widget instanceof ItemTerminalWidget) widget.renderToolTip(mouseX, mouseY); } - if (this.items != null) { + if (this.sortedItems != null) { if (this.orderButton.isHovered()) this.renderTooltip(I18n.format("info." + PrettyPipes.ID + ".order", I18n.format("info." + PrettyPipes.ID + ".order." + this.order.name().toLowerCase(Locale.ROOT))), mouseX, mouseY); if (this.ascendingButton.isHovered()) @@ -155,8 +203,8 @@ public class ItemTerminalGui extends ContainerScreen { this.getMinecraft().getTextureManager().bindTexture(TEXTURE); this.blit(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize); - if (this.items != null && this.items.size() >= 9 * 4) { - float percentage = this.scrollOffset / (float) (this.items.size() / 9 - 3); + if (this.sortedItems != null && this.sortedItems.size() >= 9 * 4) { + float percentage = this.scrollOffset / (float) (this.sortedItems.size() / 9 - 3); this.blit(this.guiLeft + 172, this.guiTop + 18 + (int) (percentage * (70 - 15)), 244, 0, 12, 15); } else { this.blit(this.guiLeft + 172, this.guiTop + 18, 244, 15, 12, 15); @@ -165,8 +213,8 @@ public class ItemTerminalGui extends ContainerScreen { @Override public boolean mouseScrolled(double x, double y, double scroll) { - if (this.items != null && this.items.size() >= 9 * 4) { - int offset = MathHelper.clamp(this.scrollOffset - (int) Math.signum(scroll), 0, this.items.size() / 9 - 3); + if (this.sortedItems != null && this.sortedItems.size() >= 9 * 4) { + int offset = MathHelper.clamp(this.scrollOffset - (int) Math.signum(scroll), 0, this.sortedItems.size() / 9 - 3); if (offset != this.scrollOffset) { this.scrollOffset = offset; this.updateWidgets(); diff --git a/src/main/resources/assets/prettypipes/textures/gui/item_terminal.png b/src/main/resources/assets/prettypipes/textures/gui/item_terminal.png index 409593fec34e5bfc0b5fddc63a7242e1de766dd0..5ad9fde4675966ca7f3b6f34c3b7454c903cb549 100644 GIT binary patch delta 1221 zcmaKreK6a17{I^z<%iH5bFQoQJUaz2wbSH9K|v zskb8j>Be-y1iN`joL00=t6gfLY0;*n(Gc$>gr)X!z1!Wse?0fx^SOJT@Asb1cL6(% zE%j#YIdSZhbJ;aBFV3Vsi}>)$OT`)4o#P`{x=KHr^(c+NB%}A953MUM_X)Owj1_UL zA*Y9T8&omh%=ntP+Ep)5V(8lHMh8))u2;C`KbGt>DQ62!+J?P@W325t6%`f5y|c^9 z?Rj_)CfDwocwuQtu!gZSFKlD=MmGn!Pp-1eCOU)OqA~<8QXi~p#vi11MqYwLY}Tlo zX6)jv)(-j1Q;XR7@TTFrHvQMJ(WtqIrs1UN!j?pu>^L*dRsezWJ~P1m0u@NW15CUT z=(`OZD|Z684Pw*7!3T+Lqh&IgtXLQgTm)bKd{UMfO2%5hu3l8l$T?M_99B=s#-Xr* z_R(Yy-EUGkhrGP8k+)cILEfQW&CQVPA}N=07=zli6^*NQ`n*UL;1khxgm~_u_FUM_@mD6d+|0jL`ADpso(HWtJCA`=V&$^LSkUN zEsi5;WSYN+)R?bAmNV2CZ+j%&h`=&T{E6rF2dQ_&5>q}<63ANrno{42GcOcwZVB{O z%llfIseky6y+g;a$Tuat-j_aqT5~;(teX_=x7lj8WtSB(&C*o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