From cf556dd0fdf2d5bf142fdff0c39d3711232d67e3 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Thu, 23 Jan 2020 22:13:53 +0200 Subject: Add support for loading nine-patch metadata from resource packs --- .../cotton/gui/client/BackgroundPainter.java | 60 +++++++++----- .../cottonmc/cotton/gui/client/LibGuiClient.java | 4 + .../cotton/gui/client/NinePatchMetadataLoader.java | 91 ++++++++++++++++++++++ 3 files changed, 136 insertions(+), 19 deletions(-) create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/client/NinePatchMetadataLoader.java (limited to 'src') 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 65cf95f..24cf6ab 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 @@ -5,6 +5,8 @@ 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. * The background painter instance of a widget can be changed to customize the look of a widget. @@ -128,11 +130,30 @@ public interface BackgroundPainter { * *
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. The default mode is {@link BackgroundPainter.NinePatch.Mode#TILING}. + * 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}. * *
{@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)}. + * + *
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). + *
Metadata files use {@linkplain java.util.Properties .properties format} with the following keys: + *
Key | + *Value | + *
---|---|
{@code mode} | + *{@link Mode#STRETCHING stretching} | {@link Mode#TILING tiling} | + *
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 tile() { - this.mode = Mode.TILING; + public NinePatch setMode(@Nullable Mode mode) { + this.mode = mode; return this; } @@ -271,6 +282,7 @@ public interface BackgroundPainter { int y2 = top + height - cornerSize; float uv1 = cornerUv; float uv2 = 1.0f - cornerUv; + Mode mode = this.mode != null ? this.mode : NinePatchMetadataLoader.INSTANCE.getProperties(texture).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); @@ -322,14 +334,24 @@ public interface BackgroundPainter { 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. - * This is the default mode. */ 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 b06369a..d457e50 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 @@ -5,8 +5,10 @@ import blue.endless.jankson.JsonElement; import blue.endless.jankson.JsonObject; import io.github.cottonmc.jankson.JanksonFactory; import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.resource.ResourceManagerHelper; import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.resource.ResourceType; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -24,6 +26,8 @@ public class LibGuiClient implements ClientModInitializer { @Override public void onInitializeClient() { config = loadConfig(); + + ResourceManagerHelper.get(ResourceType.CLIENT_RESOURCES).registerReloadListener(NinePatchMetadataLoader.INSTANCE); } public static LibGuiConfig loadConfig() { 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 new file mode 100644 index 0000000..daa1b45 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/NinePatchMetadataLoader.java @@ -0,0 +1,91 @@ +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