    001package net.minecraft.client.gui;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import net.minecraft.util.StringTranslate;
    006import net.minecraft.world.storage.ISaveFormat;
    007import net.minecraft.world.storage.WorldInfo;
    008import org.lwjgl.input.Keyboard;
    009
    010@SideOnly(Side.CLIENT)
    011public class GuiRenameWorld extends GuiScreen
    012{
    013    private GuiScreen parentGuiScreen;
    014    private GuiTextField theGuiTextField;
    015    private final String worldName;
    016
    017    public GuiRenameWorld(GuiScreen par1GuiScreen, String par2Str)
    018    {
    019        this.parentGuiScreen = par1GuiScreen;
    020        this.worldName = par2Str;
    021    }
    022
    023    /**
    024     * Called from the main game loop to update the screen.
    025     */
    026    public void updateScreen()
    027    {
    028        this.theGuiTextField.updateCursorCounter();
    029    }
    030
    031    /**
    032     * Adds the buttons (and other controls) to the screen in question.
    033     */
    034    public void initGui()
    035    {
    036        StringTranslate stringtranslate = StringTranslate.getInstance();
    037        Keyboard.enableRepeatEvents(true);
    038        this.buttonList.clear();
    039        this.buttonList.add(new GuiButton(0, this.width / 2 - 100, this.height / 4 + 96 + 12, stringtranslate.translateKey("selectWorld.renameButton")));
    040        this.buttonList.add(new GuiButton(1, this.width / 2 - 100, this.height / 4 + 120 + 12, stringtranslate.translateKey("gui.cancel")));
    041        ISaveFormat isaveformat = this.mc.getSaveLoader();
    042        WorldInfo worldinfo = isaveformat.getWorldInfo(this.worldName);
    043        String s = worldinfo.getWorldName();
    044        this.theGuiTextField = new GuiTextField(this.fontRenderer, this.width / 2 - 100, 60, 200, 20);
    045        this.theGuiTextField.setFocused(true);
    046        this.theGuiTextField.setText(s);
    047    }
    048
    049    /**
    050     * Called when the screen is unloaded. Used to disable keyboard repeat events
    051     */
    052    public void onGuiClosed()
    053    {
    054        Keyboard.enableRepeatEvents(false);
    055    }
    056
    057    /**
    058     * Fired when a control is clicked. This is the equivalent of ActionListener.actionPerformed(ActionEvent e).
    059     */
    060    protected void actionPerformed(GuiButton par1GuiButton)
    061    {
    062        if (par1GuiButton.enabled)
    063        {
    064            if (par1GuiButton.id == 1)
    065            {
    066                this.mc.displayGuiScreen(this.parentGuiScreen);
    067            }
    068            else if (par1GuiButton.id == 0)
    069            {
    070                ISaveFormat isaveformat = this.mc.getSaveLoader();
    071                isaveformat.renameWorld(this.worldName, this.theGuiTextField.getText().trim());
    072                this.mc.displayGuiScreen(this.parentGuiScreen);
    073            }
    074        }
    075    }
    076
    077    /**
    078     * Fired when a key is typed. This is the equivalent of KeyListener.keyTyped(KeyEvent e).
    079     */
    080    protected void keyTyped(char par1, int par2)
    081    {
    082        this.theGuiTextField.textboxKeyTyped(par1, par2);
    083        ((GuiButton)this.buttonList.get(0)).enabled = this.theGuiTextField.getText().trim().length() > 0;
    084
    085        if (par1 == 13)
    086        {
    087            this.actionPerformed((GuiButton)this.buttonList.get(0));
    088        }
    089    }
    090
    091    /**
    092     * Called when the mouse is clicked.
    093     */
    094    protected void mouseClicked(int par1, int par2, int par3)
    095    {
    096        super.mouseClicked(par1, par2, par3);
    097        this.theGuiTextField.mouseClicked(par1, par2, par3);
    098    }
    099
    100    /**
    101     * Draws the screen and all the components in it.
    102     */
    103    public void drawScreen(int par1, int par2, float par3)
    104    {
    105        StringTranslate stringtranslate = StringTranslate.getInstance();
    106        this.drawDefaultBackground();
    107        this.drawCenteredString(this.fontRenderer, stringtranslate.translateKey("selectWorld.renameTitle"), this.width / 2, this.height / 4 - 60 + 20, 16777215);
    108        this.drawString(this.fontRenderer, stringtranslate.translateKey("selectWorld.enterName"), this.width / 2 - 100, 47, 10526880);
    109        this.theGuiTextField.drawTextBox();
    110        super.drawScreen(par1, par2, par3);
    111    }
    112}