From 12544fcbe6ea562fd8025879b5f68a95baf329ed Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Sun, 19 Jan 2020 12:24:49 +0200 Subject: Add dynamic labels Related to #24. --- .../cottonmc/cotton/gui/widget/WDynamicLabel.java | 76 ++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/widget/WDynamicLabel.java (limited to 'src/main/java') diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WDynamicLabel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WDynamicLabel.java new file mode 100644 index 0000000..91d062d --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WDynamicLabel.java @@ -0,0 +1,76 @@ +package io.github.cottonmc.cotton.gui.widget; + +import io.github.cottonmc.cotton.gui.client.LibGuiClient; +import io.github.cottonmc.cotton.gui.client.ScreenDrawing; +import io.github.cottonmc.cotton.gui.widget.data.Alignment; + +import java.util.function.Supplier; + +/** + * Dynamic labels are labels that pull their text from a {@code Supplier}. + * They can be used for automatically getting data from a block entity or another data source. + * + *

Translating strings in dynamic labels should be done using + * {@link net.minecraft.client.resource.language.I18n#translate(String, Object...)}. + */ +public class WDynamicLabel extends WWidget { + protected Supplier text; + protected Alignment alignment = Alignment.LEFT; + protected int color; + protected int darkmodeColor; + + public static final int DEFAULT_TEXT_COLOR = 0x404040; + public static final int DEFAULT_DARKMODE_TEXT_COLOR = 0xbcbcbc; + + public WDynamicLabel(Supplier text, int color) { + this.text = text; + this.color = color; + this.darkmodeColor = (color==DEFAULT_TEXT_COLOR) ? DEFAULT_DARKMODE_TEXT_COLOR : color; + } + + public WDynamicLabel(Supplier text) { + this(text, DEFAULT_TEXT_COLOR); + } + + @Override + public void paintBackground(int x, int y, int mouseX, int mouseY) { + String tr = text.get(); + ScreenDrawing.drawString(tr, alignment, x, y, this.getWidth(), LibGuiClient.config.darkMode ? darkmodeColor : color); + } + + @Override + public boolean canResize() { + return true; + } + + @Override + public void setSize(int x, int y) { + super.setSize(x, 20); + } + + public WDynamicLabel setDarkmodeColor(int color) { + darkmodeColor = color; + return this; + } + + public WDynamicLabel disableDarkmode() { + this.darkmodeColor = this.color; + return this; + } + + public WDynamicLabel setColor(int color, int darkmodeColor) { + this.color = color; + this.darkmodeColor = darkmodeColor; + return this; + } + + public WDynamicLabel setText(Supplier text) { + this.text = text; + return this; + } + + public WDynamicLabel setAlignment(Alignment align) { + this.alignment = align; + return this; + } +} -- cgit