Skip to content

Commit

Permalink
Add vertical alignment to WDynamicLabel
Browse files Browse the repository at this point in the history
Fix compilation

Push forgotten code

Fix strange docs error
  • Loading branch information
MarcusElg committed Aug 14, 2024
1 parent cf65014 commit e0f6937
Showing 1 changed file with 93 additions and 7 deletions.
100 changes: 93 additions & 7 deletions src/main/java/io/github/cottonmc/cotton/gui/widget/WDynamicLabel.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package io.github.cottonmc.cotton.gui.widget;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
import java.util.function.Supplier;

import io.github.cottonmc.cotton.gui.client.ScreenDrawing;

Check failure on line 5 in src/main/java/io/github/cottonmc/cotton/gui/widget/WDynamicLabel.java

View workflow job for this annotation

GitHub Actions / build

Wrong order for 'io.github.cottonmc.cotton.gui.client.ScreenDrawing' import.
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 java.util.function.Supplier;
import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment;
import net.fabricmc.api.EnvType;

Check failure on line 10 in src/main/java/io/github/cottonmc/cotton/gui/widget/WDynamicLabel.java

View workflow job for this annotation

GitHub Actions / build

Wrong order for 'net.fabricmc.api.EnvType' import.
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;

/**
* Dynamic labels are labels that pull their text from a {@code Supplier<String>}.
Expand All @@ -19,31 +21,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 +81,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 +137,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 e0f6937

Please sign in to comment.