com.sr2610.steampunkd.client.gui.handbook.GuiHandbook.java Source code

Java tutorial

Introduction

Here is the source code for com.sr2610.steampunkd.client.gui.handbook.GuiHandbook.java

Source

/**
 * This class was created by <SR2610>.  It is distributed as
 * part of the Steampunk'd Mod. Get the Source Code on Github:
 * https://github.com/SR2610/Steampunkd.
 *
 * Steampunk'd is Open Source and distributed under a Creative Commons
 * Attribution-NonCommercial-ShareAlike 3.0 License
 * (http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_GB)
 */
package com.sr2610.steampunkd.client.gui.handbook;

import java.util.List;

import org.lwjgl.opengl.GL11;

import com.sr2610.steampunkd.Reference;
import com.sr2610.steampunkd.client.gui.handbook.buttons.GuiInvisibleButton;
import com.sr2610.steampunkd.handbook.HandbookHandler;
import com.sr2610.steampunkd.handbook.core.HandbookCatagory;
import com.sr2610.steampunkd.items.ModItems;

import net.minecraft.client.gui.FontRenderer;
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;

@SideOnly(Side.CLIENT)
public class GuiHandbook extends GuiScreen {

    public static GuiHandbook currentHandbook = new GuiHandbook();

    public static final ResourceLocation texture = new ResourceLocation(Reference.MOD_ID,
            "textures/gui/handbook.png");
    protected int guiWidth = 146;
    protected int guiHeight = 180;
    protected int left;

    protected int top;

    @Override
    public void initGui() {
        super.initGui();
        currentHandbook = this;

        left = (width / 2) - (guiWidth / 2);
        top = (height / 2) - (guiHeight / 2);

        buttonList.clear();
        if (isIndexPage()) {
            final int x = 18;
            for (int i = 0; i < 12; i++) {
                int y = 20 + (i * 12);
                if (isMainPage())
                    y -= 35;
                buttonList.add(new GuiInvisibleButton(i, left + x, top + y, 110, 10, ""));
            }
            populateIndex();
        }

    }

    protected boolean isMainPage() {
        return true;
    }

    @Override
    public void drawScreen(int mouseX, int mouseY, float ticks) {
        GL11.glColor4f(1F, 1F, 1F, 1F);
        final boolean unicode = fontRendererObj.getUnicodeFlag();
        fontRendererObj.setUnicodeFlag(true);
        mc.renderEngine.bindTexture(texture);
        drawTexturedModalRect(left, top, 0, 0, guiWidth, guiHeight);
        drawHeader();
        fontRendererObj.setUnicodeFlag(unicode);
        super.drawScreen(mouseX, mouseY, ticks);
    }

    protected void drawHeader() {

        drawCenteredString(fontRendererObj, getTitle(), 0);

    }

    protected void drawCenteredString(FontRenderer fontRenderer, String string, int color) {
        fontRenderer.drawString(string, ((left - (fontRendererObj.getStringWidth(string) / 2)) + (guiWidth / 2)),
                top + 10, color);
    }

    @Override
    protected void actionPerformed(GuiButton button) {
        final int i = button.id - 3;
        if (i < 0)
            return;

        final List<HandbookCatagory> categoryList = HandbookHandler.getAllCategories();
        final HandbookCatagory category = i >= categoryList.size() ? null : categoryList.get(i);

        if (category != null)
            mc.displayGuiScreen(new GuiHandbookIndex(category));
    }

    @Override
    public boolean doesGuiPauseGame() {
        return false;
    }

    protected boolean isIndexPage() {
        return true;
    }

    protected String getTitle() {
        return I18n.format(ModItems.handbook.getUnlocalizedName() + ".name");
    }

    private void populateIndex() {
        final List<HandbookCatagory> categoryList = HandbookHandler.getAllCategories();
        for (int i = 3; i < 12; i++) {
            final int i_ = i - 3;
            final GuiInvisibleButton button = (GuiInvisibleButton) buttonList.get(i);
            final HandbookCatagory category = i_ >= categoryList.size() ? null : categoryList.get(i_);
            if (category != null)
                button.displayString = I18n.format(category.getUnlocalizedName());
            else
                button.displayString = "";
        }
    }

}