aboutsummaryrefslogtreecommitdiff
path: root/common/src/main/java/dev/isxander/yacl/gui
diff options
context:
space:
mode:
authorisXander <xandersmith2008@gmail.com>2023-05-21 14:38:00 +0100
committerisXander <xandersmith2008@gmail.com>2023-05-21 14:38:00 +0100
commit590e69f4bf445a39737b0b1552cf116ff780d75e (patch)
treeab3915921839b15829e6c02232dc02f73ec027fa /common/src/main/java/dev/isxander/yacl/gui
parentc84415116455d108ad07fc8dd6232c9acc94c40f (diff)
downloadYetAnotherConfigLib-590e69f4bf445a39737b0b1552cf116ff780d75e.tar.gz
YetAnotherConfigLib-590e69f4bf445a39737b0b1552cf116ff780d75e.tar.bz2
YetAnotherConfigLib-590e69f4bf445a39737b0b1552cf116ff780d75e.zip
Cache image renderers for whole game's lifecycle
Diffstat (limited to 'common/src/main/java/dev/isxander/yacl/gui')
-rw-r--r--common/src/main/java/dev/isxander/yacl/gui/ImageRenderer.java29
-rw-r--r--common/src/main/java/dev/isxander/yacl/gui/OptionDescriptionWidget.java19
-rw-r--r--common/src/main/java/dev/isxander/yacl/gui/YACLScreen.java24
3 files changed, 25 insertions, 47 deletions
diff --git a/common/src/main/java/dev/isxander/yacl/gui/ImageRenderer.java b/common/src/main/java/dev/isxander/yacl/gui/ImageRenderer.java
index 1376a27..46deb87 100644
--- a/common/src/main/java/dev/isxander/yacl/gui/ImageRenderer.java
+++ b/common/src/main/java/dev/isxander/yacl/gui/ImageRenderer.java
@@ -3,7 +3,6 @@ package dev.isxander.yacl.gui;
import com.mojang.blaze3d.Blaze3D;
import com.mojang.blaze3d.platform.NativeImage;
import com.twelvemonkeys.imageio.plugins.webp.WebPImageReaderSpi;
-import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.texture.DynamicTexture;
@@ -12,8 +11,6 @@ import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.util.FastColor;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
@@ -21,17 +18,37 @@ import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataNode;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
-import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
+import java.lang.reflect.Field;
import java.nio.file.Path;
+import java.util.List;
+import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
-import java.util.stream.IntStream;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Supplier;
-public interface ImageRenderer extends AutoCloseable {
+public interface ImageRenderer {
int render(GuiGraphics graphics, int x, int y, int width);
+ void close();
+
+ Map<ResourceLocation, CompletableFuture<Optional<ImageRenderer>>> CACHE = new ConcurrentHashMap<>();
+
+ static CompletableFuture<Optional<ImageRenderer>> getOrMakeAsync(ResourceLocation id, Supplier<Optional<ImageRenderer>> factory) {
+ return CACHE.computeIfAbsent(id, key -> CompletableFuture.supplyAsync(factory));
+ }
+
+ static CompletableFuture<Optional<ImageRenderer>> getOrMakeSync(ResourceLocation id, Supplier<Optional<ImageRenderer>> factory) {
+ return CACHE.computeIfAbsent(id, key -> CompletableFuture.completedFuture(factory.get()));
+ }
+
+ static void closeAll() {
+ CACHE.values().forEach(future -> future.thenAccept(opt -> opt.ifPresent(ImageRenderer::close)));
+ CACHE.clear();
+ }
+
class TextureBacked implements ImageRenderer {
private final ResourceLocation location;
private final int width, height;
diff --git a/common/src/main/java/dev/isxander/yacl/gui/OptionDescriptionWidget.java b/common/src/main/java/dev/isxander/yacl/gui/OptionDescriptionWidget.java
index d9ad045..882d75b 100644
--- a/common/src/main/java/dev/isxander/yacl/gui/OptionDescriptionWidget.java
+++ b/common/src/main/java/dev/isxander/yacl/gui/OptionDescriptionWidget.java
@@ -5,7 +5,6 @@ import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
-import net.minecraft.client.gui.components.MultiLineLabel;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.client.gui.navigation.ScreenRectangle;
import net.minecraft.network.chat.Component;
@@ -17,7 +16,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.function.Supplier;
-public class OptionDescriptionWidget extends AbstractWidget implements AutoCloseable {
+public class OptionDescriptionWidget extends AbstractWidget {
private @Nullable OptionDescription description;
private List<FormattedCharSequence> wrappedText;
@@ -134,23 +133,7 @@ public class OptionDescriptionWidget extends AbstractWidget implements AutoClose
}
public void setOptionDescription(OptionDescription description) {
- //this.close();
this.description = description;
this.wrappedText = null;
}
-
- @Override
- public void close() {
- if (description != null) {
- description.image().thenAccept(image -> {
- if (image.isPresent()) {
- try {
- image.get().close();
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- });
- }
- }
}
diff --git a/common/src/main/java/dev/isxander/yacl/gui/YACLScreen.java b/common/src/main/java/dev/isxander/yacl/gui/YACLScreen.java
index 77090c8..0ce98ba 100644
--- a/common/src/main/java/dev/isxander/yacl/gui/YACLScreen.java
+++ b/common/src/main/java/dev/isxander/yacl/gui/YACLScreen.java
@@ -56,10 +56,6 @@ public class YACLScreen extends Screen {
@Override
protected void init() {
- if (tabs != null) {
- closeTabs();
- }
-
tabNavigationBar = TabNavigationBar.builder(tabManager, this.width)
.addTabs(tabs = config.categories()
.stream()
@@ -181,19 +177,6 @@ public class YACLScreen extends Screen {
@Override
public void onClose() {
minecraft.setScreen(parent);
- closeTabs();
- }
-
- private void closeTabs() {
- for (Tab tab : tabs) {
- if (tab instanceof Closeable closeable) {
- try {
- closeable.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
}
public static void renderMultilineTooltip(GuiGraphics graphics, Font font, MultiLineLabel text, int centerX, int yAbove, int yBelow, int screenWidth, int screenHeight) {
@@ -241,7 +224,7 @@ public class YACLScreen extends Screen {
}
}
- private class CategoryTab implements Tab, Closeable {
+ private class CategoryTab implements Tab {
private final ConfigCategory category;
private final TabListWidget<OptionListWidget> optionList;
@@ -344,11 +327,6 @@ public class YACLScreen extends Screen {
cancelResetButton.setMessage(pendingChanges ? GuiUtils.translatableFallback("yacl.gui.cancel", CommonComponents.GUI_CANCEL) : Component.translatable("controls.reset"));
cancelResetButton.setTooltip(Tooltip.create(pendingChanges ? Component.translatable("yacl.gui.cancel.tooltip") : Component.translatable("yacl.gui.reset.tooltip")));
}
-
- @Override
- public void close() {
- descriptionWidget.close();
- }
}
private class PlaceholderTab implements Tab {