mirror of
https://github.com/GTNewHorizons/NotEnoughItems.git
synced 2026-11-17 19:15:13 +00:00
Add search history (#294)
* add search history * spotlessApply * linkedlist->arraylist
This commit is contained in:
1 parent
7d7e3b61a6
commit
617ac0600d
4 files changed
+104
No files matched your search
@@ -344,6 +344,8 @@ public class NEIClientConfig {
|
||||
API.addHashBind("gui.overlay_use", Keyboard.KEY_S + NEIClientUtils.SHIFT_HASH);
|
||||
API.addHashBind("gui.overlay_hide", Keyboard.KEY_S + NEIClientUtils.CTRL_HASH);
|
||||
API.addHashBind("gui.hide_bookmarks", Keyboard.KEY_B);
|
||||
API.addKeyBind("gui.getprevioussearch", Keyboard.KEY_UP);
|
||||
API.addKeyBind("gui.getnextsearch", Keyboard.KEY_DOWN);
|
||||
|
||||
API.addKeyBind("world.chunkoverlay", Keyboard.KEY_F9);
|
||||
API.addKeyBind("world.moboverlay", Keyboard.KEY_F7);
|
||||
|
||||
@@ -9,6 +9,7 @@ import codechicken.nei.ItemList.PatternItemFilter;
|
||||
import codechicken.nei.api.API;
|
||||
import codechicken.nei.api.ItemFilter;
|
||||
import codechicken.nei.api.ItemFilter.ItemFilterProvider;
|
||||
import codechicken.nei.util.TextHistory;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -45,6 +46,7 @@ public class SearchField extends TextField implements ItemFilterProvider {
|
||||
}
|
||||
|
||||
public static List<ISearchProvider> searchProviders = new LinkedList<>();
|
||||
private static final TextHistory history = new TextHistory();
|
||||
private boolean isVisible = true;
|
||||
|
||||
long lastclicktime;
|
||||
@@ -116,6 +118,13 @@ public class SearchField extends TextField implements ItemFilterProvider {
|
||||
if (isVisible() && NEIClientConfig.isKeyHashDown("gui.search")) {
|
||||
setFocus(true);
|
||||
}
|
||||
if (NEIClientConfig.isKeyHashDown("gui.getprevioussearch") && focused()) {
|
||||
handleNavigateHistory(TextHistory.Direction.PREVIOUS);
|
||||
}
|
||||
|
||||
if (NEIClientConfig.isKeyHashDown("gui.getnextsearch") && focused()) {
|
||||
handleNavigateHistory(TextHistory.Direction.NEXT);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -156,4 +165,26 @@ public class SearchField extends TextField implements ItemFilterProvider {
|
||||
if (!secondary.isEmpty()) return new AnyMultiItemFilter(secondary);
|
||||
return new EverythingItemFilter();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFocus(boolean focus) {
|
||||
final boolean previousFocus = field.isFocused();
|
||||
|
||||
if (previousFocus != focus) {
|
||||
history.add(text());
|
||||
}
|
||||
super.setFocus(focus);
|
||||
}
|
||||
|
||||
private boolean handleNavigateHistory(TextHistory.Direction direction) {
|
||||
if (focused()) {
|
||||
return history.get(direction, text())
|
||||
.map(newText -> {
|
||||
setText(newText);
|
||||
return true;
|
||||
})
|
||||
.orElse(false);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package codechicken.nei.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Backported from JustEnoughItems/Core/src/main/java/mezz/jei/core/util/TextHistory.java
|
||||
*/
|
||||
public class TextHistory {
|
||||
private static final int MAX_HISTORY = 100;
|
||||
|
||||
public enum Direction {
|
||||
NEXT,
|
||||
PREVIOUS
|
||||
}
|
||||
|
||||
private final List<String> history = new ArrayList<>();
|
||||
|
||||
public boolean add(String currentText) {
|
||||
if (currentText.length() > 0) {
|
||||
history.remove(currentText);
|
||||
history.add(currentText);
|
||||
if (history.size() > MAX_HISTORY) {
|
||||
history.remove(0);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Optional<String> get(Direction direction, String currentText) {
|
||||
if (direction == direction.NEXT) {
|
||||
return getNext(currentText);
|
||||
} else {
|
||||
return getPrevious(currentText);
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<String> getPrevious(String currentText) {
|
||||
int historyIndex = history.indexOf(currentText);
|
||||
if (historyIndex < 0) {
|
||||
if (add(currentText)) {
|
||||
historyIndex = history.size() - 1;
|
||||
} else {
|
||||
historyIndex = history.size();
|
||||
}
|
||||
}
|
||||
if (historyIndex <= 0) {
|
||||
return Optional.empty();
|
||||
}
|
||||
String value = history.get(historyIndex - 1);
|
||||
return Optional.of(value);
|
||||
}
|
||||
|
||||
public Optional<String> getNext(String currentText) {
|
||||
int historyIndex = history.indexOf(currentText);
|
||||
if (historyIndex < 0) {
|
||||
return Optional.empty();
|
||||
}
|
||||
final String historyString;
|
||||
if (historyIndex + 1 < history.size()) {
|
||||
historyString = history.get(historyIndex + 1);
|
||||
} else {
|
||||
historyString = "";
|
||||
}
|
||||
return Optional.of(historyString);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user