Skip to content

Commit

Permalink
Ctrl+F feature for Crafting Plan/Tree/Status (#680)
Browse files Browse the repository at this point in the history
Co-authored-by: Maya <[email protected]>
  • Loading branch information
lordIcocain and serenibyss authored Feb 28, 2025
1 parent b1b1164 commit 35b2491
Show file tree
Hide file tree
Showing 10 changed files with 304 additions and 9 deletions.
105 changes: 104 additions & 1 deletion src/main/java/appeng/client/gui/implementations/GuiCraftConfirm.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@
import appeng.api.storage.data.IItemList;
import appeng.client.gui.AEBaseGui;
import appeng.client.gui.IGuiTooltipHandler;
import appeng.client.gui.widgets.GuiAeButton;
import appeng.client.gui.widgets.GuiCraftingCPUTable;
import appeng.client.gui.widgets.GuiCraftingTree;
import appeng.client.gui.widgets.GuiImgButton;
import appeng.client.gui.widgets.GuiScrollbar;
import appeng.client.gui.widgets.GuiSimpleImgButton;
import appeng.client.gui.widgets.GuiTabButton;
import appeng.client.gui.widgets.ICraftingCPUTableHolder;
import appeng.client.gui.widgets.MEGuiTextField;
import appeng.container.implementations.ContainerCraftConfirm;
import appeng.container.implementations.CraftingCPUStatus;
import appeng.core.AEConfig;
Expand Down Expand Up @@ -159,8 +161,14 @@ protected void recalculateScreenSize() {
private GuiImgButton sortingModeButton;
private GuiImgButton sortingDirectionButton;
private GuiSimpleImgButton optimizeButton;
private GuiAeButton findNext;
private GuiAeButton findPrev;
private MEGuiTextField searchField;
private int tooltip = -1;
private ItemStack hoveredStack;
private ArrayList<Integer> goToData = new ArrayList<>();
private int searchGotoIndex = -1;
private IAEItemStack needHighlight;

final GuiScrollbar scrollbar;

Expand Down Expand Up @@ -300,6 +308,40 @@ public void initGui() {
ButtonToolTips.OptimizePatterns.getLocal());
this.optimizeButton.enabled = false;
this.buttonList.add(this.optimizeButton);

this.searchField = new MEGuiTextField(52, 12, "Search") {

@Override
public void onTextChange(String oldText) {
super.onTextChange(oldText);
switch (displayMode) {
case LIST -> updateSearchGoToList();
case TREE -> craftingTree.updateSearchGoToList(this.getText().toLowerCase());
}
}
};
this.searchField.x = this.guiLeft + this.xSize - 101;
this.searchField.y = this.guiTop + 5;

this.findPrev = new GuiAeButton(
0,
this.guiLeft + this.xSize - 48,
this.guiTop + 6,
10,
10,
"↑",
ButtonToolTips.SearchGotoPrev.getLocal());
this.buttonList.add(this.findPrev);

this.findNext = new GuiAeButton(
0,
this.guiLeft + this.xSize - 36,
this.guiTop + 6,
10,
10,
"↓",
ButtonToolTips.SearchGotoNext.getLocal());
this.buttonList.add(this.findNext);
}

@Override
Expand Down Expand Up @@ -423,6 +465,38 @@ public void drawFG(final int offsetX, final int offsetY, final int mouseX, final
}
}

private void updateSearchGoToList() {
needHighlight = null;
searchGotoIndex = -1;
goToData.clear();
if (this.searchField.getText().isEmpty()) return;
String s = this.searchField.getText().toLowerCase();
int visCount = 0;
for (IAEItemStack aeis : this.visual) {
if (aeis != null && Platform.getItemDisplayName(aeis).toLowerCase().contains(s)) {
goToData.add(visCount);
}
visCount++;
}
searchGoTo(true);
}

