diff options
author | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-03-10 19:56:31 +0200 |
---|---|---|
committer | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-03-10 19:56:31 +0200 |
commit | ef2d9351241cdc082f281961a6a5544afdecf4fe (patch) | |
tree | 89e8b50e49b4547e69a2aee8105a1ecb64befdbb /src/main/java/io | |
parent | 3e98462c5b86b488ff73ccf25874a464d31e61f4 (diff) | |
download | LibGui-ef2d9351241cdc082f281961a6a5544afdecf4fe.tar.gz LibGui-ef2d9351241cdc082f281961a6a5544afdecf4fe.tar.bz2 LibGui-ef2d9351241cdc082f281961a6a5544afdecf4fe.zip |
Add a multiline text widget
Closes #36.
Diffstat (limited to 'src/main/java/io')
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java | 7 | ||||
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java | 133 |
2 files changed, 140 insertions, 0 deletions
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 43cd3a9..ce4b921 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 @@ -14,6 +14,9 @@ import net.minecraft.text.Text; import javax.annotation.Nullable; +/** + * A single-line label widget. + */ public class WLabel extends WWidget { protected Text text; protected Alignment alignment = Alignment.LEFT; @@ -37,6 +40,10 @@ public class WLabel extends WWidget { this(text, DEFAULT_TEXT_COLOR); } + public WLabel(Text text) { + this(text, DEFAULT_TEXT_COLOR); + } + @Override public void paintBackground(int x, int y, int mouseX, int mouseY) { String translated = text.asFormattedString(); 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 new file mode 100644 index 0000000..7e90b39 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java @@ -0,0 +1,133 @@ +package io.github.cottonmc.cotton.gui.widget; + +import io.github.cottonmc.cotton.gui.client.LibGuiClient; +import io.github.cottonmc.cotton.gui.client.ScreenDrawing; +import io.github.cottonmc.cotton.gui.widget.data.Alignment; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.font.TextRenderer; +import net.minecraft.client.util.Texts; +import net.minecraft.text.Text; + +import javax.annotation.Nullable; +import java.util.List; +import java.util.Objects; + +/** + * A multiline label widget. + * + * @since 1.8.0 + */ +public class WText extends WWidget { + protected Text text; + protected int color; + protected int darkmodeColor; + protected Alignment alignment = Alignment.LEFT; + private List<Text> wrappedLines; + + public WText(Text text) { + this(text, WLabel.DEFAULT_TEXT_COLOR); + } + + public WText(Text text, int color) { + this.text = Objects.requireNonNull(text, "text must not be null"); + this.color = color; + this.darkmodeColor = (color == WLabel.DEFAULT_TEXT_COLOR) ? WLabel.DEFAULT_DARKMODE_TEXT_COLOR : color; + } + + @Override + public void setSize(int x, int y) { + super.setSize(x, y); + if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { + wrapLines(); + } + } + + @Environment(EnvType.CLIENT) + private void wrapLines() { + TextRenderer font = MinecraftClient.getInstance().textRenderer; + wrappedLines = Texts.wrapLines(text, width, font, true, true); + } + + @Environment(EnvType.CLIENT) + @Nullable + protected Text getTextAt(int x, int y) { + TextRenderer font = MinecraftClient.getInstance().textRenderer; + int lineIndex = y / font.fontHeight; + + if (lineIndex >= 0 && lineIndex < wrappedLines.size()) { + Text line = wrappedLines.get(lineIndex); + int xi = 0; + for (Text part : line) { + xi += font.getStringWidth(part.asFormattedString()); + if (xi > x) return part; + } + } + + return null; + } + + @Environment(EnvType.CLIENT) + @Override + public void paintBackground(int x, int y, int mouseX, int mouseY) { + TextRenderer font = MinecraftClient.getInstance().textRenderer; + for (int i = 0; i < wrappedLines.size(); i++) { + Text line = wrappedLines.get(i); + int c = LibGuiClient.config.darkMode ? darkmodeColor : color; + String str = line.asFormattedString(); + + ScreenDrawing.drawString(str, alignment, x, y + i * font.fontHeight, width, c); + } + } + + @Override + public void onClick(int x, int y, int button) { + if (button != 0) return; // only left clicks + + Text hoveredText = getTextAt(x, y); + if (hoveredText != null) { + MinecraftClient.getInstance().currentScreen.handleTextClick(hoveredText); + } + } + + public Text getText() { + return text; + } + + public WText setText(Text text) { + this.text = text; + + if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { + wrapLines(); + } + + return this; + } + + public int getColor() { + return color; + } + + public WText setColor(int color) { + this.color = color; + return this; + } + + public WText setDarkmodeColor(int darkmodeColor) { + this.darkmodeColor = darkmodeColor; + return this; + } + + public WText setColor(int color, int darkmodeColor) { + setColor(color); + setDarkmodeColor(darkmodeColor); + return this; + } + + public WText disableDarkmode() { + this.darkmodeColor = this.color; + return this; + } +} |