From 493463e9d560ec4ec610493b76ded2ce4e9a3f6d Mon Sep 17 00:00:00 2001 From: MarcusElg Date: Wed, 14 Aug 2024 16:55:15 +0200 Subject: [PATCH] Add option for WLabel, WText and WDynamicLabel to have shadows (#248) --- .../cotton/gui/widget/WDynamicLabel.java | 29 +++++++++++- .../cottonmc/cotton/gui/widget/WLabel.java | 29 +++++++++++- .../cottonmc/cotton/gui/widget/WText.java | 45 +++++++++++++++---- 3 files changed, 92 insertions(+), 11 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 7f0aa7e9..1042e72b 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 @@ -21,6 +21,7 @@ public class WDynamicLabel extends WWidget { protected HorizontalAlignment alignment = HorizontalAlignment.LEFT; protected int color; protected int darkmodeColor; + protected boolean drawShadows; public static final int DEFAULT_TEXT_COLOR = 0x404040; public static final int DEFAULT_DARKMODE_TEXT_COLOR = 0xbcbcbc; @@ -39,7 +40,11 @@ public WDynamicLabel(Supplier text) { @Override public void paint(DrawContext context, int x, int y, int mouseX, int mouseY) { String tr = text.get(); - ScreenDrawing.drawString(context, tr, alignment, x, y, this.getWidth(), shouldRenderInDarkMode() ? darkmodeColor : color); + if (getDrawShadows()) { + ScreenDrawing.drawStringWithShadow(context, tr, alignment, x, y, this.getWidth(), shouldRenderInDarkMode() ? darkmodeColor : color); + } else { + ScreenDrawing.drawString(context, tr, alignment, x, y, this.getWidth(), shouldRenderInDarkMode() ? darkmodeColor : color); + } } @Override @@ -67,6 +72,28 @@ public WDynamicLabel setColor(int color, int darkmodeColor) { this.darkmodeColor = darkmodeColor; return this; } + + /** + * Checks whether shadows should be drawn for this label. + * + * @return {@code true} shadows should be drawn, {@code false} otherwise + * @since 11.1.0 + */ + public boolean getDrawShadows() { + return drawShadows; + } + + /** + * Sets whether shadows should be drawn for this label. + * + * @param drawShadows {@code true} if shadows should be drawn, {@code false} otherwise + * @return this label + * @since 11.1.0 + */ + public WDynamicLabel setDrawShadows(boolean drawShadows) { + this.drawShadows = drawShadows; + return this; + } public WDynamicLabel setText(Supplier text) { this.text = text; 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 143435e8..b491ef3b 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 @@ -27,6 +27,7 @@ public class WLabel extends WWidget { protected VerticalAlignment verticalAlignment = VerticalAlignment.TOP; protected int color; protected int darkmodeColor; + protected boolean drawShadows; /** * The default text color for light mode labels. @@ -65,7 +66,11 @@ public WLabel(Text text) { public void paint(DrawContext context, int x, int y, int mouseX, int mouseY) { int yOffset = TextAlignment.getTextOffsetY(verticalAlignment, height, 1); - ScreenDrawing.drawString(context, text.asOrderedText(), horizontalAlignment, x, y + yOffset, this.getWidth(), shouldRenderInDarkMode() ? darkmodeColor : color); + if (getDrawShadows()) { + ScreenDrawing.drawStringWithShadow(context, text.asOrderedText(), horizontalAlignment, x, y + yOffset, this.getWidth(), shouldRenderInDarkMode() ? darkmodeColor : color); + } else { + ScreenDrawing.drawString(context, text.asOrderedText(), horizontalAlignment, x, y + yOffset, this.getWidth(), shouldRenderInDarkMode() ? darkmodeColor : color); + } Style hoveredTextStyle = getTextStyleAt(mouseX, mouseY); ScreenDrawing.drawTextHover(context, hoveredTextStyle, x + mouseX, y + mouseY); @@ -176,6 +181,28 @@ public WLabel setColor(int color, int darkmodeColor) { return this; } + /** + * Checks whether shadows should be drawn for this label. + * + * @return {@code true} shadows should be drawn, {@code false} otherwise + * @since 11.1.0 + */ + public boolean getDrawShadows() { + return drawShadows; + } + + /** + * Sets whether shadows should be drawn for this label. + * + * @param drawShadows {@code true} if shadows should be drawn, {@code false} otherwise + * @return this label + * @since 11.1.0 + */ + public WLabel setDrawShadows(boolean drawShadows) { + this.drawShadows = drawShadows; + return this; + } + /** * Gets the text of this label. * 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 b2a44eb4..aa928735 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 @@ -30,6 +30,7 @@ public class WText extends WWidget { protected Text text; protected int color; protected int darkmodeColor; + protected boolean drawShadows; protected HorizontalAlignment horizontalAlignment = HorizontalAlignment.LEFT; protected VerticalAlignment verticalAlignment = VerticalAlignment.TOP; @Environment(EnvType.CLIENT) @@ -101,7 +102,11 @@ public void paint(DrawContext context, int x, int y, int mouseX, int mouseY) { OrderedText line = wrappedLines.get(i); int c = shouldRenderInDarkMode() ? darkmodeColor : color; - ScreenDrawing.drawString(context, line, horizontalAlignment, x, y + yOffset + i * font.fontHeight, width, c); + if (getDrawShadows()) { + ScreenDrawing.drawStringWithShadow(context, line, horizontalAlignment, x, y + yOffset + i * font.fontHeight, width, c); + } else { + ScreenDrawing.drawString(context, line, horizontalAlignment, x, y + yOffset + i * font.fontHeight, width, c); + } } Style hoveredTextStyle = getTextStyleAt(mouseX, mouseY); @@ -123,7 +128,7 @@ public InputResult onClick(int x, int y, int button) { } /** - * Gets the text of this label. + * Gets the text of this text widget. * * @return the text */ @@ -132,10 +137,10 @@ public Text getText() { } /** - * Sets the text of this label. + * Sets the text of this text widget. * * @param text the new text - * @return this label + * @return this text widget */ public WText setText(Text text) { Objects.requireNonNull(text, "text is null"); @@ -146,7 +151,7 @@ public WText setText(Text text) { } /** - * Gets the light mode color of this label. + * Gets the light mode color of this text widget. * * @return the color */ @@ -155,7 +160,7 @@ public int getColor() { } /** - * Sets the light mode color of this label. + * Sets the light mode color of this text widget. * * @param color the new color * @return this text widget @@ -166,7 +171,7 @@ public WText setColor(int color) { } /** - * Gets the dark mode color of this label. + * Gets the dark mode color of this text widget. * * @return the color * @since 2.0.0 @@ -176,7 +181,7 @@ public int getDarkmodeColor() { } /** - * Sets the dark mode color of this label. + * Sets the dark mode color of this text widget. * * @param darkmodeColor the new color * @return this text widget @@ -187,7 +192,7 @@ public WText setDarkmodeColor(int darkmodeColor) { } /** - * Sets the light and dark mode colors of this label. + * Sets the light and dark mode colors of this text widget. * * @param color the new light color * @param darkmodeColor the new dark color @@ -199,6 +204,28 @@ public WText setColor(int color, int darkmodeColor) { return this; } + /** + * Checks whether shadows should be drawn for this text widget. + * + * @return {@code true} shadows should be drawn, {@code false} otherwise + * @since 11.1.0 + */ + public boolean getDrawShadows() { + return drawShadows; + } + + /** + * Sets whether shadows should be drawn for this text widget. + * + * @param drawShadows {@code true} if shadows should be drawn, {@code false} otherwise + * @return this text widget + * @since 11.1.0 + */ + public WText setDrawShadows(boolean drawShadows) { + this.drawShadows = drawShadows; + return this; + } + /** * Disables separate dark mode coloring by copying the dark color to be the light color. *