diff options
17 files changed, 153 insertions, 77 deletions
diff --git a/GuiTest/build.gradle b/GuiTest/build.gradle index 2a71459..1cc83d9 100644 --- a/GuiTest/build.gradle +++ b/GuiTest/build.gradle @@ -34,7 +34,6 @@ dependencies { compileOnly ("com.google.code.findbugs:jsr305:3.0.2") { transitive = false } implementation project(':') - modRuntime "io.github.prospector:modmenu:$rootProject.modmenu_version" // for testing } processResources { diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/GuiBlockEntity.java b/GuiTest/src/main/java/io/github/cottonmc/test/GuiBlockEntity.java index c699b47..6a9f741 100644 --- a/GuiTest/src/main/java/io/github/cottonmc/test/GuiBlockEntity.java +++ b/GuiTest/src/main/java/io/github/cottonmc/test/GuiBlockEntity.java @@ -14,8 +14,9 @@ import net.minecraft.util.collection.DefaultedList; import javax.annotation.Nullable; public class GuiBlockEntity extends BlockEntity implements ImplementedInventory, NamedScreenHandlerFactory { + static final int INVENTORY_SIZE = 8; - DefaultedList<ItemStack> items = DefaultedList.ofSize(8, ItemStack.EMPTY); + DefaultedList<ItemStack> items = DefaultedList.ofSize(INVENTORY_SIZE, ItemStack.EMPTY); public GuiBlockEntity() { super(LibGuiTest.GUI_BLOCKENTITY_TYPE); diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/TestDescription.java b/GuiTest/src/main/java/io/github/cottonmc/test/TestDescription.java index 732494e..9d638e2 100644 --- a/GuiTest/src/main/java/io/github/cottonmc/test/TestDescription.java +++ b/GuiTest/src/main/java/io/github/cottonmc/test/TestDescription.java @@ -10,7 +10,7 @@ import net.minecraft.text.LiteralText; public class TestDescription extends SyncedGuiDescription { public TestDescription(ScreenHandlerType<?> type, int syncId, PlayerInventory playerInventory, ScreenHandlerContext context) { - super(type, syncId, playerInventory, getBlockInventory(context), null); + super(type, syncId, playerInventory, getBlockInventory(context, GuiBlockEntity.INVENTORY_SIZE), null); WGridPanel root = (WGridPanel)this.getRootPanel(); diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/client/TestClientGui.java b/GuiTest/src/main/java/io/github/cottonmc/test/client/TestClientGui.java index bcc2584..85ad3cd 100644 --- a/GuiTest/src/main/java/io/github/cottonmc/test/client/TestClientGui.java +++ b/GuiTest/src/main/java/io/github/cottonmc/test/client/TestClientGui.java @@ -1,10 +1,9 @@ package io.github.cottonmc.test.client; -import java.util.List; - import io.github.cottonmc.cotton.gui.client.BackgroundPainter; import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; +import io.github.cottonmc.cotton.gui.widget.TooltipBuilder; import io.github.cottonmc.cotton.gui.widget.WGridPanel; import io.github.cottonmc.cotton.gui.widget.WLabel; import io.github.cottonmc.cotton.gui.widget.WSlider; @@ -17,7 +16,6 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.LiteralText; -import net.minecraft.text.StringRenderable; import net.minecraft.util.Identifier; public class TestClientGui extends LightweightGuiDescription { @@ -38,8 +36,9 @@ public class TestClientGui extends LightweightGuiDescription { WGridPanel root = new WGridPanel(22); this.setRootPanel(root); WLabel title = new WLabel(new LiteralText("Client Test Gui"), WLabel.DEFAULT_TEXT_COLOR) { + @Environment(EnvType.CLIENT) @Override - public void addTooltip(List<StringRenderable> tooltip) { + public void addTooltip(TooltipBuilder tooltip) { tooltip.add(new LiteralText("Radical!")); } }; diff --git a/build.gradle b/build.gradle index 0244787..c891191 100644 --- a/build.gradle +++ b/build.gradle @@ -34,8 +34,9 @@ dependencies { compileOnly ("com.google.code.findbugs:jsr305:3.0.2") { transitive = false } - modCompileOnly "io.github.prospector:modmenu:$project.modmenu_version" - modRuntime "io.github.prospector:modmenu:$project.modmenu_version" // for testing + modRuntime(modCompileOnly("io.github.prospector:modmenu:$project.modmenu_version") { + exclude group: 'net.fabricmc.fabric-api' + }) } processResources { diff --git a/gradle.properties b/gradle.properties index 754834b..8f6ed64 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,16 +3,16 @@ org.gradle.jvmargs=-Xmx1G # Fabric Properties # check these on https://fabricmc.net/use - minecraft_version=1.16.1 - yarn_mappings=1.16.1+build.20 - loader_version=0.8.8+build.202 + minecraft_version=1.16.2-pre1 + yarn_mappings=1.16.2-pre1+build.13 + loader_version=0.9.0+build.204 # Mod Properties - mod_version = 2.3.0 + mod_version = 3.0.0 maven_group = io.github.cottonmc archives_base_name = LibGui # Dependencies - fabric_version=0.13.1+build.370-1.16 - jankson_version=3.0.0+j1.2.0 - modmenu_version=1.11.8+build.13 + fabric_version=0.16.3+build.390-1.16 + jankson_version=3.0.1+j1.2.0 + modmenu_version=1.14.5+build.30 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) { } /** |