/
IngredientListOverlay.java
394 lines (350 loc) · 12.8 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package mezz.jei.gui.overlay;
import com.google.common.collect.ImmutableList;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import mezz.jei.api.gui.handlers.IGuiProperties;
import mezz.jei.api.ingredients.IIngredientType;
import mezz.jei.api.runtime.IIngredientListOverlay;
import mezz.jei.config.IClientConfig;
import mezz.jei.config.IWorldConfig;
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.ingredients.IngredientManager;
import mezz.jei.input.CombinedMouseHandler;
import mezz.jei.input.GuiTextFieldFilter;
import mezz.jei.input.IClickedIngredient;
import mezz.jei.input.IMouseDragHandler;
import mezz.jei.input.IMouseHandler;
import mezz.jei.input.IShowsRecipeFocuses;
import mezz.jei.input.NullMouseDragHandler;
import mezz.jei.input.NullMouseHandler;
import mezz.jei.input.ProxyMouseDragHandler;
import mezz.jei.input.ProxyMouseHandler;
import mezz.jei.input.click.MouseClickState;
import mezz.jei.util.MathUtil;
import mezz.jei.util.Rectangle2dBuilder;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.client.renderer.Rectangle2d;
import net.minecraft.client.util.InputMappings;
import net.minecraft.util.Tuple;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Set;
public class IngredientListOverlay implements IIngredientListOverlay, IShowsRecipeFocuses {
private static final int BORDER_PADDING = 2;
private static final int BUTTON_SIZE = 20;
private static final int SEARCH_HEIGHT = 20;
private final GuiIconToggleButton configButton;
private final IngredientGridWithNavigation contents;
private final IClientConfig clientConfig;
private final IWorldConfig worldConfig;
private final GuiScreenHelper guiScreenHelper;
private final GuiTextFieldFilter searchField;
private final GhostIngredientDragManager ghostIngredientDragManager;
private final IMouseHandler displayedMouseHandler;
private final IMouseHandler hiddenMouseHandler;
private Rectangle2d displayArea = new Rectangle2d(0, 0, 0, 0);
private boolean hasRoom;
// properties of the gui we're next to
@Nullable
private IGuiProperties guiProperties;
public IngredientListOverlay(
IIngredientGridSource ingredientGridSource,
IngredientManager ingredientManager,
GuiScreenHelper guiScreenHelper,
IngredientGridWithNavigation contents,
IClientConfig clientConfig,
IWorldConfig worldConfig
) {
this.guiScreenHelper = guiScreenHelper;
this.contents = contents;
this.clientConfig = clientConfig;
this.worldConfig = worldConfig;
ingredientGridSource.addListener(() -> onSetFilterText(worldConfig.getFilterText()));
this.searchField = new GuiTextFieldFilter(ingredientGridSource, worldConfig);
this.configButton = ConfigButton.create(this, worldConfig);
this.ghostIngredientDragManager = new GhostIngredientDragManager(this.contents, guiScreenHelper, ingredientManager, worldConfig);
this.displayedMouseHandler = new CombinedMouseHandler(
this.configButton.getMouseHandler(),
this.contents.getMouseHandler(),
new SearchMouseHandler(),
new CheatMouseHandler(this, worldConfig, clientConfig)
);
this.hiddenMouseHandler = this.configButton.getMouseHandler();
this.clearKeyboardFocus();
}
public boolean isListDisplayed() {
return worldConfig.isOverlayEnabled() && this.guiProperties != null && this.hasRoom;
}
private static Rectangle2d createDisplayArea(IGuiProperties guiProperties) {
Rectangle2d screenRectangle = GuiProperties.getScreenRectangle(guiProperties);
int guiRight = GuiProperties.getGuiRight(guiProperties);
Tuple<Rectangle2d, Rectangle2d> result = MathUtil.splitX(screenRectangle, guiRight);
Rectangle2d displayArea = result.getB();
return new Rectangle2dBuilder(displayArea)
.insetByPadding(BORDER_PADDING)
.build();
}
public void updateScreen(@Nullable Screen guiScreen, boolean forceUpdate) {
final boolean wasDisplayed = isListDisplayed();
IGuiProperties guiProperties = guiScreenHelper.getGuiProperties(guiScreen);
if (guiProperties == null) {
if (this.guiProperties != null) {
this.guiProperties = null;
clearKeyboardFocus();
this.ghostIngredientDragManager.stopDrag();
}
} else if (forceUpdate || this.guiProperties == null || !GuiProperties.areEqual(this.guiProperties, guiProperties)) {
updateNewScreen(guiProperties);
}
if (wasDisplayed && !isListDisplayed()) {
worldConfig.saveFilterText();
}
}
private void updateNewScreen(IGuiProperties guiProperties) {
this.guiProperties = guiProperties;
this.displayArea = createDisplayArea(guiProperties);
final boolean searchBarCentered = isSearchBarCentered(this.clientConfig, guiProperties);
Set<Rectangle2d> guiExclusionAreas = guiScreenHelper.getGuiExclusionAreas();
Rectangle2d availableContentsArea = new Rectangle2dBuilder(this.displayArea)
.subtractHeight(searchBarCentered ? 0 : SEARCH_HEIGHT + BORDER_PADDING)
.build();
this.hasRoom = this.contents.updateBounds(availableContentsArea, guiExclusionAreas);
// update area to match contents size
Rectangle2d contentsArea = this.contents.getArea();
this.displayArea = new Rectangle2dBuilder(this.displayArea)
.setX(contentsArea)
.setWidth(contentsArea)
.build();
Tuple<Rectangle2d, Rectangle2d> result = getSearchAndConfigArea(searchBarCentered, guiProperties, this.displayArea);
Rectangle2d searchAndConfigArea = result.getB();
result = MathUtil.splitXRight(searchAndConfigArea, BUTTON_SIZE);
Rectangle2d searchArea = result.getA();
Rectangle2d configButtonArea = result.getB();
this.searchField.updateBounds(searchArea);
this.configButton.updateBounds(configButtonArea);
updateLayout(false);
}
private static boolean isSearchBarCentered(IClientConfig clientConfig, IGuiProperties guiProperties) {
return clientConfig.isCenterSearchBarEnabled() &&
GuiProperties.getGuiBottom(guiProperties) + SEARCH_HEIGHT < guiProperties.getScreenHeight();
}
private static Tuple<Rectangle2d, Rectangle2d> getSearchAndConfigArea(boolean searchBarCentered, IGuiProperties guiProperties, Rectangle2d displayArea) {
if (searchBarCentered) {
Rectangle2d guiRectangle = GuiProperties.getGuiRectangle(guiProperties);
Rectangle2d searchRect = new Rectangle2d(
guiRectangle.getX(),
displayArea.getHeight() - SEARCH_HEIGHT,
guiRectangle.getWidth(),
SEARCH_HEIGHT
);
return new Tuple<>(displayArea, searchRect);
} else {
return MathUtil.splitYBottom(displayArea, SEARCH_HEIGHT);
}
}
public void updateLayout(boolean filterChanged) {
this.contents.updateLayout(filterChanged);
this.searchField.update();
}
@SuppressWarnings("deprecation")
public void drawScreen(Minecraft minecraft, MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
if (isListDisplayed()) {
RenderSystem.disableLighting();
this.searchField.renderButton(matrixStack, mouseX, mouseY, partialTicks);
this.contents.draw(minecraft, matrixStack, mouseX, mouseY, partialTicks);
}
if (this.guiProperties != null) {
this.configButton.draw(matrixStack, mouseX, mouseY, partialTicks);
}
}
public void drawTooltips(Minecraft minecraft, MatrixStack matrixStack, int mouseX, int mouseY) {
if (isListDisplayed()) {
this.ghostIngredientDragManager.drawTooltips(minecraft, matrixStack, mouseX, mouseY);
this.contents.drawTooltips(minecraft, matrixStack, mouseX, mouseY);
}
if (this.guiProperties != null) {
this.configButton.drawTooltips(matrixStack, mouseX, mouseY);
}
}
public void drawOnForeground(Minecraft minecraft, MatrixStack matrixStack, ContainerScreen<?> gui, int mouseX, int mouseY) {
if (isListDisplayed()) {
matrixStack.pushPose();
matrixStack.translate(-gui.getGuiLeft(), -gui.getGuiTop(), 0);
this.ghostIngredientDragManager.drawOnForeground(minecraft, matrixStack, mouseX, mouseY);
matrixStack.popPose();
}
}
public void handleTick() {
if (this.isListDisplayed()) {
this.searchField.tick();
}
}
public boolean isMouseOver(double mouseX, double mouseY) {
if (isListDisplayed()) {
if (this.clientConfig.isCenterSearchBarEnabled() && searchField.isMouseOver(mouseX, mouseY)) {
return true;
}
return MathUtil.contains(displayArea, mouseX, mouseY) &&
!guiScreenHelper.isInGuiExclusionArea(mouseX, mouseY);
}
if (this.guiProperties != null) {
return this.configButton.isMouseOver(mouseX, mouseY);
}
return false;
}
@Override
@Nullable
public IClickedIngredient<?> getIngredientUnderMouse(double mouseX, double mouseY) {
if (isListDisplayed()) {
return this.contents.getIngredientUnderMouse(mouseX, mouseY);
}
return null;
}
@Override
public boolean canSetFocusWithMouse() {
return this.isListDisplayed() && this.contents.canSetFocusWithMouse();
}
public IMouseHandler getMouseHandler() {
return new ProxyMouseHandler(() -> {
if (this.guiProperties == null) {
return NullMouseHandler.INSTANCE;
}
if (isListDisplayed()) {
return this.displayedMouseHandler;
}
return this.hiddenMouseHandler;
});
}
public IMouseDragHandler getMouseDragHandler() {
return new ProxyMouseDragHandler(() -> {
if (isListDisplayed()) {
return this.ghostIngredientDragManager.getMouseDragHandler();
}
return NullMouseDragHandler.INSTANCE;
});
}
@Override
public boolean hasKeyboardFocus() {
return isListDisplayed() && this.searchField.isFocused();
}
public void clearKeyboardFocus() {
setKeyboardFocus(false);
}
private void setKeyboardFocus(boolean keyboardFocus) {
this.searchField.setFocused(keyboardFocus);
}
public boolean onGlobalKeyPressed(InputMappings.Input input, MouseClickState clickState) {
if (isListDisplayed()) {
if (KeyBindings.toggleCheatMode.isActiveAndMatches(input)) {
if (!clickState.isSimulate()) {
worldConfig.toggleCheatItemsEnabled();
}
return true;
}
if (KeyBindings.toggleEditMode.isActiveAndMatches(input)) {
if (!clickState.isSimulate()) {
worldConfig.toggleEditModeEnabled();
}
return true;
}
if (KeyBindings.focusSearch.isActiveAndMatches(input)) {
if (!clickState.isSimulate()) {
setKeyboardFocus(true);
}
return true;
}
}
return false;
}
public boolean onCharTyped(char codePoint, int modifiers) {
if (isListDisplayed() &&
hasKeyboardFocus() &&
searchField.charTyped(codePoint, modifiers)) {
boolean changed = worldConfig.setFilterText(searchField.getValue());
if (changed) {
updateLayout(true);
}
return true;
}
return false;
}
public boolean onKeyPressed(int keyCode, int scanCode, int modifiers) {
if (isListDisplayed()) {
if (hasKeyboardFocus() &&
searchField.keyPressed(keyCode, scanCode, modifiers)) {
boolean changed = worldConfig.setFilterText(searchField.getValue());
if (changed) {
updateLayout(true);
}
return true;
}
boolean pressed = this.contents.onKeyPressed(keyCode, scanCode, modifiers);
if (pressed) {
clearKeyboardFocus();
}
return pressed;
}
return false;
}
@Nullable
@Override
public Object getIngredientUnderMouse() {
if (isListDisplayed()) {
IIngredientListElement<?> elementUnderMouse = this.contents.getElementUnderMouse();
if (elementUnderMouse != null) {
return elementUnderMouse.getIngredient();
}
}
return null;
}
@Nullable
@Override
public <T> T getIngredientUnderMouse(IIngredientType<T> ingredientType) {
Object ingredient = getIngredientUnderMouse();
Class<? extends T> ingredientClass = ingredientType.getIngredientClass();
if (ingredientClass.isInstance(ingredient)) {
return ingredientClass.cast(ingredient);
}
return null;
}
public void onSetFilterText(String filterText) {
this.searchField.setValue(filterText);
updateLayout(true);
}
@SuppressWarnings("UnstableApiUsage")
@Override
public ImmutableList<Object> getVisibleIngredients() {
if (isListDisplayed()) {
List<IIngredientListElement<?>> visibleElements = this.contents.getVisibleElements();
return visibleElements.stream()
.map(IIngredientListElement::getIngredient)
.collect(ImmutableList.toImmutableList());
}
return ImmutableList.of();
}
private class SearchMouseHandler implements IMouseHandler {
@Nullable
@Override
public IMouseHandler handleClick(Screen screen, double mouseX, double mouseY, int mouseButton, MouseClickState clickState) {
IMouseHandler mouseHandler = searchField.getMouseHandler();
IMouseHandler handled = mouseHandler.handleClick(screen, mouseX, mouseY, mouseButton, clickState);
if (handled != null) {
if (!clickState.isSimulate()) {
updateLayout(true);
}
return this;
}
return null;
}
@Override
public void handleMouseClickedOut(int mouseButton) {
clearKeyboardFocus();
}
}
}