From da554fce60f1cba794a9c062b3ded81caf1f57b9 Mon Sep 17 00:00:00 2001 From: Juuz <6596629+Juuxel@users.noreply.github.com> Date: Thu, 27 May 2021 19:38:15 +0300 Subject: Replace bad 9patch with LibNinePatch, fix bug with ItemIcon Also adds nicer tests with client-side commands. /libgui config: opens the config GUI without Mod Menu /libgui tab: opens a tab test GUI --- .../cotton/gui/client/BackgroundPainter.java | 41 +++- .../cottonmc/cotton/gui/client/NinePatch.java | 250 --------------------- .../cotton/gui/impl/client/LibGuiClient.java | 4 - .../cotton/gui/impl/client/NinePatchInternals.java | 105 --------- .../impl/client/NinePatchTextureRendererImpl.java | 36 +++ .../cottonmc/cotton/gui/widget/WTabPanel.java | 21 +- .../cottonmc/cotton/gui/widget/icon/ItemIcon.java | 2 + 7 files changed, 83 insertions(+), 376 deletions(-) delete mode 100644 src/main/java/io/github/cottonmc/cotton/gui/client/NinePatch.java delete mode 100644 src/main/java/io/github/cottonmc/cotton/gui/impl/client/NinePatchInternals.java create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/impl/client/NinePatchTextureRendererImpl.java (limited to 'src/main/java/io') 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 fd0a12a..113464c 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,8 +4,14 @@ 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; +import juuxel.libninepatch.NinePatch; +import juuxel.libninepatch.TextureRegion; + +import java.util.function.Consumer; /** * Background painters are used to paint the background of a widget. @@ -112,28 +118,39 @@ public interface BackgroundPainter { /** * Creates a new nine-patch background painter. * - *
This method is equivalent to {@code new NinePatch(texture)}. + *
The resulting painter has a corner size of 4 px and a corner UV of 0.25. * * @param texture the background painter texture * @return a new nine-patch background painter * @since 1.5.0 */ - public static NinePatch createNinePatch(Identifier texture) { - return new NinePatch(texture); + public static BackgroundPainter createNinePatch(Identifier texture) { + return createNinePatch(new Texture(texture), builder -> builder.cornerSize(4).cornerUv(0.25f)); } /** - * Creates a new nine-patch background painter with a custom padding. - * - *
This method is equivalent to {@code new NinePatch(texture).setPadding(padding)}.
+ * Creates a new nine-patch background painter with a custom configuration.
*
- * @param texture the background painter texture
- * @param padding the padding of the painter
- * @return a new nine-patch background painter
- * @since 1.5.0
+ * @param texture the background painter texture
+ * @param configurator a consumer that configures the {@link NinePatch.Builder}
+ * @return the created nine-patch background painter
+ * @since 4.0.0
+ * @see NinePatch
+ * @see NinePatch.Builder
*/
- public static NinePatch createNinePatch(Identifier texture, int padding) {
- return new NinePatch(texture).setPadding(padding);
+ public static BackgroundPainter createNinePatch(Texture texture, Consumer 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}.
- *
- * {@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 the .properties format} with the following keys:
- * 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(MatrixStack matrices, int left, int top, WWidget panel) {
- 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 uv1 = cornerUv;
- float uv2 = 1.0f - cornerUv;
- Mode mode = this.mode != null ? this.mode : NinePatchInternals.MetadataLoader.INSTANCE.getProperties(texture).getMode();
-
- ScreenDrawing.texturedRect(matrices, left, top, cornerSize, cornerSize, texture, 0, 0, uv1, uv1, 0xFF_FFFFFF);
- ScreenDrawing.texturedRect(matrices, x2, top, cornerSize, cornerSize, texture, uv2, 0, 1, uv1, 0xFF_FFFFFF);
- ScreenDrawing.texturedRect(matrices, left, y2, cornerSize, cornerSize, texture, 0, uv2, uv1, 1, 0xFF_FFFFFF);
- ScreenDrawing.texturedRect(matrices, 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(matrices, x1 + i * tileSize, top, tileWidth, cornerSize, texture, uv1, 0, uv2 - uo, uv1, 0xFF_FFFFFF);
- ScreenDrawing.texturedRect(matrices, 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(matrices, left, y1 + j * tileSize, cornerSize, tileHeight, texture, 0, uv1, uv1, uv2 - vo, 0xFF_FFFFFF);
- ScreenDrawing.texturedRect(matrices, x2, y1 + j * tileSize, cornerSize, tileHeight, texture, uv2, uv1, 1, uv2 - vo, 0xFF_FFFFFF);
-
- ScreenDrawing.texturedRect(matrices, x1 + i * tileSize, y1 + j * tileSize, tileWidth, tileHeight, texture, uv1, uv1, uv2 - uo, uv2 - vo, 0xFF_FFFFFF);
- heightLeft -= tileSize;
- }
- widthLeft -= tileSize;
- }
- } else {
- ScreenDrawing.texturedRect(matrices, x1, top, width - 2 * cornerSize, cornerSize, texture, uv1, 0, uv2, uv1, 0xFF_FFFFFF);
- ScreenDrawing.texturedRect(matrices, left, y1, cornerSize, height - 2 * cornerSize, texture, 0, uv1, uv1, uv2, 0xFF_FFFFFF);
- ScreenDrawing.texturedRect(matrices, x1, y2, width - 2 * cornerSize, cornerSize, texture, uv1, uv2, uv2, 1, 0xFF_FFFFFF);
- ScreenDrawing.texturedRect(matrices, x2, y1, cornerSize, height - 2 * cornerSize, texture, uv2, uv1, 1, uv2, 0xFF_FFFFFF);
-
- ScreenDrawing.texturedRect(matrices, 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;
-
- /**
- * Deserializes a nine-patch mode from a string.
- *
- * @param str the mode string
- * @return the mode, or null if the string is invalid
- * @since 4.0.0
- */
- @Nullable
- public 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/impl/client/LibGuiClient.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/LibGuiClient.java
index acd8c5e..6bc69fe 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/LibGuiClient.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/LibGuiClient.java
@@ -2,9 +2,7 @@ package io.github.cottonmc.cotton.gui.impl.client;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
-import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.loader.api.FabricLoader;
-import net.minecraft.resource.ResourceType;
import blue.endless.jankson.Jankson;
import blue.endless.jankson.JsonElement;
@@ -29,8 +27,6 @@ public class LibGuiClient implements ClientModInitializer {
public void onInitializeClient() {
config = loadConfig();
- ResourceManagerHelper.get(ResourceType.CLIENT_RESOURCES).registerReloadListener(NinePatchInternals.MetadataLoader.INSTANCE);
-
ClientPlayNetworking.registerGlobalReceiver(ScreenNetworkingImpl.SCREEN_MESSAGE_S2C, (client, networkHandler, buf, responseSender) -> {
ScreenNetworkingImpl.handle(client, client.player, buf);
});
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/NinePatchInternals.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/NinePatchInternals.java
deleted file mode 100644
index ddb4301..0000000
--- a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/NinePatchInternals.java
+++ /dev/null
@@ -1,105 +0,0 @@
-package io.github.cottonmc.cotton.gui.impl.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.SinglePreparationResourceReloader;
-import net.minecraft.util.Identifier;
-import net.minecraft.util.profiler.Profiler;
-
-import io.github.cottonmc.cotton.gui.client.NinePatch;
-import io.github.cottonmc.cotton.gui.impl.LibGuiCommon;
-
-import java.io.InputStream;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Properties;
-
-@Environment(EnvType.CLIENT)
-public final class NinePatchInternals {
- @Environment(EnvType.CLIENT)
- public static class TextureProperties {
- public static final TextureProperties DEFAULT = new TextureProperties(NinePatch.Mode.STRETCHING);
-
- private final NinePatch.Mode mode;
-
- public TextureProperties(NinePatch.Mode mode) {
- this.mode = mode;
- }
-
- public NinePatch.Mode getMode() {
- return mode;
- }
- }
-
- @Environment(EnvType.CLIENT)
- public static class MetadataLoader extends SinglePreparationResourceReloaderNine-patch metadata
- * 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.
- *
- *
- *
- * @since 1.5.0
- */
-@Environment(EnvType.CLIENT)
-public class NinePatch implements BackgroundPainter {
- private final Identifier texture;
- private final int cornerSize;
- private final float cornerUv;
- 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 and a 0.25 corner UV.
- *
- * @param texture the texture ID
- */
- public NinePatch(Identifier texture) {
- this(texture, 4, 0.25f);
- }
-
- /**
- * Creates a nine-patch background painter.
- *
- * @param texture the texture ID
- * @param cornerSize the size of the corners on the screen
- * @param cornerUv the fraction of the corners from the whole texture
- */
- public NinePatch(Identifier texture, int cornerSize, float cornerUv) {
- this.texture = texture;
- this.cornerSize = cornerSize;
- this.cornerUv = cornerUv;
- }
-
- 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.
- *
- *
- * Key
- * Value
- * Description
- *
- *
- * {@code mode}
- * {@link Mode#STRETCHING stretching} | {@link Mode#TILING tiling}
- * The texture filling mode
- *