/
IngredientListOverlay.java
312 lines (275 loc) · 11.1 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
package mezz.jei.common.gui.overlay;
import com.mojang.blaze3d.vertex.PoseStack;
import mezz.jei.api.gui.handlers.IGuiProperties;
import mezz.jei.api.ingredients.IIngredientType;
import mezz.jei.api.ingredients.ITypedIngredient;
import mezz.jei.api.runtime.IIngredientListOverlay;
import mezz.jei.common.filter.IFilterTextSource;
import mezz.jei.common.gui.GuiProperties;
import mezz.jei.common.gui.GuiScreenHelper;
import mezz.jei.common.gui.elements.GuiIconToggleButton;
import mezz.jei.common.gui.ghost.GhostIngredientDragManager;
import mezz.jei.common.gui.textures.Textures;
import mezz.jei.common.ingredients.RegisteredIngredients;
import mezz.jei.common.input.ICharTypedHandler;
import mezz.jei.common.input.IClickedIngredient;
import mezz.jei.common.input.IDragHandler;
import mezz.jei.common.input.IInternalKeyMappings;
import mezz.jei.common.input.IRecipeFocusSource;
import mezz.jei.common.input.IUserInputHandler;
import mezz.jei.common.input.MouseUtil;
import mezz.jei.common.input.handlers.NullDragHandler;
import mezz.jei.common.input.handlers.ProxyDragHandler;
import mezz.jei.common.network.IConnectionToServer;
import mezz.jei.common.platform.IPlatformScreenHelper;
import mezz.jei.common.platform.Services;
import mezz.jei.common.util.ImmutableRect2i;
import mezz.jei.core.config.IClientConfig;
import mezz.jei.core.config.IWorldConfig;
import mezz.jei.common.input.GuiTextFieldFilter;
import mezz.jei.common.input.handlers.CheatInputHandler;
import mezz.jei.common.input.handlers.CombinedInputHandler;
import mezz.jei.common.input.handlers.NullInputHandler;
import mezz.jei.common.input.handlers.ProxyInputHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
public class IngredientListOverlay implements IIngredientListOverlay, IRecipeFocusSource, ICharTypedHandler {
private static final int BORDER_MARGIN = 6;
private static final int INNER_PADDING = 2;
private static final int BUTTON_SIZE = 20;
private static final int SEARCH_HEIGHT = BUTTON_SIZE;
private final GuiIconToggleButton configButton;
private final IngredientGridWithNavigation contents;
private final IClientConfig clientConfig;
private final IWorldConfig worldConfig;
private final IConnectionToServer serverConnection;
private final GuiScreenHelper guiScreenHelper;
private final GuiTextFieldFilter searchField;
private final GhostIngredientDragManager ghostIngredientDragManager;
private ImmutableRect2i displayArea = ImmutableRect2i.EMPTY;
private boolean hasRoom;
// properties of the gui we're next to
@Nullable
private IGuiProperties guiProperties;
public IngredientListOverlay(
IIngredientGridSource ingredientGridSource,
IFilterTextSource filterTextSource,
RegisteredIngredients registeredIngredients,
GuiScreenHelper guiScreenHelper,
IngredientGridWithNavigation contents,
IClientConfig clientConfig,
IWorldConfig worldConfig,
IConnectionToServer serverConnection,
Textures textures,
IInternalKeyMappings keyBindings
) {
this.guiScreenHelper = guiScreenHelper;
this.contents = contents;
this.clientConfig = clientConfig;
this.worldConfig = worldConfig;
this.serverConnection = serverConnection;
this.searchField = new GuiTextFieldFilter(textures);
this.searchField.setValue(filterTextSource.getFilterText());
this.searchField.setFocused(false);
this.searchField.setResponder(filterTextSource::setFilterText);
filterTextSource.addListener(this.searchField::setValue);
ingredientGridSource.addSourceListChangedListener(() -> updateBounds(true));
this.configButton = ConfigButton.create(this::isListDisplayed, worldConfig, textures, keyBindings);
this.ghostIngredientDragManager = new GhostIngredientDragManager(this.contents, guiScreenHelper, registeredIngredients, worldConfig);
}
@Override
public boolean isListDisplayed() {
return worldConfig.isOverlayEnabled() && this.guiProperties != null && this.hasRoom;
}
private static ImmutableRect2i createDisplayArea(IGuiProperties guiProperties) {
ImmutableRect2i screenRectangle = GuiProperties.getScreenRectangle(guiProperties);
int guiRight = GuiProperties.getGuiRight(guiProperties);
return screenRectangle
.cropLeft(guiRight)
.insetBy(BORDER_MARGIN);
}
public void updateScreen(@Nullable Screen guiScreen, boolean exclusionAreasChanged) {
IGuiProperties guiProperties = guiScreenHelper.getGuiProperties(guiScreen);
if (guiProperties == null) {
if (this.guiProperties != null) {
this.guiProperties = null;
this.searchField.setFocused(false);
this.ghostIngredientDragManager.stopDrag();
}
} else {
final boolean guiPropertiesChanged = this.guiProperties == null || !GuiProperties.areEqual(this.guiProperties, guiProperties);
if (exclusionAreasChanged || guiPropertiesChanged) {
updateNewScreen(guiProperties, guiPropertiesChanged);
}
}
}
private void updateNewScreen(IGuiProperties guiProperties, boolean guiPropertiesChanged) {
this.guiProperties = guiProperties;
this.displayArea = createDisplayArea(guiProperties);
if (guiPropertiesChanged) {
this.ghostIngredientDragManager.stopDrag();
}
updateBounds(false);
}
private void updateBounds(boolean resetToFirstPage) {
if (this.guiProperties == null) {
return;
}
final boolean searchBarCentered = isSearchBarCentered(this.clientConfig, this.guiProperties);
final ImmutableRect2i availableContentsArea = getAvailableContentsArea(searchBarCentered);
final Set<ImmutableRect2i> guiExclusionAreas = guiScreenHelper.getGuiExclusionAreas();
this.hasRoom = this.contents.updateBounds(availableContentsArea, guiExclusionAreas);
final ImmutableRect2i searchAndConfigArea = getSearchAndConfigArea(searchBarCentered, guiProperties);
final ImmutableRect2i searchArea = searchAndConfigArea.cropRight(BUTTON_SIZE);
final ImmutableRect2i configButtonArea = searchAndConfigArea.keepRight(BUTTON_SIZE);
int searchTextColor = this.contents.isEmpty() ? 0xFFFF0000 : 0xFFFFFFFF;
this.searchField.setTextColor(searchTextColor);
this.searchField.updateBounds(searchArea);
this.configButton.updateBounds(configButtonArea);
if (this.hasRoom) {
this.contents.updateLayout(resetToFirstPage);
}
}
private static boolean isSearchBarCentered(IClientConfig clientConfig, IGuiProperties guiProperties) {
return clientConfig.isCenterSearchBarEnabled() &&
GuiProperties.getGuiBottom(guiProperties) + SEARCH_HEIGHT < guiProperties.getScreenHeight();
}
private ImmutableRect2i getAvailableContentsArea(boolean searchBarCentered) {
if (searchBarCentered) {
return this.displayArea;
}
return this.displayArea.cropBottom(SEARCH_HEIGHT + INNER_PADDING);
}
private ImmutableRect2i getSearchAndConfigArea(boolean searchBarCentered, IGuiProperties guiProperties) {
if (searchBarCentered) {
ImmutableRect2i guiRectangle = GuiProperties.getGuiRectangle(guiProperties);
return this.displayArea
.keepBottom(SEARCH_HEIGHT)
.matchWidthAndX(guiRectangle);
} else if (this.hasRoom) {
final ImmutableRect2i contentsArea = this.contents.getBackgroundArea();
return this.displayArea
.keepBottom(SEARCH_HEIGHT)
.matchWidthAndX(contentsArea);
} else {
return this.displayArea.keepBottom(SEARCH_HEIGHT);
}
}
public void drawScreen(Minecraft minecraft, PoseStack poseStack, int mouseX, int mouseY, float partialTicks) {
if (isListDisplayed()) {
this.searchField.renderButton(poseStack, mouseX, mouseY, partialTicks);
this.contents.draw(minecraft, poseStack, mouseX, mouseY, partialTicks);
}
if (this.guiProperties != null) {
this.configButton.draw(poseStack, mouseX, mouseY, partialTicks);
}
}
public void drawTooltips(Minecraft minecraft, PoseStack poseStack, int mouseX, int mouseY) {
if (isListDisplayed()) {
this.ghostIngredientDragManager.drawTooltips(minecraft, poseStack, mouseX, mouseY);
this.contents.drawTooltips(minecraft, poseStack, mouseX, mouseY);
}
if (this.guiProperties != null) {
this.configButton.drawTooltips(poseStack, mouseX, mouseY);
}
}
public void drawOnForeground(Minecraft minecraft, PoseStack poseStack, AbstractContainerScreen<?> gui, int mouseX, int mouseY) {
if (isListDisplayed()) {
poseStack.pushPose();
{
IPlatformScreenHelper screenHelper = Services.PLATFORM.getScreenHelper();
poseStack.translate(-screenHelper.getGuiLeft(gui), -screenHelper.getGuiTop(gui), 0);
this.ghostIngredientDragManager.drawOnForeground(minecraft, poseStack, mouseX, mouseY);
}
poseStack.popPose();
}
}
public void handleTick() {
if (this.isListDisplayed()) {
this.searchField.tick();
}
}
@Override
public Stream<IClickedIngredient<?>> getIngredientUnderMouse(double mouseX, double mouseY) {
if (isListDisplayed()) {
return this.contents.getIngredientUnderMouse(mouseX, mouseY);
}
return Stream.empty();
}
public IUserInputHandler createInputHandler() {
final IUserInputHandler displayedInputHandler = new CombinedInputHandler(
this.searchField.createInputHandler(),
this.configButton.createInputHandler(),
this.contents.createInputHandler(),
new CheatInputHandler(this.contents, worldConfig, clientConfig, serverConnection)
);
final IUserInputHandler hiddenInputHandler = this.configButton.createInputHandler();
return new ProxyInputHandler(() -> {
if (this.guiProperties == null) {
return NullInputHandler.INSTANCE;
}
if (isListDisplayed()) {
return displayedInputHandler;
}
return hiddenInputHandler;
});
}
public IDragHandler createDragHandler() {
final IDragHandler displayedDragHandler = this.ghostIngredientDragManager.createDragHandler();
return new ProxyDragHandler(() -> {
if (isListDisplayed()) {
return displayedDragHandler;
}
return NullDragHandler.INSTANCE;
});
}
@Override
public boolean hasKeyboardFocus() {
return isListDisplayed() && this.searchField.isFocused();
}
@Override
public boolean onCharTyped(char codePoint, int modifiers) {
return searchField.charTyped(codePoint, modifiers);
}
@Override
public Optional<ITypedIngredient<?>> getIngredientUnderMouse() {
if (isListDisplayed()) {
double mouseX = MouseUtil.getX();
double mouseY = MouseUtil.getY();
return this.contents.getIngredientUnderMouse(mouseX, mouseY)
.<ITypedIngredient<?>>map(IClickedIngredient::getTypedIngredient)
.findFirst();
}
return Optional.empty();
}
@Nullable
@Override
public <T> T getIngredientUnderMouse(IIngredientType<T> ingredientType) {
if (isListDisplayed()) {
double mouseX = MouseUtil.getX();
double mouseY = MouseUtil.getY();
return this.contents.getIngredientUnderMouse(mouseX, mouseY)
.map(IClickedIngredient::getTypedIngredient)
.map(i -> i.getIngredient(ingredientType))
.flatMap(Optional::stream)
.findFirst()
.orElse(null);
}
return null;
}
@Override
public <T> List<T> getVisibleIngredients(IIngredientType<T> ingredientType) {
if (isListDisplayed()) {
return this.contents.getVisibleIngredients(ingredientType)
.toList();
}
return Collections.emptyList();
}
}