/
RecipesGui.java
388 lines (322 loc) · 11.6 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
package mezz.jei.gui;
import mezz.jei.api.gui.IDrawable;
import mezz.jei.api.recipe.IRecipeCategory;
import mezz.jei.config.Constants;
import mezz.jei.config.KeyBindings;
import mezz.jei.input.IShowsRecipeFocuses;
import mezz.jei.input.InputHandler;
import mezz.jei.transfer.RecipeTransferUtil;
import mezz.jei.util.StringUtil;
import mezz.jei.util.Translator;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.client.config.GuiButtonExt;
import net.minecraftforge.fml.client.config.HoverChecker;
import org.lwjgl.input.Mouse;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class RecipesGui extends GuiScreen implements IShowsRecipeFocuses {
private static final int borderPadding = 6;
private static final int textPadding = 5;
private static final int buttonWidth = 13;
private static final int buttonHeight = 12;
private int titleHeight;
private int headerHeight;
/* 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;
@Nullable
private GuiScreen parentScreen;
private int xSize;
private int ySize;
private int guiLeft;
private int guiTop;
public RecipesGui() {
this.mc = Minecraft.getMinecraft();
}
public int getGuiLeft() {
return guiLeft;
}
public int getXSize() {
return xSize;
}
@Override
public boolean doesGuiPauseGame() {
return false;
}
@Override
public void initGui() {
super.initGui();
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 = (width - this.xSize) / 2;
this.guiTop = (height - this.ySize) / 2;
this.titleHeight = fontRendererObj.FONT_HEIGHT + borderPadding;
this.headerHeight = titleHeight + fontRendererObj.FONT_HEIGHT + textPadding;
final int rightButtonX = guiLeft + xSize - borderPadding - buttonWidth;
final int leftButtonX = guiLeft + borderPadding;
int recipeClassButtonTop = guiTop + titleHeight - buttonHeight + 1;
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();
}
private void addButtons() {
this.buttonList.add(nextRecipeCategory);
this.buttonList.add(previousRecipeCategory);
this.buttonList.add(nextPage);
this.buttonList.add(previousPage);
}
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
drawGuiContainerBackgroundLayer(partialTicks, mouseX, mouseY);
GlStateManager.disableBlend();
drawRect(guiLeft + borderPadding + buttonWidth,
guiTop + borderPadding - 2,
guiLeft + xSize - borderPadding - buttonWidth,
guiTop + borderPadding + 10,
0x30000000);
drawRect(guiLeft + borderPadding + buttonWidth,
guiTop + titleHeight + textPadding - 2,
guiLeft + xSize - borderPadding - buttonWidth,
guiTop + titleHeight + textPadding + 10,
0x30000000);
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
StringUtil.drawCenteredString(fontRendererObj, title, xSize, guiLeft, guiTop + borderPadding, Color.WHITE.getRGB(), true);
StringUtil.drawCenteredString(fontRendererObj, pageString, xSize, guiLeft, guiTop + titleHeight + textPadding, Color.WHITE.getRGB(), true);
nextRecipeCategory.drawButton(mc, mouseX, mouseY);
previousRecipeCategory.drawButton(mc, mouseX, mouseY);
nextPage.drawButton(mc, mouseX, mouseY);
previousPage.drawButton(mc, mouseX, mouseY);
RecipeLayout hovered = null;
for (RecipeLayout recipeWidget : recipeLayouts) {
if (recipeWidget.getFocusUnderMouse(mouseX, mouseY) != null) {
hovered = recipeWidget;
} else {
recipeWidget.draw(mc, mouseX, mouseY);
}
}
if (hovered != null) {
hovered.draw(mc, 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(mc, showAllRecipesString, mouseX, mouseY);
}
}
}
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
drawDefaultBackground();
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
mc.getTextureManager().bindTexture(backgroundTexture);
int x = (width - xSize) / 2;
int y = (height - ySize) / 2;
this.zLevel = 0;
drawTexturedModalRect(x, y, 0, 0, xSize, ySize);
}
public boolean isMouseOver(int mouseX, int mouseY) {
return mc.currentScreen == this && (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;
}
@Override
public boolean canSetFocusWithMouse() {
return true;
}
@Override
public void handleMouseInput() throws IOException {
final int x = Mouse.getEventX() * width / mc.displayWidth;
final int y = height - Mouse.getEventY() * height / mc.displayHeight - 1;
if (isMouseOver(x, y)) {
int scrollDelta = Mouse.getEventDWheel();
if (scrollDelta < 0) {
logic.nextPage();
updateLayout();
return;
} else if (scrollDelta > 0) {
logic.previousPage();
updateLayout();
return;
}
}
super.handleMouseInput();
}
@Override
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
if (!isMouseOver(mouseX, mouseY)) {
return;
}
if (titleHoverChecker.checkHover(mouseX, mouseY)) {
if (logic.setCategoryFocus()) {
updateLayout();
}
} else {
for (RecipeLayout recipeLayout : recipeLayouts) {
if (recipeLayout.handleClick(mc, mouseX, mouseY, mouseButton)) {
return;
}
}
}
super.mouseClicked(mouseX, mouseY, mouseButton);
}
@Override
protected void keyTyped(char typedChar, int keyCode) throws IOException {
if (InputHandler.isInventoryCloseKey(keyCode) || InputHandler.isInventoryToggleKey(keyCode)) {
close();
} else if (KeyBindings.recipeBack.isActiveAndMatches(keyCode)) {
back();
}
}
private void open() {
if (mc.currentScreen != this) {
parentScreen = mc.currentScreen;
}
mc.displayGuiScreen(this);
}
private void close() {
if (parentScreen != null) {
mc.displayGuiScreen(parentScreen);
parentScreen = null;
} else {
mc.thePlayer.closeScreen();
}
logic.clearHistory();
}
public void showRecipes(@Nonnull Focus focus) {
focus.setMode(Focus.Mode.OUTPUT);
if (logic.setFocus(focus)) {
open();
}
}
public void showUses(@Nonnull Focus focus) {
focus.setMode(Focus.Mode.INPUT);
if (logic.setFocus(focus)) {
open();
}
}
public void showCategories(@Nonnull List<String> recipeCategoryUids) {
if (logic.setCategoryFocus(recipeCategoryUids)) {
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);
boolean maxTransfer = GuiScreen.isShiftKeyDown();
Container container = getParentContainer();
if (container != null && RecipeTransferUtil.transferRecipe(container, recipeLayout, mc.thePlayer, maxTransfer)) {
close();
updateLayout = false;
}
} else {
updateLayout = false;
}
if (updateLayout) {
updateLayout();
}
}
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 = guiLeft + (xSize - recipeBackground.getWidth()) / 2;
final int recipeSpacing = (ySize - headerHeight - (recipesPerPage * recipeBackground.getHeight())) / (recipesPerPage + 1);
logic.setRecipesPerPage(recipesPerPage);
title = recipeCategory.getTitle();
final int titleWidth = fontRendererObj.getStringWidth(title);
final int titleX = guiLeft + (xSize - titleWidth) / 2;
final int titleY = guiTop + borderPadding;
titleHoverChecker = new HoverChecker(titleY, titleY + fontRendererObj.FONT_HEIGHT, titleX, titleX + titleWidth, 0);
int spacingY = recipeBackground.getHeight() + recipeSpacing;
recipeLayouts.clear();
recipeLayouts.addAll(logic.getRecipeWidgets(recipeXOffset, guiTop + headerHeight + recipeSpacing, spacingY));
addRecipeTransferButtons(recipeLayouts);
nextPage.enabled = previousPage.enabled = logic.hasMultiplePages();
nextRecipeCategory.enabled = previousRecipeCategory.enabled = logic.hasMultipleCategories();
pageString = logic.getPageString();
}
private void addRecipeTransferButtons(List<RecipeLayout> recipeLayouts) {
buttonList.clear();
addButtons();
EntityPlayer player = mc.thePlayer;
Container container = getParentContainer();
for (RecipeLayout recipeLayout : recipeLayouts) {
RecipeTransferButton button = recipeLayout.getRecipeTransferButton();
button.init(container, recipeLayout, player);
buttonList.add(button);
}
}
@Nullable
public GuiScreen getParentScreen() {
return parentScreen;
}
@Nullable
private Container getParentContainer() {
if (parentScreen instanceof GuiContainer) {
return ((GuiContainer) parentScreen).inventorySlots;
}
return null;
}
}