001package net.minecraft.client.gui;
002
003import cpw.mods.fml.relauncher.Side;
004import cpw.mods.fml.relauncher.SideOnly;
005import java.util.ArrayList;
006import java.util.Iterator;
007import java.util.List;
008import net.minecraft.client.Minecraft;
009import net.minecraft.util.MathHelper;
010import net.minecraft.util.StringTranslate;
011import net.minecraft.util.StringUtils;
012import org.lwjgl.opengl.GL11;
013
014@SideOnly(Side.CLIENT)
015public class GuiNewChat extends Gui
016{
017    /** The Minecraft instance. */
018    private final Minecraft mc;
019
020    /** A list of messages previously sent through the chat GUI */
021    private final List sentMessages = new ArrayList();
022
023    /** Chat lines to be displayed in the chat box */
024    private final List chatLines = new ArrayList();
025    private final List field_96134_d = new ArrayList();
026    private int field_73768_d = 0;
027    private boolean field_73769_e = false;
028
029    public GuiNewChat(Minecraft par1Minecraft)
030    {
031        this.mc = par1Minecraft;
032    }
033
034    public void drawChat(int par1)
035    {
036        if (this.mc.gameSettings.chatVisibility != 2)
037        {
038            int j = this.func_96127_i();
039            boolean flag = false;
040            int k = 0;
041            int l = this.field_96134_d.size();
042            float f = this.mc.gameSettings.chatOpacity * 0.9F + 0.1F;
043
044            if (l > 0)
045            {
046                if (this.getChatOpen())
047                {
048                    flag = true;
049                }
050
051                float f1 = this.func_96131_h();
052                int i1 = MathHelper.ceiling_float_int((float)this.func_96126_f() / f1);
053                GL11.glPushMatrix();
054                GL11.glTranslatef(2.0F, 20.0F, 0.0F);
055                GL11.glScalef(f1, f1, 1.0F);
056                int j1;
057                int k1;
058                int l1;
059
060                for (j1 = 0; j1 + this.field_73768_d < this.field_96134_d.size() && j1 < j; ++j1)
061                {
062                    ChatLine chatline = (ChatLine)this.field_96134_d.get(j1 + this.field_73768_d);
063
064                    if (chatline != null)
065                    {
066                        k1 = par1 - chatline.getUpdatedCounter();
067
068                        if (k1 < 200 || flag)
069                        {
070                            double d0 = (double)k1 / 200.0D;
071                            d0 = 1.0D - d0;
072                            d0 *= 10.0D;
073
074                            if (d0 < 0.0D)
075                            {
076                                d0 = 0.0D;
077                            }
078
079                            if (d0 > 1.0D)
080                            {
081                                d0 = 1.0D;
082                            }
083
084                            d0 *= d0;
085                            l1 = (int)(255.0D * d0);
086
087                            if (flag)
088                            {
089                                l1 = 255;
090                            }
091
092                            l1 = (int)((float)l1 * f);
093                            ++k;
094
095                            if (l1 > 3)
096                            {
097                                byte b0 = 0;
098                                int i2 = -j1 * 9;
099                                drawRect(b0, i2 - 9, b0 + i1 + 4, i2, l1 / 2 << 24);
100                                GL11.glEnable(GL11.GL_BLEND);
101                                String s = chatline.getChatLineString();
102
103                                if (!this.mc.gameSettings.chatColours)
104                                {
105                                    s = StringUtils.stripControlCodes(s);
106                                }
107
108                                this.mc.fontRenderer.drawStringWithShadow(s, b0, i2 - 8, 16777215 + (l1 << 24));
109                            }
110                        }
111                    }
112                }
113
114                if (flag)
115                {
116                    j1 = this.mc.fontRenderer.FONT_HEIGHT;
117                    GL11.glTranslatef(-3.0F, 0.0F, 0.0F);
118                    int j2 = l * j1 + l;
119                    k1 = k * j1 + k;
120                    int k2 = this.field_73768_d * k1 / l;
121                    int l2 = k1 * k1 / j2;
122
123                    if (j2 != k1)
124                    {
125                        l1 = k2 > 0 ? 170 : 96;
126                        int i3 = this.field_73769_e ? 13382451 : 3355562;
127                        drawRect(0, -k2, 2, -k2 - l2, i3 + (l1 << 24));
128                        drawRect(2, -k2, 1, -k2 - l2, 13421772 + (l1 << 24));
129                    }
130                }
131
132                GL11.glPopMatrix();
133            }
134        }
135    }
136
137    /**
138     * Clears the chat.
139     */
140    public void clearChatMessages()
141    {
142        this.field_96134_d.clear();
143        this.chatLines.clear();
144        this.sentMessages.clear();
145    }
146
147    /**
148     * takes a String and prints it to chat
149     */
150    public void printChatMessage(String par1Str)
151    {
152        this.printChatMessageWithOptionalDeletion(par1Str, 0);
153    }
154
155    /**
156     * prints the String to Chat. If the ID is not 0, deletes an existing Chat Line of that ID from the GUI
157     */
158    public void printChatMessageWithOptionalDeletion(String par1Str, int par2)
159    {
160        this.func_96129_a(par1Str, par2, this.mc.ingameGUI.getUpdateCounter(), false);
161        this.mc.func_98033_al().func_98233_a("[CHAT] " + par1Str);
162    }
163
164    private void func_96129_a(String par1Str, int par2, int par3, boolean par4)
165    {
166        boolean flag1 = this.getChatOpen();
167        boolean flag2 = true;
168
169        if (par2 != 0)
170        {
171            this.deleteChatLine(par2);
172        }
173
174        Iterator iterator = this.mc.fontRenderer.listFormattedStringToWidth(par1Str, MathHelper.floor_float((float)this.func_96126_f() / this.func_96131_h())).iterator();
175
176        while (iterator.hasNext())
177        {
178            String s1 = (String)iterator.next();
179
180            if (flag1 && this.field_73768_d > 0)
181            {
182                this.field_73769_e = true;
183                this.scroll(1);
184            }
185
186            if (!flag2)
187            {
188                s1 = " " + s1;
189            }
190
191            flag2 = false;
192            this.field_96134_d.add(0, new ChatLine(par3, s1, par2));
193        }
194
195        while (this.field_96134_d.size() > 100)
196        {
197            this.field_96134_d.remove(this.field_96134_d.size() - 1);
198        }
199
200        if (!par4)
201        {
202            this.chatLines.add(0, new ChatLine(par3, par1Str.trim(), par2));
203
204            while (this.chatLines.size() > 100)
205            {
206                this.chatLines.remove(this.chatLines.size() - 1);
207            }
208        }
209    }
210
211    public void func_96132_b()
212    {
213        this.field_96134_d.clear();
214        this.resetScroll();
215
216        for (int i = this.chatLines.size() - 1; i >= 0; --i)
217        {
218            ChatLine chatline = (ChatLine)this.chatLines.get(i);
219            this.func_96129_a(chatline.getChatLineString(), chatline.getChatLineID(), chatline.getUpdatedCounter(), true);
220        }
221    }
222
223    /**
224     * Gets the list of messages previously sent through the chat GUI
225     */
226    public List getSentMessages()
227    {
228        return this.sentMessages;
229    }
230
231    /**
232     * Adds this string to the list of sent messages, for recall using the up/down arrow keys
233     */
234    public void addToSentMessages(String par1Str)
235    {
236        if (this.sentMessages.isEmpty() || !((String)this.sentMessages.get(this.sentMessages.size() - 1)).equals(par1Str))
237        {
238            this.sentMessages.add(par1Str);
239        }
240    }
241
242    /**
243     * Resets the chat scroll (executed when the GUI is closed)
244     */
245    public void resetScroll()
246    {
247        this.field_73768_d = 0;
248        this.field_73769_e = false;
249    }
250
251    /**
252     * Scrolls the chat by the given number of lines.
253     */
254    public void scroll(int par1)
255    {
256        this.field_73768_d += par1;
257        int j = this.field_96134_d.size();
258
259        if (this.field_73768_d > j - this.func_96127_i())
260        {
261            this.field_73768_d = j - this.func_96127_i();
262        }
263
264        if (this.field_73768_d <= 0)
265        {
266            this.field_73768_d = 0;
267            this.field_73769_e = false;
268        }
269    }
270
271    public ChatClickData func_73766_a(int par1, int par2)
272    {
273        if (!this.getChatOpen())
274        {
275            return null;
276        }
277        else
278        {
279            ScaledResolution scaledresolution = new ScaledResolution(this.mc.gameSettings, this.mc.displayWidth, this.mc.displayHeight);
280            int k = scaledresolution.getScaleFactor();
281            float f = this.func_96131_h();
282            int l = par1 / k - 3;
283            int i1 = par2 / k - 25;
284            l = MathHelper.floor_float((float)l / f);
285            i1 = MathHelper.floor_float((float)i1 / f);
286
287            if (l >= 0 && i1 >= 0)
288            {
289                int j1 = Math.min(this.func_96127_i(), this.field_96134_d.size());
290
291                if (l <= MathHelper.floor_float((float)this.func_96126_f() / this.func_96131_h()) && i1 < this.mc.fontRenderer.FONT_HEIGHT * j1 + j1)
292                {
293                    int k1 = i1 / (this.mc.fontRenderer.FONT_HEIGHT + 1) + this.field_73768_d;
294                    return new ChatClickData(this.mc.fontRenderer, (ChatLine)this.field_96134_d.get(k1), l, i1 - (k1 - this.field_73768_d) * this.mc.fontRenderer.FONT_HEIGHT + k1);
295                }
296                else
297                {
298                    return null;
299                }
300            }
301            else
302            {
303                return null;
304            }
305        }
306    }
307
308    /**
309     * Adds a message to the chat after translating to the client's locale.
310     */
311    public void addTranslatedMessage(String par1Str, Object ... par2ArrayOfObj)
312    {
313        this.printChatMessage(StringTranslate.getInstance().translateKeyFormat(par1Str, par2ArrayOfObj));
314    }
315
316    /**
317     * @return {@code true} if the chat GUI is open
318     */
319    public boolean getChatOpen()
320    {
321        return this.mc.currentScreen instanceof GuiChat;
322    }
323
324    /**
325     * finds and deletes a Chat line by ID
326     */
327    public void deleteChatLine(int par1)
328    {
329        Iterator iterator = this.field_96134_d.iterator();
330        ChatLine chatline;
331
332        do
333        {
334            if (!iterator.hasNext())
335            {
336                iterator = this.chatLines.iterator();
337
338                do
339                {
340                    if (!iterator.hasNext())
341                    {
342                        return;
343                    }
344
345                    chatline = (ChatLine)iterator.next();
346                }
347                while (chatline.getChatLineID() != par1);
348
349                iterator.remove();
350                return;
351            }
352
353            chatline = (ChatLine)iterator.next();
354        }
355        while (chatline.getChatLineID() != par1);
356
357        iterator.remove();
358    }
359
360    public int func_96126_f()
361    {
362        return func_96128_a(this.mc.gameSettings.field_96692_F);
363    }
364
365    public int func_96133_g()
366    {
367        return func_96130_b(this.getChatOpen() ? this.mc.gameSettings.field_96694_H : this.mc.gameSettings.field_96693_G);
368    }
369
370    public float func_96131_h()
371    {
372        return this.mc.gameSettings.field_96691_E;
373    }
374
375    public static final int func_96128_a(float par0)
376    {
377        short short1 = 320;
378        byte b0 = 40;
379        return MathHelper.floor_float(par0 * (float)(short1 - b0) + (float)b0);
380    }
381
382    public static final int func_96130_b(float par0)
383    {
384        short short1 = 180;
385        byte b0 = 20;
386        return MathHelper.floor_float(par0 * (float)(short1 - b0) + (float)b0);
387    }
388
389    public int func_96127_i()
390    {
391        return this.func_96133_g() / 9;
392    }
393}