Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for WLabel, WText and WDynamicLabel to have shadows #248

Merged
merged 4 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
MarcusElg marked this conversation as resolved.
Show resolved Hide resolved
*
* @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 labbel.
MarcusElg marked this conversation as resolved.
Show resolved Hide resolved
*
* @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
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.
MarcusElg marked this conversation as resolved.
Show resolved Hide resolved
*
* @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.
MarcusElg marked this conversation as resolved.
Show resolved Hide resolved
*
* @param drawShadows {@code true} if shadows should be drawn, {@code false} otherwise
* @return this label
MarcusElg marked this conversation as resolved.
Show resolved Hide resolved
* @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