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
036    /** "Done" button for the GUI. */
037    private GuiButton doneBtn;
038
039    public GuiEditSign(TileEntitySign par1TileEntitySign)
040    {
041        this.entitySign = par1TileEntitySign;
042    }
043
044    /**
045     * Adds the buttons (and other controls) to the screen in question.
046     */
047    public void initGui()
048    {
049        this.buttonList.clear();
050        Keyboard.enableRepeatEvents(true);
051        this.buttonList.add(this.doneBtn = new GuiButton(0, this.width / 2 - 100, this.height / 4 + 120, "Done"));
052        this.entitySign.setEditable(false);
053    }
054
055    /**
056     * Called when the screen is unloaded. Used to disable keyboard repeat events
057     */
058    public void onGuiClosed()
059    {
060        Keyboard.enableRepeatEvents(false);
061        NetClientHandler netclienthandler = this.mc.getNetHandler();
062
063        if (netclienthandler != null)
064        {
065            netclienthandler.addToSendQueue(new Packet130UpdateSign(this.entitySign.xCoord, this.entitySign.yCoord, this.entitySign.zCoord, this.entitySign.signText));
066        }
067
068        this.entitySign.setEditable(true);
069    }
070
071    /**
072     * Called from the main game loop to update the screen.
073     */
074    public void updateScreen()
075    {
076        ++this.updateCounter;
077    }
078
079    /**
080     * Fired when a control is clicked. This is the equivalent of ActionListener.actionPerformed(ActionEvent e).
081     */
082    protected void actionPerformed(GuiButton par1GuiButton)
083    {
084        if (par1GuiButton.enabled)
085        {
086            if (par1GuiButton.id == 0)
087            {
088                this.entitySign.onInventoryChanged();
089                this.mc.displayGuiScreen((GuiScreen)null);
090            }
091        }
092    }
093
094    /**
095     * Fired when a key is typed. This is the equivalent of KeyListener.keyTyped(KeyEvent e).
096     */
097    protected void keyTyped(char par1, int par2)
098    {
099        if (par2 == 200)
100        {
101            this.editLine = this.editLine - 1 & 3;
102        }
103
104        if (par2 == 208 || par2 == 28)
105        {
106            this.editLine = this.editLine + 1 & 3;
107        }
108
109        if (par2 == 14 && this.entitySign.signText[this.editLine].length() > 0)
110        {
111            this.entitySign.signText[this.editLine] = this.entitySign.signText[this.editLine].substring(0, this.entitySign.signText[this.editLine].length() - 1);
112        }
113
114        if (allowedCharacters.indexOf(par1) >= 0 && this.entitySign.signText[this.editLine].length() < 15)
115        {
116            this.entitySign.signText[this.editLine] = this.entitySign.signText[this.editLine] + par1;
117        }
118
119        if (par2 == 1)
120        {
121            this.actionPerformed(this.doneBtn);
122        }
123    }
124
125    /**
126     * Draws the screen and all the components in it.
127     */
128    public void drawScreen(int par1, int par2, float par3)
129    {
130        this.drawDefaultBackground();
131        this.drawCenteredString(this.fontRenderer, this.screenTitle, this.width / 2, 40, 16777215);
132        GL11.glPushMatrix();
133        GL11.glTranslatef((float)(this.width / 2), 0.0F, 50.0F);
134        float f1 = 93.75F;
135        GL11.glScalef(-f1, -f1, -f1);
136        GL11.glRotatef(180.0F, 0.0F, 1.0F, 0.0F);
137        Block block = this.entitySign.getBlockType();
138
139        if (block == Block.signPost)
140        {
141            float f2 = (float)(this.entitySign.getBlockMetadata() * 360) / 16.0F;
142            GL11.glRotatef(f2, 0.0F, 1.0F, 0.0F);
143            GL11.glTranslatef(0.0F, -1.0625F, 0.0F);
144        }
145        else
146        {
147            int k = this.entitySign.getBlockMetadata();
148            float f3 = 0.0F;
149
150            if (k == 2)
151            {
152                f3 = 180.0F;
153            }
154
155            if (k == 4)
156            {
157                f3 = 90.0F;
158            }
159
160            if (k == 5)
161            {
162                f3 = -90.0F;
163            }
164
165            GL11.glRotatef(f3, 0.0F, 1.0F, 0.0F);
166            GL11.glTranslatef(0.0F, -1.0625F, 0.0F);
167        }
168
169        if (this.updateCounter / 6 % 2 == 0)
170        {
171            this.entitySign.lineBeingEdited = this.editLine;
172        }
173
174        TileEntityRenderer.instance.renderTileEntityAt(this.entitySign, -0.5D, -0.75D, -0.5D, 0.0F);
175        this.entitySign.lineBeingEdited = -1;
176        GL11.glPopMatrix();
177        super.drawScreen(par1, par2, par3);
178    }
179}