diff options
author | Juuz <6596629+Juuxel@users.noreply.github.com> | 2023-10-15 15:47:02 +0300 |
---|---|---|
committer | Juuz <6596629+Juuxel@users.noreply.github.com> | 2023-10-15 15:48:20 +0300 |
commit | bc2fb79b4ea5db644ef4e64641239893f349f579 (patch) | |
tree | 49fa68ae847063f0e3fa6e44b6892ad85247b14b /src/main | |
parent | e4473aef5fa7e2f0f389ebafafa966aaaf491353 (diff) | |
download | LibGui-bc2fb79b4ea5db644ef4e64641239893f349f579.tar.gz LibGui-bc2fb79b4ea5db644ef4e64641239893f349f579.tar.bz2 LibGui-bc2fb79b4ea5db644ef4e64641239893f349f579.zip |
Fix WLabel/WText.getTextStyleAt with non-left-top alignments
Diffstat (limited to 'src/main')
3 files changed, 56 insertions, 17 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/TextAlignment.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/TextAlignment.java new file mode 100644 index 0000000..0c005eb --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/TextAlignment.java @@ -0,0 +1,46 @@ +package io.github.cottonmc.cotton.gui.impl.client; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.font.TextRenderer; +import net.minecraft.text.OrderedText; + +import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; +import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment; + +public final class TextAlignment { + public static int getTextOffsetX(HorizontalAlignment alignment, int width, OrderedText text) { + return switch (alignment) { + case LEFT -> 0; + + case CENTER -> { + TextRenderer renderer = MinecraftClient.getInstance().textRenderer; + int textWidth = renderer.getWidth(text); + yield width / 2 - textWidth / 2; + } + + case RIGHT -> { + TextRenderer renderer = MinecraftClient.getInstance().textRenderer; + int textWidth = renderer.getWidth(text); + yield width - textWidth; + } + }; + } + + public static int getTextOffsetY(VerticalAlignment alignment, int height, int lines) { + return switch (alignment) { + case TOP -> 0; + + case CENTER -> { + TextRenderer renderer = MinecraftClient.getInstance().textRenderer; + int textHeight = renderer.fontHeight * lines; + yield height / 2 - textHeight / 2; + } + + case BOTTOM -> { + TextRenderer renderer = MinecraftClient.getInstance().textRenderer; + int textHeight = renderer.fontHeight * lines; + yield height - textHeight; + } + }; + } +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java index be0b1e4..143435e 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java @@ -3,7 +3,6 @@ package io.github.cottonmc.cotton.gui.widget; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; -import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; @@ -13,6 +12,7 @@ import net.minecraft.text.Text; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.impl.client.LibGuiConfig; +import io.github.cottonmc.cotton.gui.impl.client.TextAlignment; import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; import io.github.cottonmc.cotton.gui.widget.data.InputResult; import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment; @@ -63,13 +63,7 @@ public class WLabel extends WWidget { @Environment(EnvType.CLIENT) @Override public void paint(DrawContext context, int x, int y, int mouseX, int mouseY) { - MinecraftClient mc = MinecraftClient.getInstance(); - TextRenderer renderer = mc.textRenderer; - int yOffset = switch (verticalAlignment) { - case CENTER -> height / 2 - renderer.fontHeight / 2; - case BOTTOM -> height - renderer.fontHeight; - case TOP -> 0; - }; + int yOffset = TextAlignment.getTextOffsetY(verticalAlignment, height, 1); ScreenDrawing.drawString(context, text.asOrderedText(), horizontalAlignment, x, y + yOffset, this.getWidth(), shouldRenderInDarkMode() ? darkmodeColor : color); @@ -102,7 +96,8 @@ public class WLabel extends WWidget { @Nullable public Style getTextStyleAt(int x, int y) { if (isWithinBounds(x, y)) { - return MinecraftClient.getInstance().textRenderer.getTextHandler().getStyleAt(text, x); + int xOffset = TextAlignment.getTextOffsetX(horizontalAlignment, width, text.asOrderedText()); + return MinecraftClient.getInstance().textRenderer.getTextHandler().getStyleAt(text, x - xOffset); } return null; } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java index 2b4ad31..b2a44eb 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java @@ -12,6 +12,7 @@ import net.minecraft.text.Style; import net.minecraft.text.Text; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; +import io.github.cottonmc.cotton.gui.impl.client.TextAlignment; import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; import io.github.cottonmc.cotton.gui.widget.data.InputResult; import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment; @@ -73,11 +74,13 @@ public class WText extends WWidget { @Nullable public Style getTextStyleAt(int x, int y) { TextRenderer font = MinecraftClient.getInstance().textRenderer; - int lineIndex = y / font.fontHeight; + int yOffset = TextAlignment.getTextOffsetY(verticalAlignment, height, wrappedLines.size()); + int lineIndex = (y - yOffset) / font.fontHeight; if (lineIndex >= 0 && lineIndex < wrappedLines.size()) { OrderedText line = wrappedLines.get(lineIndex); - return font.getTextHandler().getStyleAt(line, x); + int xOffset = TextAlignment.getTextOffsetX(horizontalAlignment, width, line); + return font.getTextHandler().getStyleAt(line, x - xOffset); } return null; @@ -92,12 +95,7 @@ public class WText extends WWidget { } TextRenderer font = MinecraftClient.getInstance().textRenderer; - - int yOffset = switch (verticalAlignment) { - case CENTER -> height / 2 - font.fontHeight * wrappedLines.size() / 2; - case BOTTOM -> height - font.fontHeight * wrappedLines.size(); - case TOP -> 0; - }; + int yOffset = TextAlignment.getTextOffsetY(verticalAlignment, height, wrappedLines.size()); for (int i = 0; i < wrappedLines.size(); i++) { OrderedText line = wrappedLines.get(i); |