Skip to content

Commit

Permalink
Close #64 keep search history (use up and down keys)
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Dec 15, 2015
1 parent e40c461 commit 4921de8
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 22 deletions.
31 changes: 9 additions & 22 deletions src/main/java/mezz/jei/gui/ItemListOverlay.java
Expand Up @@ -29,6 +29,7 @@
import mezz.jei.gui.ingredients.GuiItemStackFast;
import mezz.jei.gui.ingredients.GuiItemStackFastBatch;
import mezz.jei.gui.ingredients.GuiItemStackGroup;
import mezz.jei.input.GuiTextFieldFilter;
import mezz.jei.input.IKeyable;
import mezz.jei.input.IMouseHandler;
import mezz.jei.input.IShowsRecipeFocuses;
Expand All @@ -46,7 +47,6 @@ public class ItemListOverlay implements IShowsRecipeFocuses, IMouseHandler, IKey
private static final int itemStackPadding = 1;
private static final int itemStackWidth = GuiItemStackGroup.getWidth(itemStackPadding);
private static final int itemStackHeight = GuiItemStackGroup.getHeight(itemStackPadding);
private static final int maxSearchLength = 32;
private static int pageNum = 0;

private final ItemFilter itemFilter;
Expand All @@ -58,7 +58,7 @@ public class ItemListOverlay implements IShowsRecipeFocuses, IMouseHandler, IKey
private GuiButton configButton;
private IDrawable configButtonIcon;
private HoverChecker configButtonHoverChecker;
private GuiTextField searchField;
private GuiTextFieldFilter searchField;
private int pageCount;

private String pageNumDisplayString;
Expand Down Expand Up @@ -121,10 +121,9 @@ public void initGui(@Nonnull GuiContainer guiContainer) {

int searchFieldY = screenHeight - searchHeight - borderPadding - 2;
int searchFieldWidth = rightEdge - leftEdge - configButtonSize - 1;
searchField = new GuiTextField(0, fontRenderer, leftEdge, searchFieldY, searchFieldWidth, searchHeight);
searchField.setMaxStringLength(maxSearchLength);
searchField = new GuiTextFieldFilter(0, fontRenderer, leftEdge, searchFieldY, searchFieldWidth, searchHeight);
setKeyboardFocus(false);
searchField.setText(itemFilter.getFilterText());
searchField.setItemFilter(itemFilter);

updateLayout();
}
Expand Down Expand Up @@ -158,13 +157,7 @@ private void updateLayout() {
pageNumDisplayX = ((backButton.xPosition + backButton.width) + nextButton.xPosition) / 2 - (pageDisplayWidth / 2);
pageNumDisplayY = backButton.yPosition + Math.round((backButton.height - fontRendererObj.FONT_HEIGHT) / 2.0f);

if (itemList.size() == 0) {
searchField.setTextColor(Color.red.getRGB());
searchField.setMaxStringLength(searchField.getText().length());
} else {
searchField.setTextColor(Color.white.getRGB());
searchField.setMaxStringLength(maxSearchLength);
}
searchField.update();
}

