diff options
Diffstat (limited to 'src')
13 files changed, 142 insertions, 111 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/LibGui.java b/src/main/java/io/github/cottonmc/cotton/gui/client/LibGui.java index 90bb985..f781f81 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/LibGui.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/LibGui.java @@ -1,5 +1,7 @@ package io.github.cottonmc.cotton.gui.client; +import io.github.cottonmc.cotton.gui.impl.client.LibGuiClient; + /** * This class provides access to LibGui configuration and other global data. * 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 index 0375d8d..29b2bea 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/NinePatch.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/NinePatch.java @@ -2,25 +2,14 @@ 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.client.util.math.MatrixStack; -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 io.github.cottonmc.cotton.gui.impl.client.NinePatchInternals; import io.github.cottonmc.cotton.gui.widget.WWidget; import org.jetbrains.annotations.Nullable; -import java.io.InputStream; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; - /** * The nine-patch background painter paints rectangles using a nine-patch texture. * @@ -179,7 +168,7 @@ public class NinePatch implements BackgroundPainter { int y2 = top + height - cornerSize; float uv1 = cornerUv; float uv2 = 1.0f - cornerUv; - Mode mode = this.mode != null ? this.mode : MetadataLoader.INSTANCE.getProperties(texture).getMode(); + 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); @@ -240,8 +229,15 @@ public class NinePatch implements BackgroundPainter { */ 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 - static Mode fromString(String str) { + public static Mode fromString(String str) { if (str == null) return null; if (str.equalsIgnoreCase("stretching")) return STRETCHING; @@ -251,83 +247,4 @@ public class NinePatch implements BackgroundPainter { } } - public static class TextureProperties { - public static final TextureProperties DEFAULT = new TextureProperties(Mode.STRETCHING); - - private final Mode mode; - - public TextureProperties(Mode mode) { - this.mode = mode; - } - - public Mode getMode() { - return mode; - } - } - - 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); - properties.put(id, texProperties); - } - } - } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/package-info.java b/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/package-info.java deleted file mode 100644 index ad040bf..0000000 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * Mod Menu support for LibGui. - */ -package io.github.cottonmc.cotton.gui.client.modmenu; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/LibGuiClient.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/LibGuiClient.java index 3c1f72f..97109c2 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/LibGuiClient.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/LibGuiClient.java @@ -1,4 +1,4 @@ -package io.github.cottonmc.cotton.gui.client; +package io.github.cottonmc.cotton.gui.impl.client; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; @@ -13,18 +13,14 @@ import io.github.cottonmc.cotton.gui.impl.ScreenNetworkingImpl; import io.github.cottonmc.jankson.JanksonFactory; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.jetbrains.annotations.ApiStatus; import java.io.File; import java.io.FileOutputStream; import java.nio.charset.StandardCharsets; -@ApiStatus.Internal public class LibGuiClient implements ClientModInitializer { public static final Logger logger = LogManager.getLogger(); public static final String MODID = "libgui"; - /** @deprecated Replaced with {@link LibGui} */ - @Deprecated public static volatile LibGuiConfig config; public static final Jankson jankson = JanksonFactory.createJankson(); @@ -33,7 +29,7 @@ public class LibGuiClient implements ClientModInitializer { public void onInitializeClient() { config = loadConfig(); - ResourceManagerHelper.get(ResourceType.CLIENT_RESOURCES).registerReloadListener(NinePatch.MetadataLoader.INSTANCE); + 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/client/LibGuiConfig.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/LibGuiConfig.java index b971e31..b1985af 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/LibGuiConfig.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/LibGuiConfig.java @@ -1,4 +1,4 @@ -package io.github.cottonmc.cotton.gui.client; +package io.github.cottonmc.cotton.gui.impl.client; import blue.endless.jankson.Comment; 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 new file mode 100644 index 0000000..7a18413 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/NinePatchInternals.java @@ -0,0 +1,104 @@ +package io.github.cottonmc.cotton.gui.impl.client; + +import io.github.cottonmc.cotton.gui.client.NinePatch; + +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.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 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(); + + NinePatch.Mode mode = TextureProperties.DEFAULT.getMode(); +// float cornerUv = TextureProperties.DEFAULT.getCornerUv(); + + if (props.containsKey("mode")) { + String modeStr = props.getProperty("mode"); + mode = 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); + properties.put(id, texProperties); + } + } + } +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/package-info.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/package-info.java new file mode 100644 index 0000000..16c1859 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/package-info.java @@ -0,0 +1,7 @@ +/** + * Internal implementation classes. + */ +@ApiStatus.Internal +package io.github.cottonmc.cotton.gui.impl.client; + +import org.jetbrains.annotations.ApiStatus; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/ConfigGui.java index cd3fe0a..625b67c 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/ConfigGui.java @@ -1,11 +1,11 @@ -package io.github.cottonmc.cotton.gui.client.modmenu; +package io.github.cottonmc.cotton.gui.impl.modmenu; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; import net.minecraft.text.TranslatableText; import io.github.cottonmc.cotton.gui.client.BackgroundPainter; -import io.github.cottonmc.cotton.gui.client.LibGuiClient; +import io.github.cottonmc.cotton.gui.impl.client.LibGuiClient; import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription; import io.github.cottonmc.cotton.gui.widget.WButton; import io.github.cottonmc.cotton.gui.widget.WGridPanel; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ModMenuSupport.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/ModMenuSupport.java index ccdff93..80b57bf 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ModMenuSupport.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/ModMenuSupport.java @@ -1,4 +1,4 @@ -package io.github.cottonmc.cotton.gui.client.modmenu; +package io.github.cottonmc.cotton.gui.impl.modmenu; import net.minecraft.text.TranslatableText; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/WKirbSprite.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/WKirbSprite.java index 2f7832d..4ea5324 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/WKirbSprite.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/WKirbSprite.java @@ -1,4 +1,4 @@ -package io.github.cottonmc.cotton.gui.client.modmenu; +package io.github.cottonmc.cotton.gui.impl.modmenu; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/package-info.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/package-info.java new file mode 100644 index 0000000..68b85be --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/package-info.java @@ -0,0 +1,7 @@ +/** + * Mod Menu support for LibGui. + */ +@ApiStatus.Internal +package io.github.cottonmc.cotton.gui.impl.modmenu; + +import org.jetbrains.annotations.ApiStatus; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java index 1fc6018..78133b5 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java @@ -1,5 +1,7 @@ package io.github.cottonmc.cotton.gui.widget; +import io.github.cottonmc.cotton.gui.impl.client.LibGuiConfig; + import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; @@ -33,7 +35,7 @@ public class WLabel extends WWidget { public static final int DEFAULT_TEXT_COLOR = 0x404040; /** - * The default text color for {@linkplain io.github.cottonmc.cotton.gui.client.LibGuiConfig#darkMode dark mode} labels. + * The default text color for {@linkplain LibGuiConfig#darkMode dark mode} labels. */ public static final int DEFAULT_DARKMODE_TEXT_COLOR = 0xbcbcbc; diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index b540edf..1030297 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -19,8 +19,8 @@ "environment": "*", "entrypoints": { "main": ["io.github.cottonmc.cotton.gui.impl.LibGuiCommon"], - "client": ["io.github.cottonmc.cotton.gui.client.LibGuiClient"], - "modmenu": ["io.github.cottonmc.cotton.gui.client.modmenu.ModMenuSupport"] + "client": ["io.github.cottonmc.cotton.gui.impl.client.LibGuiClient"], + "modmenu": ["io.github.cottonmc.cotton.gui.impl.modmenu.ModMenuSupport"] }, "mixins": [ "mixins.libgui.accessors.json" |