-
Notifications
You must be signed in to change notification settings - Fork 81
/
WorkbenchGui.java
203 lines (179 loc) · 7.28 KB
/
WorkbenchGui.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
package com.progwml6.natura.common.gui.client;
import java.io.IOException;
import com.progwml6.natura.common.gui.common.WorkbenchContainer;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiButtonImage;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.gui.recipebook.GuiRecipeBook;
import net.minecraft.client.gui.recipebook.IRecipeShownListener;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.ClickType;
import net.minecraft.inventory.ContainerWorkbench;
import net.minecraft.inventory.Slot;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class WorkbenchGui extends GuiContainer implements IRecipeShownListener
{
private static final ResourceLocation CRAFTING_TABLE_GUI_TEXTURES = new ResourceLocation("textures/gui/container/crafting_table.png");
private GuiButtonImage recipeButton;
private final GuiRecipeBook recipeBookGui;
private boolean widthTooNarrow;
public WorkbenchGui(InventoryPlayer playerInv, World worldIn)
{
this(playerInv, worldIn, BlockPos.ORIGIN);
}
public WorkbenchGui(InventoryPlayer playerInv, World worldIn, BlockPos blockPosition)
{
super(new WorkbenchContainer(playerInv, worldIn, blockPosition));
this.recipeBookGui = new GuiRecipeBook();
}
/**
* Adds the buttons (and other controls) to the screen in question. Called when the GUI is displayed and when the
* window resizes, the buttonList is cleared beforehand.
*/
@Override
public void initGui()
{
super.initGui();
this.widthTooNarrow = this.width < 379;
this.recipeBookGui.init(this.width, this.height, this.mc, this.widthTooNarrow, this.inventorySlots, ((WorkbenchContainer) this.inventorySlots).craftMatrix);
this.guiLeft = this.recipeBookGui.updateScreenPosition(this.widthTooNarrow, this.width, this.xSize);
this.recipeButton = new GuiButtonImage(10, this.guiLeft + 5, this.height / 2 - 49, 20, 18, 0, 168, 19, CRAFTING_TABLE_GUI_TEXTURES);
this.buttonList.add(this.recipeButton);
}
/**
* Called from the main game loop to update the screen.
*/
@Override
public void updateScreen()
{
super.updateScreen();
this.recipeBookGui.tick();
}
/**
* Draws the screen and all the components in it.
*/
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks)
{
this.drawDefaultBackground();
if (this.recipeBookGui.isVisible() && this.widthTooNarrow)
{
this.drawGuiContainerBackgroundLayer(partialTicks, mouseX, mouseY);
this.recipeBookGui.render(mouseX, mouseY, partialTicks);
}
else
{
this.recipeBookGui.render(mouseX, mouseY, partialTicks);
super.drawScreen(mouseX, mouseY, partialTicks);
this.recipeBookGui.renderGhostRecipe(this.guiLeft, this.guiTop, true, partialTicks);
}
this.renderHoveredToolTip(mouseX, mouseY);
this.recipeBookGui.renderTooltip(this.guiLeft, this.guiTop, mouseX, mouseY);
}
/**
* Draw the foreground layer for the GuiContainer (everything in front of the items)
*/
@Override
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY)
{
this.fontRenderer.drawString(I18n.format("container.crafting"), 28, 6, 4210752);
this.fontRenderer.drawString(I18n.format("container.inventory"), 8, this.ySize - 96 + 2, 4210752);
}
/**
* Draws the background layer of this container (behind the items).
*/
@Override
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY)
{
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
this.mc.getTextureManager().bindTexture(CRAFTING_TABLE_GUI_TEXTURES);
int i = this.guiLeft;
int j = (this.height - this.ySize) / 2;
this.drawTexturedModalRect(i, j, 0, 0, this.xSize, this.ySize);
}
/**
* Test if the 2D point is in a rectangle (relative to the GUI). Args : rectX, rectY, rectWidth, rectHeight, pointX,
* pointY
*/
@Override
protected boolean isPointInRegion(int rectX, int rectY, int rectWidth, int rectHeight, int pointX, int pointY)
{
return (!this.widthTooNarrow || !this.recipeBookGui.isVisible()) && super.isPointInRegion(rectX, rectY, rectWidth, rectHeight, pointX, pointY);
}
/**
* Called when the mouse is clicked. Args : mouseX, mouseY, clickedButton
*/
@Override
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException
{
if (!this.recipeBookGui.mouseClicked(mouseX, mouseY, mouseButton))
{
if (!this.widthTooNarrow || !this.recipeBookGui.isVisible())
{
super.mouseClicked(mouseX, mouseY, mouseButton);
}
}
}
@Override
protected boolean hasClickedOutside(int p_193983_1_, int p_193983_2_, int p_193983_3_, int p_193983_4_)
{
boolean flag = p_193983_1_ < p_193983_3_ || p_193983_2_ < p_193983_4_ || p_193983_1_ >= p_193983_3_ + this.xSize || p_193983_2_ >= p_193983_4_ + this.ySize;
return this.recipeBookGui.hasClickedOutside(p_193983_1_, p_193983_2_, this.guiLeft, this.guiTop, this.xSize, this.ySize) && flag;
}
/**
* Called by the controls from the buttonList when activated. (Mouse pressed for buttons)
*/
@Override
protected void actionPerformed(GuiButton button) throws IOException
{
if (button.id == 10)
{
this.recipeBookGui.initVisuals(this.widthTooNarrow, ((ContainerWorkbench) this.inventorySlots).craftMatrix);
this.recipeBookGui.toggleVisibility();
this.guiLeft = this.recipeBookGui.updateScreenPosition(this.widthTooNarrow, this.width, this.xSize);
this.recipeButton.setPosition(this.guiLeft + 5, this.height / 2 - 49);
}
}
/**
* Fired when a key is typed (except F11 which toggles full screen). This is the equivalent of
* KeyListener.keyTyped(KeyEvent e). Args : character (character on the key), keyCode (lwjgl Keyboard key code)
*/
@Override
protected void keyTyped(char typedChar, int keyCode) throws IOException
{
if (!this.recipeBookGui.keyPressed(typedChar, keyCode))
{
super.keyTyped(typedChar, keyCode);
}
}
/**
* Called when the mouse is clicked over a slot or outside the gui.
*/
@Override
protected void handleMouseClick(Slot slotIn, int slotId, int mouseButton, ClickType type)
{
super.handleMouseClick(slotIn, slotId, mouseButton, type);
this.recipeBookGui.slotClicked(slotIn);
}
@Override
public void recipesUpdated()
{
this.recipeBookGui.recipesUpdated();
}
/**
* Called when the screen is unloaded. Used to disable keyboard repeat events
*/
@Override
public void onGuiClosed()
{
this.recipeBookGui.removed();
super.onGuiClosed();
}
}