Skip to content

Commit

Permalink
Add cache to line wraps
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Jul 27, 2023
1 parent a11f02a commit 0ba5ab2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.locale.Language;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.network.chat.Style;
import net.minecraft.util.FormattedCharSequence;
import org.joml.Matrix4f;

import java.time.Instant;
Expand All @@ -28,6 +30,8 @@ public class WidgetCommentEntry extends AbstractWidget implements IGuiCommon {
private final CommentEntry comment;
private final Font font;

private List<FormattedCharSequence> wrappedText = List.of();

public boolean showImage = true;

public static final int TOP_SINK = 12;
Expand All @@ -49,8 +53,10 @@ public void setBounds(int x, int y, int width) {
private void calculateHeight() {
int picWidth = (comment.image.url.isEmpty() || !showImage) ? 0 : ((width - 20) / 3);
int textWidth = width - 20 - picWidth - (picWidth > 0 ? 4 : 0);
wrappedText = Language.getInstance().getVisualOrder(
font.getSplitter().splitLines(comment.message, textWidth, Style.EMPTY));
int textHeight = 26
+ (comment.message.isEmpty() ? 0 : font.wordWrapHeight(comment.message, textWidth))
+ (comment.message.isEmpty() ? 0 : 9 * wrappedText.size())
+ 4;
int picHeight = 20 + ((comment.image.url.isEmpty() || !showImage) ? 0 : (picWidth * 9 / 16)) + 4 + 4;
height = Math.max(Math.max(textHeight, picHeight), 28 + 4);
Expand All @@ -65,12 +71,14 @@ guiGraphics, getX(), getY(), getWidth(), getHeight(),
);

int picWidth = (comment.image.url.isEmpty() || !showImage) ? 0 : ((width - 20) / 3);
int textWidth = width - 20 - picWidth - (picWidth > 0 ? 4 : 0);
int picHeight = ((comment.image.url.isEmpty() || !showImage) ? 0 : (picWidth * 9 / 16)) + 4;

if (!comment.message.isEmpty()) {
guiGraphics.drawWordWrap(font, FormattedText.of(comment.message), getX() + 16, getY() + 26,
textWidth, 0xFF444444);
int lineY = getY() + 26;
for (FormattedCharSequence formattedCharSequence : wrappedText) {
guiGraphics.drawString(font, formattedCharSequence, getX() + 16, lineY, 0xFF444444, false);
lineY += font.lineHeight;
}
}

if (!comment.image.url.isEmpty() && showImage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,49 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

public class CommentOverlayRenderer {

private static final Map<CommentEntry, WidgetCommentEntry> widgets = new WeakHashMap<>();
private static final List<CommentEntry> cachedComments = new ArrayList<>();
private static final List<WidgetCommentEntry> cachedWidgets = new ArrayList<>();
private static int cachedWidth = 0;

private static WidgetCommentEntry getWidget(CommentEntry entry) {
return widgets.computeIfAbsent(entry, WidgetCommentEntry::new);
private static void calculateLayout(int width) {
cachedComments.clear();
cachedComments.addAll(ClientRayPicking.pickedComments);
cachedWidgets.clear();
int yOffset = 0;
for (CommentEntry comment : cachedComments) {
WidgetCommentEntry widget = new WidgetCommentEntry(comment);
widget.setBounds(width / 2 + 10, yOffset, width / 2 - 30);
yOffset += widget.getHeight() + 10;
cachedWidgets.add(widget);
}
}

public static void render(GuiGraphics guiGraphics) {
int pickedCommentsSize = ClientRayPicking.pickedComments.size();
if (pickedCommentsSize > 0) {
int[] yOffsets = new int[pickedCommentsSize];
for (int i = 0; i < pickedCommentsSize; i++) {
CommentEntry comment = ClientRayPicking.pickedComments.get(i);
WidgetCommentEntry widget = getWidget(comment);
widget.setBounds(
guiGraphics.guiWidth() / 2 + 10,
yOffsets[i],
guiGraphics.guiWidth() / 2 - 30
);
if (i < pickedCommentsSize - 1) {
yOffsets[i + 1] = yOffsets[i] + widget.getHeight() + 10;
}
}
if (cachedWidth != guiGraphics.guiWidth() || !ClientRayPicking.pickedComments.equals(cachedComments)) {
calculateLayout(guiGraphics.guiWidth());
cachedWidth = guiGraphics.guiWidth();
}
if (cachedComments.size() > 0) {
guiGraphics.pose().pushPose();
int baseYOffset = guiGraphics.guiHeight() / 2
- (yOffsets[ClientRayPicking.overlayOffset] + WidgetCommentEntry.TOP_SINK);
for (int i = 0; i < pickedCommentsSize; i++) {
CommentEntry comment = ClientRayPicking.pickedComments.get(i);
WidgetCommentEntry widget = getWidget(comment);
widget.setBounds(
guiGraphics.guiWidth() / 2 + 10,
yOffsets[i] + baseYOffset,
guiGraphics.guiWidth() / 2 - 30
);
if (widget.getY() + widget.getHeight() > 0 && widget.getY() < guiGraphics.guiHeight()) {
- (cachedWidgets.get(ClientRayPicking.overlayOffset).getY() + WidgetCommentEntry.TOP_SINK);
guiGraphics.pose().translate(0, baseYOffset, 0);
for (WidgetCommentEntry widget : cachedWidgets) {
if (widget.getY() + baseYOffset + widget.getHeight() > 0
&& widget.getY() + baseYOffset < guiGraphics.guiHeight()) {
widget.render(guiGraphics, 0, 0, 0);
}
}
if (pickedCommentsSize > 1) {
String pageStr = String.format("↕ %d / %d", ClientRayPicking.overlayOffset + 1, pickedCommentsSize);
guiGraphics.pose().popPose();
if (cachedComments.size() > 1) {
String pageStr = String.format("↕ %d / %d", ClientRayPicking.overlayOffset + 1, cachedComments.size());
guiGraphics.drawString(Minecraft.getInstance().font, pageStr,
guiGraphics.guiWidth() / 2 - 10 - Minecraft.getInstance().font.width(pageStr),
guiGraphics.guiHeight() / 2 - 8 / 2, 0xFFA5D6A7, true);
Expand Down

0 comments on commit 0ba5ab2

Please sign in to comment.