Compare commits

..
Author SHA1 Message Date
slprime 331a9fd638 code refactoring 2024-06-11 00:15:24 +03:00
slprime 98c49caff9 move autofocus config to seatch widget panel 2024-06-10 23:56:43 +03:00
Martin Robertz 622b696389 Merge branch 'master' into jei_search 2024-06-10 22:22:29 +02:00
OvermindDL1andMartin Robertz 8da541460f Added ability to have the NEI search be auto-focused on open. (#492)
* Added open to have the NEI search be auto-focused on open.  It will automatically blue if the mouse is moved before typing anything so immediate 'r' and such use works as expected.

* Added open to have the NEI search be auto-focused on open.  It will automatically blue if the mouse is moved before typing anything so immediate 'r' and such use works as expected.

* Add an allowlist for what classnames to allow the search auto-focus to work on

* sa

* Add `INEIAutoFocusSearchEnable` interface for opting in to autofocus search box by a mod.  Moved the autofocus list from config into a standalone file at the loss of live-updating.

* Remove now-superfluous lang entries for search focus prefix allowlist

* Undo intellij automatic star-imports...

---------

Co-authored-by: Martin Robertz <[email protected]>
2024-06-10 22:19:25 +02:00
slprime 985e1e0425 add regexp to extended patternMode 2024-06-08 20:21:10 +03:00
8 changed files with 135 additions and 21 deletions

No files matched your search

@@ -149,6 +149,7 @@ public class ClientHandler {
loadSerialHandlers();
loadHeightHackHandlers();
loadHiddenHandlers();
loadEnableAutoFocus();
ItemInfo.preInit();
StackInfo.loadGuidFilters();
}
@@ -225,6 +226,30 @@ public class ClientHandler {
}
}
public static void loadEnableAutoFocus() {
File file = NEIClientConfig.enableAutoFocusFile;
if (!file.exists()) {
try (FileWriter writer = new FileWriter(file)) {
NEIClientConfig.logger.info("Creating default enable auto focus list {}", file);
URL defaultEnableAutoFocusResource = ClientHandler.class
.getResource("/assets/nei/cfg/enableautofocus.cfg");
if (defaultEnableAutoFocusResource != null) {
IOUtils.copy(defaultEnableAutoFocusResource.openStream(), writer);
}
} catch (IOException e) {
NEIClientConfig.logger.error("Failed to save default enable auto focus list to file {}", file, e);
}
}
try (FileReader reader = new FileReader(file)) {
NEIClientConfig.logger.info("Loading enable auto focus from file {}", file);
NEIClientConfig.enableAutoFocusPrefixes = IOUtils.readLines(reader).stream()
.filter((line) -> !line.startsWith("#")).collect(Collectors.toCollection(ArrayList::new));
} catch (IOException e) {
NEIClientConfig.logger.error("Failed to load enable auto focus from file {}", file, e);
}
}
public static void load() {
instance = new ClientHandler();
@@ -12,10 +12,11 @@ import org.lwjgl.opengl.GL11;
import codechicken.lib.render.CCRenderState;
import codechicken.lib.vec.Rectangle4i;
import codechicken.nei.api.INEIAutoFocusSearchEnable;
import codechicken.nei.api.INEIGuiHandler;
import codechicken.nei.api.TaggedInventoryArea;
public class GuiExtendedCreativeInv extends GuiContainer implements INEIGuiHandler {
public class GuiExtendedCreativeInv extends GuiContainer implements INEIGuiHandler, INEIAutoFocusSearchEnable {
public GuiExtendedCreativeInv(Container par1Container) {
super(par1Container);
@@ -52,6 +52,7 @@ import codechicken.lib.gui.GuiDraw;
import codechicken.nei.KeyManager.IKeyStateTracker;
import codechicken.nei.api.API;
import codechicken.nei.api.GuiInfo;
import codechicken.nei.api.INEIAutoFocusSearchEnable;
import codechicken.nei.api.IRecipeOverlayRenderer;
import codechicken.nei.api.ItemInfo;
import codechicken.nei.api.LayoutStyle;
@@ -87,6 +88,9 @@ public class LayoutManager implements IContainerInputHandler, IContainerTooltipH
public static SubsetWidget dropDown;
public static PresetsWidget presetsPanel;
public static SearchField searchField;
public static boolean searchInitFocusedCancellable = false;
protected static int mousePriorX;
protected static int mousePriorY;
public static ButtonCycled options;
public static ButtonCycled bookmarksButton;
@@ -163,10 +167,36 @@ public class LayoutManager implements IContainerInputHandler, IContainerTooltipH
return getSideWidth(gui);
}
protected boolean isAllowedGuiAutoSearchFocus(GuiContainer gui) {
if (gui instanceof INEIAutoFocusSearchEnable) {
return true;
}
String guiClassName = gui.getClass().getName();
for (String prefix : NEIClientConfig.enableAutoFocusPrefixes) {
if (guiClassName.startsWith(prefix)) {
return true;
}
}
return false;
}
protected void searchFocusInitCancelCheck() {
if (searchInitFocusedCancellable) {
if (searchField.isVisible() && NEIClientConfig.isFocusSearchWidgetOnOpen()
&& getInputFocused() == searchField) {
searchField.setFocus(false);
setInputFocused(null);
}
searchInitFocusedCancellable = false;
}
}
@Override
public void onMouseClicked(GuiContainer gui, int mousex, int mousey, int button) {
if (isHidden()) return;
searchFocusInitCancelCheck();
for (Widget widget : controlWidgets) widget.onGuiClick(mousex, mousey);
}
@@ -176,6 +206,8 @@ public class LayoutManager implements IContainerInputHandler, IContainerTooltipH
if (!isEnabled()) return options.contains(mousex, mousey) && options.handleClick(mousex, mousey, button);
searchFocusInitCancelCheck();
for (Widget widget : controlWidgets) {
widget.onGuiClick(mousex, mousey);
if (widget.contains(mousex, mousey) ? widget.handleClick(mousex, mousey, button)
@@ -196,6 +228,7 @@ public class LayoutManager implements IContainerInputHandler, IContainerTooltipH
public boolean keyTyped(GuiContainer gui, char keyChar, int keyID) {
if (isEnabled() && !isHidden()) {
searchInitFocusedCancellable = false;
if (inputFocused != null) return inputFocused.handleKeyPress(keyID, keyChar);
for (Widget widget : controlWidgets) if (widget.handleKeyPress(keyID, keyChar)) return true;
@@ -234,6 +267,7 @@ public class LayoutManager implements IContainerInputHandler, IContainerTooltipH
public void onMouseUp(GuiContainer gui, int mx, int my, int button) {
if (!isHidden() && isEnabled()) {
searchFocusInitCancelCheck();
for (Widget widget : controlWidgets) widget.mouseUp(mx, my, button);
}
}
@@ -241,6 +275,7 @@ public class LayoutManager implements IContainerInputHandler, IContainerTooltipH
@Override
public void onMouseDragged(GuiContainer gui, int mx, int my, int button, long heldTime) {
if (!isHidden() && isEnabled()) {
searchFocusInitCancelCheck();
for (Widget widget : controlWidgets) widget.mouseDragged(mx, my, button, heldTime);
}
}
@@ -259,6 +294,14 @@ public class LayoutManager implements IContainerInputHandler, IContainerTooltipH
public void renderObjects(GuiContainer gui, int mousex, int mousey) {
if (!isHidden()) {
layout(gui);
if (mousePriorX == -1) {
mousePriorX = mousex;
mousePriorY = mousey;
} else if (mousePriorX != mousex || mousePriorY != mousey) {
searchFocusInitCancelCheck();
mousePriorX = mousex;
mousePriorY = mousey;
}
if (isEnabled()) {
getLayoutStyle().drawBackground(GuiContainerManager.getManager(gui));
for (Widget widget : drawWidgets) widget.draw(mousex, mousey);
@@ -620,6 +663,15 @@ public class LayoutManager implements IContainerInputHandler, IContainerTooltipH
getLayoutStyle().init();
layout(gui);
mousePriorX = -1;
mousePriorY = -1;
if (searchField.isVisible() && NEIClientConfig.isFocusSearchWidgetOnOpen()
&& isAllowedGuiAutoSearchFocus(gui)) {
searchField.setFocus(true);
setInputFocused(searchField);
searchInitFocusedCancellable = true;
}
}
NEIController.load(gui);
@@ -721,6 +773,7 @@ public class LayoutManager implements IContainerInputHandler, IContainerTooltipH
@Override
public boolean mouseScrolled(GuiContainer gui, int mousex, int mousey, int scrolled) {
if (isHidden() || !isEnabled()) return false;
searchFocusInitCancelCheck();
for (Widget widget : drawWidgets) if (widget.onMouseWheel(scrolled, mousex, mousey)) return true;
@@ -1,6 +1,7 @@
package codechicken.nei;
import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
@@ -77,6 +78,7 @@ public class NEIClientConfig {
public static final File heightHackHandlersFile = new File(configDir, "heighthackhandlers.cfg");
public static final File handlerOrderingFile = new File(configDir, "handlerordering.csv");
public static final File hiddenHandlersFile = new File(configDir, "hiddenhandlers.csv");
public static final File enableAutoFocusFile = new File(configDir, "enableautofocus.cfg");
@Deprecated
public static File bookmarkFile;
@@ -96,6 +98,9 @@ public class NEIClientConfig {
// Any handler not in the map will be assigned to 0, and negative numbers are fine.
public static HashMap<String, Integer> handlerOrdering = new HashMap<>();
// List of prefixes of classes that should enable the autofocus search widget on open.
public static ArrayList<String> enableAutoFocusPrefixes = new ArrayList<>();
// Function that extracts the handler ID from a handler, with special logic for
// TemplateRecipeHandler: prefer using the overlay ID if it exists.
public static final Function<IRecipeHandler, String> HANDLER_ID_FUNCTION = handler -> Objects
@@ -167,6 +172,11 @@ public class NEIClientConfig {
tag.getTag("inventory.search.widgetPosition").setComment("Widget Position").getBooleanValue(true);
API.addOption(new OptionToggleButton("inventory.search.widgetPosition", true));
tag.getTag("inventory.search.widgetAutofocus")
.setComment("Focus Search Widget on Open, blurs on mouse move unless typing has started first")
.getBooleanValue(false);
API.addOption(new OptionToggleButton("inventory.search.widgetAutofocus", true));
tag.getTag("inventory.search.patternMode").setComment("Search Mode").getIntValue(1);
API.addOption(new OptionCycled("inventory.search.patternMode", 3, true));
@@ -674,6 +684,10 @@ public class NEIClientConfig {
return getBooleanSetting("inventory.search.widgetPosition");
}
public static boolean isFocusSearchWidgetOnOpen() {
return getBooleanSetting("inventory.search.widgetAutofocus");
}
public static boolean areJEIStyleTabsVisible() {
return getBooleanSetting("inventory.jei_style_tabs");
}
+27 -18
View File
@@ -343,33 +343,42 @@ public class SearchField extends TextField implements ItemFilterProvider {
}
public static Pattern getPattern(String search) {
switch (NEIClientConfig.getIntSetting("inventory.search.patternMode")) {
return getPattern(search, NEIClientConfig.getIntSetting("inventory.search.patternMode"));
}
public static Pattern getPattern(String search, int patternMode) {
switch (patternMode) {
case 0: // plain
search = Pattern.quote(search);
break;
case 1: // extended
final Matcher matcher = Pattern.compile("(\\?|\\*)").matcher(search);
String cleanedString = "";
int lastEndIndex = 0;
while (matcher.find()) {
cleanedString += Pattern.quote(search.substring(lastEndIndex, matcher.start()));
if (search.length() >= 3 && search.startsWith("r/") && search.endsWith("/")) {
search = search.substring(2, search.length() - 1);
} else {
final Matcher matcher = Pattern.compile("(\\?|\\*)").matcher(search);
String cleanedString = "";
int lastEndIndex = 0;
switch (matcher.group(0).charAt(0)) {
case '?':
cleanedString += ".";
break;
case '*':
cleanedString += ".+?";
break;
default:
break;
while (matcher.find()) {
cleanedString += Pattern.quote(search.substring(lastEndIndex, matcher.start()));
switch (matcher.group(0).charAt(0)) {
case '?':
cleanedString += ".";
break;
case '*':
cleanedString += ".+?";
break;
default:
break;
}
lastEndIndex = matcher.end();
}
lastEndIndex = matcher.end();
search = cleanedString + Pattern.quote(search.substring(lastEndIndex, search.length()));
}
search = cleanedString + Pattern.quote(search.substring(lastEndIndex, search.length()));
break;
}
@@ -0,0 +1,8 @@
package codechicken.nei.api;
/**
* Implement this interface on a GuiContainer to enable the autofocus search in NEI (if the autofocus option is enabled)
* without needing to add the classname to the config file .
*/
public interface INEIAutoFocusSearchEnable {
}
@@ -0,0 +1,3 @@
# Each line in this file should either be a comment (line starts with '#'), or a prefix of a fully-qualified class name
# (that includes the package name) for a Minecraft GUI Container screen.
# If you delete this file, it will be regenerated with the default enable autofocus list.
@@ -176,11 +176,13 @@ nei.options.inventory.showRecipeMarker.false=No
nei.options.inventory.showItemQuantityWidget=Show Item Quantity Widget
nei.options.inventory.showItemQuantityWidget.true=Yes
nei.options.inventory.showItemQuantityWidget.false=No
nei.options.inventory.search=Search Widget
nei.options.inventory.search.widgetPosition=Widget Position
nei.options.inventory.search.widgetPosition.true=Center
nei.options.inventory.search.widgetPosition.false=Right
nei.options.inventory.search.widgetAutofocus=Autofocus
nei.options.inventory.search.widgetAutofocus.true=Yes
nei.options.inventory.search.widgetAutofocus.false=No
nei.options.inventory.search.patternMode=Search Mode
nei.options.inventory.search.patternMode.0=Plain
nei.options.inventory.search.patternMode.1=Extended
@@ -208,7 +210,6 @@ nei.options.inventory.search.subsetsSearchMode=Item Subsets Search
nei.options.inventory.search.subsetsSearchMode.0=Always
nei.options.inventory.search.subsetsSearchMode.1=Prefix: %s
nei.options.inventory.search.subsetsSearchMode.2=Never
nei.options.inventory.creative_tab_style=Creative or JEI tab style
nei.options.inventory.creative_tab_style.true=Creative
nei.options.inventory.creative_tab_style.false=JEI