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.Rectangle; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.gui.ScaledResolution; import net.minecraft.client.renderer.RenderHelper; import net.minecraft.client.renderer.Tessellator; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.EnumChatFormatting; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL12; import rheel.ac.common.periodic.PeriodicIcon; import rheel.ac.common.periodic.PeriodicItem; import rheel.ac.common.periodic.PeriodicPlayer; import rheel.ac.common.periodic.PeriodicTable; import rheel.ac.common.util.TextureLoader; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; /** * @author Rheel */ @SideOnly(Side.CLIENT) public class GuiPeriodicBook extends GuiScreen { private final EntityPlayer player; private final boolean fullBook; private int xSize; private int ySize; private int leftX; private int leftY; private int mouseX = 0; private int mouseY = 0; private float texScale = 1; private final Map<Rectangle, List<String>> overlays = new HashMap<Rectangle, List<String>>(); private final Map<Rectangle, GuiScreen> clicks = new HashMap<Rectangle, GuiScreen>(); private final List<Rectangle> itemRects = new ArrayList<Rectangle>(); private int scale; private final PeriodicItem[][] rndItems = new PeriodicItem[7][14]; public GuiPeriodicBook(EntityPlayer player, boolean full) { super(); this.player = player; this.fullBook = full; for (int i = 0; i < 7; i++) { for (int j = 0; j < 14; j++) { this.rndItems[i][j] = PeriodicItem.getRandomDummyItem(); } } } @Override public void drawScreen(int mouseX, int mouseY, float par3) { GL11.glDisable(GL11.GL_LIGHTING); GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); this.mc.getTextureManager().bindTexture(TextureLoader.getGUITexture("PeriodicBook_Table.png")); final ScaledResolution res = new ScaledResolution(this.mc.gameSettings, this.mc.displayWidth, this.mc.displayHeight); this.scale = res.getScaleFactor(); this.mouseX = mouseX; this.mouseY = mouseY; this.setWorldAndResolution(this.mc, res.getScaledWidth() / this.scale, res.getScaledHeight() / this.scale); this.xSize = 992 / this.scale; this.ySize = 604 / this.scale; this.leftX = (this.width * this.scale - this.xSize) / 2; this.leftY = (this.height * this.scale - this.ySize) / 2; this.texScale = 4.0F / this.scale; this.drawBackground(this.leftX, this.leftY, 0, 0, this.xSize, this.ySize); final PeriodicPlayer player = PeriodicPlayer.getInstance(this.player); for (int per = 0; per < 7; per++) { for (int group = 0; group < 14; group++) { final PeriodicItem item = PeriodicTable.getItemAt(per, group); final int x = this.leftX + (138 + 56 * group) / this.scale; final int y = this.leftY + (78 + 67 * per) / this.scale; if (item != null && (player.getTable().isBaseItemDiscovered(PeriodicTable.getItemAt(per, group)) || this.fullBook)) { this.drawPeriodicTableItem(x, y, this.scale, item); } else if (item != null) { this.drawPeriodicTableItem(x, y, this.scale, this.rndItems[per][group]); } } } this.renderOverlays(); GL11.glEnable(GL11.GL_LIGHTING); } public void drawPeriodicTableItem(int x, int y, float scale, PeriodicItem item) { if (item != null) { GL11.glPushMatrix(); GL11.glDisable(GL11.GL_LIGHTING); GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); this.mc.getTextureManager().bindTexture(TextureLoader.getGUITexture("PeriodicBook_Icons.png")); for (int i = 0; i < item.getIcons().length; i++) { PeriodicIcon icon = item.getIcons()[i]; if (!PeriodicPlayer.getInstance(this.player).getTable().isIconDiscovered(item, icon) && !this.fullBook) { icon = PeriodicIcon.ICON_UNKNOWN; } final int iconX = x + 1 + (int) (i * 13 / scale); final int iconY = y + 1; this.drawIcon(iconX, iconY, icon.getTexCoordX(), icon.getTexCoordY(), scale); final List<String> overlayText = new ArrayList<String>(); overlayText.add(icon.getMouseOverText()); for (final String s : icon.getMouseOverSubText()) { overlayText.add(EnumChatFormatting.YELLOW + s); } this.overlays.put(new Rectangle(iconX, iconY, (int) (12 / scale), (int) ((12 + scale) / scale)), overlayText); } GL11.glPushMatrix(); String symbol = item.getSymbol(); if (!item.isRandom() && !PeriodicPlayer.getInstance(this.player).getTable().isSymbolDiscovered(item) && !this.fullBook) { symbol = "?"; } float xAdd = (51 - this.fontRenderer.getStringWidth(symbol) / (4 / scale)) / 2 / scale; float yAdd = (55 - this.fontRenderer.FONT_HEIGHT / (4 / scale)) / 2 / scale; GL11.glScalef(4 / scale, 4 / scale, 4 / scale); this.fontRenderer.drawString(symbol, (int) ((x + xAdd) / (4 / scale)) - (symbol.length() == 1 ? 0 : 3) - (scale == 1 ? 2 : scale == 2 ? 1 : scale == 4 ? -1 : 0), (int) ((y + yAdd) / (4 / scale)) + 1, 0x404040); GL11.glPopMatrix(); final List<String> nameOverlay = new ArrayList<String>(); String name = item.getName(); nameOverlay.add(item.getName()); PeriodicItem clickItem = item; PeriodicPlayer player = PeriodicPlayer.getInstance(this.player); if (!item.isRandom() && !player.getTable().isStructureDiscovered(item)) { item = this.rndItems[item.getPeriod()][player.getTable().getGroupForItem(item)]; } this.overlays.put(new Rectangle(x, y + (int) (16 / scale), (int) (54 / scale), (int) (51 / scale)), nameOverlay); this.clicks.put(new Rectangle(x, y + (int) (16 / scale), (int) (54 / scale), (int) (51 / scale)), new GuiPeriodicPage(this.player, item, this.fullBook)); this.itemRects.add(new Rectangle(x, y, (int) (54 / scale), (int) (66 / scale))); this.mc.getTextureManager().bindTexture(TextureLoader.getGUITexture("PeriodicBook_Icons.png")); if (item.isComplex() && (PeriodicPlayer.getInstance(this.player).getTable().isSymbolDiscovered(item) || this.fullBook)) { GL11.glPushMatrix(); GL11.glDisable(GL11.GL_DEPTH_TEST); GL11.glDepthMask(false); GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); GL11.glDisable(GL11.GL_ALPHA_TEST); this.drawComplex(x + 1, y + (int) (18 / scale), 0, 20, scale); GL11.glPopMatrix(); } GL11.glEnable(GL11.GL_LIGHTING); GL11.glPopMatrix(); } } public void drawBackground(int x, int y, int u, int v, int w, int h) { final float f = 0.00390625F / (this.texScale <= 0 ? 1 : this.texScale); final Tessellator tessellator = Tessellator.instance; tessellator.startDrawingQuads(); tessellator.addVertexWithUV(x + 0, y + h, this.zLevel, (u + 0) * f, (v + h) * f); tessellator.addVertexWithUV(x + w, y + h, this.zLevel, (u + w) * f, (v + h) * f); tessellator.addVertexWithUV(x + w, y + 0, this.zLevel, (u + w) * f, (v + 0) * f); tessellator.addVertexWithUV(x + 0, y + 0, this.zLevel, (u + 0) * f, (v + 0) * f); tessellator.draw(); } public void drawIcon(int x, int y, int u, int v, float scale) { GL11.glPushMatrix(); GL11.glDisable(GL11.GL_LIGHTING); GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); final float f = 0.00390625F; final int w = (int) (12 / scale); final int h = (int) ((12 + scale) / scale); final Tessellator tessellator = Tessellator.instance; tessellator.startDrawingQuads(); tessellator.addVertexWithUV(x + 0, y + h, this.zLevel, (u + 0) * f, (v + h * scale) * f); tessellator.addVertexWithUV(x + w, y + h, this.zLevel, (u + w * scale) * f, (v + h * scale) * f); tessellator.addVertexWithUV(x + w, y + 0, this.zLevel, (u + w * scale) * f, (v + 0) * f); tessellator.addVertexWithUV(x + 0, y + 0, this.zLevel, (u + 0) * f, (v + 0) * f); tessellator.draw(); GL11.glEnable(GL11.GL_LIGHTING); GL11.glPopMatrix(); } public void drawComplex(int x, int y, int u, int v, float scale) { GL11.glPushMatrix(); GL11.glDisable(GL11.GL_LIGHTING); GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); final float f = 0.00390625F; final int w = (int) (50 / scale); final int h = (int) ((41 + scale) / scale); final Tessellator tessellator = Tessellator.instance; tessellator.startDrawingQuads(); tessellator.addVertexWithUV(x + 0, y + h, this.zLevel, (u + 0) * f, (v + h * scale) * f); tessellator.addVertexWithUV(x + w, y + h, this.zLevel, (u + w * scale) * f, (v + h * scale) * f); tessellator.addVertexWithUV(x + w, y + 0, this.zLevel, (u + w * scale) * f, (v + 0) * f); tessellator.addVertexWithUV(x + 0, y + 0, this.zLevel, (u + 0) * f, (v + 0) * f); tessellator.draw(); GL11.glEnable(GL11.GL_LIGHTING); GL11.glPopMatrix(); } @Override public boolean doesGuiPauseGame() { return false; } @Override protected void mouseClicked(int x, int y, int type) { if (type == 0) { for (final Rectangle r : this.clicks.keySet()) { if (r.contains(this.mouseX, this.mouseY)) { this.mc.displayGuiScreen(this.clicks.get(r)); } } } } @Override protected void keyTyped(char par1, int par2) { if (par2 == 1 || par2 == this.mc.gameSettings.keyBindInventory.keyCode) { this.mc.displayGuiScreen(null); this.mc.setIngameFocus(); } } private void renderOverlays() { for (final Rectangle r : this.overlays.keySet()) { if (r.contains(this.mouseX, this.mouseY)) { this.drawHoveringText(this.overlays.get(r), this.mouseX, this.mouseY, this.mc.fontRenderer); } } } 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); } } }