aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorJuuxel <6596629+Juuxel@users.noreply.github.com>2020-01-24 10:27:18 +0200
committerJuuxel <6596629+Juuxel@users.noreply.github.com>2020-01-24 10:27:18 +0200
commit0c4177bf9e30b1243ad94b8394621f046667f196 (patch)
treed42d336233b2824f57aadf70eef0ccaf7586df1d /src/main/java
parent98eb93549113df3d5f23e6160006388790f0d2c1 (diff)
downloadLibGui-0c4177bf9e30b1243ad94b8394621f046667f196.tar.gz
LibGui-0c4177bf9e30b1243ad94b8394621f046667f196.tar.bz2
LibGui-0c4177bf9e30b1243ad94b8394621f046667f196.zip
Move nine-patch into its own class, revert making it the default
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/CottonCraftingController.java2
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java249
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/LibGuiClient.java2
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/LightweightGuiDescription.java2
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/NinePatch.java337
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/NinePatchMetadataLoader.java100
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WTitle.java3
7 files changed, 348 insertions, 347 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/CottonCraftingController.java b/src/main/java/io/github/cottonmc/cotton/gui/CottonCraftingController.java
index 8524afb..54038ec 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/CottonCraftingController.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/CottonCraftingController.java
@@ -83,7 +83,7 @@ public class CottonCraftingController extends CraftingContainer<Inventory> imple
@Environment(EnvType.CLIENT)
public void addPainters() {
if (this.rootPanel!=null) {
- this.rootPanel.setBackgroundPainter(BackgroundPainter.VANILLA_9PATCH);
+ this.rootPanel.setBackgroundPainter(BackgroundPainter.VANILLA);
}
}
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 014d458..70d8ba4 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
@@ -3,9 +3,6 @@ package io.github.cottonmc.cotton.gui.client;
import io.github.cottonmc.cotton.gui.widget.WItemSlot;
import io.github.cottonmc.cotton.gui.widget.WWidget;
import net.minecraft.util.Identifier;
-import net.minecraft.util.math.MathHelper;
-
-import javax.annotation.Nullable;
/**
* Background painters are used to paint the background of a widget.
@@ -25,20 +22,20 @@ public interface BackgroundPainter {
* The {@code VANILLA} background painter draws a vanilla-like gui panel using {@link ScreenDrawing#drawGuiPanel(int, int, int, int)}.
*
* <p>This background painter applies a padding of 8 pixels to all sides around the widget.
+ *
+ * <p>This background painter is the default painter for root panels.
+ * * You can override {@link io.github.cottonmc.cotton.gui.GuiDescription#addPainters()} to customize the painter yourself.
*/
public static BackgroundPainter VANILLA = (left, top, panel) -> {
ScreenDrawing.drawGuiPanel(left-8, top-8, panel.getWidth()+16, panel.getHeight()+16);
};
/**
- * The {@code VANILLA_9PATCH} background painter draws a vanilla-like gui panel using nine-patch textures.
+ * The {@code VANILLA_9PATCH} background painter draws a vanilla-like gui panel using {@linkplain NinePatch nine-patch textures}.
*
* <p>This background painter uses {@code libgui:textures/widget/panel_light.png} as the light texture and
* {@code libgui:textures/widget/panel_dark.png} as the dark texture.
*
- * <p>This background painter is the default painter for root panels.
- * You can override {@link io.github.cottonmc.cotton.gui.GuiDescription#addPainters()} to customize the painter yourself.
- *
* <p>This background painter applies a padding of 8 pixels to all sides around the widget.
*
* @since 1.5.0
@@ -112,7 +109,7 @@ public interface BackgroundPainter {
* @return a new nine-patch background painter
* @since 1.5.0
*/
- public static BackgroundPainter.NinePatch createNinePatch(Identifier texture) {
+ public static NinePatch createNinePatch(Identifier texture) {
return new NinePatch(texture);
}
@@ -126,7 +123,7 @@ public interface BackgroundPainter {
* @return a new nine-patch background painter
* @since 1.5.0
*/
- public static BackgroundPainter.NinePatch createNinePatch(Identifier texture, int padding) {
+ public static NinePatch createNinePatch(Identifier texture, int padding) {
return new NinePatch(texture).setPadding(padding);
}
@@ -145,238 +142,4 @@ public interface BackgroundPainter {
else light.paintBackground(left, top, panel);
};
}
-
- /**
- * The nine-patch background painter paints rectangles using a nine-patch texture.
- *
- * <p>Nine-patch textures are separated into nine sections: four corners, four edges and a center part.
- * The edges and the center are either tiled or stretched, depending on the {@linkplain BackgroundPainter.NinePatch.Mode mode},
- * to fill the area between the corners. By default, the texture mode is loaded from the texture metadata.
- * The default mode for that is {@link BackgroundPainter.NinePatch.Mode#STRETCHING}.
- *
- * <p>{@code NinePatch} painters have a customizable padding that can be applied.
- * For example, a GUI panel for a container block might have a padding of 8 pixels, like {@link BackgroundPainter#VANILLA}.
- * You can set the padding using {@link NinePatch#setPadding(int)}.
- *
- * <h2>Nine-patch metadata</h2>
- * You can specify metadata for a nine-patch texture in a resource pack by creating a metadata file.
- * Metadata files can currently specify the filling mode of the painter that paints the texture.
- * <p>The metadata file for a texture has to be placed in the same directory as the texture.
- * The file name must be {@code X.9patch} where X is the texture file name (including .png).
- * <p>Metadata files use {@linkplain java.util.Properties .properties format} with the following keys:
- * <table border="1">
- * <caption>Properties</caption>
- * <tr>
- * <th>Key</th>
- * <th>Value</th>
- * <th>Description</th>
- * </tr>
- * <tr>
- * <td>{@code mode}</td>
- * <td>{@link Mode#STRETCHING stretching} | {@link Mode#TILING tiling}</td>
- * <td>The texture filling mode</td>
- * </tr>
- * <tr>
- * <td>{@code cornerUv}</td>
- * <td>a float</td>
- * <td>the fraction of the corners of the whole texture</td>
- * </tr>
- * </table>
- *
- * @since 1.5.0
- */
- public static class NinePatch implements BackgroundPainter {
- private final Identifier texture;
- private final int cornerSize;
- private int topPadding = 0;
- private int leftPadding = 0;
- private int bottomPadding = 0;
- private int rightPadding = 0;
- private Mode mode = null;
-
- /**
- * Creates a nine-patch background painter with 4 px corners.
- *
- * @param texture the texture ID
- */
- public NinePatch(Identifier texture) {
- this(texture, 4);
- }
-
- /**
- * Creates a nine-patch background painter.
- *
- * @param texture the texture ID
- * @param cornerSize the size of the corners on the screen
- */
- public NinePatch(Identifier texture, int cornerSize) {
- this.texture = texture;
- this.cornerSize = cornerSize;
- }
-
- public int getTopPadding() {
- return topPadding;
- }
-
- public NinePatch setTopPadding(int topPadding) {
- this.topPadding = topPadding;
- return this;
- }
-
- public int getLeftPadding() {
- return leftPadding;
- }
-
- public NinePatch setLeftPadding(int leftPadding) {
- this.leftPadding = leftPadding;
- return this;
- }
-
- public int getBottomPadding() {
- return bottomPadding;
- }
-
- public NinePatch setBottomPadding(int bottomPadding) {
- this.bottomPadding = bottomPadding;
- return this;
- }
-
- public int getRightPadding() {
- return rightPadding;
- }
-
- public NinePatch setRightPadding(int rightPadding) {
- this.rightPadding = rightPadding;
- return this;
- }
-
- public NinePatch setPadding(int padding) {
- this.topPadding = this.leftPadding = this.bottomPadding = this.rightPadding = padding;
- return this;
- }
-
- public NinePatch setPadding(int vertical, int horizontal) {
- this.topPadding = this.bottomPadding = vertical;
- this.leftPadding = this.rightPadding = horizontal;
- return this;
- }
-
- public NinePatch setPadding(int topPadding, int leftPadding, int bottomPadding, int rightPadding) {
- this.topPadding = topPadding;
- this.leftPadding = leftPadding;
- this.bottomPadding = bottomPadding;
- this.rightPadding = rightPadding;
-
- return this;
- }
-
- public Identifier getTexture() {
- return texture;
- }
-
- public int getCornerSize() {
- return cornerSize;
- }
-
- @Nullable
- public Mode getMode() {
- return mode;
- }
-
- /**
- * Sets the {@linkplain Mode mode} of this painter to the specified mode.
- * <p>If the {@code mode} is not null, it will override the one specified in the texture metadata.
- * A null mode uses the texture metadata.
- */
- public NinePatch setMode(@Nullable Mode mode) {
- this.mode = mode;
- return this;
- }
-
- @Override
- public void paintBackground(int left, int top, WWidget panel) {
- NinePatchMetadataLoader.TextureProperties properties = NinePatchMetadataLoader.INSTANCE.getProperties(texture);
- int width = panel.getWidth() + leftPadding + rightPadding;
- int height = panel.getHeight() + topPadding + bottomPadding;
- left = left - leftPadding;
- top = top - topPadding;
- int x1 = left + cornerSize;
- int x2 = left + width - cornerSize;
- int y1 = top + cornerSize;
- int y2 = top + height - cornerSize;
- float cornerUv = properties.getCornerUv();
- float uv1 = cornerUv;
- float uv2 = 1.0f - cornerUv;
- Mode mode = this.mode != null ? this.mode : properties.getMode();
-
- ScreenDrawing.texturedRect(left, top, cornerSize, cornerSize, texture, 0, 0, uv1, uv1, 0xFF_FFFFFF);
- ScreenDrawing.texturedRect(x2, top, cornerSize, cornerSize, texture, uv2, 0, 1, uv1, 0xFF_FFFFFF);
- ScreenDrawing.texturedRect(left, y2, cornerSize, cornerSize, texture, 0, uv2, uv1, 1, 0xFF_FFFFFF);
- ScreenDrawing.texturedRect(x2, y2, cornerSize, cornerSize, texture, uv2, uv2, 1, 1, 0xFF_FFFFFF);
-
- if (mode == Mode.TILING) {
- int tileSize = (int) (cornerSize / cornerUv - 2 * cornerSize);
- int widthLeft = width - 2 * cornerSize;
- int heightLeft = height - 2 * cornerSize;
- int tileCountX = MathHelper.ceil((float) widthLeft / tileSize);
- int tileCountY = MathHelper.ceil((float) heightLeft / tileSize);
- for (int i = 0; i < tileCountX; i++) {
- float px = 1 / 16f;
- int tileWidth = Math.min(widthLeft, tileSize);
- float uo = (tileSize - tileWidth) * px; // Used to remove unnecessary pixels on the X axis
-
- ScreenDrawing.texturedRect(x1 + i * tileSize, top, tileWidth, cornerSize, texture, uv1, 0, uv2 - uo, uv1, 0xFF_FFFFFF);
- ScreenDrawing.texturedRect(x1 + i * tileSize, y2, tileWidth, cornerSize, texture, uv1, uv2, uv2 - uo, 1, 0xFF_FFFFFF);
-
- // Reset the height left each time the Y is looped
- heightLeft = height - 2 * cornerSize;
-
- for (int j = 0; j < tileCountY; j++) {
- int tileHeight = Math.min(heightLeft, tileSize);
- float vo = (tileSize - tileHeight) * px; // Used to remove unnecessary pixels on the Y axis
-
- ScreenDrawing.texturedRect(left, y1 + j * tileSize, cornerSize, tileHeight, texture, 0, uv1, uv1, uv2 - vo, 0xFF_FFFFFF);
- ScreenDrawing.texturedRect(x2, y1 + j * tileSize, cornerSize, tileHeight, texture, uv2, uv1, 1, uv2 - vo, 0xFF_FFFFFF);
-
- ScreenDrawing.texturedRect(x1 + i * tileSize, y1 + j * tileSize, tileWidth, tileHeight, texture, uv1, uv1, uv2 - uo, uv2 - vo, 0xFF_FFFFFF);
- heightLeft -= tileSize;
- }
- widthLeft -= tileSize;
- }
- } else {
- ScreenDrawing.texturedRect(x1, top, width - 2 * cornerSize, cornerSize, texture, uv1, 0, uv2, uv1, 0xFF_FFFFFF);
- ScreenDrawing.texturedRect(left, y1, cornerSize, height - 2 * cornerSize, texture, 0, uv1, uv1, uv2, 0xFF_FFFFFF);
- ScreenDrawing.texturedRect(x1, y2, width - 2 * cornerSize, cornerSize, texture, uv1, uv2, uv2, 1, 0xFF_FFFFFF);
- ScreenDrawing.texturedRect(x2, y1, cornerSize, height - 2 * cornerSize, texture, uv2, uv1, 1, uv2, 0xFF_FFFFFF);
-
- ScreenDrawing.texturedRect(x1, y1, width - 2 * cornerSize, height - 2 * cornerSize, texture, uv1, uv1, uv2, uv2, 0xFF_FFFFFF);
- }
- }
-
- /**
- * The mode of a nine-patch painter defines how it fills the area between the corners.
- */
- public enum Mode {
- /**
- * The texture is stretched to fill the edges and the center.
- * This is the default mode.
- */
- STRETCHING,
-
- /**
- * The texture is tiled to fill the edges and the center.
- */
- TILING;
-
- @Nullable
- static Mode fromString(String str) {
- if (str == null) return null;
-
- if (str.equalsIgnoreCase("stretching")) return STRETCHING;
- if (str.equalsIgnoreCase("tiling")) return TILING;
-
- return null;
- }
- }
- }
}
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/LibGuiClient.java b/src/main/java/io/github/cottonmc/cotton/gui/client/LibGuiClient.java
index d457e50..013382f 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/client/LibGuiClient.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/client/LibGuiClient.java
@@ -27,7 +27,7 @@ public class LibGuiClient implements ClientModInitializer {
public void onInitializeClient() {
config = loadConfig();
- ResourceManagerHelper.get(ResourceType.CLIENT_RESOURCES).registerReloadListener(NinePatchMetadataLoader.INSTANCE);
+ ResourceManagerHelper.get(ResourceType.CLIENT_RESOURCES).registerReloadListener(NinePatch.MetadataLoader.INSTANCE);
}
public static LibGuiConfig loadConfig() {
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/LightweightGuiDescription.java b/src/main/java/io/github/cottonmc/cotton/gui/client/LightweightGuiDescription.java
index 078b390..6284c2f 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/client/LightweightGuiDescription.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/client/LightweightGuiDescription.java
@@ -45,7 +45,7 @@ public class LightweightGuiDescription implements GuiDescription {
@Override
public void addPainters() {
if (this.rootPanel!=null) {
- this.rootPanel.setBackgroundPainter(BackgroundPainter.VANILLA_9PATCH);
+ this.rootPanel.setBackgroundPainter(BackgroundPainter.VANILLA);
}
}
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/NinePatch.java b/src/main/java/io/github/cottonmc/cotton/gui/client/NinePatch.java
new file mode 100644
index 0000000..baae61a
--- /dev/null
+++ b/src/main/java/io/github/cottonmc/cotton/gui/client/NinePatch.java
@@ -0,0 +1,337 @@
+package io.github.cottonmc.cotton.gui.client;
+
+import io.github.cottonmc.cotton.gui.widget.WWidget;
+import net.fabricmc.api.EnvType;
+import net.fabricmc.api.Environment;
+import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
+import net.minecraft.resource.Resource;
+import net.minecraft.resource.ResourceManager;
+import net.minecraft.resource.SinglePreparationResourceReloadListener;
+import net.minecraft.util.Identifier;
+import net.minecraft.util.math.MathHelper;
+import net.minecraft.util.profiler.Profiler;
+
+import javax.annotation.Nullable;
+import java.io.InputStream;
+import java.util.*;
+
+/**
+ * The nine-patch background painter paints rectangles using a nine-patch texture.
+ *
+ * <p>Nine-patch textures are separated into nine sections: four corners, four edges and a center part.
+ * The edges and the center are either tiled or stretched, depending on the {@linkplain Mode mode},
+ * to fill the area between the corners. By default, the texture mode is loaded from the texture metadata.
+ * The default mode for that is {@link Mode#STRETCHING}.
+ *
+ * <p>{@code NinePatch} painters have a customizable padding that can be applied.
+ * For example, a GUI panel for a container block might have a padding of 8 pixels, like {@link BackgroundPainter#VANILLA}.
+ * You can set the padding using {@link NinePatch#setPadding(int)}.
+ *
+ * <h2>Nine-patch metadata</h2>
+ * You can specify metadata for a nine-patch texture in a resource pack by creating a metadata file.
+ * Metadata files can currently specify the filling mode of the painter that paints the texture.
+ * <p>The metadata file for a texture has to be placed in the same directory as the texture.
+ * The file name must be {@code X.9patch} where X is the texture file name (including .png).
+ * <p>Metadata files use {@linkplain java.util.Properties the .properties format} with the following keys:
+ * <table border="1">
+ * <caption>Properties</caption>
+ * <tr>
+ * <th>Key</th>
+ * <th>Value</th>
+ * <th>Description</th>
+ * </tr>
+ * <tr>
+ * <td>{@code mode}</td>
+ * <td>{@link Mode#STRETCHING stretching} | {@link Mode#TILING tiling}</td>
+ * <td>The texture filling mode</td>
+ * </tr>
+ * <tr>
+ * <td>{@code cornerUv}</td>
+ * <td>a float</td>
+ * <td>the fraction of the corners of the whole texture</td>
+ * </tr>
+ * </table>
+ *
+ * @since 1.5.0
+ */
+public class NinePatch implements BackgroundPainter {
+ private final Identifier texture;
+ private final int cornerSize;
+ private int topPadding = 0;
+ private int leftPadding = 0;
+ private int bottomPadding = 0;
+ private int rightPadding = 0;
+ private Mode mode = null;
+
+ /**
+ * Creates a nine-patch background painter with 4 px corners.
+ *
+ * @param texture the texture ID
+ */
+ public NinePatch(Identifier texture) {
+ this(texture, 4);
+ }
+
+ /**
+ * Creates a nine-patch background painter.
+ *
+ * @param texture the texture ID
+ * @param cornerSize the size of the corners on the screen
+ */
+ public NinePatch(Identifier texture, int cornerSize) {
+ this.texture = texture;
+ this.cornerSize = cornerSize;
+ }
+
+ public int getTopPadding() {
+ return topPadding;
+ }
+
+ public NinePatch setTopPadding(int topPadding) {
+ this.topPadding = topPadding;
+ return this;
+ }
+
+ public int getLeftPadding() {
+ return leftPadding;
+ }
+
+ public NinePatch setLeftPadding(int leftPadding) {
+ this.leftPadding = leftPadding;
+ return this;
+ }
+
+ public int getBottomPadding() {
+ return bottomPadding;
+ }
+
+ public NinePatch setBottomPadding(int bottomPadding) {
+ this.bottomPadding = bottomPadding;
+ return this;
+ }
+
+ public int getRightPadding() {
+ return rightPadding;
+ }
+
+ public NinePatch setRightPadding(int rightPadding) {
+ this.rightPadding = rightPadding;
+ return this;
+ }
+
+ public NinePatch setPadding(int padding) {
+ this.topPadding = this.leftPadding = this.bottomPadding = this.rightPadding = padding;
+ return this;
+ }
+
+ public NinePatch setPadding(int vertical, int horizontal) {
+ this.topPadding = this.bottomPadding = vertical;
+ this.leftPadding = this.rightPadding = horizontal;
+ return this;
+ }
+
+ public NinePatch setPadding(int topPadding, int leftPadding, int bottomPadding, int rightPadding) {
+ this.topPadding = topPadding;
+ this.leftPadding = leftPadding;
+ this.bottomPadding = bottomPadding;
+ this.rightPadding = rightPadding;
+
+ return this;
+ }
+
+ public Identifier getTexture() {
+ return texture;
+ }
+
+ public int getCornerSize() {
+ return cornerSize;
+ }
+
+ @Nullable
+ public Mode getMode() {
+ return mode;
+ }
+
+ /**
+ * Sets the {@linkplain Mode mode} of this painter to the specified mode.
+ * <p>If the {@code mode} is not null, it will override the one specified in the texture metadata.
+ * A null mode uses the texture metadata.
+ */
+ public NinePatch setMode(@Nullable Mode mode) {
+ this.mode = mode;
+ return this;
+ }
+
+ @Override
+ public void paintBackground(int left, int top, WWidget panel) {
+ TextureProperties properties = MetadataLoader.INSTANCE.getProperties(texture);
+ int width = panel.getWidth() + leftPadding + rightPadding;
+ int height = panel.getHeight() + topPadding + bottomPadding;
+ left = left - leftPadding;
+ top = top - topPadding;
+ int x1 = left + cornerSize;
+ int x2 = left + width - cornerSize;
+ int y1 = top + cornerSize;
+ int y2 = top + height - cornerSize;
+ float cornerUv = properties.getCornerUv();
+ float uv1 = cornerUv;
+ float uv2 = 1.0f - cornerUv;
+ Mode mode = this.mode != null ? this.mode : properties.getMode();
+
+ ScreenDrawing.texturedRect(left, top, cornerSize, cornerSize, texture, 0, 0, uv1, uv1, 0xFF_FFFFFF);
+ ScreenDrawing.texturedRect(x2, top, cornerSize, cornerSize, texture, uv2, 0, 1, uv1, 0xFF_FFFFFF);
+ ScreenDrawing.texturedRect(left, y2, cornerSize, cornerSize, texture, 0, uv2, uv1, 1, 0xFF_FFFFFF);
+ ScreenDrawing.texturedRect(x2, y2, cornerSize, cornerSize, texture, uv2, uv2, 1, 1, 0xFF_FFFFFF);
+
+ if (mode == Mode.TILING) {
+ int tileSize = (int) (cornerSize / cornerUv - 2 * cornerSize);
+ int widthLeft = width - 2 * cornerSize;
+ int heightLeft = height - 2 * cornerSize;
+ int tileCountX = MathHelper.ceil((float) widthLeft / tileSize);
+ int tileCountY = MathHelper.ceil((float) heightLeft / tileSize);
+ for (int i = 0; i < tileCountX; i++) {
+ float px = 1 / 16f;
+ int tileWidth = Math.min(widthLeft, tileSize);
+ float uo = (tileSize - tileWidth) * px; // Used to remove unnecessary pixels on the X axis
+
+ ScreenDrawing.texturedRect(x1 + i * tileSize, top, tileWidth, cornerSize, texture, uv1, 0, uv2 - uo, uv1, 0xFF_FFFFFF);
+ ScreenDrawing.texturedRect(x1 + i * tileSize, y2, tileWidth, cornerSize, texture, uv1, uv2, uv2 - uo, 1, 0xFF_FFFFFF);
+
+ // Reset the height left each time the Y is looped
+ heightLeft = height - 2 * cornerSize;
+
+ for (int j = 0; j < tileCountY; j++) {
+ int tileHeight = Math.min(heightLeft, tileSize);
+ float vo = (tileSize - tileHeight) * px; // Used to remove unnecessary pixels on the Y axis
+
+ ScreenDrawing.texturedRect(left, y1 + j * tileSize, cornerSize, tileHeight, texture, 0, uv1, uv1, uv2 - vo, 0xFF_FFFFFF);
+ ScreenDrawing.texturedRect(x2, y1 + j * tileSize, cornerSize, tileHeight, texture, uv2, uv1, 1, uv2 - vo, 0xFF_FFFFFF);
+
+ ScreenDrawing.texturedRect(x1 + i * tileSize, y1 + j * tileSize, tileWidth, tileHeight, texture, uv1, uv1, uv2 - uo, uv2 - vo, 0xFF_FFFFFF);
+ heightLeft -= tileSize;
+ }
+ widthLeft -= tileSize;
+ }
+ } else {
+ ScreenDrawing.texturedRect(x1, top, width - 2 * cornerSize, cornerSize, texture, uv1, 0, uv2, uv1, 0xFF_FFFFFF);
+ ScreenDrawing.texturedRect(left, y1, cornerSize, height - 2 * cornerSize, texture, 0, uv1, uv1, uv2, 0xFF_FFFFFF);
+ ScreenDrawing.texturedRect(x1, y2, width - 2 * cornerSize, cornerSize, texture, uv1, uv2, uv2, 1, 0xFF_FFFFFF);
+ ScreenDrawing.texturedRect(x2, y1, cornerSize, height - 2 * cornerSize, texture, uv2, uv1, 1, uv2, 0xFF_FFFFFF);
+
+ ScreenDrawing.texturedRect(x1, y1, width - 2 * cornerSize, height - 2 * cornerSize, texture, uv1, uv1, uv2, uv2, 0xFF_FFFFFF);
+ }
+ }
+
+ /**
+ * The mode of a nine-patch painter defines how it fills the area between the corners.
+ */
+ public enum Mode {
+ /**
+ * The texture is stretched to fill the edges and the center.
+ * This is the default mode.
+ */
+ STRETCHING,
+
+ /**
+ * The texture is tiled to fill the edges and the center.
+ */
+ TILING;
+
+ @Nullable
+ static Mode fromString(String str) {
+ if (str == null) return null;
+
+ if (str.equalsIgnoreCase("stretching")) return STRETCHING;
+ if (str.equalsIgnoreCase("tiling")) return TILING;
+
+ return null;
+ }
+ }
+
+ public static class TextureProperties {
+ public static final TextureProperties DEFAULT = new TextureProperties(Mode.STRETCHING, 0.25f);
+
+ private final Mode mode;
+ private final float cornerUv;
+
+ public TextureProperties(Mode mode, float cornerUv) {
+ this.mode = mode;
+ this.cornerUv = cornerUv;
+ }
+
+ public Mode getMode() {
+ return mode;
+ }
+
+ public float getCornerUv() {
+ return cornerUv;
+ }
+ }
+
+ @Environment(EnvType.CLIENT)
+ public static class MetadataLoader extends SinglePreparationResourceReloadListener<Map<Identifier, Properties>> implements IdentifiableResourceReloadListener {
+ public static final MetadataLoader INSTANCE = new MetadataLoader();
+
+ private static final Identifier ID = new Identifier("libgui", "9patch_metadata");
+ private static final String SUFFIX = ".9patch";
+
+ private Map<Identifier, TextureProperties> properties = Collections.emptyMap();
+
+ public TextureProperties getProperties(Identifier texture) {
+ return properties.getOrDefault(texture, TextureProperties.DEFAULT);
+ }
+
+ @Override
+ public Identifier getFabricId() {
+ return ID;
+ }
+
+ @Override
+ protected Map<Identifier, Properties> prepare(ResourceManager manager, Profiler profiler) {
+ Collection<Identifier> ids = manager.findResources("textures", s -> s.endsWith(SUFFIX));
+ Map<Identifier, Properties> result = new HashMap<>();
+
+ for (Identifier input : ids) {
+ try (Resource resource = manager.getResource(input);
+ InputStream stream = resource.getInputStream()) {
+ Properties props = new Properties();
+ props.load(stream);
+ Identifier textureId = new Identifier(input.getNamespace(), input.getPath().substring(0, input.getPath().length() - SUFFIX.length()));
+ result.put(textureId, props);
+ } catch (Exception e) {
+ LibGuiClient.logger.error("Error while loading metadata file {}, skipping...", input, e);
+ }
+ }
+
+ return result;
+ }
+
+ @Override
+ protected void apply(Map<Identifier, Properties> meta, ResourceManager manager, Profiler profiler) {
+ properties = new HashMap<>();
+ for (Map.Entry<Identifier, Properties> entry : meta.entrySet()) {
+ Identifier id = entry.getKey();
+ Properties props = entry.getValue();
+
+ Mode mode = TextureProperties.DEFAULT.getMode();
+ float cornerUv = TextureProperties.DEFAULT.getCornerUv();
+
+ if (props.containsKey("mode")) {
+ String modeStr = props.getProperty("mode");
+ mode = Mode.fromString(modeStr);
+ if (mode == null) {
+ LibGuiClient.logger.error("Invalid mode '{}' in nine-patch metadata file for texture {}", modeStr, id);
+ continue;
+ }
+ }
+
+ if (props.containsKey("cornerUv")) {
+ cornerUv = Float.parseFloat(props.getProperty("cornerUv"));
+ }
+
+ TextureProperties texProperties = new TextureProperties(mode, cornerUv);
+ properties.put(id, texProperties);
+ }
+ }
+ }
+}
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/NinePatchMetadataLoader.java b/src/main/java/io/github/cottonmc/cotton/gui/client/NinePatchMetadataLoader.java
deleted file mode 100644
index f55e6ac..0000000
--- a/src/main/java/io/github/cottonmc/cotton/gui/client/NinePatchMetadataLoader.java
+++ /dev/null
@@ -1,100 +0,0 @@
-package io.github.cottonmc.cotton.gui.client;
-
-import net.fabricmc.api.EnvType;
-import net.fabricmc.api.Environment;
-import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
-import net.minecraft.resource.Resource;
-import net.minecraft.resource.ResourceManager;
-import net.minecraft.resource.SinglePreparationResourceReloadListener;
-import net.minecraft.util.Identifier;
-import net.minecraft.util.profiler.Profiler;
-
-import java.io.InputStream;
-import java.util.*;
-
-@Environment(EnvType.CLIENT)
-public class NinePatchMetadataLoader extends SinglePreparationResourceReloadListener<Map<Identifier, Properties>> implements IdentifiableResourceReloadListener {
- public static final NinePatchMetadataLoader INSTANCE = new NinePatchMetadataLoader();
-
- private static final Identifier ID = new Identifier("libgui", "9patch_metadata");
- private static final String SUFFIX = ".9patch";
-
- private Map<Identifier, TextureProperties> properties = Collections.emptyMap();
-
- public TextureProperties getProperties(Identifier texture) {
- return properties.getOrDefault(texture, TextureProperties.DEFAULT);
- }
-
- @Override
- public Identifier getFabricId() {
- return ID;
- }
-
- @Override
- protected Map<Identifier, Properties> prepare(ResourceManager manager, Profiler profiler) {
- Collection<Identifier> ids = manager.findResources("textures", s -> s.endsWith(SUFFIX));
- Map<Identifier, Properties> result = new HashMap<>();
-
- for (Identifier input : ids) {
- try (Resource resource = manager.getResource(input);
- InputStream stream = resource.getInputStream()) {
- Properties props = new Properties();
- props.load(stream);
- Identifier textureId = new Identifier(input.getNamespace(), input.getPath().substring(0, input.getPath().length() - SUFFIX.length()));
- result.put(textureId, props);
- } catch (Exception e) {
- LibGuiClient.logger.error("Error while loading metadata file {}, skipping...", input, e);
- }
- }
-
- return result;
- }
-
- @Override
- protected void apply(Map<Identifier, Properties> meta, ResourceManager manager, Profiler profiler) {
- properties = new HashMap<>();
- for (Map.Entry<Identifier, Properties> entry : meta.entrySet()) {
- Identifier id = entry.getKey();
- Properties props = entry.getValue();
-
- BackgroundPainter.NinePatch.Mode mode = TextureProperties.DEFAULT.getMode();
- float cornerUv = TextureProperties.DEFAULT.getCornerUv();
-
- if (props.containsKey("mode")) {
- String modeStr = props.getProperty("mode");
- mode = BackgroundPainter.NinePatch.Mode.fromString(modeStr);
- if (mode == null) {
- LibGuiClient.logger.error("Invalid mode '{}' in nine-patch metadata file for texture {}", modeStr, id);
- continue;
- }
- }
-
- if (props.containsKey("cornerUv")) {
- cornerUv = Float.parseFloat(props.getProperty("cornerUv"));
- }
-
- TextureProperties texProperties = new TextureProperties(mode, cornerUv);
- properties.put(id, texProperties);
- }
- }
-
- public static class TextureProperties {
- public static final TextureProperties DEFAULT = new TextureProperties(BackgroundPainter.NinePatch.Mode.STRETCHING, 0.25f);
-
- private final BackgroundPainter.NinePatch.Mode mode;
- private final float cornerUv;
-
- public TextureProperties(BackgroundPainter.NinePatch.Mode mode, float cornerUv) {
- this.mode = mode;
- this.cornerUv = cornerUv;
- }
-
- public BackgroundPainter.NinePatch.Mode getMode() {
- return mode;
- }
-
- public float getCornerUv() {
- return cornerUv;
- }
- }
-}
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
index b415970..d17835b 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTitle.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTitle.java
@@ -1,6 +1,7 @@
package io.github.cottonmc.cotton.gui.widget;
import io.github.cottonmc.cotton.gui.client.BackgroundPainter;
+import io.github.cottonmc.cotton.gui.client.NinePatch;
import io.github.cottonmc.cotton.gui.client.ScreenDrawing;
import io.github.cottonmc.cotton.gui.widget.data.Alignment;
import net.fabricmc.api.EnvType;
@@ -17,7 +18,7 @@ import javax.annotation.Nullable;
*
* <p>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.
+ * a {@linkplain NinePatch nine-patch background painter} with a padding of 8.
*/
public class WTitle extends WWidget {
public static final BackgroundPainter DEFAULT_BACKGROUND_PAINTER =