From 5a147e615c3b66bd21fb2d07db11a83e5b9d64fb Mon Sep 17 00:00:00 2001 From: isXander Date: Fri, 19 Jan 2024 17:05:59 +0000 Subject: Finalise the image reloader --- .../yacl3/gui/image/ImageRendererManager.java | 21 ++++++++++++++------- .../yacl3/gui/image/YACLImageReloadListener.java | 7 +++++-- .../image/impl/AnimatedDynamicTextureImage.java | 8 ++++++++ .../platform/neoforge/YACLForgeEntrypoint.java | 1 - .../main/java/dev/isxander/yacl3/test/GuiTest.java | 18 +++++++++++++++--- .../assets/yacl3/reach-around-placement.webp | Bin 14840 -> 0 bytes .../yacl3/textures/reach-around-placement.webp | Bin 0 -> 14840 bytes .../resources/assets/yacl3/textures/sample-1.webp | Bin 0 -> 10474 bytes .../resources/assets/yacl3/textures/sample-2.webp | Bin 0 -> 22308 bytes .../resources/assets/yacl3/textures/sample-3.webp | Bin 0 -> 17078 bytes .../resources/assets/yacl3/textures/sample-4.webp | Bin 0 -> 20772 bytes .../resources/assets/yacl3/textures/sample-5.webp | Bin 0 -> 11166 bytes 12 files changed, 42 insertions(+), 13 deletions(-) delete mode 100644 test-common/src/main/resources/assets/yacl3/reach-around-placement.webp create mode 100644 test-common/src/main/resources/assets/yacl3/textures/reach-around-placement.webp create mode 100644 test-common/src/main/resources/assets/yacl3/textures/sample-1.webp create mode 100644 test-common/src/main/resources/assets/yacl3/textures/sample-2.webp create mode 100644 test-common/src/main/resources/assets/yacl3/textures/sample-3.webp create mode 100644 test-common/src/main/resources/assets/yacl3/textures/sample-4.webp create mode 100644 test-common/src/main/resources/assets/yacl3/textures/sample-5.webp diff --git a/common/src/main/java/dev/isxander/yacl3/gui/image/ImageRendererManager.java b/common/src/main/java/dev/isxander/yacl3/gui/image/ImageRendererManager.java index 0c9b8a3..2f7ef50 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/image/ImageRendererManager.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/image/ImageRendererManager.java @@ -3,14 +3,16 @@ package dev.isxander.yacl3.gui.image; import com.mojang.blaze3d.systems.RenderSystem; import dev.isxander.yacl3.gui.image.impl.AnimatedDynamicTextureImage; import dev.isxander.yacl3.impl.utils.YACLConstants; +import dev.isxander.yacl3.platform.YACLPlatform; import net.minecraft.client.Minecraft; import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.packs.resources.Resource; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.concurrent.*; -import java.util.function.Function; +import java.util.function.BiFunction; import java.util.function.Predicate; import java.util.function.Supplier; @@ -23,11 +25,11 @@ public class ImageRendererManager { static final List PRELOADED_IMAGE_FACTORIES = List.of( new PreloadedImageFactory( location -> location.getPath().endsWith(".webp"), - AnimatedDynamicTextureImage::createWEBPFromTexture + AnimatedDynamicTextureImage::createWEBPFromResource ), new PreloadedImageFactory( location -> location.getPath().endsWith(".gif"), - AnimatedDynamicTextureImage::createGIFFromTexture + AnimatedDynamicTextureImage::createGIFFromResource ) ); @@ -37,6 +39,13 @@ public class ImageRendererManager { } if (IMAGE_CACHE.containsKey(id)) { + // warn developers if they don't put their webp/gif images `/textures` folder + if (YACLPlatform.isDevelopmentEnv()) { + if (PRELOADED_IMAGE_FACTORIES.stream().anyMatch(factory -> factory.predicate().test(id))) { + YACLConstants.LOGGER.error("Image '{}' not preloaded. MAKE SURE THAT ALL YACL WEBP/GIF IMAGES ARE INSIDE YOUR ASSETS `/textures` FOLDER, ELSE THEY WILL NOT BE PRELOADED!!! THIS ERROR WILL NOT APPEAR IN PROD", id); + } + } + return Optional.ofNullable((T) IMAGE_CACHE.get(id).getNow(null)); } @@ -45,8 +54,6 @@ public class ImageRendererManager { @SuppressWarnings("unchecked") public static CompletableFuture registerImage(ResourceLocation id, ImageRendererFactory factory) { - System.out.println(PRELOADED_IMAGE_CACHE.get(id)); - if (IMAGE_CACHE.containsKey(id)) { return (CompletableFuture) IMAGE_CACHE.get(id); } @@ -66,7 +73,7 @@ public class ImageRendererManager { return (CompletableFuture) future; } - private static void completeImageFactory(ResourceLocation id, Supplier> supplier, CompletableFuture future) { + private static void completeImageFactory(ResourceLocation id, Supplier> supplier, CompletableFuture future) { RenderSystem.assertOnRenderThread(); ImageRendererFactory.ImageSupplier completableImage = supplier.get().orElse(null); @@ -111,7 +118,7 @@ public class ImageRendererManager { } } - public record PreloadedImageFactory(Predicate predicate, Function factory) { + public record PreloadedImageFactory(Predicate predicate, BiFunction factory) { } private record CompletedSupplier(T get) implements Supplier { diff --git a/common/src/main/java/dev/isxander/yacl3/gui/image/YACLImageReloadListener.java b/common/src/main/java/dev/isxander/yacl3/gui/image/YACLImageReloadListener.java index cc259b0..bac8c56 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/image/YACLImageReloadListener.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/image/YACLImageReloadListener.java @@ -28,8 +28,9 @@ public class YACLImageReloadListener implements PreparableReloadListener { Executor backgroundExecutor, Executor gameExecutor ) { + YACLConstants.LOGGER.info("YACL is reloading images"); Map imageResources = resourceManager.listResources( - "", + "textures", location -> ImageRendererManager.PRELOADED_IMAGE_FACTORIES .stream() .anyMatch(factory -> factory.predicate().test(location)) @@ -51,7 +52,7 @@ public class YACLImageReloadListener implements PreparableReloadListener { ImageRendererFactory imageFactory = ImageRendererManager.PRELOADED_IMAGE_FACTORIES .stream() .filter(factory -> factory.predicate().test(location)) - .map(factory -> factory.factory().apply(location)) + .map(factory -> factory.factory().apply(resource, location)) .findAny() .orElseThrow(); @@ -94,6 +95,8 @@ public class YACLImageReloadListener implements PreparableReloadListener { }); } + YACLConstants.LOGGER.info("YACL has found {} images", imageResources.size()); + return CompletableFuture.allOf(futures.toArray(CompletableFuture[]::new)); } } diff --git a/common/src/main/java/dev/isxander/yacl3/gui/image/impl/AnimatedDynamicTextureImage.java b/common/src/main/java/dev/isxander/yacl3/gui/image/impl/AnimatedDynamicTextureImage.java index 39ddb55..2136f56 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/image/impl/AnimatedDynamicTextureImage.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/image/impl/AnimatedDynamicTextureImage.java @@ -103,6 +103,10 @@ public class AnimatedDynamicTextureImage extends DynamicTextureImage { }; } + public static ImageRendererFactory createGIFFromResource(Resource resource, ResourceLocation resourceLocation) { + return () -> createGIFSupplier(resource.open(), resourceLocation); + } + public static ImageRendererFactory createGIFFromPath(Path path, ResourceLocation uniqueLocation) { return () -> createGIFSupplier(new FileInputStream(path.toFile()), uniqueLocation); } @@ -116,6 +120,10 @@ public class AnimatedDynamicTextureImage extends DynamicTextureImage { }; } + public static ImageRendererFactory createWEBPFromResource(Resource resource, ResourceLocation resourceLocation) { + return () -> createWEBPSupplier(resource.open(), resourceLocation); + } + public static ImageRendererFactory createWEBPFromPath(Path path, ResourceLocation uniqueLocation) { return () -> createWEBPSupplier(new FileInputStream(path.toFile()), uniqueLocation); } diff --git a/neoforge/src/main/java/dev/isxander/yacl3/platform/neoforge/YACLForgeEntrypoint.java b/neoforge/src/main/java/dev/isxander/yacl3/platform/neoforge/YACLForgeEntrypoint.java index 4dfe2dd..488cbb1 100644 --- a/neoforge/src/main/java/dev/isxander/yacl3/platform/neoforge/YACLForgeEntrypoint.java +++ b/neoforge/src/main/java/dev/isxander/yacl3/platform/neoforge/YACLForgeEntrypoint.java @@ -11,7 +11,6 @@ import net.neoforged.neoforge.common.NeoForge; public class YACLForgeEntrypoint { public YACLForgeEntrypoint(IEventBus modEventBus) { modEventBus.addListener(RegisterClientReloadListenersEvent.class, event -> { - System.out.println("image reload event"); event.registerReloadListener(new YACLImageReloadListener()); }); } diff --git a/test-common/src/main/java/dev/isxander/yacl3/test/GuiTest.java b/test-common/src/main/java/dev/isxander/yacl3/test/GuiTest.java index 52a51e1..f81db33 100644 --- a/test-common/src/main/java/dev/isxander/yacl3/test/GuiTest.java +++ b/test-common/src/main/java/dev/isxander/yacl3/test/GuiTest.java @@ -83,7 +83,7 @@ public class GuiTest { .append(Component.literal("e").withStyle(style -> style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.literal("e"))))) .withStyle(style -> style.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://isxander.dev"))) ) - .webpImage(new ResourceLocation("yacl3", "reach-around-placement.webp")) + .webpImage(new ResourceLocation("yacl3", "textures/reach-around-placement.webp")) .build()) .binding( defaults.booleanToggle, @@ -100,7 +100,7 @@ public class GuiTest { .name(Component.literal("Custom Boolean Toggle")) .description(val -> OptionDescription.createBuilder() .text(Component.literal("You can customize controllers like so! YACL is truly infinitely customizable! This tooltip is long in order to demonstrate the cool, smooth scrolling of these descriptions. Did you know, they are also super clickable?! I know, cool right, YACL 3.x really is amazing.")) - .image(Path.of("D:\\Xander\\Downloads\\_MG_0860-Enhanced-NR.png"), new ResourceLocation("yacl", "f.webp")) // TODO: Add img file to git? + .webpImage(new ResourceLocation("yacl3", "textures/sample-1.webp")) .build()) .binding( defaults.customBooleanToggle, @@ -114,7 +114,10 @@ public class GuiTest { .build()) .option(Option.createBuilder() .name(Component.literal("Tick Box")) - .description(OptionDescription.of(Component.literal("There are even alternate methods of displaying the same data type!"))) + .description(OptionDescription.createBuilder() + .text(Component.literal("There are even alternate methods of displaying the same data type!")) + .webpImage(new ResourceLocation("yacl3", "textures/sample-2.webp")) + .build()) .binding( defaults.tickbox, () -> config.tickbox, @@ -127,6 +130,9 @@ public class GuiTest { .name(Component.literal("Slider Controllers")) .option(Option.createBuilder() .name(Component.literal("Int Slider")) + .description(OptionDescription.createBuilder() + .webpImage(new ResourceLocation("yacl3", "textures/sample-3.webp")) + .build()) .binding( defaults.intSlider, () -> config.intSlider, @@ -136,6 +142,9 @@ public class GuiTest { .build()) .option(Option.createBuilder() .name(Component.literal("Double Slider")) + .description(OptionDescription.createBuilder() + .webpImage(new ResourceLocation("yacl3", "textures/sample-4.webp")) + .build()) .binding( defaults.doubleSlider, () -> config.doubleSlider, @@ -145,6 +154,9 @@ public class GuiTest { .build()) .option(Option.createBuilder() .name(Component.literal("Float Slider")) + .description(OptionDescription.createBuilder() + .webpImage(new ResourceLocation("yacl3", "textures/sample-5.webp")) + .build()) .binding( defaults.floatSlider, () -> config.floatSlider, diff --git a/test-common/src/main/resources/assets/yacl3/reach-around-placement.webp b/test-common/src/main/resources/assets/yacl3/reach-around-placement.webp deleted file mode 100644 index 1937809..0000000 Binary files a/test-common/src/main/resources/assets/yacl3/reach-around-placement.webp and /dev/null differ diff --git a/test-common/src/main/resources/assets/yacl3/textures/reach-around-placement.webp b/test-common/src/main/resources/assets/yacl3/textures/reach-around-placement.webp new file mode 100644 index 0000000..1937809 Binary files /dev/null and b/test-common/src/main/resources/assets/yacl3/textures/reach-around-placement.webp differ diff --git a/test-common/src/main/resources/assets/yacl3/textures/sample-1.webp b/test-common/src/main/resources/assets/yacl3/textures/sample-1.webp new file mode 100644 index 0000000..0da983e Binary files /dev/null and b/test-common/src/main/resources/assets/yacl3/textures/sample-1.webp differ diff --git a/test-common/src/main/resources/assets/yacl3/textures/sample-2.webp b/test-common/src/main/resources/assets/yacl3/textures/sample-2.webp new file mode 100644 index 0000000..e887f8c Binary files /dev/null and b/test-common/src/main/resources/assets/yacl3/textures/sample-2.webp differ diff --git a/test-common/src/main/resources/assets/yacl3/textures/sample-3.webp b/test-common/src/main/resources/assets/yacl3/textures/sample-3.webp new file mode 100644 index 0000000..eda78a9 Binary files /dev/null and b/test-common/src/main/resources/assets/yacl3/textures/sample-3.webp differ diff --git a/test-common/src/main/resources/assets/yacl3/textures/sample-4.webp b/test-common/src/main/resources/assets/yacl3/textures/sample-4.webp new file mode 100644 index 0000000..8bbe329 Binary files /dev/null and b/test-common/src/main/resources/assets/yacl3/textures/sample-4.webp differ diff --git a/test-common/src/main/resources/assets/yacl3/textures/sample-5.webp b/test-common/src/main/resources/assets/yacl3/textures/sample-5.webp new file mode 100644 index 0000000..ed91379 Binary files /dev/null and b/test-common/src/main/resources/assets/yacl3/textures/sample-5.webp differ -- cgit