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 vertical alignment to WDynamicLabel #249

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Changes from all 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 @@ -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;

Expand All @@ -19,31 +22,53 @@
public class WDynamicLabel extends WWidget {
protected Supplier<String> text;
protected HorizontalAlignment alignment = HorizontalAlignment.LEFT;
MarcusElg marked this conversation as resolved.
Show resolved Hide resolved
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<String> 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<String> text) {
this(text, DEFAULT_TEXT_COLOR);
}

@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);
}
}

Expand All @@ -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;
Expand Down Expand Up @@ -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<String> 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;
}
}