-
Notifications
You must be signed in to change notification settings - Fork 497
/
GuiBuildCraft.java
367 lines (309 loc) · 13.5 KB
/
GuiBuildCraft.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
/** Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team http://www.mod-buildcraft.com
* <p/>
* BuildCraft is distributed under the terms of the Minecraft Mod Public License 1.0, or MMPL. Please check the contents
* of the license located in http://www.mod-buildcraft.com/MMPL-1.0.txt */
package buildcraft.core.lib.gui;
import java.io.IOException;
import java.util.Collection;
import org.lwjgl.opengl.GL11;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fluids.FluidStack;
import buildcraft.core.lib.gui.slots.IPhantomSlot;
import buildcraft.core.lib.gui.tooltips.IToolTipProvider;
import buildcraft.core.lib.gui.tooltips.ToolTip;
import buildcraft.core.lib.gui.tooltips.ToolTipLine;
import buildcraft.core.lib.gui.widgets.Widget;
import buildcraft.core.lib.render.FluidRenderer;
import buildcraft.core.lib.render.FluidRenderer.FluidType;
import buildcraft.core.lib.render.RenderUtils;
public abstract class GuiBuildCraft extends GuiContainer {
public static final ResourceLocation LEDGER_TEXTURE = new ResourceLocation("buildcraftcore:textures/gui/ledger.png");
public final LedgerManager ledgerManager = new LedgerManager(this);
public final TileEntity tile;
public final BuildCraftContainer container;
public ResourceLocation texture;
public GuiBuildCraft(BuildCraftContainer container, IInventory inventory, ResourceLocation texture) {
super(container);
this.container = container;
this.texture = texture;
if (inventory instanceof TileEntity) {
tile = (TileEntity) inventory;
} else {
tile = null;
}
initLedgers(inventory);
}
public FontRenderer getFontRenderer() {
return fontRendererObj;
}
protected void initLedgers(IInventory inventory) {}
// Protected/private methods made public
public int xSize() {
return xSize;
}
public int ySize() {
return ySize;
}
@Override
public void drawGradientRect(int left, int top, int right, int bottom, int startColor, int endColor) {
super.drawGradientRect(left, top, right, bottom, startColor, endColor);
}
/** Draws the screen and all the components in it. */
@Override
public void drawScreen(int mouseX, int mouseY, float par3) {
super.drawScreen(mouseX, mouseY, par3);
int left = this.guiLeft;
int top = this.guiTop;
GlStateManager.disableDepth();
GL11.glPushMatrix();
GL11.glTranslatef(left, top, 0.0F);
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
RenderHelper.disableStandardItemLighting();
InventoryPlayer playerInv = this.mc.thePlayer.inventory;
if (playerInv.getItemStack() == null) {
drawToolTips(container.getWidgets(), mouseX - left, mouseY - top, left, top);
drawToolTips(buttonList, mouseX, mouseY, 0, 0);
drawToolTips(inventorySlots.inventorySlots, mouseX, mouseY, 0, 0);
}
GL11.glPopMatrix();
GlStateManager.enableDepth();
}
private void drawToolTips(Collection<?> objects, int mouseX, int mouseY, int offsetX, int offsetY) {
for (Object obj : objects) {
if (!(obj instanceof IToolTipProvider)) {
continue;
}
IToolTipProvider provider = (IToolTipProvider) obj;
if (!provider.isToolTipVisible()) {
continue;
}
ToolTip tips = provider.getToolTip();
if (tips == null) {
continue;
}
boolean mouseOver = provider.isMouseOver(mouseX, mouseY);
tips.onTick(mouseOver);
if (mouseOver && tips.isReady()) {
tips.refresh();
drawToolTips(tips, mouseX + offsetX, mouseY + offsetY);
}
}
}
/** Draws a fluid into the gui. This will automatically cut and/or repeat the fluid sprite to ensure that it can
* fill up any sized tank while keeping the sprite the correct size and scale. */
public void drawFluid(FluidStack fluid, int x, int y, int width, int height, int maxCapacity) {
if (fluid == null || fluid.getFluid() == null) {
return;
}
TextureAtlasSprite sprite = FluidRenderer.getFluidTexture(fluid.getFluid(), FluidType.STILL);
mc.renderEngine.bindTexture(TextureMap.locationBlocksTexture);
RenderUtils.setGLColorFromInt(fluid.getFluid().getColor(fluid));
int fullX = width / 16;
int fullY = height / 16;
int lastX = width - fullX * 16;
int lastY = height - fullY * 16;
int level = fluid.amount * height / maxCapacity;
int fullLvl = (height - level) / 16;
int lastLvl = (height - level) - fullLvl * 16;
for (int i = 0; i < fullX; i++) {
for (int j = 0; j < fullY; j++) {
if (j >= fullLvl) {
drawCutIcon(sprite, x + i * 16, y + j * 16, 16, 16, j == fullLvl ? lastLvl : 0);
}
}
}
for (int i = 0; i < fullX; i++) {
drawCutIcon(sprite, x + i * 16, y + fullY * 16, 16, lastY, fullLvl == fullY ? lastLvl : 0);
}
for (int i = 0; i < fullY; i++) {
if (i >= fullLvl) {
drawCutIcon(sprite, x + fullX * 16, y + i * 16, lastX, 16, i == fullLvl ? lastLvl : 0);
}
}
drawCutIcon(sprite, x + fullX * 16, y + fullY * 16, lastX, lastY, fullLvl == fullY ? lastLvl : 0);
GlStateManager.color(1, 1, 1, 1);
}
// The magic is here
private void drawCutIcon(TextureAtlasSprite icon, int x, int y, int width, int height, int cut) {
Tessellator tess = Tessellator.getInstance();
WorldRenderer wr = tess.getWorldRenderer();
wr.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX);
vertexUV(wr, x, y + height, zLevel, icon.getMinU(), icon.getInterpolatedV(height));
vertexUV(wr, x + width, y + height, zLevel, icon.getInterpolatedU(width), icon.getInterpolatedV(height));
vertexUV(wr, x + width, y + cut, zLevel, icon.getInterpolatedU(width), icon.getInterpolatedV(cut));
vertexUV(wr, x, y + cut, zLevel, icon.getMinU(), icon.getInterpolatedV(cut));
tess.draw();
}
private static void vertexUV(WorldRenderer wr, double x, double y, double z, double u, double v) {
wr.pos(x, y, z).tex(u, v).endVertex();;
}
@Override
protected void drawGuiContainerBackgroundLayer(float f, int mouseX, int mouseY) {
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
bindTexture(texture);
drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);
int mX = mouseX - guiLeft;
int mY = mouseY - guiTop;
drawWidgets(mX, mY);
}
protected void drawWidgets(int mX, int mY) {
for (Widget widget : container.getWidgets()) {
if (widget.hidden) {
continue;
}
bindTexture(texture);
widget.draw(this, guiLeft, guiTop, mX, mY);
}
}
@Override
protected void drawGuiContainerForegroundLayer(int par1, int par2) {
drawLedgers(par1, par2);
}
protected void drawLedgers(int x, int y) {
ledgerManager.drawLedgers(x, y);
}
public void drawCenteredString(String string, int xCenter, int yCenter, int textColor) {
int x = xCenter - fontRendererObj.getStringWidth(string) / 2;
int y = yCenter - fontRendererObj.FONT_HEIGHT / 2;
fontRendererObj.drawString(string, x, y, textColor);
// Reset the colour afterwards as drawString leaves it at the last colour drawn
GlStateManager.color(1, 1, 1, 1);
}
protected int getCenteredOffset(String string) {
return getCenteredOffset(string, xSize);
}
protected int getCenteredOffset(String string, int xWidth) {
return (xWidth - fontRendererObj.getStringWidth(string)) / 2;
}
/** Returns if the passed mouse position is over the specified slot. */
private boolean isMouseOverSlot(Slot slot, int mouseX, int mouseY) {
int left = this.guiLeft;
int top = this.guiTop;
int realMouseX = mouseX - left;
int realMouseY = mouseY - top;
return realMouseX >= slot.xDisplayPosition - 1 && realMouseX < slot.xDisplayPosition + 16 + 1 && realMouseY >= slot.yDisplayPosition - 1
&& realMouseY < slot.yDisplayPosition + 16 + 1;
}
// / MOUSE CLICKS
@Override
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
int mX = mouseX - guiLeft;
int mY = mouseY - guiTop;
for (Widget widget : container.getWidgets()) {
if (widget.hidden) {
continue;
} else if (!widget.isMouseOver(mX, mY)) {
continue;
} else if (widget.handleMouseClick(mX, mY, mouseButton)) {
return;
}
}
super.mouseClicked(mouseX, mouseY, mouseButton);
// / Handle ledger clicks
ledgerManager.handleMouseClicked(mouseX, mouseY, mouseButton);
}
@Override
protected void mouseClickMove(int mouseX, int mouseY, int mouseButton, long time) {
int mX = mouseX - guiLeft;
int mY = mouseY - guiTop;
for (Widget widget : container.getWidgets()) {
if (widget.hidden) {
continue;
}
widget.handleMouseMove(mX, mY, mouseButton, time);
}
Slot slot = getSlotAtPosition(mouseX, mouseY);
if (mouseButton == 1 && slot instanceof IPhantomSlot) {
return;
}
super.mouseClickMove(mouseX, mouseY, mouseButton, time);
}
@Override
protected void mouseReleased(int mouseX, int mouseY, int eventType) {
super.mouseReleased(mouseX, mouseY, eventType);
int mX = mouseX - guiLeft;
int mY = mouseY - guiTop;
for (Widget widget : container.getWidgets()) {
if (widget.hidden) {
continue;
}
widget.handleMouseRelease(mX, mY, eventType);
}
}
public Slot getSlotAtPosition(int x, int y) {
for (int slotIndex = 0; slotIndex < this.inventorySlots.inventorySlots.size(); ++slotIndex) {
Slot slot = (Slot) this.inventorySlots.inventorySlots.get(slotIndex);
if (isMouseOverSlot(slot, x, y)) {
return slot;
}
}
return null;
}
public static void bindTexture(ResourceLocation texture) {
Minecraft.getMinecraft().renderEngine.bindTexture(texture);
}
private void drawToolTips(ToolTip toolTips, int mouseX, int mouseY) {
if (toolTips.size() > 0) {
int left = this.guiLeft;
int top = this.guiTop;
int length = 0;
int x;
int y;
for (ToolTipLine tip : toolTips) {
y = this.fontRendererObj.getStringWidth(tip.text);
if (y > length) {
length = y;
}
}
x = mouseX - left + 12;
y = mouseY - top - 12;
int var14 = 8;
if (toolTips.size() > 1) {
var14 += 2 + (toolTips.size() - 1) * 10;
}
this.zLevel = 300.0F;
itemRender.zLevel = 300.0F;
int var15 = -267386864;
this.drawGradientRect(x - 3, y - 4, x + length + 3, y - 3, var15, var15);
this.drawGradientRect(x - 3, y + var14 + 3, x + length + 3, y + var14 + 4, var15, var15);
this.drawGradientRect(x - 3, y - 3, x + length + 3, y + var14 + 3, var15, var15);
this.drawGradientRect(x - 4, y - 3, x - 3, y + var14 + 3, var15, var15);
this.drawGradientRect(x + length + 3, y - 3, x + length + 4, y + var14 + 3, var15, var15);
int var16 = 1347420415;
int var17 = (var16 & 16711422) >> 1 | var16 & -16777216;
this.drawGradientRect(x - 3, y - 3 + 1, x - 3 + 1, y + var14 + 3 - 1, var16, var17);
this.drawGradientRect(x + length + 2, y - 3 + 1, x + length + 3, y + var14 + 3 - 1, var16, var17);
this.drawGradientRect(x - 3, y - 3, x + length + 3, y - 3 + 1, var16, var16);
this.drawGradientRect(x - 3, y + var14 + 2, x + length + 3, y + var14 + 3, var17, var17);
for (ToolTipLine tip : toolTips) {
String line = tip.text;
if (tip.color == -1) {
line = "\u00a77" + line;
} else {
line = "\u00a7" + Integer.toHexString(tip.color) + line;
}
this.fontRendererObj.drawStringWithShadow(line, x, y, -1);
y += 10 + tip.getSpacing();
}
this.zLevel = 0.0F;
itemRender.zLevel = 0.0F;
}
}
public BuildCraftContainer getContainer() {
return container;
}
}