private void searchGoTo(boolean forward) {
String s = this.searchField.getText().toLowerCase();
if (s.isEmpty() || goToData.isEmpty()) return;
if (forward) {
searchGotoIndex++;
if (searchGotoIndex >= goToData.size()) searchGotoIndex = 0;
} else {
if (searchGotoIndex <= 0) searchGotoIndex = goToData.size();
searchGotoIndex--;
}

IAEItemStack aeis = this.visual.get(goToData.get(searchGotoIndex));
this.getScrollBar().setCurrentScroll(goToData.get(searchGotoIndex) / 3 - this.rows / 2);
needHighlight = aeis.copy();
}

private void drawListFG(final int offsetX, final int offsetY, final int mouseX, final int mouseY) {
String dsp = null;

Expand Down Expand Up @@ -607,6 +681,18 @@ private void drawListFG(final int offsetX, final int offsetY, final int mouseX,
GuiColors.CraftConfirmMissingItem.getColor());
}

if (!this.searchField.getText().isEmpty() && goToData.contains(z)) {
final int startX = x * (1 + sectionLength) + xo;
final int startY = posY - 4;
final int color = needHighlight != null && needHighlight.isSameType(refStack)
? GuiColors.SearchGoToHighlight.getColor()
: GuiColors.SearchHighlight.getColor();
drawVerticalLine(startX, startY, startY + offY, color);
drawVerticalLine(startX + sectionLength - 1, startY, startY + offY, color);
drawHorizontalLine(startX + 1, startX + sectionLength - 2, startY + 1, color);
drawHorizontalLine(startX + 1, startX + sectionLength - 2, startY + offY - 1, color);
}

x++;

if (x > 2) {
Expand Down Expand Up @@ -683,6 +769,9 @@ public void drawBG(final int offsetX, final int offsetY, final int mouseX, final
TREE_VIEW_TEXTURE_HEIGHT);
}
}
this.bindTexture("guis/searchField.png");
this.drawTexturedModalRect(this.guiLeft + this.xSize - 101, this.guiTop + 5, 0, 0, 52, 12);
this.searchField.drawTextBox();
}

private void setScrollBar() {
Expand Down Expand Up @@ -870,7 +959,9 @@ protected void keyTyped(final char character, final int key) {
if (key == Keyboard.KEY_RETURN || key == Keyboard.KEY_NUMPADENTER) {
this.actionPerformed(this.start);
}
super.keyTyped(character, key);
if (!(this.searchField.textboxKeyTyped(character, key))) {
super.keyTyped(character, key);
}
}
}

Expand All @@ -889,6 +980,7 @@ protected void actionPerformed(final GuiButton btn) {
this.displayMode = this.displayMode.next();
recalculateScreenSize();
this.setWorldAndResolution(mc, width, height);
this.searchField.setText("");
} else if (btn == this.takeScreenshot) {
if (craftingTree != null) {
craftingTree.saveScreenshot();
Expand Down Expand Up @@ -929,6 +1021,16 @@ protected void actionPerformed(final GuiButton btn) {
} catch (final Throwable e) {
AELog.debug(e);
}
} else if (btn == this.findNext) {
switch (displayMode) {
case LIST -> searchGoTo(true);
case TREE -> craftingTree.searchGoTo(true);
}
} else if (btn == this.findPrev) {
switch (displayMode) {
case LIST -> searchGoTo(false);
case TREE -> craftingTree.searchGoTo(false);
}
}
}

