aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java26
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/NinePatchBackgroundPainter.java105
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WTabPanel.java18
3 files changed, 113 insertions, 36 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java b/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java
index 61fcbb8..36c6c3a 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java
@@ -4,7 +4,6 @@ import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.Identifier;
import io.github.cottonmc.cotton.gui.impl.LibGuiCommon;
-import io.github.cottonmc.cotton.gui.impl.client.NinePatchTextureRendererImpl;
import io.github.cottonmc.cotton.gui.widget.WItemSlot;
import io.github.cottonmc.cotton.gui.widget.WWidget;
import io.github.cottonmc.cotton.gui.widget.data.Texture;
@@ -16,17 +15,6 @@ import java.util.function.Consumer;
/**
* Background painters are used to paint the background of a widget.
* The background painter instance of a widget can be changed to customize the look of a widget.
- *
- * <h2>Nine-patch background painters</h2>
- *
- * Nine-patch background painters paint rectangles using a special nine-patch texture.
- * The texture is divided into nine sections: four corners, four edges and a center part.
- * The edges and the center are either tiled or stretched, depending on the mode of the painter,
- * to fill the area between the corners. By default, the texture is tiled.
- *
- * <p>Nine-patch background painters are created using {@link #createNinePatch(Identifier)} or
- * {@link #createNinePatch(Texture, Consumer)}. The latter lets you customise the look of
- * the background more finely.
*/
@FunctionalInterface
public interface BackgroundPainter {
@@ -134,8 +122,9 @@ public interface BackgroundPainter {
* @param texture the background painter texture
* @return a new nine-patch background painter
* @since 1.5.0
+ * @see NinePatchBackgroundPainter
*/
- public static BackgroundPainter createNinePatch(Identifier texture) {
+ public static NinePatchBackgroundPainter createNinePatch(Identifier texture) {
return createNinePatch(new Texture(texture), builder -> builder.cornerSize(4).cornerUv(0.25f));
}
@@ -148,18 +137,13 @@ public interface BackgroundPainter {
* @since 4.0.0
* @see NinePatch
* @see NinePatch.Builder
+ * @see NinePatchBackgroundPainter
*/
- public static BackgroundPainter createNinePatch(Texture texture, Consumer<NinePatch.Builder<Identifier>> configurator) {
+ public static NinePatchBackgroundPainter createNinePatch(Texture texture, Consumer<NinePatch.Builder<Identifier>> configurator) {
TextureRegion<Identifier> region = new TextureRegion<>(texture.image(), texture.u1(), texture.v1(), texture.u2(), texture.v2());
var builder = NinePatch.builder(region);
configurator.accept(builder);
- var ninePatch = builder.build();
- return (matrices, left, top, panel) -> {
- matrices.push();
- matrices.translate(left, top, 0);
- ninePatch.draw(NinePatchTextureRendererImpl.INSTANCE, matrices, panel.getWidth(), panel.getHeight());
- matrices.pop();
- };
+ return new NinePatchBackgroundPainter(builder.build());
}
/**
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/NinePatchBackgroundPainter.java b/src/main/java/io/github/cottonmc/cotton/gui/client/NinePatchBackgroundPainter.java
new file mode 100644
index 0000000..04e7a04
--- /dev/null
+++ b/src/main/java/io/github/cottonmc/cotton/gui/client/NinePatchBackgroundPainter.java
@@ -0,0 +1,105 @@
+package io.github.cottonmc.cotton.gui.client;
+
+import net.fabricmc.api.EnvType;
+import net.fabricmc.api.Environment;
+import net.minecraft.client.util.math.MatrixStack;
+import net.minecraft.util.Identifier;
+
+import io.github.cottonmc.cotton.gui.impl.client.NinePatchTextureRendererImpl;
+import io.github.cottonmc.cotton.gui.widget.WWidget;
+import io.github.cottonmc.cotton.gui.widget.data.Texture;
+import juuxel.libninepatch.NinePatch;
+
+import java.util.function.Consumer;
+
+/**
+ * Nine-patch background painters paint rectangles using a special nine-patch texture.
+ * The texture is divided into nine sections: four corners, four edges and a center part.
+ * The edges and the center are either tiled or stretched, depending on the mode of the painter,
+ * to fill the area between the corners. By default, the texture is tiled.
+ *
+ * <p>Nine-patch background painters can be created using {@link BackgroundPainter#createNinePatch(Identifier)},
+ * {@link #createNinePatch(Texture, Consumer)}, or with the constructor directly. The latter two let you customise
+ * the look of the background more finely.
+ *
+ * <p>{@code NinePatchBackgroundPainter} has a customizable padding that can be applied.
+ * By default there is no padding, but you can set it using {@link NinePatchBackgroundPainter#setPadding(int)}.
+ *
+ * @since 4.0.0
+ */
+@Environment(EnvType.CLIENT)
+public final class NinePatchBackgroundPainter implements BackgroundPainter {
+ private final NinePatch<Identifier> ninePatch;
+ private int topPadding = 0;
+ private int leftPadding = 0;
+ private int bottomPadding = 0;
+ private int rightPadding = 0;
+
+ public NinePatchBackgroundPainter(NinePatch<Identifier> ninePatch) {
+ this.ninePatch = ninePatch;
+ }
+
+ public int getTopPadding() {
+ return topPadding;
+ }
+
+ public NinePatchBackgroundPainter setTopPadding(int topPadding) {
+ this.topPadding = topPadding;
+ return this;
+ }
+
+ public int getLeftPadding() {
+ return leftPadding;
+ }
+
+ public NinePatchBackgroundPainter setLeftPadding(int leftPadding) {
+ this.leftPadding = leftPadding;
+ return this;
+ }
+
+ public int getBottomPadding() {
+ return bottomPadding;
+ }
+
+ public NinePatchBackgroundPainter setBottomPadding(int bottomPadding) {
+ this.bottomPadding = bottomPadding;
+ return this;
+ }
+
+ public int getRightPadding() {
+ return rightPadding;
+ }
+
+ public NinePatchBackgroundPainter setRightPadding(int rightPadding) {
+ this.rightPadding = rightPadding;
+ return this;
+ }
+
+ public NinePatchBackgroundPainter setPadding(int padding) {
+ this.topPadding = this.leftPadding = this.bottomPadding = this.rightPadding = padding;
+ return this;
+ }
+
+ public NinePatchBackgroundPainter setPadding(int vertical, int horizontal) {
+ this.topPadding = this.bottomPadding = vertical;
+ this.leftPadding = this.rightPadding = horizontal;
+ return this;
+ }
+
+ public NinePatchBackgroundPainter setPadding(int topPadding, int leftPadding, int bottomPadding, int rightPadding) {
+ this.topPadding = topPadding;
+ this.leftPadding = leftPadding;
+ this.bottomPadding = bottomPadding;
+ this.rightPadding = rightPadding;
+
+ return this;
+ }
+
+ @Override
+ public void paintBackground(MatrixStack matrices, int left, int top, WWidget panel) {
+ matrices.push();
+ matrices.translate(left - leftPadding, top - topPadding, 0);
+ ninePatch.draw(NinePatchTextureRendererImpl.INSTANCE, matrices, panel.getWidth() + leftPadding + rightPadding, panel.getHeight() + topPadding + bottomPadding);
+ matrices.pop();
+ }
+}
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTabPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTabPanel.java
index fab8941..2ee7e92 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTabPanel.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTabPanel.java
@@ -326,23 +326,11 @@ public class WTabPanel extends WPanel {
}
}
- // Make the tab a bit higher...
- if (selected) {
- height += 2;
- y -= 2;
- }
-
(selected ? Painters.SELECTED_TAB : Painters.UNSELECTED_TAB).paintBackground(matrices, x, y, this);
if (isFocused()) {
(selected ? Painters.SELECTED_TAB_FOCUS_BORDER : Painters.UNSELECTED_TAB_FOCUS_BORDER).paintBackground(matrices, x, y, this);
}
- // ...and revert the size change here
- if (selected) {
- height -= 2;
- y += 2;
- }
-
int iconX = 6;
if (title != null) {
@@ -378,8 +366,8 @@ public class WTabPanel extends WPanel {
@Environment(EnvType.CLIENT)
final static class Painters {
static final BackgroundPainter SELECTED_TAB = BackgroundPainter.createLightDarkVariants(
- BackgroundPainter.createNinePatch(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/tab/selected_light.png")),
- BackgroundPainter.createNinePatch(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/tab/selected_dark.png"))
+ BackgroundPainter.createNinePatch(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/tab/selected_light.png")).setTopPadding(2),
+ BackgroundPainter.createNinePatch(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/tab/selected_dark.png")).setTopPadding(2)
);
static final BackgroundPainter UNSELECTED_TAB = BackgroundPainter.createLightDarkVariants(
@@ -387,7 +375,7 @@ public class WTabPanel extends WPanel {
BackgroundPainter.createNinePatch(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/tab/unselected_dark.png"))
);
- static final BackgroundPainter SELECTED_TAB_FOCUS_BORDER = BackgroundPainter.createNinePatch(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/tab/focus.png"));
+ static final BackgroundPainter SELECTED_TAB_FOCUS_BORDER = BackgroundPainter.createNinePatch(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/tab/focus.png")).setTopPadding(2);
static final BackgroundPainter UNSELECTED_TAB_FOCUS_BORDER = BackgroundPainter.createNinePatch(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/tab/focus.png"));
}
}