Move search field logic to another class so we can reuse it in the crafter manager
This commit is contained in:
@@ -302,7 +302,6 @@ public class CraftingManager implements ICraftingManager {
|
||||
|
||||
patterns.addAll(container.getPatterns());
|
||||
|
||||
// @todo: Crafter first!
|
||||
if (!containerInventories.containsKey(container.getName())) {
|
||||
containerInventories.put(container.getName(), new ArrayList<>());
|
||||
}
|
||||
|
||||
@@ -8,10 +8,10 @@ import com.raoulvdberge.refinedstorage.container.slot.SlotCrafterManager;
|
||||
import com.raoulvdberge.refinedstorage.gui.control.Scrollbar;
|
||||
import com.raoulvdberge.refinedstorage.gui.control.SideButtonGridSize;
|
||||
import com.raoulvdberge.refinedstorage.gui.control.SideButtonRedstoneMode;
|
||||
import com.raoulvdberge.refinedstorage.gui.control.TextFieldSearch;
|
||||
import com.raoulvdberge.refinedstorage.tile.TileCrafterManager;
|
||||
import com.raoulvdberge.refinedstorage.tile.data.TileDataManager;
|
||||
import com.raoulvdberge.refinedstorage.util.RenderUtils;
|
||||
import net.minecraft.client.gui.GuiTextField;
|
||||
import net.minecraft.client.renderer.GlStateManager;
|
||||
import net.minecraft.client.resources.I18n;
|
||||
import net.minecraft.inventory.Slot;
|
||||
@@ -22,7 +22,8 @@ import java.util.Map;
|
||||
public class GuiCrafterManager extends GuiBase implements IResizableDisplay {
|
||||
private ContainerCrafterManager container;
|
||||
private NetworkNodeCrafterManager crafterManager;
|
||||
private GuiTextField searchField;
|
||||
|
||||
private TextFieldSearch searchField;
|
||||
|
||||
public GuiCrafterManager(NetworkNodeCrafterManager crafterManager) {
|
||||
super(null, 193, 0);
|
||||
@@ -120,10 +121,7 @@ public class GuiCrafterManager extends GuiBase implements IResizableDisplay {
|
||||
int sy = y + 6 + 1;
|
||||
|
||||
if (searchField == null) {
|
||||
searchField = new GuiTextField(0, fontRenderer, sx, sy, 88 - 6, fontRenderer.FONT_HEIGHT);
|
||||
searchField.setEnableBackgroundDrawing(false);
|
||||
searchField.setVisible(true);
|
||||
searchField.setTextColor(16777215);
|
||||
searchField = new TextFieldSearch(0, fontRenderer, sx, sy, 88 - 6);
|
||||
} else {
|
||||
searchField.x = sx;
|
||||
searchField.y = sy;
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
package com.raoulvdberge.refinedstorage.gui.control;
|
||||
|
||||
import com.raoulvdberge.refinedstorage.RSKeyBindings;
|
||||
import net.minecraft.client.gui.FontRenderer;
|
||||
import net.minecraft.client.gui.GuiTextField;
|
||||
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
|
||||
import org.lwjgl.input.Keyboard;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class TextFieldSearch extends GuiTextField {
|
||||
private static final List<String> SEARCH_HISTORY = new ArrayList<>();
|
||||
|
||||
private int searchHistoryIndex = -1;
|
||||
|
||||
private List<Runnable> listeners = new LinkedList<>();
|
||||
|
||||
public TextFieldSearch(int componentId, FontRenderer fontRenderer, int x, int y, int width) {
|
||||
super(componentId, fontRenderer, x, y, width, fontRenderer.FONT_HEIGHT);
|
||||
|
||||
setEnableBackgroundDrawing(false);
|
||||
setVisible(true);
|
||||
setTextColor(16777215);
|
||||
}
|
||||
|
||||
public void addListener(Runnable listener) {
|
||||
listeners.add(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseClicked(int mouseX, int mouseY, int mouseButton) {
|
||||
boolean wasFocused = isFocused();
|
||||
|
||||
boolean result = super.mouseClicked(mouseX, mouseY, mouseButton);
|
||||
|
||||
boolean flag = mouseX >= this.x && mouseX < this.x + this.width && mouseY >= this.y && mouseY < this.y + this.height;
|
||||
|
||||
if (flag && mouseButton == 1) {
|
||||
setText("");
|
||||
setFocused(true);
|
||||
|
||||
listeners.forEach(Runnable::run);
|
||||
} else if (wasFocused != isFocused()) {
|
||||
saveHistory();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean textboxKeyTyped(char typedChar, int keyCode) {
|
||||
boolean canLoseFocus = ObfuscationReflectionHelper.getPrivateValue(GuiTextField.class, this, 10);
|
||||
|
||||
boolean result = super.textboxKeyTyped(typedChar, keyCode);
|
||||
|
||||
if (isFocused()) {
|
||||
if (keyCode == Keyboard.KEY_UP) {
|
||||
updateSearchHistory(-1);
|
||||
|
||||
result = true;
|
||||
} else if (keyCode == Keyboard.KEY_DOWN) {
|
||||
updateSearchHistory(1);
|
||||
|
||||
result = true;
|
||||
} else if (keyCode == Keyboard.KEY_RETURN) {
|
||||
saveHistory();
|
||||
|
||||
if (canLoseFocus) {
|
||||
setFocused(false);
|
||||
}
|
||||
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (keyCode == RSKeyBindings.FOCUS_SEARCH_BAR.getKeyCode() && canLoseFocus) {
|
||||
setFocused(!isFocused());
|
||||
|
||||
saveHistory();
|
||||
|
||||
result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private void updateSearchHistory(int delta) {
|
||||
if (SEARCH_HISTORY.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (searchHistoryIndex == -1) {
|
||||
searchHistoryIndex = SEARCH_HISTORY.size();
|
||||
}
|
||||
|
||||
searchHistoryIndex += delta;
|
||||
|
||||
if (searchHistoryIndex < 0) {
|
||||
searchHistoryIndex = 0;
|
||||
} else if (searchHistoryIndex > SEARCH_HISTORY.size() - 1) {
|
||||
searchHistoryIndex = SEARCH_HISTORY.size() - 1;
|
||||
|
||||
if (delta == 1) {
|
||||
setText("");
|
||||
|
||||
listeners.forEach(Runnable::run);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
setText(SEARCH_HISTORY.get(searchHistoryIndex));
|
||||
|
||||
listeners.forEach(Runnable::run);
|
||||
}
|
||||
|
||||
private void saveHistory() {
|
||||
if (!SEARCH_HISTORY.isEmpty() && SEARCH_HISTORY.get(SEARCH_HISTORY.size() - 1).equals(getText())) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!getText().trim().isEmpty()) {
|
||||
SEARCH_HISTORY.add(getText());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -44,19 +44,15 @@ import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.fml.client.config.GuiCheckBox;
|
||||
import net.minecraftforge.fml.client.config.GuiUtils;
|
||||
import net.minecraftforge.fml.common.FMLCommonHandler;
|
||||
import org.lwjgl.input.Keyboard;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class GuiGrid extends GuiBase implements IResizableDisplay {
|
||||
private static final List<String> SEARCH_HISTORY = new ArrayList<>();
|
||||
|
||||
private IGridView view;
|
||||
|
||||
private GuiTextField searchField;
|
||||
private TextFieldSearch searchField;
|
||||
private GuiCheckBox oredictPattern;
|
||||
private GuiCheckBox processingPattern;
|
||||
private GuiCheckBox blockingPattern;
|
||||
@@ -72,8 +68,6 @@ public class GuiGrid extends GuiBase implements IResizableDisplay {
|
||||
|
||||
private int slotNumber;
|
||||
|
||||
private int searchHistoryIndex = -1;
|
||||
|
||||
public GuiGrid(ContainerGrid container, IGrid grid) {
|
||||
super(container, grid.getType() == GridType.FLUID ? 193 : 227, 0);
|
||||
|
||||
@@ -117,10 +111,12 @@ public class GuiGrid extends GuiBase implements IResizableDisplay {
|
||||
int sy = y + 6 + 1 + getTabHeight();
|
||||
|
||||
if (searchField == null) {
|
||||
searchField = new GuiTextField(0, fontRenderer, sx, sy, 88 - 6, fontRenderer.FONT_HEIGHT);
|
||||
searchField.setEnableBackgroundDrawing(false);
|
||||
searchField.setVisible(true);
|
||||
searchField.setTextColor(16777215);
|
||||
searchField = new TextFieldSearch(0, fontRenderer, sx, sy, 88 - 6);
|
||||
searchField.addListener(() -> {
|
||||
view.sort();
|
||||
|
||||
updateJEI();
|
||||
});
|
||||
|
||||
updateSearchFieldFocus(grid.getSearchBoxMode());
|
||||
} else {
|
||||
@@ -646,20 +642,7 @@ public class GuiGrid extends GuiBase implements IResizableDisplay {
|
||||
}
|
||||
|
||||
if (searchField != null) {
|
||||
boolean wasSearchFieldFocused = searchField.isFocused();
|
||||
|
||||
searchField.mouseClicked(mouseX, mouseY, clickedButton);
|
||||
|
||||
if (clickedButton == 1 && inBounds(79, 5 + getTabHeight(), 90, 12, mouseX - guiLeft, mouseY - guiTop)) {
|
||||
searchField.setText("");
|
||||
searchField.setFocused(true);
|
||||
|
||||
view.sort();
|
||||
|
||||
updateJEI();
|
||||
} else if (wasSearchFieldFocused != searchField.isFocused()) {
|
||||
saveHistory();
|
||||
}
|
||||
}
|
||||
|
||||
boolean clickedClear = clickedButton == 0 && isOverClear(mouseX - guiLeft, mouseY - guiTop);
|
||||
@@ -724,26 +707,9 @@ public class GuiGrid extends GuiBase implements IResizableDisplay {
|
||||
// NO OP
|
||||
} else if (searchField.textboxKeyTyped(character, keyCode)) {
|
||||
updateJEI();
|
||||
|
||||
view.sort();
|
||||
|
||||
keyHandled = true;
|
||||
} else if (searchField.isFocused() && (keyCode == Keyboard.KEY_UP || keyCode == Keyboard.KEY_DOWN || keyCode == Keyboard.KEY_RETURN)) {
|
||||
if (keyCode == Keyboard.KEY_UP) {
|
||||
updateSearchHistory(-1);
|
||||
} else if (keyCode == Keyboard.KEY_DOWN) {
|
||||
updateSearchHistory(1);
|
||||
} else {
|
||||
saveHistory();
|
||||
|
||||
if (grid.getSearchBoxMode() == IGrid.SEARCH_BOX_MODE_NORMAL || grid.getSearchBoxMode() == IGrid.SEARCH_BOX_MODE_JEI_SYNCHRONIZED) {
|
||||
searchField.setFocused(false);
|
||||
}
|
||||
}
|
||||
keyHandled = true;
|
||||
} else if (keyCode == RSKeyBindings.FOCUS_SEARCH_BAR.getKeyCode() && (grid.getSearchBoxMode() == IGrid.SEARCH_BOX_MODE_NORMAL || grid.getSearchBoxMode() == IGrid.SEARCH_BOX_MODE_JEI_SYNCHRONIZED)) {
|
||||
searchField.setFocused(!searchField.isFocused());
|
||||
|
||||
saveHistory();
|
||||
keyHandled = true;
|
||||
} else if (keyCode == RSKeyBindings.CLEAR_GRID_CRAFTING_MATRIX.getKeyCode()) {
|
||||
RS.INSTANCE.network.sendToServer(new MessageGridClear());
|
||||
@@ -752,50 +718,6 @@ public class GuiGrid extends GuiBase implements IResizableDisplay {
|
||||
}
|
||||
}
|
||||
|
||||
private void updateSearchHistory(int delta) {
|
||||
if (SEARCH_HISTORY.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (searchHistoryIndex == -1) {
|
||||
searchHistoryIndex = SEARCH_HISTORY.size();
|
||||
}
|
||||
|
||||
searchHistoryIndex += delta;
|
||||
|
||||
if (searchHistoryIndex < 0) {
|
||||
searchHistoryIndex = 0;
|
||||
} else if (searchHistoryIndex > SEARCH_HISTORY.size() - 1) {
|
||||
searchHistoryIndex = SEARCH_HISTORY.size() - 1;
|
||||
|
||||
if (delta == 1) {
|
||||
searchField.setText("");
|
||||
|
||||
view.sort();
|
||||
|
||||
updateJEI();
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
searchField.setText(SEARCH_HISTORY.get(searchHistoryIndex));
|
||||
|
||||
view.sort();
|
||||
|
||||
updateJEI();
|
||||
}
|
||||
|
||||
private void saveHistory() {
|
||||
if (!SEARCH_HISTORY.isEmpty() && SEARCH_HISTORY.get(SEARCH_HISTORY.size() - 1).equals(searchField.getText())) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!searchField.getText().trim().isEmpty()) {
|
||||
SEARCH_HISTORY.add(searchField.getText());
|
||||
}
|
||||
}
|
||||
|
||||
private void updateJEI() {
|
||||
if (IntegrationJEI.isLoaded() && (grid.getSearchBoxMode() == IGrid.SEARCH_BOX_MODE_JEI_SYNCHRONIZED || grid.getSearchBoxMode() == IGrid.SEARCH_BOX_MODE_JEI_SYNCHRONIZED_AUTOSELECTED)) {
|
||||
RSJEIPlugin.INSTANCE.getRuntime().getIngredientFilter().setFilterText(searchField.getText());
|
||||
@@ -809,7 +731,6 @@ public class GuiGrid extends GuiBase implements IResizableDisplay {
|
||||
}
|
||||
}
|
||||
|
||||
// @todo: Move logic to other class so we can reuse search field in crafter manager
|
||||
public GuiTextField getSearchField() {
|
||||
return searchField;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user