Skip to content

Commit

Permalink
Implement draw shadow option for WDynamicLabel and WText
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusElg committed Aug 3, 2024
1 parent 462fbe8 commit 59429a3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 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 labbel.
*
* @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;
}

public WDynamicLabel setText(Supplier<String> text) {
this.text = text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ public WLabel setColor(int color, int darkmodeColor) {
* Checks whether shadows should be drawn for this label.
*
* @return {@code true} shadows should be drawn, {@code false} otherwise
* @since 11.0.1
* @since 11.1.0
*/
public boolean areShadowsDrawn() {
public boolean getDrawShadows() {
return drawShadows;
}

Expand All @@ -195,7 +195,7 @@ public boolean areShadowsDrawn() {
*
* @param drawShadows {@code true} if shadows should be drawn, {@code false} otherwise
* @return this label
* @since 11.0.1
* @since 11.1.0
*/
public WLabel setDrawShadows(boolean drawShadows) {
this.drawShadows = drawShadows;
Expand Down
29 changes: 28 additions & 1 deletion 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 Down Expand Up @@ -199,6 +204,28 @@ public WText 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 labbel.
*
* @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;
}

/**
* Disables separate dark mode coloring by copying the dark color to be the light color.
*
Expand Down

0 comments on commit 59429a3

Please sign in to comment.