/
GuiTextFieldFilter.java
205 lines (181 loc) · 5.81 KB
/
GuiTextFieldFilter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package mezz.jei.input;
import com.mojang.blaze3d.matrix.MatrixStack;
import java.util.LinkedList;
import java.util.List;
import com.mojang.blaze3d.systems.RenderSystem;
import mezz.jei.input.click.MouseClickState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.client.renderer.Rectangle2d;
import mezz.jei.Internal;
import mezz.jei.config.IWorldConfig;
import mezz.jei.config.KeyBindings;
import mezz.jei.gui.HoverChecker;
import mezz.jei.gui.elements.DrawableNineSliceTexture;
import mezz.jei.gui.ingredients.IIngredientListElement;
import mezz.jei.gui.overlay.IIngredientGridSource;
import net.minecraft.util.text.StringTextComponent;
import org.lwjgl.glfw.GLFW;
public class GuiTextFieldFilter extends TextFieldWidget {
private static final int MAX_HISTORY = 100;
private static final int maxSearchLength = 128;
private static final List<String> history = new LinkedList<>();
private final HoverChecker hoverChecker;
private final IIngredientGridSource ingredientSource;
private final IWorldConfig worldConfig;
private final IMouseHandler mouseHandler;
private boolean previousKeyboardRepeatEnabled;
private final DrawableNineSliceTexture background;
public GuiTextFieldFilter(IIngredientGridSource ingredientSource, IWorldConfig worldConfig) {
// TODO narrator string
super(Minecraft.getInstance().font, 0, 0, 0, 0, StringTextComponent.EMPTY);
this.worldConfig = worldConfig;
setMaxLength(maxSearchLength);
this.hoverChecker = new HoverChecker();
this.ingredientSource = ingredientSource;
this.background = Internal.getTextures().getSearchBackground();
this.mouseHandler = new MouseHandler();
}
public void updateBounds(Rectangle2d area) {
this.x = area.getX();
this.y = area.getY();
this.width = area.getWidth();
this.height = area.getHeight();
this.hoverChecker.updateBounds(area.getY(), area.getY() + area.getHeight(), area.getX(), area.getX() + area.getWidth());
setHighlightPos(getCursorPosition());
}
public void update() {
String filterText = worldConfig.getFilterText();
if (!filterText.equals(getValue())) {
setValue(filterText);
}
List<IIngredientListElement<?>> ingredientList = ingredientSource.getIngredientList(filterText);
if (ingredientList.size() == 0) {
setTextColor(0xFFFF0000);
} else {
setTextColor(0xFFFFFFFF);
}
}
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
boolean handled = super.keyPressed(keyCode, scanCode, modifiers);
if (!handled && !history.isEmpty()) {
if (keyCode == GLFW.GLFW_KEY_UP) {
String currentText = getValue();
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);
setValue(historyString);
handled = true;
}
} else if (keyCode == GLFW.GLFW_KEY_DOWN) {
String currentText = getValue();
int historyIndex = history.indexOf(currentText);
if (historyIndex >= 0) {
String historyString;
if (historyIndex + 1 < history.size()) {
historyString = history.get(historyIndex + 1);
} else {
historyString = "";
}
setValue(historyString);
handled = true;
}
} else if (KeyBindings.isEnterKey(keyCode)) {
saveHistory();
}
}
return handled;
}
@Override
public boolean isMouseOver(double mouseX, double mouseY) {
return hoverChecker.checkHover(mouseX, mouseY);
}
public IMouseHandler getMouseHandler() {
return mouseHandler;
}
@Override
public void setFocused(boolean keyboardFocus) {
final boolean previousFocus = isFocused();
super.setFocused(keyboardFocus);
if (previousFocus != keyboardFocus) {
Minecraft minecraft = Minecraft.getInstance();
if (keyboardFocus) {
previousKeyboardRepeatEnabled = minecraft.keyboardHandler.sendRepeatsToGui;
minecraft.keyboardHandler.setSendRepeatsToGui(true);
} else {
minecraft.keyboardHandler.setSendRepeatsToGui(previousKeyboardRepeatEnabled);
}
saveHistory();
}
}
private boolean saveHistory() {
String text = getValue();
if (text.length() > 0) {
history.remove(text);
history.add(text);
if (history.size() > MAX_HISTORY) {
history.remove(0);
}
return true;
}
return false;
}
// begin hack to draw our own background texture instead of the default one
private boolean isDrawing = false;
@Override
protected boolean isBordered() {
if (this.isDrawing) {
return false;
}
return super.isBordered();
}
@SuppressWarnings("deprecation")
@Override
public void renderButton(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
this.isDrawing = true;
if (this.isVisible()) {
RenderSystem.color4f(1, 1, 1, 1);
background.draw(matrixStack, this.x, this.y, this.width, this.height);
}
super.renderButton(matrixStack, mouseX, mouseY, partialTicks);
this.isDrawing = false;
}
// end background hack
private class MouseHandler implements IMouseHandler {
@Override
public IMouseHandler handleClick(Screen screen, double mouseX, double mouseY, int mouseButton, MouseClickState clickState) {
if (!isMouseOver(mouseX, mouseY)) {
return null;
}
if (mouseButton == 1) {
if (!clickState.isSimulate()) {
setValue("");
setFocused(true);
worldConfig.setFilterText("");
}
return this;
}
if (!clickState.isSimulate()) {
if (GuiTextFieldFilter.super.mouseClicked(mouseX, mouseY, mouseButton)) {
return this;
}
return null;
} else {
return this; // can't easily simulate the click, just say we could handle it
}
}
@Override
public void handleMouseClickedOut(int mouseButton) {
setFocused(false);
}
}
}