Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.16.x] Add simple search bar #626

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.screen.Screen;
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.renderer.texture.AtlasTexture;
Expand All @@ -28,32 +29,38 @@
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import org.lwjgl.glfw.GLFW;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public abstract class WaystoneSelectionScreenBase extends ContainerScreen<WaystoneSelectionContainer> {

private final List<IWaystone> waystones;
private List<IWaystone> searchedWaystones;
private final List<ITooltipProvider> tooltipProviders = new ArrayList<>();

private Button btnPrevPage;
private Button btnNextPage;
private TextFieldWidget searchBox;
private int pageOffset;
private int headerY;
private boolean isLocationHeaderHovered;
private int buttonsPerPage;

private static final int headerHeight = 40;
private static final int headerHeight = 64;
private static final int footerHeight = 25;
private static final int entryHeight = 25;

public WaystoneSelectionScreenBase(WaystoneSelectionContainer container, PlayerInventory playerInventory, ITextComponent title) {
super(container, playerInventory, title);
waystones = container.getWaystones();
searchedWaystones = new ArrayList<>(waystones);
xSize = 270;
ySize = 200;
}
Expand Down Expand Up @@ -84,6 +91,13 @@ public void init() {
});
addButton(btnNextPage);

searchBox = new TextFieldWidget(font, width / 2 - 99, guiTop + headerHeight - 24, 198, 20, StringTextComponent.EMPTY);
searchBox.setResponder(text -> {
searchWaystones();
});

addListener(searchBox);

updateList();
}

Expand All @@ -99,7 +113,7 @@ private void updateList() {
headerY = 0;

btnPrevPage.active = pageOffset > 0;
btnNextPage.active = pageOffset < (waystones.size() - 1) / buttonsPerPage;
btnNextPage.active = pageOffset < (searchedWaystones.size() - 1) / buttonsPerPage;

tooltipProviders.clear();
buttons.removeIf(button -> button instanceof WaystoneButton || button instanceof SortWaystoneButton || button instanceof RemoveWaystoneButton);
Expand All @@ -108,8 +122,8 @@ private void updateList() {
int y = guiTop + headerHeight + headerY;
for (int i = 0; i < buttonsPerPage; i++) {
int entryIndex = pageOffset * buttonsPerPage + i;
if (entryIndex >= 0 && entryIndex < waystones.size()) {
IWaystone waystone = waystones.get(entryIndex);
if (entryIndex >= 0 && entryIndex < searchedWaystones.size()) {
IWaystone waystone = searchedWaystones.get(entryIndex);

addButton(createWaystoneButton(y, waystone));

Expand All @@ -121,7 +135,7 @@ private void updateList() {
addButton(sortUpButton);

SortWaystoneButton sortDownButton = new SortWaystoneButton(width / 2 + 108, y + 13, 1, y, 20, it -> sortWaystone(entryIndex, 1));
if (entryIndex == waystones.size() - 1) {
if (entryIndex == searchedWaystones.size() - 1) {
sortDownButton.active = false;
}
addButton(sortDownButton);
Expand All @@ -144,8 +158,8 @@ private void updateList() {
}
}

btnPrevPage.y = guiTop + headerY + headerHeight + buttonsPerPage * 22 + (waystones.size() > 0 ? 10 : 0);
btnNextPage.y = guiTop + headerY + headerHeight + buttonsPerPage * 22 + (waystones.size() > 0 ? 10 : 0);
btnPrevPage.y = guiTop + headerY + headerHeight + buttonsPerPage * 22 + (searchedWaystones.size() > 0 ? 10 : 0);
btnNextPage.y = guiTop + headerY + headerHeight + buttonsPerPage * 22 + (searchedWaystones.size() > 0 ? 10 : 0);
}

private WaystoneButton createWaystoneButton(int y, final IWaystone waystone) {
Expand Down Expand Up @@ -180,6 +194,15 @@ private void sortWaystone(int index, int sortDir) {

PlayerWaystoneManager.swapWaystoneSorting(Minecraft.getInstance().player, index, otherIndex);
NetworkHandler.channel.sendToServer(new SortWaystoneMessage(index, otherIndex));
searchWaystones();
}

private void searchWaystones() {
searchedWaystones = waystones.stream().filter((waystone) -> waystone.getName().toLowerCase().contains(searchBox.getText().toLowerCase())).collect(Collectors.toList());
int calculatedPageOffset = (searchedWaystones.size() - 1) / buttonsPerPage;
if (!searchBox.getText().isEmpty() && calculatedPageOffset < pageOffset)
pageOffset = calculatedPageOffset;

updateList();
}

Expand All @@ -203,6 +226,8 @@ public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partia
func_243308_b(matrixStack, tooltipProvider.getTooltip(), mouseX, mouseY);
}
}

searchBox.render(matrixStack, mouseX, mouseY, partialTicks);
}

@Override
Expand Down Expand Up @@ -273,4 +298,12 @@ protected boolean allowSorting() {
protected boolean allowDeletion() {
return true;
}

@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (!this.searchBox.isFocused() || (keyCode == GLFW.GLFW_KEY_ESCAPE && this.shouldCloseOnEsc())) {
return super.keyPressed(keyCode, scanCode, modifiers);
}
return this.searchBox.keyPressed(keyCode, scanCode, modifiers);
}
}