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