diff options
author | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-01-19 12:24:49 +0200 |
---|---|---|
committer | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-01-19 12:24:49 +0200 |
commit | 12544fcbe6ea562fd8025879b5f68a95baf329ed (patch) | |
tree | 3cd846913d4d1379be5a7e75e68b1affde6659f3 /src/main/java/io | |
parent | b2a2ea44eadc4a6b6fc1aed1a311ed25c156aa86 (diff) | |
download | LibGui-12544fcbe6ea562fd8025879b5f68a95baf329ed.tar.gz LibGui-12544fcbe6ea562fd8025879b5f68a95baf329ed.tar.bz2 LibGui-12544fcbe6ea562fd8025879b5f68a95baf329ed.zip |
Add dynamic labels
Related to #24.
Diffstat (limited to 'src/main/java/io')
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WDynamicLabel.java | 76 |
1 files changed, 76 insertions, 0 deletions
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<String>}. + * They can be used for automatically getting data from a block entity or another data source. + * + * <p>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<String> 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<String> text, int color) { + this.text = text; + this.color = color; + this.darkmodeColor = (color==DEFAULT_TEXT_COLOR) ? DEFAULT_DARKMODE_TEXT_COLOR : color; + } + + public WDynamicLabel(Supplier<String> 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<String> text) { + this.text = text; + return this; + } + + public WDynamicLabel setAlignment(Alignment align) { + this.alignment = align; + return this; + } +} |