Expand All @@ -953,6 +1055,7 @@ public GuiButton getCancelButton() {
protected void mouseClicked(int xCoord, int yCoord, int btn) {
super.mouseClicked(xCoord, yCoord, btn);
cpuTable.mouseClicked(xCoord - guiLeft, yCoord - guiTop, btn);
this.searchField.mouseClicked(xCoord, yCoord, btn);
}

@Override
Expand Down
119 changes: 116 additions & 3 deletions src/main/java/appeng/client/gui/implementations/GuiCraftingCPU.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@
import appeng.api.util.DimensionalCoord;
import appeng.client.gui.AEBaseGui;
import appeng.client.gui.IGuiTooltipHandler;
import appeng.client.gui.widgets.GuiAeButton;
import appeng.client.gui.widgets.GuiImgButton;
import appeng.client.gui.widgets.GuiScrollbar;
import appeng.client.gui.widgets.ISortSource;
import appeng.client.gui.widgets.ITooltip;
import appeng.client.gui.widgets.MEGuiTextField;
import appeng.client.render.BlockPosHighlighter;
import appeng.container.implementations.ContainerCraftingCPU;
import appeng.core.AEConfig;
import appeng.core.AELog;
import appeng.core.localization.ButtonToolTips;
import appeng.core.localization.GuiColors;
import appeng.core.localization.GuiText;
import appeng.core.localization.PlayerMessages;
Expand Down Expand Up @@ -168,6 +171,12 @@ public int getStringWidth() {
private final RemainingOperations remainingOperations = new RemainingOperations();
private ItemStack hoveredStack;
private ItemStack hoveredNbtStack;
private GuiAeButton findNext;
private GuiAeButton findPrev;
private MEGuiTextField searchField;
private ArrayList<Integer> goToData = new ArrayList<>();
private int searchGotoIndex = -1;
private IAEItemStack needHighlight;

public GuiCraftingCPU(final InventoryPlayer inventoryPlayer, final Object te) {
this(new ContainerCraftingCPU(inventoryPlayer, te));
Expand Down Expand Up @@ -201,13 +210,17 @@ protected void actionPerformed(final GuiButton btn) {
} catch (final IOException e) {
AELog.debug(e);
}
}
if (this.toggleHideStored == btn) {
} else if (this.toggleHideStored == btn) {
this.hideStored ^= true;
AEConfig.instance.getConfigManager().putSetting(Settings.HIDE_STORED, hideStored ? YesNo.YES : YesNo.NO);
this.toggleHideStored.set(hideStored ? YesNo.YES : YesNo.NO);
hideStoredSorting();
this.setScrollBar();
updateSearchGoToList(true);
} else if (btn == this.findNext) {
searchGoTo(true);
} else if (btn == this.findPrev) {
searchGoTo(false);
}

}
Expand All @@ -226,6 +239,7 @@ protected void mouseClicked(final int xCoord, final int yCoord, final int btn) {
mc.thePlayer.closeScreen();
}
super.mouseClicked(xCoord, yCoord, btn);
this.searchField.mouseClicked(xCoord, yCoord, btn);
}

@Override
Expand All @@ -246,6 +260,37 @@ public void initGui() {
AEConfig.instance.getConfigManager().getSetting(Settings.HIDE_STORED));
this.buttonList.add(this.toggleHideStored);
this.buttonList.add(this.cancel);

this.searchField = new MEGuiTextField(52, 12, "Search") {

@Override
public void onTextChange(String oldText) {
super.onTextChange(oldText);
updateSearchGoToList(true);
}
};
this.searchField.x = this.guiLeft + this.xSize - 101;
this.searchField.y = this.guiTop + 5;

this.findPrev = new GuiAeButton(
0,
this.guiLeft + this.xSize - 48,
this.guiTop + 6,
10,
10,
"↑",
ButtonToolTips.SearchGotoPrev.getLocal());
this.buttonList.add(this.findPrev);

this.findNext = new GuiAeButton(
0,
this.guiLeft + this.xSize - 36,
this.guiTop + 6,
10,
10,
"↓",
ButtonToolTips.SearchGotoNext.getLocal());
this.buttonList.add(this.findNext);
}

