    001    package net.minecraft.client.gui;
    002
    003    import cpw.mods.fml.relauncher.Side;
    004    import cpw.mods.fml.relauncher.SideOnly;
    005    import java.util.ArrayList;
    006    import java.util.Iterator;
    007    import java.util.List;
    008    import net.minecraft.client.Minecraft;
    009    import net.minecraft.util.StringTranslate;
    010    import net.minecraft.util.StringUtils;
    011    import org.lwjgl.opengl.GL11;
    012
    013    @SideOnly(Side.CLIENT)
    014    public class GuiNewChat extends Gui
    015    {
    016        /** The Minecraft instance. */
    017        private final Minecraft mc;
    018
    019        /** A list of messages previously sent through the chat GUI */
    020        private final List sentMessages = new ArrayList();
    021
    022        /** Chat lines to be displayed in the chat box */
    023        private final List chatLines = new ArrayList();
    024        private int field_73768_d = 0;
    025        private boolean field_73769_e = false;
    026
    027        public GuiNewChat(Minecraft par1Minecraft)
    028        {
    029            this.mc = par1Minecraft;
    030        }
    031
    032        public void drawChat(int par1)
    033        {
    034            if (this.mc.gameSettings.chatVisibility != 2)
    035            {
    036                byte var2 = 10;
    037                boolean var3 = false;
    038                int var4 = 0;
    039                int var5 = this.chatLines.size();
    040                float var6 = this.mc.gameSettings.chatOpacity * 0.9F + 0.1F;
    041
    042                if (var5 > 0)
    043                {
    044                    if (this.getChatOpen())
    045                    {
    046                        var2 = 20;
    047                        var3 = true;
    048                    }
    049
    050                    int var7;
    051                    int var9;
    052                    int var12;
    053
    054                    for (var7 = 0; var7 + this.field_73768_d < this.chatLines.size() && var7 < var2; ++var7)
    055                    {
    056                        ChatLine var8 = (ChatLine)this.chatLines.get(var7 + this.field_73768_d);
    057
    058                        if (var8 != null)
    059                        {
    060                            var9 = par1 - var8.getUpdatedCounter();
    061
    062                            if (var9 < 200 || var3)
    063                            {
    064                                double var10 = (double)var9 / 200.0D;
    065                                var10 = 1.0D - var10;
    066                                var10 *= 10.0D;
    067
    068                                if (var10 < 0.0D)
    069                                {
    070                                    var10 = 0.0D;
    071                                }
    072
    073                                if (var10 > 1.0D)
    074                                {
    075                                    var10 = 1.0D;
    076                                }
    077
    078                                var10 *= var10;
    079                                var12 = (int)(255.0D * var10);
    080
    081                                if (var3)
    082                                {
    083                                    var12 = 255;
    084                                }
    085
    086                                var12 = (int)((float)var12 * var6);
    087                                ++var4;
    088
    089                                if (var12 > 3)
    090                                {
    091                                    byte var13 = 3;
    092                                    int var14 = -var7 * 9;
    093                                    drawRect(var13, var14 - 1, var13 + 320 + 4, var14 + 8, var12 / 2 << 24);
    094                                    GL11.glEnable(GL11.GL_BLEND);
    095                                    String var15 = var8.getChatLineString();
    096
    097                                    if (!this.mc.gameSettings.chatColours)
    098                                    {
    099                                        var15 = StringUtils.stripControlCodes(var15);
    100                                    }
    101
    102                                    this.mc.fontRenderer.drawStringWithShadow(var15, var13, var14, 16777215 + (var12 << 24));
    103                                }
    104                            }
    105                        }
    106                    }
    107
    108                    if (var3)
    109                    {
    110                        var7 = this.mc.fontRenderer.FONT_HEIGHT;
    111                        GL11.glTranslatef(0.0F, (float)var7, 0.0F);
    112                        int var16 = var5 * var7 + var5;
    113                        var9 = var4 * var7 + var4;
    114                        int var17 = this.field_73768_d * var9 / var5;
    115                        int var11 = var9 * var9 / var16;
    116
    117                        if (var16 != var9)
    118                        {
    119                            var12 = var17 > 0 ? 170 : 96;
    120                            int var18 = this.field_73769_e ? 13382451 : 3355562;
    121                            drawRect(0, -var17, 2, -var17 - var11, var18 + (var12 << 24));
    122                            drawRect(2, -var17, 1, -var17 - var11, 13421772 + (var12 << 24));
    123                        }
    124                    }
    125                }
    126            }
    127        }
    128
    129        public void func_73761_a()
    130        {
    131            this.chatLines.clear();
    132            this.sentMessages.clear();
    133        }
    134
    135        /**
    136         * takes a String and prints it to chat
    137         */
    138        public void printChatMessage(String par1Str)
    139        {
    140            this.printChatMessageWithOptionalDeletion(par1Str, 0);
    141        }
    142
    143        /**
    144         * prints the String to Chat. If the ID is not 0, deletes an existing Chat Line of that ID from the GUI
    145         */
    146        public void printChatMessageWithOptionalDeletion(String par1Str, int par2)
    147        {
    148            boolean var3 = this.getChatOpen();
    149            boolean var4 = true;
    150
    151            if (par2 != 0)
    152            {
    153                this.deleteChatLine(par2);
    154            }
    155
    156            Iterator var5 = this.mc.fontRenderer.listFormattedStringToWidth(par1Str, 320).iterator();
    157
    158            while (var5.hasNext())
    159            {
    160                String var6 = (String)var5.next();
    161
    162                if (var3 && this.field_73768_d > 0)
    163                {
    164                    this.field_73769_e = true;
    165                    this.scroll(1);
    166                }
    167
    168                if (!var4)
    169                {
    170                    var6 = " " + var6;
    171                }
    172
    173                var4 = false;
    174                this.chatLines.add(0, new ChatLine(this.mc.ingameGUI.getUpdateCounter(), var6, par2));
    175            }
    176
    177            while (this.chatLines.size() > 100)
    178            {
    179                this.chatLines.remove(this.chatLines.size() - 1);
    180            }
    181        }
    182
    183        /**
    184         * Gets the list of messages previously sent through the chat GUI
    185         */
    186        public List getSentMessages()
    187        {
    188            return this.sentMessages;
    189        }
    190
    191        /**
    192         * Adds this string to the list of sent messages, for recall using the up/down arrow keys
    193         */
    194        public void addToSentMessages(String par1Str)
    195        {
    196            if (this.sentMessages.isEmpty() || !((String)this.sentMessages.get(this.sentMessages.size() - 1)).equals(par1Str))
    197            {
    198                this.sentMessages.add(par1Str);
    199            }
    200        }
    201
    202        /**
    203         * Resets the chat scroll (executed when the GUI is closed)
    204         */
    205        public void resetScroll()
    206        {
    207            this.field_73768_d = 0;
    208            this.field_73769_e = false;
    209        }
    210
    211        /**
    212         * Scrolls the chat by the given number of lines.
    213         */
    214        public void scroll(int par1)
    215        {
    216            this.field_73768_d += par1;
    217            int var2 = this.chatLines.size();
    218
    219            if (this.field_73768_d > var2 - 20)
    220            {
    221                this.field_73768_d = var2 - 20;
    222            }
    223
    224            if (this.field_73768_d <= 0)
    225            {
    226                this.field_73768_d = 0;
    227                this.field_73769_e = false;
    228            }
    229        }
    230
    231        public ChatClickData func_73766_a(int par1, int par2)
    232        {
    233            if (!this.getChatOpen())
    234            {
    235                return null;
    236            }
    237            else
    238            {
    239                ScaledResolution var3 = new ScaledResolution(this.mc.gameSettings, this.mc.displayWidth, this.mc.displayHeight);
    240                int var4 = var3.getScaleFactor();
    241                int var5 = par1 / var4 - 3;
    242                int var6 = par2 / var4 - 40;
    243
    244                if (var5 >= 0 && var6 >= 0)
    245                {
    246                    int var7 = Math.min(20, this.chatLines.size());
    247
    248                    if (var5 <= 320 && var6 < this.mc.fontRenderer.FONT_HEIGHT * var7 + var7)
    249                    {
    250                        int var8 = var6 / (this.mc.fontRenderer.FONT_HEIGHT + 1) + this.field_73768_d;
    251                        return new ChatClickData(this.mc.fontRenderer, (ChatLine)this.chatLines.get(var8), var5, var6 - (var8 - this.field_73768_d) * this.mc.fontRenderer.FONT_HEIGHT + var8);
    252                    }
    253                    else
    254                    {
    255                        return null;
    256                    }
    257                }
    258                else
    259                {
    260                    return null;
    261                }
    262            }
    263        }
    264
    265        /**
    266         * Adds a message to the chat after translating to the client's locale.
    267         */
    268        public void addTranslatedMessage(String par1Str, Object ... par2ArrayOfObj)
    269        {
    270            this.printChatMessage(StringTranslate.getInstance().translateKeyFormat(par1Str, par2ArrayOfObj));
    271        }
    272
    273        /**
    274         * @return {@code true} if the chat GUI is open
    275         */
    276        public boolean getChatOpen()
    277        {
    278            return this.mc.currentScreen instanceof GuiChat;
    279        }
    280
    281        /**
    282         * finds and deletes a Chat line by ID
    283         */
    284        public void deleteChatLine(int par1)
    285        {
    286            Iterator var2 = this.chatLines.iterator();
    287            ChatLine var3;
    288
    289            do
    290            {
    291                if (!var2.hasNext())
    292                {
    293                    return;
    294                }
    295
    296                var3 = (ChatLine)var2.next();
    297            }
    298            while (var3.getChatLineID() != par1);
    299
    300            var2.remove();
    301        }
    302    }