From 88ba61509ee4158787bba16ef58ed6168b7d22fa Mon Sep 17 00:00:00 2001 From: MarcusElg Date: Sat, 3 Aug 2024 17:23:53 +0200 Subject: [PATCH] Add vertical alignment to WDynamicLabel Fix compilation Push forgotten code Fix strange docs error --- .../cotton/gui/widget/WDynamicLabel.java | 91 ++++++++++++++++++- 1 file changed, 89 insertions(+), 2 deletions(-) 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..e35503fc 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 @@ -5,7 +5,10 @@ import net.minecraft.client.gui.DrawContext; 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.VerticalAlignment; import java.util.function.Supplier; @@ -19,19 +22,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 +61,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 +82,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 +138,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; + } }