/
RecipesGui.java
411 lines (333 loc) · 11.9 KB
/
RecipesGui.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package mezz.jei.gui;
import com.google.common.collect.ImmutableList;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.awt.Color;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.client.FMLClientHandler;
import net.minecraftforge.fml.client.config.GuiButtonExt;
import org.lwjgl.opengl.GL11;
import mezz.jei.api.JEIManager;
import mezz.jei.api.gui.IDrawable;
import mezz.jei.api.recipe.IRecipeCategory;
import mezz.jei.api.recipe.IRecipeHandler;
import mezz.jei.api.recipe.IRecipeWrapper;
import mezz.jei.config.Constants;
import mezz.jei.input.IMouseHandler;
import mezz.jei.input.IShowsItemStacks;
import mezz.jei.util.Log;
import mezz.jei.util.MathUtil;
import mezz.jei.util.StringUtil;
public class RecipesGui extends GuiScreen implements IShowsItemStacks, IMouseHandler {
private enum Mode {
INPUT, OUTPUT
}
private static final int borderPadding = 8;
private static final int textPadding = 5;
private int titleHeight;
private int headerHeight;
private int buttonWidth;
/* Whether this GUI is dispzzlaying input or output recipes */
private Mode mode;
/* The ItemStack that is the focus of this GUI */
private ItemStack focusStack;
/* List of Recipe Categories that involve "focusStack" */
@Nonnull
private ImmutableList<IRecipeCategory> recipeCategories = ImmutableList.of();
/* List of RecipeWidget to display */
@Nonnull
private final List<RecipeWidget> recipeWidgets = new ArrayList<>();
/* List of recipes for the currently selected recipeClass */
@Nonnull
private ImmutableList<Object> recipes = ImmutableList.of();
private int recipesPerPage;
private int recipeCategoryIndex = 0;
private int pageIndex = 0;
private String pageString;
private String title;
private GuiButton nextRecipeCategory;
private GuiButton previousRecipeCategory;
private GuiButton nextPage;
private GuiButton previousPage;
private ResourceLocation backgroundTexture;
private boolean isOpen = false;
private int guiLeft;
private int guiTop;
private int xSize;
private int ySize;
public void initGui(@Nonnull Minecraft minecraft) {
setWorldAndResolution(minecraft, minecraft.currentScreen.width, minecraft.currentScreen.height);
this.xSize = 176;
if (this.height > 300) {
this.ySize = 256;
this.backgroundTexture = new ResourceLocation(Constants.RESOURCE_DOMAIN, Constants.TEXTURE_GUI_PATH + "recipeBackgroundTall.png");
} else {
this.ySize = 166;
this.backgroundTexture = new ResourceLocation(Constants.RESOURCE_DOMAIN, Constants.TEXTURE_GUI_PATH + "recipeBackground.png");
}
this.guiLeft = (minecraft.currentScreen.width - this.xSize) / 2;
this.guiTop = (minecraft.currentScreen.height - this.ySize) / 2;
this.titleHeight = fontRendererObj.FONT_HEIGHT + borderPadding;
this.headerHeight = titleHeight + fontRendererObj.FONT_HEIGHT + textPadding;
buttonWidth = 13;
int buttonHeight = fontRendererObj.FONT_HEIGHT + 3;
int rightButtonX = guiLeft + xSize - borderPadding - buttonWidth;
int leftButtonX = guiLeft + borderPadding;
int recipeClassButtonTop = guiTop + borderPadding - 2;
nextRecipeCategory = new GuiButtonExt(2, rightButtonX, recipeClassButtonTop, buttonWidth, buttonHeight, ">");
previousRecipeCategory = new GuiButtonExt(3, leftButtonX, recipeClassButtonTop, buttonWidth, buttonHeight, "<");
int pageButtonTop = guiTop + titleHeight + 3;
nextPage = new GuiButtonExt(4, rightButtonX, pageButtonTop, buttonWidth, buttonHeight, ">");
previousPage = new GuiButtonExt(5, leftButtonX, pageButtonTop, buttonWidth, buttonHeight, "<");
addButtons();
// on screen resize
if (recipeWidgets.size() > 0) {
resetLayout();
}
}
// don't post GUI events or we end up in an infinite loop handling them
@Override
public void setWorldAndResolution(Minecraft mc, int width, int height) {
this.mc = mc;
this.itemRender = mc.getRenderItem();
this.fontRendererObj = mc.fontRendererObj;
this.width = width;
this.height = height;
this.buttonList.clear();
this.initGui();
}
private void resetLayout() {
recipeWidgets.clear();
pageIndex = 0;
updateLayout();
}
@SuppressWarnings("unchecked")
private void addButtons() {
this.buttonList.add(nextRecipeCategory);
this.buttonList.add(previousRecipeCategory);
this.buttonList.add(nextPage);
this.buttonList.add(previousPage);
}
@Override
public boolean isMouseOver(int mouseX, int mouseY) {
return isOpen() && (mouseX >= guiLeft) && (mouseY >= guiTop) && (mouseX < guiLeft + xSize) && (mouseY < guiTop + ySize);
}
@Nullable
@Override
public ItemStack getStackUnderMouse(int mouseX, int mouseY) {
if (!isMouseOver(mouseX, mouseY)) {
return null;
}
for (RecipeWidget recipeWidget : recipeWidgets) {
ItemStack stack = recipeWidget.getStackUnderMouse(mouseX, mouseY);
if (stack != null) {
return stack;
}
}
return null;
}
@Override
public boolean handleMouseClicked(int mouseX, int mouseY, int mouseButton) {
if (!isMouseOver(mouseX, mouseY)) {
return false;
}
try {
handleMouseInput();
} catch (IOException e) {
Log.error("IOException on mouse click.", e);
}
return false;
}
@Override
public boolean handleMouseScrolled(int mouseX, int mouseY, int scrollDelta) {
if (!isMouseOver(mouseX, mouseY)) {
return false;
}
if (scrollDelta < 0) {
nextPage();
return true;
} else if (scrollDelta > 0) {
previousPage();
return true;
}
return false;
}
@Override
public void open() {
this.isOpen = true;
}
@Override
public void close() {
this.isOpen = false;
}
@Override
public boolean isOpen() {
return isOpen && recipes.size() > 0;
}
public void showRecipes(@Nonnull ItemStack stack) {
if (setStack(stack, Mode.OUTPUT)) {
open();
}
}
public void showUses(@Nonnull ItemStack stack) {
if (setStack(stack, Mode.INPUT)) {
open();
}
}
@Override
protected void actionPerformed(@Nonnull GuiButton guibutton) {
if (guibutton.id == nextPage.id) {
nextPage();
} else if (guibutton.id == previousPage.id) {
previousPage();
} else if (guibutton.id == nextRecipeCategory.id) {
nextRecipeCategory();
} else if (guibutton.id == previousRecipeCategory.id) {
previousRecipeCategory();
}
}
private boolean setStack(@Nullable ItemStack stack, @Nonnull Mode mode) {
if (stack == null) {
return false;
}
if (this.focusStack != null && this.focusStack.equals(stack) && this.mode == mode) {
return true;
}
ImmutableList<IRecipeCategory> types = null;
switch (mode) {
case INPUT:
types = JEIManager.recipeRegistry.getRecipeCategoriesWithInput(stack);
break;
case OUTPUT:
types = JEIManager.recipeRegistry.getRecipeCategoriesWithOutput(stack);
break;
}
if (types.isEmpty()) {
return false;
}
this.recipeCategories = types;
this.focusStack = stack;
this.mode = mode;
this.recipeCategoryIndex = 0;
this.pageIndex = 0;
updateLayout();
return true;
}
private void nextRecipeCategory() {
int recipesTypesCount = recipeCategories.size();
recipeCategoryIndex = (recipeCategoryIndex + 1) % recipesTypesCount;
pageIndex = 0;
updateLayout();
}
private void previousRecipeCategory() {
int recipesTypesCount = recipeCategories.size();
recipeCategoryIndex = (recipesTypesCount + recipeCategoryIndex - 1) % recipesTypesCount;
pageIndex = 0;
updateLayout();
}
private void nextPage() {
int pageCount = pageCount();
pageIndex = (pageIndex + 1) % pageCount;
updateLayout();
}
private void previousPage() {
int pageCount = pageCount();
pageIndex = (pageCount + pageIndex - 1) % pageCount;
updateLayout();
}
private int pageCount() {
if (recipes.size() <= 1) {
return 1;
}
return MathUtil.divideCeil(recipes.size(), recipesPerPage);
}
private void updateLayout() {
if (recipeCategories.isEmpty()) {
return;
}
IRecipeCategory recipeCategory = recipeCategories.get(recipeCategoryIndex);
title = recipeCategory.getCategoryTitle();
switch (mode) {
case INPUT:
recipes = JEIManager.recipeRegistry.getRecipesWithInput(recipeCategory, focusStack);
break;
case OUTPUT:
recipes = JEIManager.recipeRegistry.getRecipesWithOutput(recipeCategory, focusStack);
break;
}
IDrawable recipeBackground = recipeCategory.getBackground();
recipesPerPage = (ySize - headerHeight) / (recipeBackground.getHeight() + borderPadding);
final int recipeXOffset = (xSize - recipeBackground.getWidth()) / 2;
final int recipeSpacing = (ySize - headerHeight - (recipesPerPage * recipeBackground.getHeight())) / (recipesPerPage + 1);
final int posX = guiLeft + recipeXOffset;
int posY = guiTop + headerHeight + recipeSpacing;
recipeWidgets.clear();
for (int recipeIndex = pageIndex * recipesPerPage; recipeIndex < recipes.size() && recipeWidgets.size() < recipesPerPage; recipeIndex++) {
Object recipe = recipes.get(recipeIndex);
IRecipeHandler recipeHandler = JEIManager.recipeRegistry.getRecipeHandler(recipe.getClass());
if (recipeHandler == null) {
Log.error("Couldn't find recipe handler for recipe: {}", recipe);
continue;
}
RecipeWidget recipeWidget = new RecipeWidget(recipeCategory);
recipeWidget.setPosition(posX, posY);
posY += recipeBackground.getHeight() + recipeSpacing;
@SuppressWarnings("unchecked")
IRecipeWrapper recipeWrapper = recipeHandler.getRecipeWrapper(recipe);
recipeWidget.setRecipe(recipeWrapper, focusStack);
recipeWidgets.add(recipeWidget);
}
nextPage.enabled = previousPage.enabled = (pageCount() > 1);
nextRecipeCategory.enabled = previousRecipeCategory.enabled = (recipeCategories.size() > 1);
this.pageString = (pageIndex + 1) + "/" + pageCount();
}
public void draw(int mouseX, int mouseY) {
Minecraft minecraft = Minecraft.getMinecraft();
nextRecipeCategory.drawButton(minecraft, mouseX, mouseY);
previousRecipeCategory.drawButton(minecraft, mouseX, mouseY);
nextPage.drawButton(minecraft, mouseX, mouseY);
previousPage.drawButton(minecraft, mouseX, mouseY);
GL11.glPushMatrix();
{
GL11.glTranslatef(guiLeft, guiTop, 0.0F);
drawRect(borderPadding + buttonWidth, borderPadding - 2, xSize - borderPadding - buttonWidth, borderPadding + 10, 0x30000000);
drawRect(borderPadding + buttonWidth, titleHeight + textPadding - 2, xSize - borderPadding - buttonWidth, titleHeight + textPadding + 10, 0x30000000);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
StringUtil.drawCenteredString(fontRendererObj, title, xSize, borderPadding, Color.WHITE.getRGB(), true);
StringUtil.drawCenteredString(fontRendererObj, pageString, xSize, titleHeight + textPadding, Color.WHITE.getRGB(), true);
}
GL11.glPopMatrix();
RecipeWidget hovered = null;
for (RecipeWidget recipeWidget : recipeWidgets) {
if (recipeWidget.getStackUnderMouse(mouseX, mouseY) != null) {
hovered = recipeWidget;
} else {
recipeWidget.draw(minecraft, mouseX, mouseY);
}
}
if (hovered != null) {
hovered.draw(minecraft, mouseX, mouseY);
}
}
public void drawBackground() {
this.zLevel = -100;
this.drawDefaultBackground();
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
bindTexture(backgroundTexture);
int x = (width - xSize) / 2;
int y = (height - ySize) / 2;
this.zLevel = 0;
drawTexturedModalRect(x, y, 0, 0, xSize, ySize);
}
private void bindTexture(ResourceLocation texturePath) {
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
FMLClientHandler.instance().getClient().getTextureManager().bindTexture(texturePath);
}
}