/
RecipesGui.java
378 lines (306 loc) · 11.4 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
package mezz.jei.gui;
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.client.renderer.GlStateManager;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.client.FMLClientHandler;
import net.minecraftforge.fml.client.config.GuiButtonExt;
import net.minecraftforge.fml.client.config.HoverChecker;
import mezz.jei.api.gui.IDrawable;
import mezz.jei.api.recipe.IRecipeCategory;
import mezz.jei.config.Constants;
import mezz.jei.input.IMouseHandler;
import mezz.jei.input.IShowsRecipeFocuses;
import mezz.jei.transfer.RecipeTransferUtil;
import mezz.jei.util.Log;
import mezz.jei.util.StringUtil;
import mezz.jei.util.Translator;
public class RecipesGui extends GuiScreen implements IShowsRecipeFocuses, IMouseHandler {
private static final int borderPadding = 8;
private static final int textPadding = 5;
private int titleHeight;
private int headerHeight;
private int buttonWidth;
/* Internal logic for the gui, handles finding recipes */
private final IRecipeGuiLogic logic = new RecipeGuiLogic();
/* List of RecipeLayout to display */
@Nonnull
private final List<RecipeLayout> recipeLayouts = new ArrayList<>();
private String pageString;
private String title;
private ResourceLocation backgroundTexture;
private HoverChecker titleHoverChecker;
private GuiButton nextRecipeCategory;
private GuiButton previousRecipeCategory;
private GuiButton nextPage;
private GuiButton previousPage;
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();
updateLayout();
}
/**
* Fire our own RecipesGuiInitEvent instead of Forge's GuiInitEvent to avoid confusion.
* When a gui is init, so is this recipes gui, so it would be fired twice.
*/
@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;
MinecraftForge.EVENT_BUS.post(new RecipesGuiInitEvent());
this.buttonList.clear();
this.initGui();
}
@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 Focus getFocusUnderMouse(int mouseX, int mouseY) {
if (!isMouseOver(mouseX, mouseY)) {
return null;
}
for (RecipeLayout recipeLayouts : this.recipeLayouts) {
Focus focus = recipeLayouts.getFocusUnderMouse(mouseX, mouseY);
if (focus != null) {
return focus;
}
}
return null;
}
// workaround to see if a button was clicked
private boolean guiActionPerformed = false;
@Override
public boolean handleMouseClicked(int mouseX, int mouseY, int mouseButton) {
if (!isMouseOver(mouseX, mouseY)) {
return false;
}
guiActionPerformed = false;
try {
handleMouseInput();
} catch (IOException e) {
Log.error("IOException on mouse click.", e);
}
if (!guiActionPerformed) {
if (titleHoverChecker.checkHover(mouseX, mouseY)) {
boolean success = logic.setCategoryFocus();
if (success) {
updateLayout();
}
return success;
}
}
return guiActionPerformed;
}
@Override
public boolean handleMouseScrolled(int mouseX, int mouseY, int scrollDelta) {
if (!isMouseOver(mouseX, mouseY)) {
return false;
}
if (scrollDelta < 0) {
logic.nextPage();
updateLayout();
return true;
} else if (scrollDelta > 0) {
logic.previousPage();
updateLayout();
return true;
}
return false;
}
@Override
public void open() {
this.isOpen = true;
}
@Override
public void close() {
this.isOpen = false;
}
@Override
public boolean isOpen() {
return isOpen && logic.getRecipeCategory() != null;
}
public void showRecipes(@Nonnull Focus focus) {
focus.setMode(Focus.Mode.OUTPUT);
if (logic.setFocus(focus)) {
updateLayout();
open();
}
}
public void showUses(@Nonnull Focus focus) {
focus.setMode(Focus.Mode.INPUT);
if (logic.setFocus(focus)) {
updateLayout();
open();
}
}
public void back() {
if (logic.back()) {
updateLayout();
}
}
@Override
protected void actionPerformed(@Nonnull GuiButton guibutton) {
boolean updateLayout = true;
if (guibutton.id == nextPage.id) {
logic.nextPage();
} else if (guibutton.id == previousPage.id) {
logic.previousPage();
} else if (guibutton.id == nextRecipeCategory.id) {
logic.nextRecipeCategory();
} else if (guibutton.id == previousRecipeCategory.id) {
logic.previousRecipeCategory();
} else if (guibutton.id >= RecipeLayout.recipeTransferButtonIndex) {
int recipeIndex = guibutton.id - RecipeLayout.recipeTransferButtonIndex;
RecipeLayout recipeLayout = recipeLayouts.get(recipeIndex);
if (RecipeTransferUtil.transferRecipe(recipeLayout, Minecraft.getMinecraft().thePlayer)) {
close();
guiActionPerformed = true;
updateLayout = false;
}
} else {
updateLayout = false;
}
if (updateLayout) {
updateLayout();
guiActionPerformed = true;
}
}
private void updateLayout() {
IRecipeCategory recipeCategory = logic.getRecipeCategory();
if (recipeCategory == null) {
return;
}
IDrawable recipeBackground = recipeCategory.getBackground();
final int recipesPerPage = Math.max(1, (ySize - headerHeight) / (recipeBackground.getHeight() + borderPadding));
final int recipeXOffset = (xSize - recipeBackground.getWidth()) / 2;
final int recipeSpacing = (ySize - headerHeight - (recipesPerPage * recipeBackground.getHeight())) / (recipesPerPage + 1);
logic.setRecipesPerPage(recipesPerPage);
title = recipeCategory.getTitle();
int titleWidth = fontRendererObj.getStringWidth(title);
int titleX = guiLeft + (xSize - titleWidth) / 2;
int titleY = guiTop + borderPadding;
titleHoverChecker = new HoverChecker(titleY, titleY + fontRendererObj.FONT_HEIGHT, titleX, titleX + titleWidth, 0);
int posX = guiLeft + recipeXOffset;
int posY = guiTop + headerHeight + recipeSpacing;
int spacingY = recipeBackground.getHeight() + recipeSpacing;
recipeLayouts.clear();
recipeLayouts.addAll(logic.getRecipeWidgets(posX, posY, spacingY));
addRecipeTransferButtons(recipeLayouts);
nextPage.enabled = previousPage.enabled = logic.hasMultiplePages();
nextRecipeCategory.enabled = previousRecipeCategory.enabled = logic.hasMultipleCategories();
pageString = logic.getPageString();
}
@SuppressWarnings("unchecked")
private void addRecipeTransferButtons(List<RecipeLayout> recipeLayouts) {
buttonList.clear();
addButtons();
EntityPlayer player = Minecraft.getMinecraft().thePlayer;
for (RecipeLayout recipeLayout : recipeLayouts) {
RecipeTransferButton button = recipeLayout.getRecipeTransferButton();
button.init(recipeLayout, player);
buttonList.add(button);
}
}
public void draw(int mouseX, int mouseY) {
if (!isOpen()) {
return;
}
Minecraft minecraft = Minecraft.getMinecraft();
nextRecipeCategory.drawButton(minecraft, mouseX, mouseY);
previousRecipeCategory.drawButton(minecraft, mouseX, mouseY);
nextPage.drawButton(minecraft, mouseX, mouseY);
previousPage.drawButton(minecraft, mouseX, mouseY);
GlStateManager.disableBlend();
GlStateManager.pushMatrix();
{
GlStateManager.translate(guiLeft, guiTop, 0.0F);
GlStateManager.disableBlend();
drawRect(borderPadding + buttonWidth, borderPadding - 2, xSize - borderPadding - buttonWidth, borderPadding + 10, 0x30000000);
drawRect(borderPadding + buttonWidth, titleHeight + textPadding - 2, xSize - borderPadding - buttonWidth, titleHeight + textPadding + 10, 0x30000000);
GlStateManager.color(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);
}
GlStateManager.popMatrix();
RecipeLayout hovered = null;
for (RecipeLayout recipeWidget : recipeLayouts) {
if (recipeWidget.getFocusUnderMouse(mouseX, mouseY) != null) {
hovered = recipeWidget;
} else {
recipeWidget.draw(minecraft, mouseX, mouseY);
}
}
if (hovered != null) {
hovered.draw(minecraft, mouseX, mouseY);
}
if (titleHoverChecker.checkHover(mouseX, mouseY)) {
Focus focus = logic.getFocus();
if (focus != null && !focus.isBlank()) {
String showAllRecipesString = Translator.translateToLocal("jei.tooltip.show.all.recipes");
TooltipRenderer.drawHoveringText(minecraft, showAllRecipesString, mouseX, mouseY);
}
}
}
public void drawBackground() {
this.zLevel = -100;
this.drawDefaultBackground();
GlStateManager.color(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) {
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
FMLClientHandler.instance().getClient().getTextureManager().bindTexture(texturePath);
}
}