Skip to content

Commit

Permalink
Add vertical alignment to WDynamicLabel (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusElg authored Aug 15, 2024
1 parent cf65014 commit bc7cbcb
Showing 1 changed file with 89 additions and 2 deletions.
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;
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;
}
}

0 comments on commit bc7cbcb

Please sign in to comment.