private void nextPage() {
Expand Down Expand Up @@ -288,16 +281,11 @@ private boolean handleMouseClickedButtons(int mouseX, int mouseY) {
}

private boolean handleMouseClickedSearch(int mouseX, int mouseY, int mouseButton) {
boolean searchClicked = mouseX >= searchField.xPosition && mouseX < searchField.xPosition + searchField.width && mouseY >= searchField.yPosition && mouseY < searchField.yPosition + searchField.height;
boolean searchClicked = searchField.isMouseOver(mouseX, mouseY);
setKeyboardFocus(searchClicked);
if (searchClicked) {
if (mouseButton == 1) {
searchField.setText("");
if (itemFilter.setFilterText(searchField.getText())) {
updateLayout();
}
} else {
searchField.mouseClicked(mouseX, mouseY, mouseButton);
if (searchField.handleMouseClicked(mouseX, mouseY, mouseButton)) {
updateLayout();
}
}
return searchClicked;
Expand All @@ -311,14 +299,13 @@ public boolean hasKeyboardFocus() {
@Override
public void setKeyboardFocus(boolean keyboardFocus) {
searchField.setFocused(keyboardFocus);
Keyboard.enableRepeatEvents(keyboardFocus);
}

@Override
public boolean onKeyPressed(int keyCode) {
if (searchField.isFocused()) {
boolean success = searchField.textboxKeyTyped(Keyboard.getEventCharacter(), Keyboard.getEventKey());
if (success && itemFilter.setFilterText(searchField.getText())) {
if (success) {
updateLayout();
}
return true;
Expand Down
122 changes: 122 additions & 0 deletions src/main/java/mezz/jei/input/GuiTextFieldFilter.java
@@ -0,0 +1,122 @@
package mezz.jei.input;

import java.awt.Color;
import java.util.LinkedList;
import java.util.List;

import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiTextField;

import net.minecraftforge.fml.client.config.HoverChecker;

import org.lwjgl.input.Keyboard;

import mezz.jei.ItemFilter;
import mezz.jei.util.ItemStackElement;

public class GuiTextFieldFilter extends GuiTextField {
private static final int MAX_HISTORY = 100;
private static final int maxSearchLength = 32;

private final List<String> history = new LinkedList<>();
private ItemFilter itemFilter;
private HoverChecker hoverChecker;

public GuiTextFieldFilter(int componentId, FontRenderer fontrendererObj, int x, int y, int width, int height) {
super(componentId, fontrendererObj, x, y, width, height);
setMaxStringLength(maxSearchLength);
this.hoverChecker = new HoverChecker(y, y + height, x, x + width, 0);
}

public void setItemFilter(ItemFilter itemFilter) {
this.itemFilter = itemFilter;
setText(itemFilter.getFilterText());
}

public void update() {
List<ItemStackElement> itemList = itemFilter.getItemList();
if (itemList.size() == 0) {
setTextColor(Color.red.getRGB());
setMaxStringLength(getText().length());
} else {
setTextColor(Color.white.getRGB());
setMaxStringLength(maxSearchLength);
}
}

@Override
public boolean textboxKeyTyped(char character, int keyCode) {
boolean handled = super.textboxKeyTyped(character, keyCode);
if (!handled && !history.isEmpty()) {
if (keyCode == Keyboard.KEY_UP) {
String currentText = getText();
int historyIndex = history.indexOf(currentText);
if (historyIndex < 0) {
if (saveHistory()) {
historyIndex = history.size() - 1;
} else {
historyIndex = history.size();
}
}
if (historyIndex > 0) {
String historyString = history.get(historyIndex - 1);
setText(historyString);
handled = true;
}
} else if (keyCode == Keyboard.KEY_DOWN) {
String currentText = getText();
int historyIndex = history.indexOf(currentText);
if (historyIndex >= 0) {
String historyString;
if (historyIndex + 1 < history.size()) {
historyString = history.get(historyIndex + 1);
} else {
historyString = "";
}
setText(historyString);
handled = true;
}
} else if (keyCode == Keyboard.KEY_RETURN) {
saveHistory();
}
}
return handled && itemFilter.setFilterText(getText());
}

public boolean isMouseOver(int mouseX, int mouseY) {
return hoverChecker.checkHover(mouseX, mouseY);
}

public boolean handleMouseClicked(int mouseX, int mouseY, int mouseButton) {
if (mouseButton == 1) {
setText("");
return itemFilter.setFilterText("");
} else {
super.mouseClicked(mouseX, mouseY, mouseButton);
}
return false;
}

@Override
public void setFocused(boolean keyboardFocus) {
super.setFocused(keyboardFocus);
Keyboard.enableRepeatEvents(keyboardFocus);

if (!keyboardFocus) {
saveHistory();
}
}

private boolean saveHistory() {
String text = getText();
if (text.length() > 0) {
history.remove(text);
history.add(text);
if (history.size() > MAX_HISTORY) {
history.remove(0);
}
return true;
}
return false;
}
}

0 comments on commit 4921de8

Please sign in to comment.