private void setScrollBar() {
Expand Down Expand Up @@ -294,6 +339,47 @@ public void drawScreen(final int mouseX, final int mouseY, final float btn) {
super.drawScreen(mouseX, mouseY, btn);
}

private void updateSearchGoToList(boolean dropIndex) {
needHighlight = null;
goToData.clear();
if (this.searchField.getText().isEmpty()) return;
String s = this.searchField.getText().toLowerCase();
int visCount = 0;
for (IAEItemStack aeis : hideStored ? this.visualHiddenStored : this.visual) {
if (aeis != null && Platform.getItemDisplayName(aeis).toLowerCase().contains(s)) {
goToData.add(visCount);
}
visCount++;
}
if (dropIndex) {
searchGotoIndex = -1;
searchGoTo(true);
}
}

private void searchGoTo(boolean forward) {
String s = this.searchField.getText().toLowerCase();
if (s.isEmpty() || goToData.isEmpty()) return;
if (forward) {
searchGotoIndex++;
if (searchGotoIndex >= goToData.size()) searchGotoIndex = 0;
} else {
if (searchGotoIndex <= 0) searchGotoIndex = goToData.size();
searchGotoIndex--;
}

List<IAEItemStack> visualTemp;
if (this.hideStored) {
visualTemp = this.visualHiddenStored;
} else {
visualTemp = this.visual;
}

IAEItemStack aeis = visualTemp.get(goToData.get(searchGotoIndex));
this.getScrollBar().setCurrentScroll(goToData.get(searchGotoIndex) / 3 - this.rows / 2);
needHighlight = aeis.copy();
}

private void updateRemainingOperations() {
int interval = 1000;
if (this.remainingOperations.getRefreshTick() >= this.remainingOperations.getLastWorkingTick() + interval) {
Expand Down Expand Up @@ -326,7 +412,7 @@ public void drawFG(final int offsetX, final int offsetY, final int mouseX, final
updateRemainingOperations();
this.fontRendererObj.drawString(
String.valueOf(remainingOperations.getRemainingOperations()),
TITLE_LEFT_OFFSET + 200 - this.remainingOperations.getStringWidth(),
TITLE_LEFT_OFFSET + 128 - this.remainingOperations.getStringWidth(),
TITLE_TOP_OFFSET,
GuiColors.CraftingCPUTitle.getColor());

Expand Down Expand Up @@ -473,6 +559,18 @@ public void drawFG(final int offsetX, final int offsetY, final int mouseX, final

this.drawItem(posX, posY, is);

if (!this.searchField.getText().isEmpty() && goToData.contains(z)) {
final int startX = x * (1 + SECTION_LENGTH) + ITEMSTACK_LEFT_OFFSET;
final int startY = posY - 4;
final int color = needHighlight != null && needHighlight.isSameType(refStack)
? GuiColors.SearchGoToHighlight.getColor()
: GuiColors.SearchHighlight.getColor();
drawVerticalLine(startX, startY, startY + offY, color);
drawVerticalLine(startX + SECTION_LENGTH - 1, startY, startY + offY, color);
drawHorizontalLine(startX + 1, startX + SECTION_LENGTH - 2, startY + 1, color);
drawHorizontalLine(startX + 1, startX + SECTION_LENGTH - 2, startY + offY - 1, color);
}

x++;

if (x > 2) {
Expand Down Expand Up @@ -524,6 +622,20 @@ protected void addItemTooltip(IAEItemStack refStack, List<String> lineList) {
public void drawBG(final int offsetX, final int offsetY, final int mouseX, final int mouseY) {
this.bindTexture("guis/craftingcpu.png");
this.drawTexturedModalRect(offsetX, offsetY, 0, 0, this.xSize, this.ySize);
drawSearch();
}

public void drawSearch() {
this.bindTexture("guis/searchField.png");
this.drawTexturedModalRect(this.guiLeft + this.xSize - 101, this.guiTop + 5, 0, 0, 52, 12);
this.searchField.drawTextBox();
}

@Override
protected void keyTyped(final char character, final int key) {
if (!(this.searchField.textboxKeyTyped(character, key))) {
super.keyTyped(character, key);
}
}

public void postUpdate(IAEItemStack is) {
Expand Down Expand Up @@ -565,6 +677,7 @@ public void postUpdate(final List<IAEItemStack> list, final byte ref) {
}

if (this.hideStored) this.hideStoredSorting();
updateSearchGoToList(false);
this.setScrollBar();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ public void drawBG(int offsetX, int offsetY, int mouseX, int mouseY) {
this.drawTexturedModalRect(offsetX, offsetY, 0, 0, this.xSize, this.ySize);
}
this.cpuTable.drawBG(offsetX, offsetY);
drawSearch();
}

@Override
Expand Down
Loading

0 comments on commit 35b2491

Please sign in to comment.