diff options
Diffstat (limited to 'common/src/main/java/dev/isxander')
4 files changed, 26 insertions, 49 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 { diff --git a/common/src/main/java/dev/isxander/yacl/impl/OptionImpl.java b/common/src/main/java/dev/isxander/yacl/impl/OptionImpl.java index ef4d13b..27f43b8 100644 --- a/common/src/main/java/dev/isxander/yacl/impl/OptionImpl.java +++ b/common/src/main/java/dev/isxander/yacl/impl/OptionImpl.java @@ -52,8 +52,7 @@ public final class OptionImpl<T> implements Option<T> { this.listeners = new ArrayList<>(listeners); this.controller = controlGetter.apply(this); - var memoizedDescriptionFunction = Util.memoize(descriptionFunction); - addListener((opt, pending) -> description = memoizedDescriptionFunction.apply(pending)); + addListener((opt, pending) -> description = descriptionFunction.apply(pending)); requestSet(binding().getValue()); } |