From c5171de132afaab8cb4e5c94178cc6ae7831bda6 Mon Sep 17 00:00:00 2001 From: Swedz Date: Wed, 25 Sep 2024 23:15:49 -0400 Subject: [PATCH] Allow line wrapping in modular multiblock guis (#21) --- .../ModularMultiblockGuiClient.java | 14 ++++++++++++-- .../ModularMultiblockGuiLine.java | 11 +++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/swedz/tesseract/neoforge/compat/mi/guicomponent/modularmultiblock/ModularMultiblockGuiClient.java b/src/main/java/net/swedz/tesseract/neoforge/compat/mi/guicomponent/modularmultiblock/ModularMultiblockGuiClient.java index 72bf14d2..ce3071b0 100644 --- a/src/main/java/net/swedz/tesseract/neoforge/compat/mi/guicomponent/modularmultiblock/ModularMultiblockGuiClient.java +++ b/src/main/java/net/swedz/tesseract/neoforge/compat/mi/guicomponent/modularmultiblock/ModularMultiblockGuiClient.java @@ -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; @@ -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 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++; + } } } diff --git a/src/main/java/net/swedz/tesseract/neoforge/compat/mi/guicomponent/modularmultiblock/ModularMultiblockGuiLine.java b/src/main/java/net/swedz/tesseract/neoforge/compat/mi/guicomponent/modularmultiblock/ModularMultiblockGuiLine.java index 87722e8d..e2414325 100644 --- a/src/main/java/net/swedz/tesseract/neoforge/compat/mi/guicomponent/modularmultiblock/ModularMultiblockGuiLine.java +++ b/src/main/java/net/swedz/tesseract/neoforge/compat/mi/guicomponent/modularmultiblock/ModularMultiblockGuiLine.java @@ -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); @@ -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()); } }