| 1 | package net.minecraft.client.gui;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.Iterator;
|
| 5 | import java.util.List;
|
| 6 | import net.minecraft.client.Minecraft;
|
| 7 | import net.minecraft.client.renderer.GlStateManager;
|
| 8 | import net.minecraft.entity.player.EntityPlayer;
|
| 9 | import net.minecraft.util.ChatComponentText;
|
| 10 | import net.minecraft.util.IChatComponent;
|
| 11 | import net.minecraft.util.MathHelper;
|
| 12 | import org.apache.logging.log4j.LogManager;
|
| 13 | import org.apache.logging.log4j.Logger;
|
| 14 |
|
| 15 | public class GuiNewChat extends Gui
|
| 16 | {
|
| 17 | private static final Logger logger = LogManager.getLogger();
|
| 18 | private final Minecraft mc;
|
| 19 | private final List<String> sentMessages = Lists.<String>newArrayList();
|
| 20 | private final List<ChatLine> chatLines = Lists.<ChatLine>newArrayList();
|
| 21 | private final List<ChatLine> drawnChatLines = Lists.<ChatLine>newArrayList();
|
| 22 | private int scrollPos;
|
| 23 | private boolean isScrolled;
|
| 24 |
|
| 25 | public GuiNewChat(Minecraft mcIn)
|
| 26 | {
|
| 27 | this.mc = mcIn;
|
| 28 | }
|
| 29 |
|
| 30 | public void drawChat(int updateCounter)
|
| 31 | {
|
| 32 | if (this.mc.gameSettings.chatVisibility != EntityPlayer.EnumChatVisibility.HIDDEN)
|
| 33 | {
|
| 34 | int i = this.getLineCount();
|
| 35 | boolean flag = false;
|
| 36 | int j = 0;
|
| 37 | int k = this.drawnChatLines.size();
|
| 38 | float f = this.mc.gameSettings.chatOpacity * 0.9F + 0.1F;
|
| 39 |
|
| 40 | if (k > 0)
|
| 41 | {
|
| 42 | if (this.getChatOpen())
|
| 43 | {
|
| 44 | flag = true;
|
| 45 | }
|
| 46 |
|
| 47 | float f1 = this.getChatScale();
|
| 48 | int l = MathHelper.ceiling_float_int((float)this.getChatWidth() / f1);
|
| 49 | GlStateManager.pushMatrix();
|
| 50 | GlStateManager.translate(2.0F, 20.0F, 0.0F);
|
| 51 | GlStateManager.scale(f1, f1, 1.0F);
|
| 52 |
|
| 53 | for (int i1 = 0; i1 + this.scrollPos < this.drawnChatLines.size() && i1 < i; ++i1)
|
| 54 | {
|
| 55 | ChatLine chatline = (ChatLine)this.drawnChatLines.get(i1 + this.scrollPos);
|
| 56 |
|
| 57 | if (chatline != null)
|
| 58 | {
|
| 59 | int j1 = updateCounter - chatline.getUpdatedCounter();
|
| 60 |
|
| 61 | if (j1 < 200 || flag)
|
| 62 | {
|
| 63 | double d0 = (double)j1 / 200.0D;
|
| 64 | d0 = 1.0D - d0;
|
| 65 | d0 = d0 * 10.0D;
|
| 66 | d0 = MathHelper.clamp_double(d0, 0.0D, 1.0D);
|
| 67 | d0 = d0 * d0;
|
| 68 | int l1 = (int)(255.0D * d0);
|
| 69 |
|
| 70 | if (flag)
|
| 71 | {
|
| 72 | l1 = 255;
|
| 73 | }
|
| 74 |
|
| 75 | l1 = (int)((float)l1 * f);
|
| 76 | ++j;
|
| 77 |
|
| 78 | if (l1 > 3)
|
| 79 | {
|
| 80 | int i2 = 0;
|
| 81 | int j2 = -i1 * 9;
|
| 82 | drawRect(i2, j2 - 9, i2 + l + 4, j2, l1 / 2 << 24);
|
| 83 | String s = chatline.getChatComponent().getFormattedText();
|
| 84 | GlStateManager.enableBlend();
|
| 85 | this.mc.fontRendererObj.drawStringWithShadow(s, (float)i2, (float)(j2 - 8), 16777215 + (l1 << 24));
|
| 86 | GlStateManager.disableAlpha();
|
| 87 | GlStateManager.disableBlend();
|
| 88 | }
|
| 89 | }
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | if (flag)
|
| 94 | {
|
| 95 | int k2 = this.mc.fontRendererObj.FONT_HEIGHT;
|
| 96 | GlStateManager.translate(-3.0F, 0.0F, 0.0F);
|
| 97 | int l2 = k * k2 + k;
|
| 98 | int i3 = j * k2 + j;
|
| 99 | int j3 = this.scrollPos * i3 / k;
|
| 100 | int k1 = i3 * i3 / l2;
|
| 101 |
|
| 102 | if (l2 != i3)
|
| 103 | {
|
| 104 | int k3 = j3 > 0 ? 170 : 96;
|
| 105 | int l3 = this.isScrolled ? 13382451 : 3355562;
|
| 106 | drawRect(0, -j3, 2, -j3 - k1, l3 + (k3 << 24));
|
| 107 | drawRect(2, -j3, 1, -j3 - k1, 13421772 + (k3 << 24));
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | GlStateManager.popMatrix();
|
| 112 | }
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | /**
|
| 117 | * Clears the chat.
|
| 118 | */
|
| 119 | public void clearChatMessages()
|
| 120 | {
|
| 121 | this.drawnChatLines.clear();
|
| 122 | this.chatLines.clear();
|
| 123 | this.sentMessages.clear();
|
| 124 | }
|
| 125 |
|
| 126 | public void printChatMessage(IChatComponent chatComponent)
|
| 127 | {
|
| 128 | this.printChatMessageWithOptionalDeletion(chatComponent, 0);
|
| 129 | }
|
| 130 |
|
| 131 | /**
|
| 132 | * prints the ChatComponent to Chat. If the ID is not 0, deletes an existing Chat Line of that ID from the GUI
|
| 133 | */
|
| 134 | public void printChatMessageWithOptionalDeletion(IChatComponent chatComponent, int chatLineId)
|
| 135 | {
|
| 136 | this.setChatLine(chatComponent, chatLineId, this.mc.ingameGUI.getUpdateCounter(), false);
|
| 137 | logger.info("[CHAT] " + chatComponent.getUnformattedText());
|
| 138 | }
|
| 139 |
|
| 140 | private void setChatLine(IChatComponent chatComponent, int chatLineId, int updateCounter, boolean displayOnly)
|
| 141 | {
|
| 142 | if (chatLineId != 0)
|
| 143 | {
|
| 144 | this.deleteChatLine(chatLineId);
|
| 145 | }
|
| 146 |
|
| 147 | int i = MathHelper.floor_float((float)this.getChatWidth() / this.getChatScale());
|
| 148 | List<IChatComponent> list = GuiUtilRenderComponents.splitText(chatComponent, i, this.mc.fontRendererObj, false, false);
|
| 149 | boolean flag = this.getChatOpen();
|
| 150 |
|
| 151 | for (IChatComponent ichatcomponent : list)
|
| 152 | {
|
| 153 | if (flag && this.scrollPos > 0)
|
| 154 | {
|
| 155 | this.isScrolled = true;
|
| 156 | this.scroll(1);
|
| 157 | }
|
| 158 |
|
| 159 | this.drawnChatLines.add(0, new ChatLine(updateCounter, ichatcomponent, chatLineId));
|
| 160 | }
|
| 161 |
|
| 162 | while (this.drawnChatLines.size() > 100)
|
| 163 | {
|
| 164 | this.drawnChatLines.remove(this.drawnChatLines.size() - 1);
|
| 165 | }
|
| 166 |
|
| 167 | if (!displayOnly)
|
| 168 | {
|
| 169 | this.chatLines.add(0, new ChatLine(updateCounter, chatComponent, chatLineId));
|
| 170 |
|
| 171 | while (this.chatLines.size() > 100)
|
| 172 | {
|
| 173 | this.chatLines.remove(this.chatLines.size() - 1);
|
| 174 | }
|
| 175 | }
|
| 176 | }
|
| 177 |
|
| 178 | public void refreshChat()
|
| 179 | {
|
| 180 | this.drawnChatLines.clear();
|
| 181 | this.resetScroll();
|
| 182 |
|
| 183 | for (int i = this.chatLines.size() - 1; i >= 0; --i)
|
| 184 | {
|
| 185 | ChatLine chatline = (ChatLine)this.chatLines.get(i);
|
| 186 | this.setChatLine(chatline.getChatComponent(), chatline.getChatLineID(), chatline.getUpdatedCounter(), true);
|
| 187 | }
|
| 188 | }
|
| 189 |
|
| 190 | public List<String> getSentMessages()
|
| 191 | {
|
| 192 | return this.sentMessages;
|
| 193 | }
|
| 194 |
|
| 195 | /**
|
| 196 | * Adds this string to the list of sent messages, for recall using the up/down arrow keys
|
| 197 | *
|
| 198 | * @param message The message to add in the sendMessage List
|
| 199 | */
|
| 200 | public void addToSentMessages(String message)
|
| 201 | {
|
| 202 | if (this.sentMessages.isEmpty() || !((String)this.sentMessages.get(this.sentMessages.size() - 1)).equals(message))
|
| 203 | {
|
| 204 | this.sentMessages.add(message);
|
| 205 | }
|
| 206 | }
|
| 207 |
|
| 208 | /**
|
| 209 | * Resets the chat scroll (executed when the GUI is closed, among others)
|
| 210 | */
|
| 211 | public void resetScroll()
|
| 212 | {
|
| 213 | this.scrollPos = 0;
|
| 214 | this.isScrolled = false;
|
| 215 | }
|
| 216 |
|
| 217 | /**
|
| 218 | * Scrolls the chat by the given number of lines.
|
| 219 | *
|
| 220 | * @param amount The amount to scroll
|
| 221 | */
|
| 222 | public void scroll(int amount)
|
| 223 | {
|
| 224 | this.scrollPos += amount;
|
| 225 | int i = this.drawnChatLines.size();
|
| 226 |
|
| 227 | if (this.scrollPos > i - this.getLineCount())
|
| 228 | {
|
| 229 | this.scrollPos = i - this.getLineCount();
|
| 230 | }
|
| 231 |
|
| 232 | if (this.scrollPos <= 0)
|
| 233 | {
|
| 234 | this.scrollPos = 0;
|
| 235 | this.isScrolled = false;
|
| 236 | }
|
| 237 | }
|
| 238 |
|
| 239 | /**
|
| 240 | * Gets the chat component under the mouse
|
| 241 | *
|
| 242 | * @param mouseX The x position of the mouse
|
| 243 | * @param mouseY The y position of the mouse
|
| 244 | */
|
| 245 | public IChatComponent getChatComponent(int mouseX, int mouseY)
|
| 246 | {
|
| 247 | if (!this.getChatOpen())
|
| 248 | {
|
| 249 | return null;
|
| 250 | }
|
| 251 | else
|
| 252 | {
|
| 253 | ScaledResolution scaledresolution = new ScaledResolution(this.mc);
|
| 254 | int i = scaledresolution.getScaleFactor();
|
| 255 | float f = this.getChatScale();
|
| 256 | int j = mouseX / i - 3;
|
| 257 | int k = mouseY / i - 27;
|
| 258 | j = MathHelper.floor_float((float)j / f);
|
| 259 | k = MathHelper.floor_float((float)k / f);
|
| 260 |
|
| 261 | if (j >= 0 && k >= 0)
|
| 262 | {
|
| 263 | int l = Math.min(this.getLineCount(), this.drawnChatLines.size());
|
| 264 |
|
| 265 | if (j <= MathHelper.floor_float((float)this.getChatWidth() / this.getChatScale()) && k < this.mc.fontRendererObj.FONT_HEIGHT * l + l)
|
| 266 | {
|
| 267 | int i1 = k / this.mc.fontRendererObj.FONT_HEIGHT + this.scrollPos;
|
| 268 |
|
| 269 | if (i1 >= 0 && i1 < this.drawnChatLines.size())
|
| 270 | {
|
| 271 | ChatLine chatline = (ChatLine)this.drawnChatLines.get(i1);
|
| 272 | int j1 = 0;
|
| 273 |
|
| 274 | for (IChatComponent ichatcomponent : chatline.getChatComponent())
|
| 275 | {
|
| 276 | if (ichatcomponent instanceof ChatComponentText)
|
| 277 | {
|
| 278 | j1 += this.mc.fontRendererObj.getStringWidth(GuiUtilRenderComponents.func_178909_a(((ChatComponentText)ichatcomponent).getChatComponentText_TextValue(), false));
|
| 279 |
|
| 280 | if (j1 > j)
|
| 281 | {
|
| 282 | return ichatcomponent;
|
| 283 | }
|
| 284 | }
|
| 285 | }
|
| 286 | }
|
| 287 |
|
| 288 | return null;
|
| 289 | }
|
| 290 | else
|
| 291 | {
|
| 292 | return null;
|
| 293 | }
|
| 294 | }
|
| 295 | else
|
| 296 | {
|
| 297 | return null;
|
| 298 | }
|
| 299 | }
|
| 300 | }
|
| 301 |
|
| 302 | /**
|
| 303 | * Returns true if the chat GUI is open
|
| 304 | */
|
| 305 | public boolean getChatOpen()
|
| 306 | {
|
| 307 | return this.mc.currentScreen instanceof GuiChat;
|
| 308 | }
|
| 309 |
|
| 310 | /**
|
| 311 | * finds and deletes a Chat line by ID
|
| 312 | *
|
| 313 | * @param id The ChatLine's id to delete
|
| 314 | */
|
| 315 | public void deleteChatLine(int id)
|
| 316 | {
|
| 317 | Iterator<ChatLine> iterator = this.drawnChatLines.iterator();
|
| 318 |
|
| 319 | while (iterator.hasNext())
|
| 320 | {
|
| 321 | ChatLine chatline = (ChatLine)iterator.next();
|
| 322 |
|
| 323 | if (chatline.getChatLineID() == id)
|
| 324 | {
|
| 325 | iterator.remove();
|
| 326 | }
|
| 327 | }
|
| 328 |
|
| 329 | iterator = this.chatLines.iterator();
|
| 330 |
|
| 331 | while (iterator.hasNext())
|
| 332 | {
|
| 333 | ChatLine chatline1 = (ChatLine)iterator.next();
|
| 334 |
|
| 335 | if (chatline1.getChatLineID() == id)
|
| 336 | {
|
| 337 | iterator.remove();
|
| 338 | break;
|
| 339 | }
|
| 340 | }
|
| 341 | }
|
| 342 |
|
| 343 | public int getChatWidth()
|
| 344 | {
|
| 345 | return calculateChatboxWidth(this.mc.gameSettings.chatWidth);
|
| 346 | }
|
| 347 |
|
| 348 | public int getChatHeight()
|
| 349 | {
|
| 350 | return calculateChatboxHeight(this.getChatOpen() ? this.mc.gameSettings.chatHeightFocused : this.mc.gameSettings.chatHeightUnfocused);
|
| 351 | }
|
| 352 |
|
| 353 | /**
|
| 354 | * Returns the chatscale from mc.gameSettings.chatScale
|
| 355 | */
|
| 356 | public float getChatScale()
|
| 357 | {
|
| 358 | return this.mc.gameSettings.chatScale;
|
| 359 | }
|
| 360 |
|
| 361 | public static int calculateChatboxWidth(float scale)
|
| 362 | {
|
| 363 | int i = 320;
|
| 364 | int j = 40;
|
| 365 | return MathHelper.floor_float(scale * (float)(i - j) + (float)j);
|
| 366 | }
|
| 367 |
|
| 368 | public static int calculateChatboxHeight(float scale)
|
| 369 | {
|
| 370 | int i = 180;
|
| 371 | int j = 20;
|
| 372 | return MathHelper.floor_float(scale * (float)(i - j) + (float)j);
|
| 373 | }
|
| 374 |
|
| 375 | public int getLineCount()
|
| 376 | {
|
| 377 | return this.getChatHeight() / 9;
|
| 378 | }
|
| 379 | }
|
| 380 |
|