aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorisXander <xander@isxander.dev>2024-04-14 21:11:32 +0100
committerisXander <xander@isxander.dev>2024-04-14 21:11:32 +0100
commit26aec79e10025ff3427ceb47602156ebd670b2ac (patch)
tree280515927ea98bc510385d81de2d508f94910126 /src
parentbb0690af2536edf9ff920c0c229de8b919215850 (diff)
downloadYetAnotherConfigLib-26aec79e10025ff3427ceb47602156ebd670b2ac.tar.gz
YetAnotherConfigLib-26aec79e10025ff3427ceb47602156ebd670b2ac.tar.bz2
YetAnotherConfigLib-26aec79e10025ff3427ceb47602156ebd670b2ac.zip
Fix gifs not being preloaded + add testmod samples
Diffstat (limited to 'src')
-rw-r--r--src/main/java/dev/isxander/yacl3/gui/image/ImageRendererManager.java12
-rw-r--r--src/main/java/dev/isxander/yacl3/impl/OptionDescriptionImpl.java21
-rw-r--r--src/testmod/java/dev/isxander/yacl3/test/GuiTest.java16
-rw-r--r--src/testmod/resources/assets/yacl_test/textures/images/sample1.webpbin0 -> 10474 bytes
-rw-r--r--src/testmod/resources/assets/yacl_test/textures/images/sample3.webpbin0 -> 2058520 bytes
-rw-r--r--src/testmod/resources/assets/yacl_test/textures/images/sample4.gifbin0 -> 13717 bytes
6 files changed, 30 insertions, 19 deletions
diff --git a/src/main/java/dev/isxander/yacl3/gui/image/ImageRendererManager.java b/src/main/java/dev/isxander/yacl3/gui/image/ImageRendererManager.java
index 0c9b8a3..5674c85 100644
--- a/src/main/java/dev/isxander/yacl3/gui/image/ImageRendererManager.java
+++ b/src/main/java/dev/isxander/yacl3/gui/image/ImageRendererManager.java
@@ -44,8 +44,10 @@ public class ImageRendererManager {
}
@SuppressWarnings("unchecked")
- public static <T extends ImageRenderer> CompletableFuture<T> registerImage(ResourceLocation id, ImageRendererFactory factory) {
- System.out.println(PRELOADED_IMAGE_CACHE.get(id));
+ public static <T extends ImageRenderer> CompletableFuture<T> registerOrGetImage(ResourceLocation id, Supplier<ImageRendererFactory> factorySupplier) {
+ if (PRELOADED_IMAGE_CACHE.containsKey(id)) {
+ return CompletableFuture.completedFuture((T) PRELOADED_IMAGE_CACHE.get(id));
+ }
if (IMAGE_CACHE.containsKey(id)) {
return (CompletableFuture<T>) IMAGE_CACHE.get(id);
@@ -54,6 +56,7 @@ public class ImageRendererManager {
var future = new CompletableFuture<ImageRenderer>();
IMAGE_CACHE.put(id, future);
+ ImageRendererFactory factory = factorySupplier.get();
SINGLE_THREAD_EXECUTOR.submit(() -> {
Supplier<Optional<ImageRendererFactory.ImageSupplier>> supplier =
factory.requiresOffThreadPreparation()
@@ -66,6 +69,11 @@ public class ImageRendererManager {
return (CompletableFuture<T>) future;
}
+ @Deprecated
+ public static <T extends ImageRenderer> CompletableFuture<T> registerImage(ResourceLocation id, ImageRendererFactory factory) {
+ return registerOrGetImage(id, () -> factory);
+ }
+
private static <T extends ImageRenderer> void completeImageFactory(ResourceLocation id, Supplier<Optional<ImageRendererFactory.ImageSupplier>> supplier, CompletableFuture<ImageRenderer> future) {
RenderSystem.assertOnRenderThread();
diff --git a/src/main/java/dev/isxander/yacl3/impl/OptionDescriptionImpl.java b/src/main/java/dev/isxander/yacl3/impl/OptionDescriptionImpl.java
index 67fa6a6..96664a4 100644
--- a/src/main/java/dev/isxander/yacl3/impl/OptionDescriptionImpl.java
+++ b/src/main/java/dev/isxander/yacl3/impl/OptionDescriptionImpl.java
@@ -11,8 +11,6 @@ import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;
import org.apache.commons.lang3.Validate;
-import java.io.FileInputStream;
-import java.io.IOException;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.CompletableFuture;
@@ -41,7 +39,7 @@ public record OptionDescriptionImpl(Component text, CompletableFuture<Optional<I
Validate.isTrue(width > 0, "Width must be greater than 0!");
Validate.isTrue(height > 0, "Height must be greater than 0!");
- this.image = ImageRendererManager.registerImage(image, ResourceTextureImage.createFactory(image, 0, 0, width, height, width, height)).thenApply(Optional::of);
+ this.image = ImageRendererManager.registerOrGetImage(image, () -> ResourceTextureImage.createFactory(image, 0, 0, width, height, width, height)).thenApply(Optional::of);
imageUnset = false;
return this;
}
@@ -52,7 +50,7 @@ public record OptionDescriptionImpl(Component text, CompletableFuture<Optional<I
Validate.isTrue(width > 0, "Width must be greater than 0!");
Validate.isTrue(height > 0, "Height must be greater than 0!");
- this.image = ImageRendererManager.registerImage(image, ResourceTextureImage.createFactory(image, u, v, width, height, textureWidth, textureHeight)).thenApply(Optional::of);
+ this.image = ImageRendererManager.registerOrGetImage(image, () -> ResourceTextureImage.createFactory(image, u, v, width, height, textureWidth, textureHeight)).thenApply(Optional::of);
imageUnset = false;
return this;
}
@@ -61,7 +59,7 @@ public record OptionDescriptionImpl(Component text, CompletableFuture<Optional<I
public Builder image(Path path, ResourceLocation uniqueLocation) {
Validate.isTrue(imageUnset, "Image already set!");
- this.image = ImageRendererManager.registerImage(uniqueLocation, DynamicTextureImage.fromPath(path, uniqueLocation)).thenApply(Optional::of);
+ this.image = ImageRendererManager.registerOrGetImage(uniqueLocation, () -> DynamicTextureImage.fromPath(path, uniqueLocation)).thenApply(Optional::of);
imageUnset = false;
return this;
}
@@ -70,7 +68,7 @@ public record OptionDescriptionImpl(Component text, CompletableFuture<Optional<I
public Builder gifImage(ResourceLocation image) {
Validate.isTrue(imageUnset, "Image already set!");
- this.image = ImageRendererManager.registerImage(image, AnimatedDynamicTextureImage.createGIFFromTexture(image)).thenApply(Optional::of);
+ this.image = ImageRendererManager.registerOrGetImage(image, () -> AnimatedDynamicTextureImage.createGIFFromTexture(image)).thenApply(Optional::of);
imageUnset = false;
return this;
}
@@ -79,7 +77,7 @@ public record OptionDescriptionImpl(Component text, CompletableFuture<Optional<I
public Builder gifImage(Path path, ResourceLocation uniqueLocation) {
Validate.isTrue(imageUnset, "Image already set!");
- this.image = ImageRendererManager.registerImage(uniqueLocation, AnimatedDynamicTextureImage.createGIFFromPath(path, uniqueLocation)).thenApply(Optional::of);
+ this.image = ImageRendererManager.registerOrGetImage(uniqueLocation, () -> AnimatedDynamicTextureImage.createGIFFromPath(path, uniqueLocation)).thenApply(Optional::of);
imageUnset = false;
return this;
}
@@ -88,12 +86,7 @@ public record OptionDescriptionImpl(Component text, CompletableFuture<Optional<I
public Builder webpImage(ResourceLocation image) {
Validate.isTrue(imageUnset, "Image already set!");
- Optional<ImageRenderer> completedImage = ImageRendererManager.getImage(image);
- if (completedImage.isPresent()) {
- this.image = CompletableFuture.completedFuture(completedImage);
- } else {
- this.image = ImageRendererManager.registerImage(image, AnimatedDynamicTextureImage.createWEBPFromTexture(image)).thenApply(Optional::of);
- }
+ this.image = ImageRendererManager.registerOrGetImage(image, () -> AnimatedDynamicTextureImage.createWEBPFromTexture(image)).thenApply(Optional::of);
imageUnset = false;
return this;
@@ -103,7 +96,7 @@ public record OptionDescriptionImpl(Component text, CompletableFuture<Optional<I
public Builder webpImage(Path path, ResourceLocation uniqueLocation) {
Validate.isTrue(imageUnset, "Image already set!");
- this.image = ImageRendererManager.registerImage(uniqueLocation, AnimatedDynamicTextureImage.createWEBPFromPath(path, uniqueLocation)).thenApply(Optional::of);
+ this.image = ImageRendererManager.registerOrGetImage(uniqueLocation, () -> AnimatedDynamicTextureImage.createWEBPFromPath(path, uniqueLocation)).thenApply(Optional::of);
imageUnset = false;
return this;
}
diff --git a/src/testmod/java/dev/isxander/yacl3/test/GuiTest.java b/src/testmod/java/dev/isxander/yacl3/test/GuiTest.java
index 34ae06a..3ddfce6 100644
--- a/src/testmod/java/dev/isxander/yacl3/test/GuiTest.java
+++ b/src/testmod/java/dev/isxander/yacl3/test/GuiTest.java
@@ -64,7 +64,7 @@ public class GuiTest {
private static Screen getFullTestSuite(Screen parent) {
AtomicReference<Option<Boolean>> booleanOption = new AtomicReference<>();
- ConfigTest.GSON.serializer().load();
+ ConfigTest.GSON.load();
return YetAnotherConfigLib.create(ConfigTest.GSON, (defaults, config, builder) -> builder
.title(Component.literal("Test GUI"))
.category(ConfigCategory.createBuilder()
@@ -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-test", "reach-around-placement.webp"))
+ .webpImage(imageSample("sample1.webp"))
.build())
.binding(
defaults.booleanToggle,
@@ -113,7 +113,10 @@ public class GuiTest {
.build())
.option(Option.<Boolean>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(imageSample("sample3.webp"))
+ .build())
.binding(
defaults.tickbox,
() -> config.tickbox,
@@ -126,6 +129,9 @@ public class GuiTest {
.name(Component.literal("Slider Controllers"))
.option(Option.<Integer>createBuilder()
.name(Component.literal("Int Slider"))
+ .description(OptionDescription.createBuilder()
+ .gifImage(imageSample("sample4.gif"))
+ .build())
.binding(
defaults.intSlider,
() -> config.intSlider,
@@ -489,6 +495,10 @@ public class GuiTest {
.generateScreen(parent);
}
+ private static ResourceLocation imageSample(String name) {
+ return new ResourceLocation("yacl_test", "textures/images/" + name);
+ }
+
private static boolean myBooleanOption = true;
private static Screen getWikiGetStarted(Screen parent) {
diff --git a/src/testmod/resources/assets/yacl_test/textures/images/sample1.webp b/src/testmod/resources/assets/yacl_test/textures/images/sample1.webp
new file mode 100644
index 0000000..0da983e
--- /dev/null
+++ b/src/testmod/resources/assets/yacl_test/textures/images/sample1.webp
Binary files differ
diff --git a/src/testmod/resources/assets/yacl_test/textures/images/sample3.webp b/src/testmod/resources/assets/yacl_test/textures/images/sample3.webp
new file mode 100644
index 0000000..bc91220
--- /dev/null
+++ b/src/testmod/resources/assets/yacl_test/textures/images/sample3.webp
Binary files differ
diff --git a/src/testmod/resources/assets/yacl_test/textures/images/sample4.gif b/src/testmod/resources/assets/yacl_test/textures/images/sample4.gif
new file mode 100644
index 0000000..9231bae
--- /dev/null
+++ b/src/testmod/resources/assets/yacl_test/textures/images/sample4.gif
Binary files differ