/
IngredientGridWithNavigation.java
295 lines (267 loc) · 9.86 KB
/
IngredientGridWithNavigation.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
package mezz.jei.gui.overlay;
import com.mojang.blaze3d.matrix.MatrixStack;
import javax.annotation.Nullable;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import mezz.jei.util.Rectangle2dBuilder;
import net.minecraft.client.GameSettings;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.renderer.Rectangle2d;
import net.minecraft.client.util.InputMappings;
import net.minecraft.item.ItemStack;
import mezz.jei.config.IFilterTextSource;
import mezz.jei.config.IWorldConfig;
import mezz.jei.config.KeyBindings;
import mezz.jei.gui.GuiScreenHelper;
import mezz.jei.gui.PageNavigation;
import mezz.jei.gui.ghost.IGhostIngredientDragSource;
import mezz.jei.gui.ingredients.IIngredientListElement;
import mezz.jei.gui.recipes.RecipesGui;
import mezz.jei.input.IClickedIngredient;
import mezz.jei.input.IMouseHandler;
import mezz.jei.input.IPaged;
import mezz.jei.input.IShowsRecipeFocuses;
import mezz.jei.input.MouseUtil;
import mezz.jei.render.IngredientListElementRenderer;
import mezz.jei.render.IngredientListSlot;
import mezz.jei.util.CommandUtil;
import mezz.jei.util.MathUtil;
import net.minecraft.util.Tuple;
/**
* Displays a list of ingredients with navigation at the top.
*/
public class IngredientGridWithNavigation implements IShowsRecipeFocuses, IMouseHandler, IGhostIngredientDragSource {
private static final int NAVIGATION_HEIGHT = 20;
private int firstItemIndex = 0;
private final IPaged pageDelegate;
private final PageNavigation navigation;
private final GuiScreenHelper guiScreenHelper;
private final IFilterTextSource filterTextSource;
private final IWorldConfig worldConfig;
private final IngredientGrid ingredientGrid;
private final WeakReference<IIngredientGridSource> ingredientSource;
private Rectangle2d area = new Rectangle2d(0, 0, 0, 0);
public IngredientGridWithNavigation(
IIngredientGridSource ingredientSource,
IFilterTextSource filterTextSource,
GuiScreenHelper guiScreenHelper,
IngredientGrid ingredientGrid,
IWorldConfig worldConfig
) {
this.filterTextSource = filterTextSource;
this.worldConfig = worldConfig;
this.ingredientGrid = ingredientGrid;
this.ingredientSource = new WeakReference<>(ingredientSource);
this.guiScreenHelper = guiScreenHelper;
this.pageDelegate = new IngredientGridPaged();
this.navigation = new PageNavigation(this.pageDelegate, false);
}
public void updateLayout(boolean resetToFirstPage) {
if (resetToFirstPage) {
firstItemIndex = 0;
}
String filterText = filterTextSource.getFilterText();
List<IIngredientListElement<?>> ingredientList = ingredientSource.get().getIngredientList(filterText);
if (firstItemIndex >= ingredientList.size()) {
firstItemIndex = 0;
}
this.ingredientGrid.guiIngredientSlots.set(firstItemIndex, ingredientList);
this.navigation.updatePageState();
}
public boolean updateBounds(Rectangle2d availableArea, Set<Rectangle2d> guiExclusionAreas) {
Tuple<Rectangle2d, Rectangle2d> result = MathUtil.splitY(availableArea, NAVIGATION_HEIGHT);
Rectangle2d estimatedNavigationArea = result.getA();
Rectangle2d movedNavigationArea = MathUtil.moveDownToAvoidIntersection(guiExclusionAreas, estimatedNavigationArea);
int navigationMaxY = movedNavigationArea.getY() + movedNavigationArea.getHeight();
result = MathUtil.splitY(availableArea, navigationMaxY);
Rectangle2d navigationArea = result.getA();
Rectangle2d boundsWithoutNavigation = result.getB();
boolean gridHasRoom = this.ingredientGrid.updateBounds(boundsWithoutNavigation, guiExclusionAreas);
if (!gridHasRoom) {
return false;
}
Rectangle2d displayArea = this.ingredientGrid.getArea();
navigationArea = new Rectangle2dBuilder(navigationArea)
.setX(displayArea)
.setWidth(displayArea)
.build();
this.navigation.updateBounds(navigationArea);
this.area = MathUtil.union(displayArea, navigationArea);
return true;
}
public Rectangle2d getArea() {
return this.area;
}
public void draw(Minecraft minecraft, MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
this.ingredientGrid.draw(minecraft, matrixStack, mouseX, mouseY);
this.navigation.draw(minecraft, matrixStack, mouseX, mouseY, partialTicks);
}
public void drawTooltips(Minecraft minecraft, MatrixStack matrixStack, int mouseX, int mouseY) {
this.ingredientGrid.drawTooltips(minecraft, matrixStack, mouseX, mouseY);
}
@Override
public boolean isMouseOver(double mouseX, double mouseY) {
return MathUtil.contains(this.area, mouseX, mouseY) &&
!guiScreenHelper.isInGuiExclusionArea(mouseX, mouseY);
}
@Override
public boolean handleMouseClicked(double mouseX, double mouseY, int mouseButton) {
return isMouseOver(mouseX, mouseY) &&
(this.ingredientGrid.handleMouseClicked(mouseX, mouseY) ||
this.navigation.handleMouseClickedButtons(mouseX, mouseY, mouseButton));
}
@Override
public boolean handleMouseScrolled(double mouseX, double mouseY, double scrollDelta) {
if (scrollDelta < 0) {
this.pageDelegate.nextPage();
return true;
} else if (scrollDelta > 0) {
this.pageDelegate.previousPage();
return true;
}
return false;
}
public boolean onKeyPressed(int keyCode, int scanCode, int modifiers) {
InputMappings.Input input = InputMappings.getInputByCode(keyCode, scanCode);
if (KeyBindings.nextPage.isActiveAndMatches(input)) {
this.pageDelegate.nextPage();
return true;
} else if (KeyBindings.previousPage.isActiveAndMatches(input)) {
this.pageDelegate.previousPage();
return true;
}
return checkHotbarKeys(input);
}
/**
* Modeled after ContainerScreen#checkHotbarKeys(int)
* Sets the stack in a hotbar slot to the one that's hovered over.
*/
protected boolean checkHotbarKeys(InputMappings.Input input) {
Screen guiScreen = Minecraft.getInstance().currentScreen;
if (worldConfig.isCheatItemsEnabled() && guiScreen != null && !(guiScreen instanceof RecipesGui)) {
final double mouseX = MouseUtil.getX();
final double mouseY = MouseUtil.getY();
if (isMouseOver(mouseX, mouseY)) {
GameSettings gameSettings = Minecraft.getInstance().gameSettings;
for (int hotbarSlot = 0; hotbarSlot < 9; ++hotbarSlot) {
if (gameSettings.keyBindsHotbar[hotbarSlot].isActiveAndMatches(input)) {
IClickedIngredient<?> ingredientUnderMouse = getIngredientUnderMouse(mouseX, mouseY);
if (ingredientUnderMouse != null) {
ItemStack itemStack = ingredientUnderMouse.getCheatItemStack();
if (!itemStack.isEmpty()) {
CommandUtil.setHotbarStack(itemStack, hotbarSlot);
}
ingredientUnderMouse.onClickHandled();
}
return true;
}
}
}
}
return false;
}
@Nullable
@Override
public IClickedIngredient<?> getIngredientUnderMouse(double mouseX, double mouseY) {
return this.ingredientGrid.getIngredientUnderMouse(mouseX, mouseY);
}
@Nullable
@Override
public IIngredientListElement<?> getElementUnderMouse() {
return this.ingredientGrid.getElementUnderMouse();
}
@Override
public boolean canSetFocusWithMouse() {
return this.ingredientGrid.canSetFocusWithMouse();
}
public List<IIngredientListElement<?>> getVisibleElements() {
List<IIngredientListElement<?>> visibleElements = new ArrayList<>();
for (IngredientListSlot slot : this.ingredientGrid.guiIngredientSlots.getAllGuiIngredientSlots()) {
IngredientListElementRenderer<?> renderer = slot.getIngredientRenderer();
if (renderer != null) {
visibleElements.add(renderer.getElement());
}
}
return visibleElements;
}
private class IngredientGridPaged implements IPaged {
@Override
public boolean nextPage() {
String filterText = filterTextSource.getFilterText();
final int itemsCount = ingredientSource.get().getIngredientList(filterText).size();
if (itemsCount > 0) {
firstItemIndex += ingredientGrid.size();
if (firstItemIndex >= itemsCount) {
firstItemIndex = 0;
}
updateLayout(false);
return true;
} else {
firstItemIndex = 0;
updateLayout(false);
return false;
}
}
@Override
public boolean previousPage() {
final int itemsPerPage = ingredientGrid.size();
if (itemsPerPage == 0) {
firstItemIndex = 0;
updateLayout(false);
return false;
}
String filterText = filterTextSource.getFilterText();
final int itemsCount = ingredientSource.get().getIngredientList(filterText).size();
int pageNum = firstItemIndex / itemsPerPage;
if (pageNum == 0) {
pageNum = itemsCount / itemsPerPage;
} else {
pageNum--;
}
firstItemIndex = itemsPerPage * pageNum;
if (firstItemIndex > 0 && firstItemIndex == itemsCount) {
pageNum--;
firstItemIndex = itemsPerPage * pageNum;
}
updateLayout(false);
return true;
}
@Override
public boolean hasNext() {
String filterText = filterTextSource.getFilterText();
// true if there is more than one page because this wraps around
int itemsPerPage = ingredientGrid.size();
return itemsPerPage > 0 && ingredientSource.get().getIngredientList(filterText).size() > itemsPerPage;
}
@Override
public boolean hasPrevious() {
String filterText = filterTextSource.getFilterText();
// true if there is more than one page because this wraps around
int itemsPerPage = ingredientGrid.size();
return itemsPerPage > 0 && ingredientSource.get().getIngredientList(filterText).size() > itemsPerPage;
}
@Override
public int getPageCount() {
String filterText = filterTextSource.getFilterText();
final int itemCount = ingredientSource.get().getIngredientList(filterText).size();
final int stacksPerPage = ingredientGrid.size();
if (stacksPerPage == 0) {
return 1;
}
int pageCount = MathUtil.divideCeil(itemCount, stacksPerPage);
pageCount = Math.max(1, pageCount);
return pageCount;
}
@Override
public int getPageNumber() {
final int stacksPerPage = ingredientGrid.size();
if (stacksPerPage == 0) {
return 0;
}
return firstItemIndex / stacksPerPage;
}
}
}