Skip to content

Commit

Permalink
handle multiline wrapped component correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Jan 4, 2025
1 parent 439e43f commit 0c9c888
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/api/java/mcp/mobius/waila/api/component/WrappedComponent.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mcp.mobius.waila.api.component;

import java.util.List;
import java.util.Objects;

import mcp.mobius.waila.api.ITooltipComponent;
Expand All @@ -9,7 +10,10 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.locale.Language;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
import net.minecraft.util.FormattedCharSequence;

/**
* Component that renders a vanilla {@link Component}.
Expand All @@ -26,20 +30,34 @@ public WrappedComponent(Component component) {
}

public final Component component;
private List<FormattedCharSequence> lines;
private int height;

@Override
public int getWidth() {
return getFont().width(component);
var font = getFont();
var split = font.getSplitter().splitLines(component, Integer.MAX_VALUE, Style.EMPTY);
lines = Language.getInstance().getVisualOrder(split);

var width = lines.stream().mapToInt(font::width).max().orElse(0);
height = font.lineHeight * split.size();

return width;
}

@Override
public int getHeight() {
return getFont().lineHeight;
return height;
}

@Override
public void render(GuiGraphics ctx, int x, int y, DeltaTracker delta) {
ctx.drawString(getFont(), component, x, y, IApiService.INSTANCE.getFontColor());
var font = getFont();

for (var line : lines) {
ctx.drawString(font, line, x, y, IApiService.INSTANCE.getFontColor());
y += font.lineHeight;
}
}

private Font getFont() {
Expand Down

0 comments on commit 0c9c888

Please sign in to comment.