From ff038cc46833fbd324510140962ef8d6b144805e Mon Sep 17 00:00:00 2001 From: Nedelosk Date: Mon, 7 Mar 2016 08:06:30 +0100 Subject: [PATCH] Fix WidgetFluidTank --- .../forestry/core/gui/widgets/TankWidget.java | 114 +++++++++++++----- 1 file changed, 86 insertions(+), 28 deletions(-) diff --git a/src/main/java/forestry/core/gui/widgets/TankWidget.java b/src/main/java/forestry/core/gui/widgets/TankWidget.java index cb046907b..2bfe96672 100755 --- a/src/main/java/forestry/core/gui/widgets/TankWidget.java +++ b/src/main/java/forestry/core/gui/widgets/TankWidget.java @@ -10,14 +10,25 @@ ******************************************************************************/ package forestry.core.gui.widgets; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.GlStateManager; +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.EntityPlayer; import net.minecraft.inventory.Container; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; - +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.IFluidTank; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + import org.lwjgl.opengl.GL11; import forestry.api.core.IToolPipette; @@ -61,6 +72,7 @@ public class TankWidget extends Widget { @Override public void draw(int startX, int startY) { + GlStateManager.disableBlend(); IFluidTank tank = getTank(); if (tank == null || tank.getCapacity() <= 0) { return; @@ -71,43 +83,62 @@ public class TankWidget extends Widget { return; } - if (contents.getFluid().getStill() == null) { + Fluid fluid = contents.getFluid(); + if (fluid == null) { return; } - Proxies.render.bindTexture(contents.getFluid().getStill()); - int scaledLiquid = (contents.amount * height) / tank.getCapacity(); - if (scaledLiquid > height) { - scaledLiquid = height; + TextureMap textureMapBlocks = Minecraft.getMinecraft().getTextureMapBlocks(); + ResourceLocation fluidStill = fluid.getStill(); + TextureAtlasSprite fluidStillSprite = null; + if (fluidStill != null) { + fluidStillSprite = textureMapBlocks.getTextureExtry(fluidStill.toString()); + } + if (fluidStillSprite == null) { + fluidStillSprite = textureMapBlocks.getMissingSprite(); } - GL11.glPushAttrib(GL11.GL_ENABLE_BIT); - { - GL11.glDisable(GL11.GL_LIGHTING); - GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0F); + int fluidColor = fluid.getColor(contents); - int start = 0; - while (scaledLiquid > 0) { - int x; + int scaledAmount = (contents.amount * height) / tank.getCapacity(); + if (contents.amount > 0 && scaledAmount < 1) { + scaledAmount = 1; + } + if (scaledAmount > height) { + scaledAmount = height; + } - if (scaledLiquid > 16) { - x = 16; - scaledLiquid -= 16; - } else { - x = scaledLiquid; - scaledLiquid = 0; + Proxies.render.bindTexture(TextureMap.locationBlocksTexture); + setGLColorFromInt(fluidColor); + + final int xTileCount = width / 16; + final int xRemainder = width - (xTileCount * 16); + final int yTileCount = scaledAmount / 16; + final int yRemainder = scaledAmount - (yTileCount * 16); + + final int yStart = startY + height; + + for (int xTile = 0; xTile <= xTileCount; xTile++) { + for (int yTile = 0; yTile <= yTileCount; yTile++) { + int width = (xTile == xTileCount) ? xRemainder : 16; + int height = (yTile == yTileCount) ? yRemainder : 16; + int x = startX + (xTile * 16); + int y = yStart - ((yTile + 1) * 16); + if (width > 0 && height > 0) { + int maskTop = 16 - height; + int maskRight = 16 - width; + + drawFluidTexture(x + xPos, y + yPos, fluidStillSprite, maskTop, maskRight, 100); } - - manager.gui.drawTexturedModalRect(startX + xPos, startY + yPos + height - x - start, 0, 0, 16, 16 - (16 - x)); - start += 16; - } - - if (drawOverlay) { - Proxies.render.bindTexture(manager.gui.textureFile); - manager.gui.drawTexturedModalRect(startX + xPos, startY + yPos, overlayTexX, overlayTexY, 16, 60); } } - GL11.glPopAttrib(); + + if (drawOverlay) { + Proxies.render.bindTexture(manager.gui.textureFile); + manager.gui.drawTexturedModalRect(startX + xPos, startY + yPos, overlayTexX, overlayTexY, 16, 60); + } + + GlStateManager.color(1, 1, 1, 1); } @Override @@ -118,6 +149,33 @@ public class TankWidget extends Widget { } return ((StandardTank) tank).getToolTip(); } + + private static void setGLColorFromInt(int color) { + float red = (color >> 16 & 0xFF) / 255.0F; + float green = (color >> 8 & 0xFF) / 255.0F; + float blue = (color & 0xFF) / 255.0F; + + GlStateManager.color(red, green, blue, 1.0F); + } + + + private static void drawFluidTexture(double xCoord, double yCoord, TextureAtlasSprite textureSprite, int maskTop, int maskRight, double zLevel) { + double uMin = (double) textureSprite.getMinU(); + double uMax = (double) textureSprite.getMaxU(); + double vMin = (double) textureSprite.getMinV(); + double vMax = (double) textureSprite.getMaxV(); + uMax = uMax - (maskRight / 16.0 * (uMax - uMin)); + vMax = vMax - (maskTop / 16.0 * (vMax - vMin)); + + Tessellator tessellator = Tessellator.getInstance(); + WorldRenderer worldrenderer = tessellator.getWorldRenderer(); + worldrenderer.begin(7, DefaultVertexFormats.POSITION_TEX); + worldrenderer.pos(xCoord, yCoord + 16, zLevel).tex(uMin, vMax).endVertex(); + worldrenderer.pos(xCoord + 16 - maskRight, yCoord + 16, zLevel).tex(uMax, vMax).endVertex(); + worldrenderer.pos(xCoord + 16 - maskRight, yCoord + maskTop, zLevel).tex(uMax, vMin).endVertex(); + worldrenderer.pos(xCoord, yCoord + maskTop, zLevel).tex(uMin, vMin).endVertex(); + tessellator.draw(); + } @Override public void handleMouseClick(int mouseX, int mouseY, int mouseButton) {