mirror of
https://github.com/Ellpeck/PrettyPipes.git
synced 2024-11-23 04:08:34 +01:00
added a search bar to the item terminal
This commit is contained in:
parent
e1752bb46d
commit
b65a73c77b
2 changed files with 59 additions and 11 deletions
|
@ -6,10 +6,13 @@ import de.ellpeck.prettypipes.misc.ItemTerminalWidget;
|
||||||
import de.ellpeck.prettypipes.packets.PacketButton;
|
import de.ellpeck.prettypipes.packets.PacketButton;
|
||||||
import de.ellpeck.prettypipes.packets.PacketHandler;
|
import de.ellpeck.prettypipes.packets.PacketHandler;
|
||||||
import de.ellpeck.prettypipes.packets.PacketRequest;
|
import de.ellpeck.prettypipes.packets.PacketRequest;
|
||||||
|
import joptsimple.internal.Strings;
|
||||||
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
|
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.Widget;
|
||||||
import net.minecraft.client.gui.widget.button.Button;
|
import net.minecraft.client.gui.widget.button.Button;
|
||||||
import net.minecraft.client.resources.I18n;
|
import net.minecraft.client.resources.I18n;
|
||||||
|
import net.minecraft.client.util.InputMappings;
|
||||||
import net.minecraft.entity.player.PlayerInventory;
|
import net.minecraft.entity.player.PlayerInventory;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.util.ResourceLocation;
|
import net.minecraft.util.ResourceLocation;
|
||||||
|
@ -23,11 +26,14 @@ import java.util.stream.Stream;
|
||||||
public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
|
public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
|
||||||
private static final ResourceLocation TEXTURE = new ResourceLocation(PrettyPipes.ID, "textures/gui/item_terminal.png");
|
private static final ResourceLocation TEXTURE = new ResourceLocation(PrettyPipes.ID, "textures/gui/item_terminal.png");
|
||||||
private List<ItemStack> items;
|
private List<ItemStack> items;
|
||||||
|
private List<ItemStack> sortedItems;
|
||||||
private Button minusButton;
|
private Button minusButton;
|
||||||
private Button plusButton;
|
private Button plusButton;
|
||||||
private Button requestButton;
|
private Button requestButton;
|
||||||
private Button orderButton;
|
private Button orderButton;
|
||||||
private Button ascendingButton;
|
private Button ascendingButton;
|
||||||
|
private TextFieldWidget search;
|
||||||
|
private String lastSearchText;
|
||||||
private int requestAmount = 1;
|
private int requestAmount = 1;
|
||||||
private int scrollOffset;
|
private int scrollOffset;
|
||||||
private ItemOrder order;
|
private ItemOrder order;
|
||||||
|
@ -69,13 +75,13 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
|
||||||
}));
|
}));
|
||||||
this.requestButton.active = false;
|
this.requestButton.active = false;
|
||||||
this.orderButton = this.addButton(new Button(this.guiLeft - 22, this.guiTop, 20, 20, "", button -> {
|
this.orderButton = this.addButton(new Button(this.guiLeft - 22, this.guiTop, 20, 20, "", button -> {
|
||||||
if (this.items == null)
|
if (this.sortedItems == null)
|
||||||
return;
|
return;
|
||||||
int order = (this.order.ordinal() + 1) % ItemOrder.values().length;
|
int order = (this.order.ordinal() + 1) % ItemOrder.values().length;
|
||||||
PacketHandler.sendToServer(new PacketButton(this.container.tile.getPos(), PacketButton.ButtonResult.TERMINAL_ORDER, order));
|
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 -> {
|
this.ascendingButton = this.addButton(new Button(this.guiLeft - 22, this.guiTop + 22, 20, 20, "", button -> {
|
||||||
if (this.items == null)
|
if (this.sortedItems == null)
|
||||||
return;
|
return;
|
||||||
int asc = !this.ascending ? 1 : 0;
|
int asc = !this.ascending ? 1 : 0;
|
||||||
PacketHandler.sendToServer(new PacketButton(this.container.tile.getPos(), PacketButton.ButtonResult.TERMINAL_ASCENDING, asc));
|
PacketHandler.sendToServer(new PacketButton(this.container.tile.getPos(), PacketButton.ButtonResult.TERMINAL_ASCENDING, asc));
|
||||||
|
@ -84,6 +90,9 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
|
||||||
for (int x = 0; x < 9; x++)
|
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.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
|
@Override
|
||||||
|
@ -92,12 +101,30 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
|
||||||
this.requestButton.active = this.streamWidgets().anyMatch(w -> w.selected);
|
this.requestButton.active = this.streamWidgets().anyMatch(w -> w.selected);
|
||||||
this.plusButton.active = this.requestAmount < 384;
|
this.plusButton.active = this.requestAmount < 384;
|
||||||
this.minusButton.active = this.requestAmount > 1;
|
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<ItemStack> items, ItemOrder order, boolean ascending) {
|
public void updateItemList(List<ItemStack> items, ItemOrder order, boolean ascending) {
|
||||||
this.order = order;
|
this.order = order;
|
||||||
this.ascending = ascending;
|
this.ascending = ascending;
|
||||||
this.items = new ArrayList<>(items);
|
this.items = items;
|
||||||
this.updateWidgets();
|
this.updateWidgets();
|
||||||
|
|
||||||
this.ascendingButton.setMessage(this.ascending ? "^" : "v");
|
this.ascendingButton.setMessage(this.ascending ? "^" : "v");
|
||||||
|
@ -108,17 +135,38 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
|
||||||
Comparator<ItemStack> comparator = this.order.comparator;
|
Comparator<ItemStack> comparator = this.order.comparator;
|
||||||
if (!this.ascending)
|
if (!this.ascending)
|
||||||
comparator = comparator.reversed();
|
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<ItemTerminalWidget> widgets = this.streamWidgets().collect(Collectors.toList());
|
List<ItemTerminalWidget> widgets = this.streamWidgets().collect(Collectors.toList());
|
||||||
for (int i = 0; i < widgets.size(); i++) {
|
for (int i = 0; i < widgets.size(); i++) {
|
||||||
ItemTerminalWidget widget = widgets.get(i);
|
ItemTerminalWidget widget = widgets.get(i);
|
||||||
int index = i + this.scrollOffset * 9;
|
int index = i + this.scrollOffset * 9;
|
||||||
if (index >= this.items.size()) {
|
if (index >= this.sortedItems.size()) {
|
||||||
widget.stack = ItemStack.EMPTY;
|
widget.stack = ItemStack.EMPTY;
|
||||||
widget.visible = false;
|
widget.visible = false;
|
||||||
} else {
|
} else {
|
||||||
widget.stack = this.items.get(index);
|
widget.stack = this.sortedItems.get(index);
|
||||||
widget.visible = true;
|
widget.visible = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -132,7 +180,7 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
|
||||||
if (widget instanceof ItemTerminalWidget)
|
if (widget instanceof ItemTerminalWidget)
|
||||||
widget.renderToolTip(mouseX, mouseY);
|
widget.renderToolTip(mouseX, mouseY);
|
||||||
}
|
}
|
||||||
if (this.items != null) {
|
if (this.sortedItems != null) {
|
||||||
if (this.orderButton.isHovered())
|
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);
|
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())
|
if (this.ascendingButton.isHovered())
|
||||||
|
@ -155,8 +203,8 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
|
||||||
this.getMinecraft().getTextureManager().bindTexture(TEXTURE);
|
this.getMinecraft().getTextureManager().bindTexture(TEXTURE);
|
||||||
this.blit(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize);
|
this.blit(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize);
|
||||||
|
|
||||||
if (this.items != null && this.items.size() >= 9 * 4) {
|
if (this.sortedItems != null && this.sortedItems.size() >= 9 * 4) {
|
||||||
float percentage = this.scrollOffset / (float) (this.items.size() / 9 - 3);
|
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);
|
this.blit(this.guiLeft + 172, this.guiTop + 18 + (int) (percentage * (70 - 15)), 244, 0, 12, 15);
|
||||||
} else {
|
} else {
|
||||||
this.blit(this.guiLeft + 172, this.guiTop + 18, 244, 15, 12, 15);
|
this.blit(this.guiLeft + 172, this.guiTop + 18, 244, 15, 12, 15);
|
||||||
|
@ -165,8 +213,8 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean mouseScrolled(double x, double y, double scroll) {
|
public boolean mouseScrolled(double x, double y, double scroll) {
|
||||||
if (this.items != null && this.items.size() >= 9 * 4) {
|
if (this.sortedItems != null && this.sortedItems.size() >= 9 * 4) {
|
||||||
int offset = MathHelper.clamp(this.scrollOffset - (int) Math.signum(scroll), 0, this.items.size() / 9 - 3);
|
int offset = MathHelper.clamp(this.scrollOffset - (int) Math.signum(scroll), 0, this.sortedItems.size() / 9 - 3);
|
||||||
if (offset != this.scrollOffset) {
|
if (offset != this.scrollOffset) {
|
||||||
this.scrollOffset = offset;
|
this.scrollOffset = offset;
|
||||||
this.updateWidgets();
|
this.updateWidgets();
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 1.6 KiB After Width: | Height: | Size: 1.6 KiB |
Loading…
Reference in a new issue