    001/*
    002 * The FML Forge Mod Loader suite.
    003 * Copyright (C) 2012 cpw
    004 *
    005 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
    006 * Software Foundation; either version 2.1 of the License, or any later version.
    007 *
    008 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
    009 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    010 *
    011 * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51
    012 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
    013 */
    014
    015package cpw.mods.fml.client;
    016
    017import java.awt.Dimension;
    018import java.util.ArrayList;
    019
    020import net.minecraft.client.Minecraft;
    021import net.minecraft.client.gui.FontRenderer;
    022import net.minecraft.client.gui.GuiButton;
    023import net.minecraft.client.gui.GuiScreen;
    024import net.minecraft.client.gui.GuiSmallButton;
    025import net.minecraft.client.renderer.Tessellator;
    026import net.minecraft.util.StringTranslate;
    027
    028import org.lwjgl.opengl.GL11;
    029
    030import com.google.common.base.Strings;
    031
    032import cpw.mods.fml.common.Loader;
    033import cpw.mods.fml.common.ModContainer;
    034
    035/**
    036 * @author cpw
    037 *
    038 */
    039public class GuiModList extends GuiScreen
    040{
    041    private GuiScreen mainMenu;
    042    private GuiSlotModList modList;
    043    private int selected = -1;
    044    private ModContainer selectedMod;
    045    private int listWidth;
    046    private ArrayList<ModContainer> mods;
    047
    048    /**
    049     * @param mainMenu
    050     */
    051    public GuiModList(GuiScreen mainMenu)
    052    {
    053        this.mainMenu=mainMenu;
    054        this.mods=new ArrayList<ModContainer>();
    055        FMLClientHandler.instance().addSpecialModEntries(mods);
    056        for (ModContainer mod : Loader.instance().getModList()) {
    057            if (mod.getMetadata()!=null && mod.getMetadata().parentMod==null && !Strings.isNullOrEmpty(mod.getMetadata().parent)) {
    058                String parentMod = mod.getMetadata().parent;
    059                ModContainer parentContainer = Loader.instance().getIndexedModList().get(parentMod);
    060                if (parentContainer != null)
    061                {
    062                    mod.getMetadata().parentMod = parentContainer;
    063                    parentContainer.getMetadata().childMods.add(mod);
    064                    continue;
    065                }
    066            }
    067            else if (mod.getMetadata()!=null && mod.getMetadata().parentMod!=null)
    068            {
    069                 continue;
    070            }
    071            mods.add(mod);
    072        }
    073    }
    074
    075    @Override
    076
    077    /**
    078     * Adds the buttons (and other controls) to the screen in question.
    079     */
    080    public void initGui()
    081    {
    082        for (ModContainer mod : mods) {
    083            listWidth=Math.max(listWidth,getFontRenderer().getStringWidth(mod.getName()) + 10);
    084            listWidth=Math.max(listWidth,getFontRenderer().getStringWidth(mod.getVersion()) + 10);
    085        }
    086        listWidth=Math.min(listWidth, 150);
    087        StringTranslate translations = StringTranslate.getInstance();
    088        this.buttonList.add(new GuiSmallButton(6, this.width / 2 - 75, this.height - 38, translations.translateKey("gui.done")));
    089        this.modList=new GuiSlotModList(this, mods, listWidth);
    090        this.modList.registerScrollButtons(this.buttonList, 7, 8);
    091    }
    092
    093    @Override
    094
    095    /**
    096     * Fired when a control is clicked. This is the equivalent of ActionListener.actionPerformed(ActionEvent e).
    097     */
    098    protected void actionPerformed(GuiButton button) {
    099        if (button.enabled)
    100        {
    101            switch (button.id)
    102            {
    103                case 6:
    104                    this.mc.displayGuiScreen(this.mainMenu);
    105                    return;
    106            }
    107        }
    108        super.actionPerformed(button);
    109    }
    110
    111    public int drawLine(String line, int offset, int shifty)
    112    {
    113        int r = this.fontRenderer.drawString(line, offset, shifty, 0xd7edea);
    114        return shifty + 10;
    115    }
    116
    117    @Override
    118
    119    /**
    120     * Draws the screen and all the components in it.
    121     */
    122    public void drawScreen(int p_571_1_, int p_571_2_, float p_571_3_)
    123    {
    124        this.modList.drawScreen(p_571_1_, p_571_2_, p_571_3_);
    125        this.drawCenteredString(this.fontRenderer, "Mod List", this.width / 2, 16, 0xFFFFFF);
    126        int offset = this.listWidth  + 20;
    127        if (selectedMod != null) {
    128            GL11.glEnable(GL11.GL_BLEND);
    129            if (!selectedMod.getMetadata().autogenerated) {
    130                int shifty = 35;
    131                String logoFile = selectedMod.getMetadata().logoFile;
    132                if (!logoFile.isEmpty())
    133                {
    134                    GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
    135                    this.mc.renderEngine.bindTexture(logoFile);
    136                    Dimension dim = TextureFXManager.instance().getTextureDimensions(logoFile);
    137                    double scaleX = dim.width / 200.0;
    138                    double scaleY = dim.height / 65.0;
    139                    double scale = 1.0;
    140                    if (scaleX > 1 || scaleY > 1)
    141                    {
    142                        scale = 1.0 / Math.max(scaleX, scaleY);
    143                    }
    144                    dim.width *= scale;
    145                    dim.height *= scale;
    146                    int top = 32;
    147                    Tessellator tess = Tessellator.instance;
    148                    tess.startDrawingQuads();
    149                    tess.addVertexWithUV(offset,             top + dim.height, zLevel, 0, 1);
    150                    tess.addVertexWithUV(offset + dim.width, top + dim.height, zLevel, 1, 1);
    151                    tess.addVertexWithUV(offset + dim.width, top,              zLevel, 1, 0);
    152                    tess.addVertexWithUV(offset,             top,              zLevel, 0, 0);
    153                    tess.draw();
    154
    155                    shifty += 65;
    156                }
    157                this.fontRenderer.drawStringWithShadow(selectedMod.getMetadata().name, offset, shifty, 0xFFFFFF);
    158                shifty += 12;
    159
    160                shifty = drawLine(String.format("Version: %s (%s)", selectedMod.getDisplayVersion(), selectedMod.getVersion()), offset, shifty);
    161                shifty = drawLine(String.format("Mod ID: '%s' Mod State: %s", selectedMod.getModId(), Loader.instance().getModState(selectedMod)), offset, shifty);
    162                if (!selectedMod.getMetadata().credits.isEmpty()) {
    163                   shifty = drawLine(String.format("Credits: %s", selectedMod.getMetadata().credits), offset, shifty);
    164                }
    165                shifty = drawLine(String.format("Authors: %s", selectedMod.getMetadata().getAuthorList()), offset, shifty);
    166                shifty = drawLine(String.format("URL: %s", selectedMod.getMetadata().url), offset, shifty);
    167                shifty = drawLine(selectedMod.getMetadata().childMods.isEmpty() ? "No child mods for this mod" : String.format("Child mods: %s", selectedMod.getMetadata().getChildModList()), offset, shifty);
    168                int rightSide = this.width - offset - 20;
    169                if (rightSide > 20)
    170                {
    171                    this.getFontRenderer().drawSplitString(selectedMod.getMetadata().description, offset, shifty + 10, rightSide, 0xDDDDDD);
    172                }
    173            } else {
    174                offset = ( this.listWidth + this.width ) / 2;
    175                this.drawCenteredString(this.fontRenderer, selectedMod.getName(), offset, 35, 0xFFFFFF);
    176                this.drawCenteredString(this.fontRenderer, String.format("Version: %s",selectedMod.getVersion()), offset, 45, 0xFFFFFF);
    177                this.drawCenteredString(this.fontRenderer, String.format("Mod State: %s",Loader.instance().getModState(selectedMod)), offset, 55, 0xFFFFFF);
    178                this.drawCenteredString(this.fontRenderer, "No mod information found", offset, 65, 0xDDDDDD);
    179                this.drawCenteredString(this.fontRenderer, "Ask your mod author to provide a mod mcmod.info file", offset, 75, 0xDDDDDD);
    180            }
    181            GL11.glDisable(GL11.GL_BLEND);
    182        }
    183        super.drawScreen(p_571_1_, p_571_2_, p_571_3_);
    184    }
    185
    186    Minecraft getMinecraftInstance() {
    187        return mc;
    188    }
    189
    190    FontRenderer getFontRenderer() {
    191        return fontRenderer;
    192    }
    193
    194    /**
    195     * @param var1
    196     */
    197    public void selectModIndex(int var1)
    198    {
    199        this.selected=var1;
    200        if (var1>=0 && var1<=mods.size()) {
    201            this.selectedMod=mods.get(selected);
    202        } else {
    203            this.selectedMod=null;
    204        }
    205    }
    206
    207    public boolean modIndexSelected(int var1)
    208    {
    209        return var1==selected;
    210    }
    211}