com.endreman0.souleater.client.gui.GuiInfoBook.java Source code

Java tutorial

Introduction

Here is the source code for com.endreman0.souleater.client.gui.GuiInfoBook.java

Source

package com.endreman0.souleater.client.gui;

import java.util.ArrayList;
import java.util.List;

import org.lwjgl.opengl.GL11;

import com.endreman0.souleater.LogHelper;
import com.endreman0.souleater.Utility;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;

public class GuiInfoBook extends GuiScreen {
    public static final ResourceLocation texture = new ResourceLocation(Utility.RESOURCE_PREFIX,
            "textures/gui/guiBookInfo.png");
    private int x1, y1, x2, y2, guiWidth, guiHeight;
    private double scale = 1.7;
    private GuiButton buttonPrev, buttonNext;
    private int page = 0;//One per two pages; hitting the "Next" button increases this by 1
    private int lengthPages = 10;//Number of "item.souleater:page#" entries in lang file
    private String[][] text = new String[lengthPages][20];

    public GuiInfoBook() {
        String[] pages = new String[lengthPages];
        for (int i = 0; i < pages.length; i++) {
            pages[i] = ' ' + StatCollector.translateToLocal(String.format("item.souleater:page%s", i)) + ' ';
            //For the wrapping to work, there must be spaces at the beginning and end of the string that represents the whole page, otherwise the first and last words will be cut off.
        }

        for (int j = 0; j < lengthPages; j++) {//For each page, break up the text into words, and group those words into lines.
            String thisPage = pages[j];
            List<String> words = new ArrayList<String>();
            int lastIndex = 0;
            for (int i = 0; i < thisPage.length(); i++) {
                if (thisPage.charAt(i) == ' ') {
                    words.add(thisPage.substring(lastIndex, i));
                    lastIndex = i + 1;
                }
            }
            text[j][0] = "";
            int line = 0;
            for (int i = 0; i < words.size(); i++) {
                if (text[j][line].length() < 30 - words.get(i).length()) {
                    text[j][line] += words.get(i) + ' ';
                } else {
                    line++;
                    text[j][line] = "";
                    i--;
                }
            }
        }
    }

    @Override
    public void initGui() {
        LogHelper.info(
                "The Medusa's Diary GUI is an edited version of the book \"Smelting and You\" from Tinkers' Construct, licensed under Creative Commons 3.(http://creativecommons.org/licenses/by/3.0/legalcode)");
        LogHelper.info(
                "They can be found on GitHub here: https://github.com/SlimeKnights/TinkersConstruct and MinecraftForum here: http://www.minecraftforum.net/forums/mapping-and-modding/minecraft-mods/1287648-tinkers-construct");
        LogHelper.info(
                "The picture itself is not on their GitHub page that I can find; the picture was cut from a screenshot. SlimeKnights do not endorse this edited picture, this mod or anything about it in any way.");
        //That info is required for the Creative Commons license TiC's art uses.
        x1 = (int) (width * 0.0625);
        y1 = (int) (height * 0.125);
        x2 = (int) (width * 0.9475);
        y2 = (int) (height * 0.875);
        guiWidth = x2 - x1;
        guiHeight = y2 - y1;

        buttonList.clear();
        buttonPrev = new GuiButton(0, x1 + 10, y2 - 10, 50, 20, "Prev");
        buttonNext = new GuiButton(1, x2 - 60, y2 - 10, 50, 20, "Next");//50 button width + 10 margin = 60
        buttonList.add(buttonPrev);
        buttonList.add(buttonNext);
    }

    @Override
    public void drawScreen(int mouseX, int mouseY, float f) {
        drawDefaultBackground();//Dark filter over world, like in inventory and options

        GL11.glPushMatrix();//This starts GL drawing.
        mc.renderEngine.bindTexture(texture);
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        GL11.glScaled(scale, scale, scale);
        drawTexturedModalRect((int) ((width / 2 - (128 * scale)) * 2 / 3), (int) (height / 2 - (67 * scale)), 0, 0,
                256, 135);
        GL11.glPopMatrix();//Pushing and popping prevents you having to undo all of the translates, rotates and scales

        String[] thisPage = text[2 * page];
        for (int i = 0; i < thisPage.length; i++) {
            fontRendererObj.drawString(thisPage[i],
                    x1 + guiWidth / 4 - fontRendererObj.getStringWidth(thisPage[i]) / 2,
                    (int) (height * 0.23 + fontRendererObj.FONT_HEIGHT * i), 0x000000, false);
        }
        if (2 * page < lengthPages - 1) {
            thisPage = text[2 * page + 1];
            for (int i = 0; i < thisPage.length; i++) {
                fontRendererObj.drawString(thisPage[i],
                        x1 + guiWidth * 3 / 4 - fontRendererObj.getStringWidth(thisPage[i]) / 2,
                        (int) (height * 0.23 + fontRendererObj.FONT_HEIGHT * i), 0x000000, false);
            }
        }

        super.drawScreen(mouseX, mouseY, f);//Draws buttons and labels
    }

    @Override
    public void mouseClicked(int mouseX, int mouseY, int button) {
        super.mouseClicked(mouseX, mouseY, button);
        switch (button) {
        case (0)://Left button
            if (buttonPrev.mousePressed(mc, mouseX, mouseY) && page > 0) {
                page--;
            }
            if (buttonNext.mousePressed(mc, mouseX, mouseY) && (2 * page + 1) < lengthPages - 1) {
                page++;
            }
            break;
        case (1)://Right click
            if (buttonPrev.mousePressed(mc, mouseX, mouseY)) {
                page = 0;
            }
            if (buttonNext.mousePressed(mc, mouseX, mouseY)) {
                page = (int) Math.floor((lengthPages - 1) / 2);
            } //Go to last even page
            break;
        case (2)://Middle click
            break;
        }
    }
}