    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 var1 = StringTranslate.getInstance();
    037        Keyboard.enableRepeatEvents(true);
    038        this.controlList.clear();
    039        this.controlList.add(new GuiButton(0, this.width / 2 - 100, this.height / 4 + 96 + 12, var1.translateKey("selectWorld.renameButton")));
    040        this.controlList.add(new GuiButton(1, this.width / 2 - 100, this.height / 4 + 120 + 12, var1.translateKey("gui.cancel")));
    041        ISaveFormat var2 = this.mc.getSaveLoader();
    042        WorldInfo var3 = var2.getWorldInfo(this.worldName);
    043        String var4 = var3.getWorldName();
    044        this.theGuiTextField = new GuiTextField(this.fontRenderer, this.width / 2 - 100, 60, 200, 20);
    045        this.theGuiTextField.setFocused(true);
    046        this.theGuiTextField.setText(var4);
    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 var2 = this.mc.getSaveLoader();
    071                var2.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.controlList.get(0)).enabled = this.theGuiTextField.getText().trim().length() > 0;
    084
    085        if (par1 == 13)
    086        {
    087            this.actionPerformed((GuiButton)this.controlList.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 var4 = StringTranslate.getInstance();
    106        this.drawDefaultBackground();
    107        this.drawCenteredString(this.fontRenderer, var4.translateKey("selectWorld.renameTitle"), this.width / 2, this.height / 4 - 60 + 20, 16777215);
    108        this.drawString(this.fontRenderer, var4.translateKey("selectWorld.enterName"), this.width / 2 - 100, 47, 10526880);
    109        this.theGuiTextField.drawTextBox();
    110        super.drawScreen(par1, par2, par3);
    111    }
    112}