Java tutorial
/** Copyright (C) 2014 by jabelar This file is part of jabelar's Minecraft Forge modding examples; as such, you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For a copy of the GNU General Public License see <http://www.gnu.org/licenses/>. */ package com.blogspot.jabelarminecraft.magicbeans.gui; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.resources.I18n; import net.minecraft.util.ResourceLocation; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import org.lwjgl.input.Keyboard; import org.lwjgl.opengl.GL11; import com.blogspot.jabelarminecraft.magicbeans.MagicBeans; import com.blogspot.jabelarminecraft.magicbeans.networking.MessageGiveItemLeadToServer; import com.blogspot.jabelarminecraft.magicbeans.utilities.MagicBeansUtilities; /** * @author jabelar * */ public class GuiFamilyCow extends GuiScreen { private final int bookImageHeight = 192; private final int bookImageWidth = 192; private int currPage = 0; private static final int bookTotalPages = 1; private static ResourceLocation[] bookPageTextures = new ResourceLocation[bookTotalPages]; private static String[] stringPageText = new String[bookTotalPages]; private GuiButton buttonDone; private NextPageButton buttonNextPage; private NextPageButton buttonPreviousPage; public GuiFamilyCow() { // DEBUG System.out.println("GuiFamilyCow() constructor"); // Don't need to do everything in constructor because the init() function is // also directly called. bookPageTextures[0] = new ResourceLocation(MagicBeans.MODID + ":textures/gui/book.png"); stringPageText[0] = "This is your " + MagicBeansUtilities.stringToRainbow("Family Cow", true) + ". but she no longer produces any milk.\n\nSo you mother told you that you had to take the cow and find a villager to sell her to.\n\nShe handed you a lead and said 'Take the cow to town. And make sure you get a good deal!'."; } /** * Adds the buttons (and other controls) to the screen in question. */ @Override public void initGui() { // DEBUG System.out.println("GuiFamilyCow initGUI()"); buttonList.clear(); Keyboard.enableRepeatEvents(true); buttonDone = new GuiButton(0, width / 2 + 2, 4 + bookImageHeight, 98, 20, I18n.format("gui.done", new Object[0])); buttonList.add(buttonDone); int offsetFromScreenLeft = (width - bookImageWidth) / 2; buttonList.add(buttonNextPage = new NextPageButton(1, offsetFromScreenLeft + 120, 156, true)); buttonList.add(buttonPreviousPage = new NextPageButton(2, offsetFromScreenLeft + 38, 156, false)); } /** * Called from the main game loop to update the screen. */ @Override public void updateScreen() { buttonDone.visible = (currPage == bookTotalPages - 1); buttonNextPage.visible = (currPage < bookTotalPages - 1); buttonPreviousPage.visible = currPage > 0; } /** * Draws the screen and all the components in it. */ @Override public void drawScreen(int parWidth, int parHeight, float p_73863_3_) { GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); if (currPage == 0) { mc.getTextureManager().bindTexture(bookPageTextures[0]); } else { mc.getTextureManager().bindTexture(bookPageTextures[1]); } int offsetFromScreenLeft = (width - bookImageWidth) / 2; drawTexturedModalRect(offsetFromScreenLeft, 2, 0, 0, bookImageWidth, bookImageHeight); int widthOfString; String stringPageIndicator = I18n.format("book.pageIndicator", new Object[] { Integer.valueOf(currPage + 1), bookTotalPages }); widthOfString = fontRendererObj.getStringWidth(stringPageIndicator); fontRendererObj.drawString(stringPageIndicator, offsetFromScreenLeft - widthOfString + bookImageWidth - 44, 18, 0); fontRendererObj.drawSplitString(stringPageText[currPage], offsetFromScreenLeft + 36, 34, 116, 0); super.drawScreen(parWidth, parHeight, p_73863_3_); } /** * Called when a mouse button is pressed and the mouse is moved around. Parameters are : mouseX, mouseY, * lastButtonClicked & timeSinceMouseClick. */ @Override protected void mouseClickMove(int parMouseX, int parMouseY, int parLastButtonClicked, long parTimeSinceMouseClick) { } @Override protected void actionPerformed(GuiButton parButton) { if (parButton == buttonDone) { // DEBUG System.out.println("actionPerformed() buttonDone"); MagicBeans.network.sendToServer(new MessageGiveItemLeadToServer()); mc.displayGuiScreen((GuiScreen) null); } else if (parButton == buttonNextPage) { if (currPage < bookTotalPages - 1) { ++currPage; } } else if (parButton == buttonPreviousPage) { if (currPage > 0) { --currPage; } } } /** * Called when the screen is unloaded. Used to disable keyboard repeat events */ @Override public void onGuiClosed() { } /** * Returns true if this GUI should pause the game when it is displayed in single-player */ @Override public boolean doesGuiPauseGame() { return true; } @SideOnly(Side.CLIENT) static class NextPageButton extends GuiButton { private final boolean isNextButton; public NextPageButton(int parButtonId, int parPosX, int parPosY, boolean parIsNextButton) { super(parButtonId, parPosX, parPosY, 23, 13, ""); isNextButton = parIsNextButton; } /** * Draws this button to the screen. */ @Override public void drawButton(Minecraft mc, int parX, int parY) { if (visible) { boolean isButtonPressed = parX >= xPosition && parY >= yPosition && parX < xPosition + width && parY < yPosition + height; GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); mc.getTextureManager().bindTexture(bookPageTextures[0]); int textureX = 0; int textureY = 192; if (isButtonPressed) { textureX += 23; } if (!isNextButton) { textureY += 13; } drawTexturedModalRect(xPosition, yPosition, textureX, textureY, 23, 13); } } } }