diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WDynamicLabel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WDynamicLabel.java index 1042e72b..54094ee1 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WDynamicLabel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WDynamicLabel.java @@ -1,13 +1,15 @@ package io.github.cottonmc.cotton.gui.widget; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; -import net.minecraft.client.gui.DrawContext; +import java.util.function.Supplier; 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 java.util.function.Supplier; +import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.gui.DrawContext; /** * Dynamic labels are labels that pull their text from a {@code Supplier}. @@ -19,19 +21,38 @@ public class WDynamicLabel extends WWidget { protected Supplier text; protected HorizontalAlignment alignment = HorizontalAlignment.LEFT; + protected VerticalAlignment verticalAlignment = VerticalAlignment.TOP; protected int color; protected int darkmodeColor; protected boolean drawShadows; + /** + * The default text color for light mode labels. + */ public static final int DEFAULT_TEXT_COLOR = 0x404040; + + /** + * The default text color for {@linkplain LibGuiConfig#darkMode dark mode} labels. + */ public static final int DEFAULT_DARKMODE_TEXT_COLOR = 0xbcbcbc; + /** + * Constructs a new dynamic label. + * + * @param text the text of the label + * @param color the color of the label + */ public WDynamicLabel(Supplier text, int color) { this.text = text; this.color = color; this.darkmodeColor = (color==DEFAULT_TEXT_COLOR) ? DEFAULT_DARKMODE_TEXT_COLOR : color; } + /** + * Constructs a new dynamic label with the {@linkplain #DEFAULT_TEXT_COLOR default text color}. + * + * @param text the text of the label + */ public WDynamicLabel(Supplier text) { this(text, DEFAULT_TEXT_COLOR); } @@ -39,11 +60,14 @@ public WDynamicLabel(Supplier text) { @Environment(EnvType.CLIENT) @Override public void paint(DrawContext context, int x, int y, int mouseX, int mouseY) { + int yOffset = TextAlignment.getTextOffsetY(verticalAlignment, height, 1); + String tr = text.get(); + if (getDrawShadows()) { - ScreenDrawing.drawStringWithShadow(context, tr, alignment, x, y, this.getWidth(), shouldRenderInDarkMode() ? darkmodeColor : color); + ScreenDrawing.drawStringWithShadow(context, tr, alignment, x, y + yOffset, this.getWidth(), shouldRenderInDarkMode() ? darkmodeColor : color); } else { - ScreenDrawing.drawString(context, tr, alignment, x, y, this.getWidth(), shouldRenderInDarkMode() ? darkmodeColor : color); + ScreenDrawing.drawString(context, tr, alignment, x, y + yOffset, this.getWidth(), shouldRenderInDarkMode() ? darkmodeColor : color); } } @@ -57,16 +81,34 @@ public void setSize(int x, int y) { super.setSize(x, 20); } + /** + * Sets the dark mode color of this label. + * + * @param color the new color + * @return this label + */ public WDynamicLabel setDarkmodeColor(int color) { darkmodeColor = color; return this; } + /** + * Disables separate dark mode coloring by copying the dark color to be the light color. + * + * @return this label + */ public WDynamicLabel disableDarkmode() { this.darkmodeColor = this.color; return this; } + /** + * Sets the light and dark mode colors of this label. + * + * @param color the new light color + * @param darkmodeColor the new dark color + * @return this label + */ public WDynamicLabel setColor(int color, int darkmodeColor) { this.color = color; this.darkmodeColor = darkmodeColor; @@ -95,13 +137,57 @@ public WDynamicLabel setDrawShadows(boolean drawShadows) { return this; } + /** + * Sets the text of this label. + * + * @param text the new text + * @return this label + */ public WDynamicLabel setText(Supplier text) { this.text = text; return this; } + + /** + * Gets the horizontal text alignment of this label. + * + * @return the alignment + * @since 11.1.0 + */ + public HorizontalAlignment getAlignment() { + return alignment; + } + /** + * Sets the horizontal text alignment of this label. + * + * @param align the new text alignment + * @return this label + */ public WDynamicLabel setAlignment(HorizontalAlignment align) { this.alignment = align; return this; } + + /** + * Gets the vertical text alignment of this label. + * + * @return the alignment + * @since 11.1.0 + */ + public VerticalAlignment getVerticalAlignment() { + return verticalAlignment; + } + + /** + * Sets the vertical text alignment of this label. + * + * @param align the new text alignment + * @return this label + * @since 11.1.0 + */ + public WDynamicLabel setVerticalAlignment(VerticalAlignment align) { + this.verticalAlignment = align; + return this; + } }