/
IngredientListOverlay.java
362 lines (319 loc) · 11.7 KB
/
IngredientListOverlay.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package mezz.jei.gui.overlay;
import javax.annotation.Nullable;
import java.awt.Rectangle;
import java.util.List;
import java.util.Set;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.item.ItemStack;
import com.google.common.collect.ImmutableList;
import mezz.jei.api.IIngredientListOverlay;
import mezz.jei.api.gui.IGuiProperties;
import mezz.jei.config.Config;
import mezz.jei.config.KeyBindings;
import mezz.jei.gui.GuiScreenHelper;
import mezz.jei.gui.elements.GuiIconToggleButton;
import mezz.jei.gui.ghost.GhostIngredientDragManager;
import mezz.jei.gui.ingredients.IIngredientListElement;
import mezz.jei.gui.recipes.RecipesGui;
import mezz.jei.ingredients.IngredientFilter;
import mezz.jei.ingredients.IngredientRegistry;
import mezz.jei.input.GuiTextFieldFilter;
import mezz.jei.input.IClickedIngredient;
import mezz.jei.input.IMouseHandler;
import mezz.jei.input.IShowsRecipeFocuses;
import mezz.jei.util.CommandUtil;
import mezz.jei.util.Log;
public class IngredientListOverlay implements IIngredientListOverlay, IMouseHandler, IShowsRecipeFocuses {
private static final int BORDER_PADDING = 2;
private static final int BUTTON_SIZE = 20;
private static final int SEARCH_HEIGHT = 20;
private boolean hasRoom;
private static boolean isSearchBarCentered(IGuiProperties guiProperties) {
return Config.isCenterSearchBarEnabled() &&
guiProperties.getGuiTop() + guiProperties.getGuiYSize() + SEARCH_HEIGHT < guiProperties.getScreenHeight();
}
private final IngredientFilter ingredientFilter;
private final GuiIconToggleButton configButton;
private final IngredientGridWithNavigation contents;
private final GuiScreenHelper guiScreenHelper;
private final GuiTextFieldFilter searchField;
private final GhostIngredientDragManager ghostIngredientDragManager;
private Rectangle displayArea = new Rectangle();
// properties of the gui we're beside
@Nullable
private IGuiProperties guiProperties;
public IngredientListOverlay(IngredientFilter ingredientFilter, IngredientRegistry ingredientRegistry, GuiScreenHelper guiScreenHelper) {
this.ingredientFilter = ingredientFilter;
this.guiScreenHelper = guiScreenHelper;
this.contents = new IngredientGridWithNavigation(ingredientFilter, guiScreenHelper, GridAlignment.LEFT);
ingredientFilter.addListener(() -> onSetFilterText(Config.getFilterText()));
this.searchField = new GuiTextFieldFilter(0, ingredientFilter);
this.configButton = ConfigButton.create(this);
this.ghostIngredientDragManager = new GhostIngredientDragManager(this.contents, guiScreenHelper, ingredientRegistry);
this.setKeyboardFocus(false);
}
public void rebuildItemFilter() {
Log.get().info("Updating ingredient filter...");
long start_time = System.currentTimeMillis();
this.ingredientFilter.modesChanged();
Log.get().info("Updated ingredient filter in {} ms", System.currentTimeMillis() - start_time);
updateLayout(true);
}
public boolean isListDisplayed() {
return Config.isOverlayEnabled() && this.guiProperties != null && this.hasRoom;
}
private static Rectangle getDisplayArea(IGuiProperties guiProperties) {
final int x = guiProperties.getGuiLeft() + guiProperties.getGuiXSize() + BORDER_PADDING;
final int y = BORDER_PADDING;
final int width = guiProperties.getScreenWidth() - x - BORDER_PADDING;
final int height = guiProperties.getScreenHeight() - y - BORDER_PADDING;
return new Rectangle(x, y, width, height);
}
public void updateScreen(@Nullable GuiScreen guiScreen, boolean forceUpdate) {
final boolean wasDisplayed = isListDisplayed();
IGuiProperties guiProperties = guiScreenHelper.getGuiProperties(guiScreen);
if (guiProperties == null) {
if (this.guiProperties != null) {
this.guiProperties = null;
setKeyboardFocus(false);
this.ghostIngredientDragManager.stopDrag();
}
} else {
if (forceUpdate || this.guiProperties == null || !GuiProperties.areEqual(this.guiProperties, guiProperties)) {
this.guiProperties = guiProperties;
this.displayArea = getDisplayArea(guiProperties);
final boolean searchBarCentered = isSearchBarCentered(guiProperties);
final int searchHeight = searchBarCentered ? 0 : SEARCH_HEIGHT + BORDER_PADDING;
Set<Rectangle> guiExclusionAreas = guiScreenHelper.getGuiExclusionAreas();
Rectangle availableContentsArea = new Rectangle(
displayArea.x,
displayArea.y,
displayArea.width,
displayArea.height - searchHeight
);
hasRoom = this.contents.updateBounds(availableContentsArea, guiExclusionAreas, 4 * BUTTON_SIZE);
// update area to match contents size
Rectangle contentsArea = this.contents.getArea();
displayArea.x = contentsArea.x;
displayArea.width = contentsArea.width;
if (searchBarCentered && isListDisplayed()) {
searchField.updateBounds(new Rectangle(
guiProperties.getGuiLeft(),
guiProperties.getScreenHeight() - SEARCH_HEIGHT - BORDER_PADDING,
guiProperties.getGuiXSize() - BUTTON_SIZE + 1,
SEARCH_HEIGHT
));
} else {
searchField.updateBounds(new Rectangle(
displayArea.x,
displayArea.y + displayArea.height - SEARCH_HEIGHT - BORDER_PADDING,
displayArea.width - BUTTON_SIZE + 1,
SEARCH_HEIGHT
));
}
this.configButton.updateBounds(new Rectangle(
searchField.x + searchField.width - 1,
searchField.y,
BUTTON_SIZE,
BUTTON_SIZE
));
updateLayout(false);
}
}
if (wasDisplayed && !isListDisplayed()) {
Config.saveFilterText();
}
}
public void updateLayout(boolean filterChanged) {
this.contents.updateLayout(filterChanged);
this.searchField.update();
}
public void drawScreen(Minecraft minecraft, int mouseX, int mouseY, float partialTicks) {
if (this.guiProperties != null) {
if (isListDisplayed()) {
GlStateManager.disableLighting();
this.searchField.drawTextBox();
this.contents.draw(minecraft, mouseX, mouseY, partialTicks);
this.configButton.draw(minecraft, mouseX, mouseY, partialTicks);
} else {
this.configButton.draw(minecraft, mouseX, mouseY, partialTicks);
}
}
}
public void drawTooltips(Minecraft minecraft, int mouseX, int mouseY) {
if (isListDisplayed()) {
this.configButton.drawTooltips(minecraft, mouseX, mouseY);
this.ghostIngredientDragManager.drawTooltips(minecraft, mouseX, mouseY);
this.contents.drawTooltips(minecraft, mouseX, mouseY);
} else if (this.guiProperties != null) {
this.configButton.drawTooltips(minecraft, mouseX, mouseY);
}
}
public void drawOnForeground(GuiContainer gui, int mouseX, int mouseY) {
if (isListDisplayed()) {
GlStateManager.pushMatrix();
GlStateManager.translate(-gui.getGuiLeft(), -gui.getGuiTop(), 0);
this.ghostIngredientDragManager.drawOnForeground(gui.mc, mouseX, mouseY);
GlStateManager.popMatrix();
}
}
public void handleTick() {
if (this.isListDisplayed()) {
this.searchField.updateCursorCounter();
}
}
@Override
public boolean isMouseOver(int mouseX, int mouseY) {
if (isListDisplayed()) {
if (Config.isCenterSearchBarEnabled() && searchField.isMouseOver(mouseX, mouseY)) {
return true;
}
return displayArea.contains(mouseX, mouseY);
} else if (this.guiProperties != null) {
return this.configButton.isMouseOver(mouseX, mouseY);
}
return false;
}
@Override
@Nullable
public IClickedIngredient<?> getIngredientUnderMouse(int mouseX, int mouseY) {
if (isListDisplayed()) {
IClickedIngredient<?> clicked = this.contents.getIngredientUnderMouse(mouseX, mouseY);
if (clicked != null) {
clicked.setOnClickHandler(() -> setKeyboardFocus(false));
return clicked;
}
}
return null;
}
@Override
public boolean canSetFocusWithMouse() {
return this.isListDisplayed() && this.contents.canSetFocusWithMouse();
}
@Override
public boolean handleMouseClicked(int mouseX, int mouseY, int mouseButton) {
if (isListDisplayed()) {
if (this.ghostIngredientDragManager.handleMouseClicked(mouseX, mouseY)) {
return true;
}
if (this.configButton.handleMouseClick(mouseX, mouseY)) {
return true;
}
if (!isMouseOver(mouseX, mouseY)) {
setKeyboardFocus(false);
return false;
}
if (this.contents.handleMouseClicked(mouseX, mouseY, mouseButton)) {
setKeyboardFocus(false);
return true;
}
boolean searchClicked = this.searchField.isMouseOver(mouseX, mouseY);
setKeyboardFocus(searchClicked);
if (searchClicked) {
final boolean updated = this.searchField.handleMouseClicked(mouseX, mouseY, mouseButton);
if (updated) {
updateLayout(false);
}
return true;
}
Minecraft minecraft = Minecraft.getMinecraft();
GuiScreen currentScreen = minecraft.currentScreen;
if (currentScreen != null && !(currentScreen instanceof RecipesGui) &&
(mouseButton == 0 || mouseButton == 1 || minecraft.gameSettings.keyBindPickBlock.isActiveAndMatches(mouseButton - 100))) {
IClickedIngredient<?> clicked = getIngredientUnderMouse(mouseX, mouseY);
if (clicked != null) {
if (Config.isCheatItemsEnabled()) {
ItemStack itemStack = clicked.getCheatItemStack();
if (!itemStack.isEmpty()) {
CommandUtil.giveStack(itemStack, mouseButton);
}
clicked.onClickHandled();
return true;
}
EntityPlayerSP player = minecraft.player;
if (player != null) {
ItemStack mouseItem = player.inventory.getItemStack();
if (mouseItem.isEmpty() && this.ghostIngredientDragManager.handleClickGhostIngredient(currentScreen, clicked)) {
return true;
}
}
}
}
} else if (this.guiProperties != null) {
return this.configButton.handleMouseClick(mouseX, mouseY);
}
return false;
}
@Override
public boolean handleMouseScrolled(int mouseX, int mouseY, int scrollDelta) {
return isListDisplayed() &&
isMouseOver(mouseX, mouseY) &&
this.contents.handleMouseScrolled(mouseX, mouseY, scrollDelta);
}
@Override
public boolean hasKeyboardFocus() {
return isListDisplayed() && this.searchField.isFocused();
}
public void setKeyboardFocus(boolean keyboardFocus) {
this.searchField.setFocused(keyboardFocus);
}
public boolean onGlobalKeyPressed(int eventKey) {
if (isListDisplayed()) {
if (KeyBindings.toggleCheatMode.isActiveAndMatches(eventKey)) {
Config.toggleCheatItemsEnabled();
return true;
}
if (KeyBindings.focusSearch.isActiveAndMatches(eventKey)) {
setKeyboardFocus(true);
return true;
}
}
return false;
}
public boolean onKeyPressed(char typedChar, int eventKey) {
if (isListDisplayed()) {
if (hasKeyboardFocus() &&
searchField.textboxKeyTyped(typedChar, eventKey)) {
boolean changed = Config.setFilterText(searchField.getText());
if (changed) {
updateLayout(true);
}
return true;
}
return this.contents.onKeyPressed(typedChar, eventKey);
}
return false;
}
@Nullable
@Override
public Object getIngredientUnderMouse() {
if (isListDisplayed()) {
IIngredientListElement elementUnderMouse = this.contents.getElementUnderMouse();
if (elementUnderMouse != null) {
return elementUnderMouse.getIngredient();
}
}
return null;
}
public void onSetFilterText(String filterText) {
this.searchField.setText(filterText);
updateLayout(true);
}
@Override
public ImmutableList<Object> getVisibleIngredients() {
if (isListDisplayed()) {
ImmutableList.Builder<Object> visibleIngredients = ImmutableList.builder();
List<IIngredientListElement> visibleElements = this.contents.getVisibleElements();
for (IIngredientListElement element : visibleElements) {
Object ingredient = element.getIngredient();
visibleIngredients.add(ingredient);
}
return visibleIngredients.build();
}
return ImmutableList.of();
}
}