diff options
Diffstat (limited to 'src/main')
11 files changed, 137 insertions, 61 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java index 95702a6..b3f9bcd 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java @@ -100,7 +100,7 @@ public class CottonClientScreen extends Screen implements TextHoverRendererScree if (getTitle() != null && description.isTitleVisible()) { int width = description.getRootPanel().getWidth(); - ScreenDrawing.drawString(matrices, getTitle(), description.getTitleAlignment(), left + titleX, top + titleY, width, description.getTitleColor()); + ScreenDrawing.drawString(matrices, getTitle().asOrderedText(), description.getTitleAlignment(), left + titleX, top + titleY, width, description.getTitleColor()); } } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java index 4b7a726..6b65f07 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java @@ -275,7 +275,7 @@ public class CottonInventoryScreen<T extends SyncedGuiDescription> extends Handl protected void drawForeground(MatrixStack matrices, int mouseX, int mouseY) { if (description != null && description.isTitleVisible()) { int width = description.getRootPanel().getWidth(); - ScreenDrawing.drawString(matrices, getTitle(), description.getTitleAlignment(), titleX, titleY, width, description.getTitleColor()); + ScreenDrawing.drawString(matrices, getTitle().asOrderedText(), description.getTitleAlignment(), titleX, titleY, width, description.getTitleColor()); } // Don't draw the player inventory label as it's drawn by the widget itself diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java b/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java index 07d4417..5e07fbb 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java @@ -7,7 +7,7 @@ import net.minecraft.client.render.BufferBuilder; import net.minecraft.client.render.Tessellator; import net.minecraft.client.render.VertexFormats; import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.text.StringRenderable; +import net.minecraft.text.OrderedText; import net.minecraft.util.Identifier; import org.lwjgl.opengl.GL11; @@ -360,7 +360,7 @@ public class ScreenDrawing { * @param color the text color * @since 1.9.0 */ - public static void drawString(MatrixStack matrices, StringRenderable text, HorizontalAlignment align, int x, int y, int width, int color) { + public static void drawString(MatrixStack matrices, OrderedText text, HorizontalAlignment align, int x, int y, int width, int color) { switch(align) { case LEFT: { MinecraftClient.getInstance().textRenderer.draw(matrices, text, x, y, color); @@ -424,7 +424,7 @@ public class ScreenDrawing { * @param width the width of the string, used for aligning * @param color the text color */ - public static void drawStringWithShadow(MatrixStack matrices, StringRenderable text, HorizontalAlignment align, int x, int y, int width, int color) { + public static void drawStringWithShadow(MatrixStack matrices, OrderedText text, HorizontalAlignment align, int x, int y, int width, int color) { switch(align) { case LEFT: { MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, text, x, y, color); @@ -467,7 +467,7 @@ public class ScreenDrawing { * @param y the Y position * @param color the text color */ - public static void drawString(MatrixStack matrices, StringRenderable text, int x, int y, int color) { + public static void drawString(MatrixStack matrices, OrderedText text, int x, int y, int color) { MinecraftClient.getInstance().textRenderer.draw(matrices, text, x, y, color); } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/TooltipBuilder.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/TooltipBuilder.java new file mode 100644 index 0000000..f99df0a --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/TooltipBuilder.java @@ -0,0 +1,50 @@ +package io.github.cottonmc.cotton.gui.widget; + +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.text.OrderedText; +import net.minecraft.text.Text; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * A builder for widget tooltips. + * + * @since 3.0.0 + */ +@Environment(EnvType.CLIENT) +public final class TooltipBuilder { + final List<OrderedText> lines = new ArrayList<>(); + + int size() { + return lines.size(); + } + + /** + * Adds the lines to this builder. + * + * @param lines the lines + * @return this builder + */ + public TooltipBuilder add(Text... lines) { + for (Text line : lines) { + this.lines.add(line.asOrderedText()); + } + + return this; + } + + /** + * Adds the lines to this builder. + * + * @param lines the lines + * @return this builder + */ + public TooltipBuilder add(OrderedText... lines) { + Collections.addAll(this.lines, lines); + + return this; + } +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WBar.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WBar.java index 09b87fd..71973b4 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WBar.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WBar.java @@ -5,7 +5,6 @@ import net.fabricmc.api.Environment; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.screen.PropertyDelegate; import net.minecraft.text.LiteralText; -import net.minecraft.text.StringRenderable; import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; import net.minecraft.util.Identifier; @@ -14,7 +13,6 @@ import io.github.cottonmc.cotton.gui.GuiDescription; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import javax.annotation.Nullable; -import java.util.List; /** * A bar that displays int values from a {@link PropertyDelegate}. @@ -162,8 +160,9 @@ public class WBar extends WWidget { } } + @Environment(EnvType.CLIENT) @Override - public void addTooltip(List<StringRenderable> information) { + public void addTooltip(TooltipBuilder information) { if (tooltipLabel!=null) { int value = (field>=0) ? properties.get(field) : 0; int valMax = (max>=0) ? properties.get(max) : maxValue; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java index f65b513..40a7097 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java @@ -8,15 +8,15 @@ import net.minecraft.client.gui.widget.AbstractButtonWidget; import net.minecraft.client.sound.PositionedSoundInstance; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.sound.SoundEvents; -import net.minecraft.text.StringRenderable; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; +import net.minecraft.text.Text; import javax.annotation.Nullable; public class WButton extends WWidget { - private StringRenderable label; + private Text label; protected int color = WLabel.DEFAULT_TEXT_COLOR; protected int darkmodeColor = WLabel.DEFAULT_TEXT_COLOR; private boolean enabled = true; @@ -47,7 +47,7 @@ public class WButton extends WWidget { * * @param label the label */ - public WButton(StringRenderable label) { + public WButton(Text label) { this.label = label; } @@ -58,7 +58,7 @@ public class WButton extends WWidget { * @param label the label * @since 2.2.0 */ - public WButton(Icon icon, StringRenderable label) { + public WButton(Icon icon, Text label) { this.icon = icon; this.label = label; } @@ -106,7 +106,7 @@ public class WButton extends WWidget { }*/ int xOffset = (icon != null && alignment == HorizontalAlignment.LEFT) ? 18 : 0; - ScreenDrawing.drawStringWithShadow(matrices, label, alignment, x + xOffset, y + ((20 - 8) / 2), width, color); //LibGuiClient.config.darkMode ? darkmodeColor : color); + ScreenDrawing.drawStringWithShadow(matrices, label.asOrderedText(), alignment, x + xOffset, y + ((20 - 8) / 2), width, color); //LibGuiClient.config.darkMode ? darkmodeColor : color); } } @@ -166,11 +166,11 @@ public class WButton extends WWidget { return this; } - public StringRenderable getLabel() { + public Text getLabel() { return label; } - public WButton setLabel(StringRenderable label) { + public WButton setLabel(Text label) { this.label = label; return this; } 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 ffd8433..fddc09a 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 @@ -8,13 +8,13 @@ import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.LiteralText; -import net.minecraft.text.StringRenderable; import net.minecraft.text.Style; import io.github.cottonmc.cotton.gui.client.LibGuiClient; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.client.TextHoverRendererScreen; import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; +import net.minecraft.text.Text; import javax.annotation.Nullable; @@ -22,7 +22,7 @@ import javax.annotation.Nullable; * A single-line label widget. */ public class WLabel extends WWidget { - protected StringRenderable text; + protected Text text; protected HorizontalAlignment horizontalAlignment = HorizontalAlignment.LEFT; protected VerticalAlignment verticalAlignment = VerticalAlignment.TOP; protected int color; @@ -54,7 +54,7 @@ public class WLabel extends WWidget { * @param text the text of the label * @param color the color of the label */ - public WLabel(StringRenderable text, int color) { + public WLabel(Text text, int color) { this.text = text; this.color = color; this.darkmodeColor = (color==DEFAULT_TEXT_COLOR) ? DEFAULT_DARKMODE_TEXT_COLOR : color; @@ -75,7 +75,7 @@ public class WLabel extends WWidget { * @param text the text of the label * @since 1.8.0 */ - public WLabel(StringRenderable text) { + public WLabel(Text text) { this(text, DEFAULT_TEXT_COLOR); } @@ -98,7 +98,7 @@ public class WLabel extends WWidget { break; } - ScreenDrawing.drawString(matrices, text, horizontalAlignment, x, y + yOffset, this.getWidth(), LibGuiClient.config.darkMode ? darkmodeColor : color); + ScreenDrawing.drawString(matrices, text.asOrderedText(), horizontalAlignment, x, y + yOffset, this.getWidth(), LibGuiClient.config.darkMode ? darkmodeColor : color); Style hoveredTextStyle = getTextStyleAt(mouseX, mouseY); if (hoveredTextStyle != null) { @@ -216,7 +216,7 @@ public class WLabel extends WWidget { * * @return the text */ - public StringRenderable getText() { + public Text getText() { return text; } @@ -226,7 +226,7 @@ public class WLabel extends WWidget { * @param text the new text * @return this label */ - public WLabel setText(StringRenderable text) { + public WLabel setText(Text text) { this.text = text; return this; } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java index 200b191..59c4bc4 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java @@ -5,7 +5,7 @@ import net.fabricmc.api.Environment; import net.minecraft.client.gui.widget.AbstractButtonWidget; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.client.util.math.Vector3f; -import net.minecraft.text.StringRenderable; +import net.minecraft.text.Text; import net.minecraft.util.math.Quaternion; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; @@ -26,7 +26,7 @@ import javax.annotation.Nullable; public class WLabeledSlider extends WAbstractSlider { private static final Quaternion ROTATION_Z_270 = Vector3f.POSITIVE_X.getDegreesQuaternion(270); - @Nullable private StringRenderable label = null; + @Nullable private Text label = null; @Nullable private LabelUpdater labelUpdater = null; private HorizontalAlignment labelAlignment = HorizontalAlignment.CENTER; @@ -59,7 +59,7 @@ public class WLabeledSlider extends WAbstractSlider { * @param axis the slider axis * @param label the slider label (can be null) */ - public WLabeledSlider(int min, int max, Axis axis, @Nullable StringRenderable label) { + public WLabeledSlider(int min, int max, Axis axis, @Nullable Text label) { this(min, max, axis); this.label = label; } @@ -71,7 +71,7 @@ public class WLabeledSlider extends WAbstractSlider { * @param max the maximum value * @param label the slider label (can be null) */ - public WLabeledSlider(int min, int max, @Nullable StringRenderable label) { + public WLabeledSlider(int min, int max, @Nullable Text label) { this(min, max); this.label = label; } @@ -91,7 +91,7 @@ public class WLabeledSlider extends WAbstractSlider { * @return the label */ @Nullable - public StringRenderable getLabel() { + public Text getLabel() { return label; } @@ -100,7 +100,7 @@ public class WLabeledSlider extends WAbstractSlider { * * @param label the new label */ - public void setLabel(@Nullable StringRenderable label) { + public void setLabel(@Nullable Text label) { this.label = label; } @@ -194,7 +194,7 @@ public class WLabeledSlider extends WAbstractSlider { if (label != null) { int color = isMouseInsideBounds(mouseX, mouseY) ? 0xFFFFA0 : 0xE0E0E0; - ScreenDrawing.drawStringWithShadow(matrices, label, labelAlignment, 2, aHeight / 2 - 4, aWidth - 4, color); + ScreenDrawing.drawStringWithShadow(matrices, label.asOrderedText(), labelAlignment, 2, aHeight / 2 - 4, aWidth - 4, color); } matrices.pop(); } @@ -228,6 +228,6 @@ public class WLabeledSlider extends WAbstractSlider { * @param value the slider value * @return the label */ - StringRenderable updateLabel(int value); + Text updateLabel(int value); } } 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 8fb50b8..ec66a03 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 @@ -7,13 +7,14 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.text.StringRenderable; +import net.minecraft.text.OrderedText; import net.minecraft.text.Style; import io.github.cottonmc.cotton.gui.client.LibGuiClient; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.client.TextHoverRendererScreen; import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; +import net.minecraft.text.Text; import javax.annotation.Nullable; import java.util.List; @@ -25,19 +26,20 @@ import java.util.Objects; * @since 1.8.0 */ public class WText extends WWidget { - protected StringRenderable text; + protected Text text; protected int color; protected int darkmodeColor; protected HorizontalAlignment horizontalAlignment = HorizontalAlignment.LEFT; protected VerticalAlignment verticalAlignment = VerticalAlignment.TOP; - private List<StringRenderable> wrappedLines; + @Environment(EnvType.CLIENT) + private List<OrderedText> wrappedLines; private boolean wrappingScheduled = false; - public WText(StringRenderable text) { + public WText(Text text) { this(text, WLabel.DEFAULT_TEXT_COLOR); } - public WText(StringRenderable text, int 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; @@ -74,8 +76,8 @@ public class WText extends WWidget { int lineIndex = y / font.fontHeight; if (lineIndex >= 0 && lineIndex < wrappedLines.size()) { - StringRenderable line = wrappedLines.get(lineIndex); - return font.getTextHandler().trimToWidth(line, x); + OrderedText line = wrappedLines.get(lineIndex); + return font.getTextHandler().method_30876(line, x); } return null; @@ -106,7 +108,7 @@ public class WText extends WWidget { } for (int i = 0; i < wrappedLines.size(); i++) { - StringRenderable line = wrappedLines.get(i); + OrderedText line = wrappedLines.get(i); int c = LibGuiClient.config.darkMode ? darkmodeColor : color; ScreenDrawing.drawString(matrices, line, horizontalAlignment, x, y + yOffset + i * font.fontHeight, width, c); @@ -137,7 +139,7 @@ public class WText extends WWidget { * * @return the text */ - public StringRenderable getText() { + public Text getText() { return text; } @@ -147,7 +149,7 @@ public class WText extends WWidget { * @param text the new text * @return this label */ - public WText setText(StringRenderable text) { + public WText setText(Text text) { Objects.requireNonNull(text, "text is null"); this.text = text; wrappingScheduled = true; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java index b917d97..29f8648 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java @@ -6,7 +6,7 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.sound.PositionedSoundInstance; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.sound.SoundEvents; -import net.minecraft.text.StringRenderable; +import net.minecraft.text.Text; import net.minecraft.util.Identifier; import io.github.cottonmc.cotton.gui.client.LibGuiClient; @@ -25,7 +25,7 @@ public class WToggleButton extends WWidget { protected Identifier offImage; protected Identifier focusImage = DEFAULT_FOCUS_IMAGE; - @Nullable protected StringRenderable label = null; + @Nullable protected Text label = null; protected boolean isOn = false; @Nullable protected Consumer<Boolean> onToggle = null; @@ -33,25 +33,42 @@ public class WToggleButton extends WWidget { protected int color = WLabel.DEFAULT_TEXT_COLOR; protected int darkmodeColor = WLabel.DEFAULT_DARKMODE_TEXT_COLOR; - /** All default values, no text */ + /** + * Constructs a toggle button with default images and no label. + */ public WToggleButton() { this(DEFAULT_ON_IMAGE, DEFAULT_OFF_IMAGE); } - /** Defaults with text */ - public WToggleButton(StringRenderable text) { + /** + * Constructs a toggle button with default images. + * + * @param label the button label + */ + public WToggleButton(Text label) { this(DEFAULT_ON_IMAGE, DEFAULT_OFF_IMAGE); - this.label = text; + this.label = label; } - /** Custom images */ + /** + * Constructs a toggle button with custom images and no label. + * + * @param onImage the toggled on image + * @param offImage the toggled off image + */ public WToggleButton(Identifier onImage, Identifier offImage) { this.onImage = onImage; this.offImage = offImage; } - /** Custom images, with default sizes and a label */ - public WToggleButton(Identifier onImage, Identifier offImage, StringRenderable label) { + /** + * Constructs a toggle button with custom images. + * + * @param onImage the toggled on image + * @param offImage the toggled off image + * @param label the button label + */ + public WToggleButton(Identifier onImage, Identifier offImage, Text label) { this.onImage = onImage; this.offImage = offImage; this.label = label; @@ -66,7 +83,7 @@ public class WToggleButton extends WWidget { } if (label!=null) { - ScreenDrawing.drawString(matrices, label, x + 22, y+6, LibGuiClient.config.darkMode ? darkmodeColor : color); + ScreenDrawing.drawString(matrices, label.asOrderedText(), x + 22, y+6, LibGuiClient.config.darkMode ? darkmodeColor : color); } } @@ -118,11 +135,11 @@ public class WToggleButton extends WWidget { } @Nullable - public StringRenderable getLabel() { + public Text getLabel() { return label; } - public WToggleButton setLabel(@Nullable StringRenderable label) { + public WToggleButton setLabel(@Nullable Text label) { this.label = label; return this; } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java index e615c53..faa0355 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java @@ -9,7 +9,8 @@ import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.text.StringRenderable; +import net.minecraft.text.OrderedText; +import net.minecraft.text.Text; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.lwjgl.glfw.GLFW; @@ -344,19 +345,24 @@ public class WWidget { } /** - * Internal method to render tooltip data. This requires an overriden {@link #addTooltip(List) + * Internal method to render tooltip data. This requires an overriden {@link #addTooltip(TooltipBuilder) * addTooltip} method to insert data into the tooltip - without this, the method returns early, because no work + * + * @param x the X coordinate of this widget on screen + * @param y the Y coordinate of this widget on screen + * @param tX the X coordinate of the tooltip + * @param tY the Y coordinate of the tooltip */ @Environment(EnvType.CLIENT) public void renderTooltip(MatrixStack matrices, int x, int y, int tX, int tY) { - List<StringRenderable> info = new ArrayList<>(); - addTooltip(info); + TooltipBuilder builder = new TooltipBuilder(); + addTooltip(builder); - if (info.size() == 0) + if (builder.size() == 0) return; Screen screen = MinecraftClient.getInstance().currentScreen; - screen.renderTooltip(matrices, info, tX+x, tY+y); + screen.renderTooltip(matrices, builder.lines, tX+x, tY+y); } /** @@ -394,9 +400,11 @@ public class WWidget { /** * Adds lines to this widget's tooltip. If the lines remain empty after this call, no tooltip will be drawn. - * @param tooltip List containing all previous tooltip data. + * + * @param tooltip the builder to add tooltip lines to */ - public void addTooltip(List<StringRenderable> tooltip) { + @Environment(EnvType.CLIENT) + public void addTooltip(TooltipBuilder tooltip) { } /** |