Skip to content

Commit

Permalink
Allow line wrapping in modular multiblock guis (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
Swedz authored Sep 26, 2024
1 parent 3f469e1 commit c5171de
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.FormattedCharSequence;

import java.util.List;

Expand Down Expand Up @@ -86,11 +87,20 @@ private void renderInfoText(GuiGraphics graphics, int x, int y)
Minecraft minecraftClient = Minecraft.getInstance();
Font font = minecraftClient.font;

int spaceWidth = font.width(" ");
int offsetY = 23;
for(ModularMultiblockGuiLine line : text)
{
graphics.drawString(font, line.text(), x + ModularMultiblockGui.X + 5, y + ModularMultiblockGuiClient.this.y + offsetY, line.color(), false);
offsetY += 11;
List<FormattedCharSequence> wrappedLines = line.wrap() ?
font.split(line.text(), TEXTURE_WIDTH - spaceWidth) :
List.of(line.text().getVisualOrderText());
int index = 0;
for(FormattedCharSequence wrappedLine : wrappedLines)
{
graphics.drawString(font, wrappedLine, x + ModularMultiblockGui.X + 5 + (index > 0 ? spaceWidth : 0), y + ModularMultiblockGuiClient.this.y + offsetY, line.color(), false);
offsetY += 11;
index++;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentSerialization;

public record ModularMultiblockGuiLine(Component text, int color)
public record ModularMultiblockGuiLine(Component text, int color, boolean wrap)
{
public ModularMultiblockGuiLine(Component text, int color)
{
this(text, color, false);
}

public ModularMultiblockGuiLine(Component text)
{
this(text, 0xFFFFFF);
Expand All @@ -15,12 +20,14 @@ public static ModularMultiblockGuiLine read(RegistryFriendlyByteBuf buf)
{
Component text = ComponentSerialization.STREAM_CODEC.decode(buf);
int color = buf.readInt();
return new ModularMultiblockGuiLine(text, color);
boolean wrap = buf.readBoolean();
return new ModularMultiblockGuiLine(text, color, wrap);
}

public static void write(RegistryFriendlyByteBuf buf, ModularMultiblockGuiLine line)
{
ComponentSerialization.STREAM_CODEC.encode(buf, line.text());
buf.writeInt(line.color());
buf.writeBoolean(line.wrap());
}
}

0 comments on commit c5171de

Please sign in to comment.