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.19.x] Add simple search bar to waystone screen #624

Merged
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 @@ -20,6 +20,7 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.components.Widget;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.narration.NarratableEntry;
Expand All @@ -32,32 +33,37 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import org.lwjgl.glfw.GLFW;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public abstract class WaystoneSelectionScreenBase extends AbstractContainerScreen<WaystoneSelectionMenu> {

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

private Button btnPrevPage;
private Button btnNextPage;
private EditBox 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(WaystoneSelectionMenu container, Inventory playerInventory, Component title) {
super(container, playerInventory, title);
waystones = container.getWaystones();
filteredWaystones = new ArrayList<>(waystones);
imageWidth = 270;
imageHeight = 200;
}
Expand Down Expand Up @@ -89,6 +95,14 @@ public void init() {
addRenderableWidget(btnNextPage);

updateList();


searchBox = new EditBox(font, width / 2 - 99, topPos + headerHeight - 24, 198, 20, Component.empty());
searchBox.setResponder(text -> {
searchWaystones();
});

addRenderableWidget(searchBox);
}

@Override
Expand All @@ -103,7 +117,7 @@ private void updateList() {
headerY = 0;

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

tooltipProviders.clear();

Expand All @@ -115,8 +129,8 @@ private void updateList() {
int y = topPos + 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 < filteredWaystones.size()) {
IWaystone waystone = filteredWaystones.get(entryIndex);

addRenderableWidget(createWaystoneButton(y, waystone));

Expand All @@ -128,7 +142,7 @@ private void updateList() {
addRenderableWidget(sortUpButton);

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

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

private WaystoneButton createWaystoneButton(int y, final IWaystone waystone) {
Expand Down Expand Up @@ -187,6 +201,16 @@ private void sortWaystone(int index, int sortDir) {

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

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

updateList();
}

Expand Down Expand Up @@ -281,4 +305,13 @@ protected boolean allowSorting() {
protected boolean allowDeletion() {
return true;
}

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

return this.searchBox.keyPressed(key, scanCode, modifiers);
}
}