Skip to content

Commit

Permalink
Add message text to detail screen
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Jul 27, 2023
1 parent cb663f0 commit 69b29c5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void useSubScreen(int subScreen) {
Minecraft minecraft = Minecraft.getInstance();
this.prevSubScreen = this.subScreen;
this.subScreen = subScreen;
if (prevSubScreen != 3) {
if (prevSubScreen != 3 || commentList.isEmpty()) {
switch (subScreen) {
case 0 -> {
commentList.clear();
Expand Down Expand Up @@ -137,6 +137,13 @@ public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partia
bufferBuilder.vertex(matrix4f, x2, y1, 0).uv(1, 0).endVertex();
BufferUploader.drawWithShader(bufferBuilder.end());
}
WidgetCommentEntry widget = getWidget(comment);
widget.showImage = false;
int imgAreaWidth = width - 100 - 20 - 10;
widget.setBounds(100 + 10 + imgAreaWidth - (imgAreaWidth / 2), 0, imgAreaWidth / 2);
widget.setBounds(100 + 10 + imgAreaWidth - (imgAreaWidth / 2), height - 20 - widget.getHeight(),
imgAreaWidth / 2);
widget.render(guiGraphics, mouseX, mouseY, partialTick);
} else {
graphicsBlit9(guiGraphics, 100, 30, width - 120, height - 50,
176, 40, 20, 20, 256, 256,
Expand All @@ -147,6 +154,7 @@ public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partia
for (int i = commentListOffset; i < commentList.size(); i++) {
CommentEntry comment = commentList.get(i);
WidgetCommentEntry widget = getWidget(comment);
widget.showImage = true;
widget.setBounds(106, yOffset, width - 102 - 22 - 8 - 4 - 16);
widget.render(guiGraphics, mouseX, mouseY, partialTick);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public class CommentTypeButton extends Button implements IGuiCommon {

private static final int[] COMMENT_TYPE_COLOR = {
public static final int[] COMMENT_TYPE_COLOR = {
0xFF8BC34A, 0xFFCDDC39, 0xFFFFEB3B, 0xFFFF9800,
0xFF607D8B, 0xFFFFC107, 0xFF03A9F4, 0xFF009888
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cn.zbx1425.worldcomment.gui;

import cn.zbx1425.worldcomment.Main;
import cn.zbx1425.worldcomment.data.CommentEntry;
import cn.zbx1425.worldcomment.data.network.ImageDownload;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
Expand All @@ -13,19 +13,23 @@
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.network.chat.Style;
import org.joml.Matrix4f;

import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Locale;
import java.util.Optional;

public class WidgetCommentEntry extends AbstractWidget implements IGuiCommon {

private final CommentEntry comment;
private final Font font;

public boolean showImage = true;

public static final int TOP_SINK = 12;

public WidgetCommentEntry(CommentEntry comment) {
Expand All @@ -47,7 +51,7 @@ private void calculateHeight() {
int textHeight = 26
+ (comment.message.isEmpty() ? 0 : font.wordWrapHeight(comment.message, picWidth * 2 - 8))
+ 4;
int picHeight = 20 + (comment.image.url.isEmpty() ? 0 : (picWidth * 9 / 16)) + 4;
int picHeight = 20 + ((comment.image.url.isEmpty() || !showImage) ? 0 : (picWidth * 9 / 16)) + 4;
height = Math.max(Math.max(textHeight, picHeight), 28 + 4);
}

Expand All @@ -65,7 +69,7 @@ guiGraphics, getX(), getY(), getWidth(), getHeight(),
picWidth * 2 - 8, 0xFF444444);
}

if (!comment.image.url.isEmpty()) {
if (!comment.image.url.isEmpty() && showImage) {
int picWidth = (width - 16) / 3;
int picHeight = picWidth * 9 / 16;
String imageUrl = comment.image.thumbUrl.isEmpty() ? comment.image.url : comment.image.thumbUrl;
Expand All @@ -83,19 +87,32 @@ guiGraphics, getX(), getY(), getWidth(), getHeight(),
BufferUploader.drawWithShader(bufferBuilder.end());
}

guiGraphics.drawString(font,
comment.initiatorName.isEmpty() ? Component.translatable("gui.worldcomment.anonymous")
: Component.literal(comment.initiatorName),
getX() + 34, getY() + 8, 0xFFFFFFFF, true
);
String timeStr = DateTimeFormatter.ofPattern("MM-dd HH:mm", Locale.ROOT)
.format(Instant.ofEpochMilli(comment.timestamp).atZone(ZoneId.systemDefault()).toLocalDateTime());
guiGraphics.drawString(font, timeStr,
getX() + getWidth() - 6 - font.width(timeStr), getY() + 8, 0xFFBBBBBB, true);
Component nameComponent = comment.initiatorName.isEmpty() ? Component.translatable("gui.worldcomment.anonymous")
: Component.literal(comment.initiatorName);
guiGraphics.drawString(font, nameComponent,
getX() + 34, getY() + 8, 0xFFFFFFFF, true);

if (showImage) {
String timeStr = DateTimeFormatter.ofPattern("MM-dd HH:mm", Locale.ROOT)
.format(Instant.ofEpochMilli(comment.timestamp).atZone(ZoneId.systemDefault()).toLocalDateTime());
guiGraphics.drawString(font, timeStr,
getX() + getWidth() - 6 - font.width(timeStr), getY() + 8, 0xFFBBBBBB, true);
}

RenderSystem.enableBlend();
guiGraphics.blit(ATLAS_LOCATION, getX() + 6, getY() + 2, 18, 18,
((comment.messageType - 1) % 4) * 64, (int)((comment.messageType - 1) / 4) * 64 + 128, 64, 64, 256, 256);

if (mouseX > getX() + 4 && mouseX < getX() + getWidth() && mouseY > getY() && mouseY < getY() + 24) {
guiGraphics.renderTooltip(font, List.of(
Component.translatable("gui.worldcomment.comment_type." + comment.messageType)
.setStyle(Style.EMPTY.withBold(true).withColor(CommentTypeButton.COMMENT_TYPE_COLOR[comment.messageType - 1] & 0xFFFFFF))
.append(Component.literal(" (" + comment.location.toShortString() + ")").setStyle(Style.EMPTY.withBold(false).withColor(ChatFormatting.WHITE))),
Component.literal(" " + Instant.ofEpochMilli(comment.timestamp).atZone(ZoneId.systemDefault())
.toLocalDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)).withStyle(Style.EMPTY.withColor(ChatFormatting.GRAY)),
Component.literal(" " + nameComponent.getString() + " ..." + comment.initiator.toString().substring(24)).withStyle(Style.EMPTY.withColor(ChatFormatting.GRAY))
), Optional.empty(), mouseX, mouseY);
}
}

@Override
Expand Down

0 comments on commit 69b29c5

Please sign in to comment.