Skip to content

Commit

Permalink
Add option for WLabel, WText and WDynamicLabel to have shadows (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusElg authored Aug 14, 2024
1 parent 951c759 commit 493463e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,7 +40,11 @@ public WDynamicLabel(Supplier<String> 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
Expand Down Expand Up @@ -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<String> text) {
this.text = text;
Expand Down
29 changes: 28 additions & 1 deletion src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
*
Expand Down
45 changes: 36 additions & 9 deletions src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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
*/
Expand All @@ -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");
Expand All @@ -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
*/
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.
*
Expand Down

0 comments on commit 493463e

Please sign in to comment.