    001package net.minecraft.client.gui.inventory;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import net.minecraft.block.Block;
    006import net.minecraft.client.gui.GuiButton;
    007import net.minecraft.client.gui.GuiScreen;
    008import net.minecraft.client.multiplayer.NetClientHandler;
    009import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
    010import net.minecraft.network.packet.Packet130UpdateSign;
    011import net.minecraft.tileentity.TileEntitySign;
    012import net.minecraft.util.ChatAllowedCharacters;
    013import org.lwjgl.input.Keyboard;
    014import org.lwjgl.opengl.GL11;
    015
    016@SideOnly(Side.CLIENT)
    017public class GuiEditSign extends GuiScreen
    018{
    019    /**
    020     * This String is just a local copy of the characters allowed in text rendering of minecraft.
    021     */
    022    private static final String allowedCharacters = ChatAllowedCharacters.allowedCharacters;
    023
    024    /** The title string that is displayed in the top-center of the screen. */
    025    protected String screenTitle = "Edit sign message:";
    026
    027    /** Reference to the sign object. */
    028    private TileEntitySign entitySign;
    029
    030    /** Counts the number of screen updates. */
    031    private int updateCounter;
    032
    033    /** The number of the line that is being edited. */
    034    private int editLine = 0;
    035    private GuiButton field_100001_o;
    036
    037    public GuiEditSign(TileEntitySign par1TileEntitySign)
    038    {
    039        this.entitySign = par1TileEntitySign;
    040    }
    041
    042    /**
    043     * Adds the buttons (and other controls) to the screen in question.
    044     */
    045    public void initGui()
    046    {
    047        this.buttonList.clear();
    048        Keyboard.enableRepeatEvents(true);
    049        this.buttonList.add(this.field_100001_o = new GuiButton(0, this.width / 2 - 100, this.height / 4 + 120, "Done"));
    050        this.entitySign.setEditable(false);
    051    }
    052
    053    /**
    054     * Called when the screen is unloaded. Used to disable keyboard repeat events
    055     */
    056    public void onGuiClosed()
    057    {
    058        Keyboard.enableRepeatEvents(false);
    059        NetClientHandler netclienthandler = this.mc.getNetHandler();
    060
    061        if (netclienthandler != null)
    062        {
    063            netclienthandler.addToSendQueue(new Packet130UpdateSign(this.entitySign.xCoord, this.entitySign.yCoord, this.entitySign.zCoord, this.entitySign.signText));
    064        }
    065
    066        this.entitySign.setEditable(true);
    067    }
    068
    069    /**
    070     * Called from the main game loop to update the screen.
    071     */
    072    public void updateScreen()
    073    {
    074        ++this.updateCounter;
    075    }
    076
    077    /**
    078     * Fired when a control is clicked. This is the equivalent of ActionListener.actionPerformed(ActionEvent e).
    079     */
    080    protected void actionPerformed(GuiButton par1GuiButton)
    081    {
    082        if (par1GuiButton.enabled)
    083        {
    084            if (par1GuiButton.id == 0)
    085            {
    086                this.entitySign.onInventoryChanged();
    087                this.mc.displayGuiScreen((GuiScreen)null);
    088            }
    089        }
    090    }
    091
    092    /**
    093     * Fired when a key is typed. This is the equivalent of KeyListener.keyTyped(KeyEvent e).
    094     */
    095    protected void keyTyped(char par1, int par2)
    096    {
    097        if (par2 == 200)
    098        {
    099            this.editLine = this.editLine - 1 & 3;
    100        }
    101
    102        if (par2 == 208 || par2 == 28)
    103        {
    104            this.editLine = this.editLine + 1 & 3;
    105        }
    106
    107        if (par2 == 14 && this.entitySign.signText[this.editLine].length() > 0)
    108        {
    109            this.entitySign.signText[this.editLine] = this.entitySign.signText[this.editLine].substring(0, this.entitySign.signText[this.editLine].length() - 1);
    110        }
    111
    112        if (allowedCharacters.indexOf(par1) >= 0 && this.entitySign.signText[this.editLine].length() < 15)
    113        {
    114            this.entitySign.signText[this.editLine] = this.entitySign.signText[this.editLine] + par1;
    115        }
    116
    117        if (par2 == 1)
    118        {
    119            this.actionPerformed(this.field_100001_o);
    120        }
    121    }
    122
    123    /**
    124     * Draws the screen and all the components in it.
    125     */
    126    public void drawScreen(int par1, int par2, float par3)
    127    {
    128        this.drawDefaultBackground();
    129        this.drawCenteredString(this.fontRenderer, this.screenTitle, this.width / 2, 40, 16777215);
    130        GL11.glPushMatrix();
    131        GL11.glTranslatef((float)(this.width / 2), 0.0F, 50.0F);
    132        float f1 = 93.75F;
    133        GL11.glScalef(-f1, -f1, -f1);
    134        GL11.glRotatef(180.0F, 0.0F, 1.0F, 0.0F);
    135        Block block = this.entitySign.getBlockType();
    136
    137        if (block == Block.signPost)
    138        {
    139            float f2 = (float)(this.entitySign.getBlockMetadata() * 360) / 16.0F;
    140            GL11.glRotatef(f2, 0.0F, 1.0F, 0.0F);
    141            GL11.glTranslatef(0.0F, -1.0625F, 0.0F);
    142        }
    143        else
    144        {
    145            int k = this.entitySign.getBlockMetadata();
    146            float f3 = 0.0F;
    147
    148            if (k == 2)
    149            {
    150                f3 = 180.0F;
    151            }
    152
    153            if (k == 4)
    154            {
    155                f3 = 90.0F;
    156            }
    157
    158            if (k == 5)
    159            {
    160                f3 = -90.0F;
    161            }
    162
    163            GL11.glRotatef(f3, 0.0F, 1.0F, 0.0F);
    164            GL11.glTranslatef(0.0F, -1.0625F, 0.0F);
    165        }
    166
    167        if (this.updateCounter / 6 % 2 == 0)
    168        {
    169            this.entitySign.lineBeingEdited = this.editLine;
    170        }
    171
    172        TileEntityRenderer.instance.renderTileEntityAt(this.entitySign, -0.5D, -0.75D, -0.5D, 0.0F);
    173        this.entitySign.lineBeingEdited = -1;
    174        GL11.glPopMatrix();
    175        super.drawScreen(par1, par2, par3);
    176    }
    177}