From 8c5bbb305ec0a8afc870626ef7e2fb2edbaf5cd8 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Thu, 23 Jan 2020 22:16:37 +0200 Subject: Add WTitle --- .../github/cottonmc/cotton/gui/widget/WTitle.java | 96 +++++++++++++++++++++ .../assets/libgui/textures/widget/title_dark.png | Bin 0 -> 1630 bytes .../assets/libgui/textures/widget/title_light.png | Bin 0 -> 1587 bytes 3 files changed, 96 insertions(+) create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/widget/WTitle.java create mode 100644 src/main/resources/assets/libgui/textures/widget/title_dark.png create mode 100644 src/main/resources/assets/libgui/textures/widget/title_light.png (limited to 'src') diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTitle.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTitle.java new file mode 100644 index 0000000..a6336fe --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTitle.java @@ -0,0 +1,96 @@ +package io.github.cottonmc.cotton.gui.widget; + +import io.github.cottonmc.cotton.gui.client.BackgroundPainter; +import io.github.cottonmc.cotton.gui.client.ScreenDrawing; +import io.github.cottonmc.cotton.gui.widget.data.Alignment; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.text.Text; +import net.minecraft.util.Identifier; + +import javax.annotation.Nullable; + +/** + * A panel title widget with a 3D effect. + * + * + * + *
Titles should be added to their panels at (0, 0) and they should be as wide as the panel. + * The default title painter assumes that the containing panel uses {@link BackgroundPainter#VANILLA} or + * a {@linkplain BackgroundPainter.NinePatch nine-patch background painter} with a padding of 8. + */ +public class WTitle extends WWidget { + public static final BackgroundPainter DEFAULT_BACKGROUND_PAINTER = + BackgroundPainter.createLightDarkVariants( + BackgroundPainter.createNinePatch(new Identifier("libgui", "textures/widget/title_light.png"), 8).setBottomPadding(0), + BackgroundPainter.createNinePatch(new Identifier("libgui", "textures/widget/title_dark.png"), 8).setBottomPadding(0) + ); + + private Text label; + private Alignment alignment = Alignment.CENTER; + private int color = 0xFFFFFF; + + @Environment(EnvType.CLIENT) + @Nullable + private BackgroundPainter backgroundPainter = DEFAULT_BACKGROUND_PAINTER; + + public WTitle(Text label) { + this.label = label; + } + + @Override + public boolean canResize() { + return true; + } + + @Override + public void setSize(int x, int y) { + super.setSize(x, 16); + } + + @Environment(EnvType.CLIENT) + @Nullable + public BackgroundPainter getBackgroundPainter() { + return backgroundPainter; + } + + @Environment(EnvType.CLIENT) + public WTitle setBackgroundPainter(@Nullable BackgroundPainter backgroundPainter) { + this.backgroundPainter = backgroundPainter; + return this; + } + + public Text getLabel() { + return label; + } + + public WTitle setLabel(Text label) { + this.label = label; + return this; + } + + public int getColor() { + return color; + } + + public WTitle setColor(int color) { + this.color = color; + return this; + } + + public Alignment getAlignment() { + return alignment; + } + + public WTitle setAlignment(Alignment alignment) { + this.alignment = alignment; + return this; + } + + @Override + public void paintBackground(int x, int y, int mouseX, int mouseY) { + if (backgroundPainter != null) backgroundPainter.paintBackground(x, y, this); + + ScreenDrawing.drawStringWithShadow(label.asFormattedString(), alignment, x, y, getWidth(), color); + } +} diff --git a/src/main/resources/assets/libgui/textures/widget/title_dark.png b/src/main/resources/assets/libgui/textures/widget/title_dark.png new file mode 100644 index 0000000..0548651 Binary files /dev/null and b/src/main/resources/assets/libgui/textures/widget/title_dark.png differ diff --git a/src/main/resources/assets/libgui/textures/widget/title_light.png b/src/main/resources/assets/libgui/textures/widget/title_light.png new file mode 100644 index 0000000..22bf614 Binary files /dev/null and b/src/main/resources/assets/libgui/textures/widget/title_light.png differ -- cgit