Java tutorial
/* * This class belongs to the source code of Advanced Chemistry. * It might depend on other Advanced Chemistry classes to work * properly. * * For the full license, please see the licence.txt in the source * directory. */ package rheel.ac.client.gui; import java.awt.Polygon; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Random; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.renderer.RenderHelper; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL12; import rheel.ac.common.handler.GuiHandler; import rheel.ac.common.packet.PacketDropInWorld; import rheel.ac.common.packet.PacketInventoryAdd; import rheel.ac.common.packet.PacketOpenGUI; import rheel.ac.common.packet.PacketResearchItemAdd; import rheel.ac.common.packet.PacketResearchItemRemove; import rheel.ac.common.periodic.PeriodicItem; import rheel.ac.common.periodic.PeriodicPlayer; import rheel.ac.common.research.Research; import rheel.ac.common.tileentity.TileEntityResearch; import rheel.ac.common.tileentity.TileEntityResearch.StackWithPos; import rheel.ac.common.util.IGuiInfo; import rheel.ac.common.util.ItemRenderer; import rheel.ac.common.util.TextureLoader; import cpw.mods.fml.common.network.PacketDispatcher; /** * @author Rheel */ public class GuiResearch extends GuiScreen implements IGuiInfo { private final int MAX_RESEARCH_TIME = 60; private final boolean CAN_FAIL = false; private final int FAIL_PERCENTAGE_TOT = 10; private final int FAIL_PER_10000 = (int) Math.pow(this.MAX_RESEARCH_TIME, 1 / this.FAIL_PERCENTAGE_TOT) * 100; private final TileEntityResearch entity; private final EntityPlayer player; private final PeriodicPlayer per; private ItemRenderer itemRenderer; private float ball = 0; private ItemStack dragged = null; private boolean isDraggingGlass = false; private int researchX = 181; private int researchY = 27; private final GuiButtonTexture researchButton = new GuiButtonTexture(1, 0, 0, 59, 8, TextureLoader.getGUITexture("Research.png")); private long researchTime = -1; private long tickIncreseCache = 0; private boolean tickIncrese = false; private int tick = -1; private int tickCache = -1; private int stopTick = -1; public GuiResearch(TileEntityResearch entity, EntityPlayer player) { this.entity = entity; this.player = player; this.per = PeriodicPlayer.getInstance(player); this.researchButton.setUVDefault(88, 135); this.researchButton.setUVDisabled(88, 135); this.researchButton.setUVOverlay(88, 145); this.researchButton.drawButton = false; this.itemRenderer = new ItemRenderer(this); } @Override public void initGui() { final int xStart = (this.width - 256) / 2; final int yStart = (this.height - 131) / 2; this.buttonList.add(new GuiButtonNoBackground(0, xStart + 44, yStart, "Inventory")); } @Override public void drawScreen(int mouseX, int mouseY, float tick) { GL11.glPushMatrix(); final int xStart = (this.width - 256) / 2; final int yStart = (this.height - 131) / 2; this.drawBackground(this.mc, xStart, yStart, false, mouseX, mouseY); super.drawScreen(mouseX, mouseY, tick); this.drawStacks(this.mc, xStart, yStart, false, mouseX, mouseY); this.drawResearchGlass(this.mc, xStart, yStart, mouseX, mouseY); if (this.isDraggingGlass) { this.researchX = mouseX - xStart - 29; this.researchY = mouseY - yStart - 19; } this.researchButton.xPosition = xStart + this.researchX - 9; this.researchButton.yPosition = yStart + this.researchY + 24; this.researchButton.drawButton = this.isResearchAvailable(); this.researchButton.drawButton(this.mc, mouseX, mouseY); this.mc.getTextureManager().bindTexture(TextureLoader.getGUITexture("Research.png")); if (this.tick > 0) { this.drawTexturedModalRect(this.researchButton.xPosition, this.researchButton.yPosition, 88, 155, this.getBarLenght(this.tick), 8); } else if (this.tickCache > 0) { this.drawTexturedModalRect(this.researchButton.xPosition, this.researchButton.yPosition, 88, 165, this.getBarLenght(this.tickCache), 8); } GL11.glTranslatef(0, 0, -50); GL11.glDisable(GL11.GL_LIGHTING); if (this.getInventoryButton() != null && this.getInventoryButton().hasMouseOver()) { final List<String> list = new ArrayList<String>(); list.add("Click to access your inventory."); list.add(""); list.add("Drop items here to drop them in"); list.add("your inventory."); GL11.glDisable(GL11.GL_LIGHTING); this.drawHoveringText(list, mouseX, mouseY, this.fontRenderer); } GL11.glEnable(GL11.GL_LIGHTING); this.researchTick(); GL11.glPopMatrix(); } private int getBarLenght(int i) { return (int) ((float) i / (float) this.MAX_RESEARCH_TIME * 59.0); } private boolean isResearchAvailable() { ItemStack research = this.getResearch(); if (research == null) { return false; } if (PeriodicItem.getItemsDiscoverable(research, this.per).length == 0) { return false; } return true; } private ItemStack getResearch() { return this.getOver(this.researchX, this.researchY, 2); } private boolean isResearching() { return this.researchTime != -1; } private boolean setRenderer = false; public void draw(final Minecraft mc, int x, int y, boolean remote, int mouseX, int mouseY) { if (!this.setRenderer) { this.itemRenderer = new ItemRenderer(new IGuiInfo() { @Override public Minecraft getMinecraft() { return mc; } @Override public FontRenderer getFontRenderer() { return null; } }); this.setRenderer = true; } GL11.glPushMatrix(); this.drawBackground(mc, x, y, remote, mouseX, mouseY); this.drawStacks(mc, x, y, remote, mouseX, mouseY); GL11.glPopMatrix(); } private void drawBackground(Minecraft mc, int x, int y, boolean remote, int mouseX, int mouseY) { mc.getTextureManager().bindTexture(TextureLoader.getGUITexture("Research.png")); this.drawTexturedModalRect(x, y, 0, 0, 256, 131); this.drawTexturedModalRect(x + 197, y + 7, 4, 131, 18, 17); this.ball += 1.008F; final int x1 = (int) (Math.sin(Math.PI * 2 * this.ball) * 20); final int x2 = (int) (Math.sin(Math.PI * 2 * this.ball) * 3); final int y1 = (int) (Math.cos(Math.PI * 2 * this.ball) * 4); final int y2 = (int) (Math.cos(Math.PI * 2 * this.ball) * 20); if (y1 > 0 || x1 < -9 || x1 > 9) { this.drawTexturedModalRect(x + 204 + x1, y + 15 + y1, 0, 131, 4, 4); } if (x2 < 0 || y2 < -9 || y2 > 8) { this.drawTexturedModalRect(x + 205 + x2, y + 13 + y2, 0, 131, 4, 4); } } private void drawStacks(Minecraft mc, int x, int y, boolean remote, int mouseX, int mouseY) { if (this.dragged != null) { GL11.glPushMatrix(); GL11.glDisable(GL11.GL_DEPTH_TEST); GL11.glDepthMask(false); GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); GL11.glDisable(GL11.GL_ALPHA_TEST); mc.getTextureManager().bindTexture(TextureLoader.getGUITexture("Research.png")); this.drawTexturedModalRect(mouseX - 12, mouseY - 12, 22, 131, 24, 24); GL11.glPopMatrix(); this.drawItemStack(new StackWithPos(this.dragged, mouseX - 8 - x - 100, mouseY - 8 - y - 46), mc, remote, x, y); } for (final StackWithPos stack : this.entity.getItems()) { if (remote) { if (stack != null) { final StackWithPos swp = new StackWithPos(stack.item, stack.x + 100, stack.y + 46); this.drawItemStack(swp, mc, remote, x, y); } } else { this.drawItemStack(stack, mc, remote, x, y); } } } private void drawResearchGlass(Minecraft mc, int xStart, int yStart, int mouseX, int mouseY) { GL11.glPushMatrix(); GL11.glDisable(GL11.GL_DEPTH_TEST); GL11.glDepthMask(false); GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); GL11.glDisable(GL11.GL_ALPHA_TEST); mc.getTextureManager().bindTexture(TextureLoader.getGUITexture("Research.png")); this.drawTexturedModalRect(xStart + this.researchX, yStart + this.researchY, 46, 131, 40, 26); GL11.glPopMatrix(); } private void drawItemStack(StackWithPos stack, Minecraft mc, boolean remote, int xStart, int yStart) { if (stack != null && stack.item != null) { GL11.glPushMatrix(); this.zLevel = 50.0F; GL11.glEnable(GL11.GL_DEPTH_TEST); if (remote) { this.itemRenderer.renderItemStack(stack.item, stack.x + xStart, stack.y + yStart); } else { this.itemRenderer.renderItemStack(stack.item, stack.x + 100 + xStart, stack.y + yStart + 46); } this.zLevel = 0.0F; GL11.glPopMatrix(); } } private void researchTick() { this.tickIncrese = this.tickIncreseCache != this.player.worldObj.getTotalWorldTime(); this.tickIncreseCache = this.player.worldObj.getTotalWorldTime(); if (this.researchTime != -1) { this.tick = (int) (this.player.worldObj.getTotalWorldTime() - this.researchTime); this.researchButton.enabled = false; } if (this.tick >= this.MAX_RESEARCH_TIME) { Research.researchItem(this.player, this.getResearch()); this.researchTime = -1; this.tick = -1; this.researchButton.enabled = true; } else { if (this.tickIncrese) { if (this.isResearching()) { if (this.CAN_FAIL) { Random rand = this.player.worldObj.rand; if (rand.nextInt(10000) <= this.FAIL_PER_10000) { this.tickCache = this.tick; this.tick = -1; this.researchTime = -1; this.stopTick = 20; this.researchButton.enabled = false; } } } else { if (this.stopTick >= 0) { if (this.stopTick == 0) { this.tickCache = -1; this.researchButton.enabled = true; } this.stopTick--; } } } } } @Override public boolean doesGuiPauseGame() { return false; } @Override protected void mouseClicked(int par1, int par2, int par3) { final int xStart = (this.width - 256) / 2; final int yStart = (this.height - 131) / 2; if (par3 == 0) { if (this.isDraggingGlass) { this.isDraggingGlass = false; } else { if (this.dragged != null) { if (this.getInventoryButton().hasMouseOver()) { PacketDispatcher.sendPacketToServer(new PacketInventoryAdd(this.dragged).getPacket()); if (this.player.inventory.addItemStackToInventory(this.dragged)) { this.dragged = null; } } else { if (this.isInPlacableArea(par1 - xStart, par2 - yStart)) { PacketDispatcher.sendPacketToServer( new PacketResearchItemAdd(par1 - 108 - xStart, par2 - 54 - yStart).getPacket()); this.entity.addItem(this.dragged, par1 - 108 - xStart, par2 - 54 - yStart); this.dragged = null; } } } else { if (this.isMouseOverGlass(par1 - xStart - this.researchX, par2 - yStart - this.researchY) && !this.isResearching()) { this.isDraggingGlass = true; } else { super.mouseClicked(par1, par2, par3); GuiButton guibutton = this.researchButton; if (guibutton.mousePressed(this.mc, par1, par2)) { this.mc.sndManager.playSoundFX("random.click", 1.0F, 1.0F); this.actionPerformed(guibutton); } this.dragged = this.getMouseOver(par1, par2); if (this.dragged != null) { this.entity.remove(this.dragged); PacketDispatcher .sendPacketToServer(new PacketResearchItemRemove(this.dragged).getPacket()); } } } } } } private boolean isMouseOverGlass(int x, int y) { Polygon p = new Polygon(new int[] { 20, 22, 25, 39, 39, 38, 22, 20 }, new int[] { 13, 15, 15, 22, 24, 25, 17, 15 }, 8); return p.contains(x, y); } private ItemStack getOver(int xCheck, int yCheck, int radius) { xCheck -= 100; yCheck -= 46; for (final StackWithPos stack : this.entity.getItems()) { if (stack != null && stack.item != null) { if (xCheck >= stack.x - radius && xCheck <= stack.x + radius && yCheck >= stack.y - radius && yCheck <= stack.y + radius) { return stack.item; } } } return null; } private ItemStack getMouseOver(int x, int y) { for (final StackWithPos stack : this.entity.getItems()) { if (stack != null && stack.item != null) { final int mouseX = x - (this.width - 256) / 2 - 100; final int mouseY = y - (this.height - 131) / 2 - 46; if (mouseX >= stack.x && mouseX <= stack.x + 16 && mouseY >= stack.y && mouseY <= stack.y + 16) { if (!this.isResearching()) { return stack.item; } if (!ItemStack.areItemStacksEqual(this.getResearch(), stack.item)) { return stack.item; } } } } return null; } private boolean isInPlacableArea(int x, int y) { return new Polygon(new int[] { 3, 39, 218, 250 }, new int[] { 130, 19, 19, 130 }, 4).contains(x, y); } @Override protected void actionPerformed(GuiButton button) { if (button.id == 0) { PacketDispatcher.sendPacketToServer(new PacketOpenGUI(GuiHandler.GUI_RESEARCHINVENTORY, this.entity.xCoord, this.entity.yCoord, this.entity.zCoord).getPacket()); } else if (button.id == 1) { this.researchTime = this.player.worldObj.getTotalWorldTime(); } } @Override protected void keyTyped(char par1, int par2) { if (par2 == 1 || par2 == this.mc.gameSettings.keyBindInventory.keyCode) { if (this.dragged != null) { PacketDispatcher.sendPacketToServer(new PacketDropInWorld(this.dragged, this.entity.xCoord, this.entity.yCoord, this.entity.zCoord).getPacket()); } this.mc.thePlayer.closeScreen(); } } private GuiButtonNoBackground getInventoryButton() { for (final Object o : this.buttonList) { if (o instanceof GuiButtonNoBackground) { return (GuiButtonNoBackground) o; } } return null; } private void drawHoveringText(List par1List, int par2, int par3, FontRenderer font) { if (!par1List.isEmpty()) { GL11.glDisable(GL12.GL_RESCALE_NORMAL); RenderHelper.disableStandardItemLighting(); GL11.glDisable(GL11.GL_DEPTH_TEST); int k = 0; final Iterator iterator = par1List.iterator(); while (iterator.hasNext()) { final String s = (String) iterator.next(); final int l = font.getStringWidth(s); if (l > k) { k = l; } } int i1 = par2 + 12; int j1 = par3 - 12; int k1 = 8; if (par1List.size() > 1) { k1 += 2 + (par1List.size() - 1) * 10; } if (i1 + k > this.width) { i1 -= 28 + k; } if (j1 + k1 + 6 > this.height) { j1 = this.height - k1 - 6; } this.zLevel = 300.0F; final int l1 = -267386864; this.drawGradientRect(i1 - 3, j1 - 4, i1 + k + 3, j1 - 3, l1, l1); this.drawGradientRect(i1 - 3, j1 + k1 + 3, i1 + k + 3, j1 + k1 + 4, l1, l1); this.drawGradientRect(i1 - 3, j1 - 3, i1 + k + 3, j1 + k1 + 3, l1, l1); this.drawGradientRect(i1 - 4, j1 - 3, i1 - 3, j1 + k1 + 3, l1, l1); this.drawGradientRect(i1 + k + 3, j1 - 3, i1 + k + 4, j1 + k1 + 3, l1, l1); final int i2 = 1347420415; final int j2 = (i2 & 16711422) >> 1 | i2 & -16777216; this.drawGradientRect(i1 - 3, j1 - 3 + 1, i1 - 3 + 1, j1 + k1 + 3 - 1, i2, j2); this.drawGradientRect(i1 + k + 2, j1 - 3 + 1, i1 + k + 3, j1 + k1 + 3 - 1, i2, j2); this.drawGradientRect(i1 - 3, j1 - 3, i1 + k + 3, j1 - 3 + 1, i2, i2); this.drawGradientRect(i1 - 3, j1 + k1 + 2, i1 + k + 3, j1 + k1 + 3, j2, j2); for (int k2 = 0; k2 < par1List.size(); ++k2) { final String s1 = (String) par1List.get(k2); font.drawStringWithShadow(s1, i1, j1, -1); if (k2 == 0) { j1 += 2; } j1 += 10; } this.zLevel = 0.0F; GL11.glEnable(GL11.GL_DEPTH_TEST); RenderHelper.enableStandardItemLighting(); GL11.glEnable(GL12.GL_RESCALE_NORMAL); } } @Override public FontRenderer getFontRenderer() { return this.fontRenderer; } @Override public Minecraft getMinecraft() { return this.mc; } }