From 3e36feeef60e56ef8cb7f737ac8eeab9fbcd6abb Mon Sep 17 00:00:00 2001 From: isXander Date: Sat, 3 Jun 2023 23:10:03 +0100 Subject: Change package and modid to yacl3 and yet_another_config_lib_3 respectively --- .../dev/isxander/yacl3/gui/AbstractWidget.java | 94 ++++ .../isxander/yacl3/gui/DescriptionWithName.java | 11 + .../isxander/yacl3/gui/ElementListWidgetExt.java | 222 ++++++++ .../java/dev/isxander/yacl3/gui/ImageRenderer.java | 386 ++++++++++++++ .../isxander/yacl3/gui/LowProfileButtonWidget.java | 28 + .../yacl3/gui/OptionDescriptionWidget.java | 215 ++++++++ .../dev/isxander/yacl3/gui/OptionListWidget.java | 572 +++++++++++++++++++++ .../isxander/yacl3/gui/RequireRestartScreen.java | 21 + .../dev/isxander/yacl3/gui/SearchFieldWidget.java | 61 +++ .../isxander/yacl3/gui/TextScaledButtonWidget.java | 34 ++ .../isxander/yacl3/gui/TooltipButtonWidget.java | 25 + .../java/dev/isxander/yacl3/gui/YACLScreen.java | 358 +++++++++++++ .../isxander/yacl3/gui/YACLTooltipPositioner.java | 48 ++ .../yacl3/gui/controllers/ActionController.java | 120 +++++ .../yacl3/gui/controllers/BooleanController.java | 157 ++++++ .../yacl3/gui/controllers/ColorController.java | 220 ++++++++ .../yacl3/gui/controllers/ControllerWidget.java | 157 ++++++ .../yacl3/gui/controllers/LabelController.java | 193 +++++++ .../yacl3/gui/controllers/ListEntryWidget.java | 128 +++++ .../yacl3/gui/controllers/TickBoxController.java | 119 +++++ .../cycling/CyclingControllerElement.java | 60 +++ .../controllers/cycling/CyclingListController.java | 79 +++ .../gui/controllers/cycling/EnumController.java | 43 ++ .../controllers/cycling/ICyclingController.java | 38 ++ .../yacl3/gui/controllers/package-info.java | 12 + .../controllers/slider/DoubleSliderController.java | 114 ++++ .../controllers/slider/FloatSliderController.java | 114 ++++ .../gui/controllers/slider/ISliderController.java | 54 ++ .../slider/IntegerSliderController.java | 111 ++++ .../controllers/slider/LongSliderController.java | 111 ++++ .../slider/SliderControllerElement.java | 157 ++++++ .../yacl3/gui/controllers/slider/package-info.java | 10 + .../gui/controllers/string/IStringController.java | 44 ++ .../gui/controllers/string/StringController.java | 37 ++ .../string/StringControllerElement.java | 403 +++++++++++++++ .../string/number/DoubleFieldController.java | 104 ++++ .../string/number/FloatFieldController.java | 104 ++++ .../string/number/IntegerFieldController.java | 109 ++++ .../string/number/LongFieldController.java | 109 ++++ .../string/number/NumberFieldController.java | 69 +++ .../controllers/string/number/package-info.java | 10 + .../isxander/yacl3/gui/tab/ListHolderWidget.java | 116 +++++ .../yacl3/gui/tab/ScrollableNavigationBar.java | 110 ++++ .../java/dev/isxander/yacl3/gui/tab/TabExt.java | 9 + .../dev/isxander/yacl3/gui/utils/GuiUtils.java | 32 ++ 45 files changed, 5328 insertions(+) create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/AbstractWidget.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/DescriptionWithName.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/ElementListWidgetExt.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/ImageRenderer.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/LowProfileButtonWidget.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/OptionDescriptionWidget.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/OptionListWidget.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/RequireRestartScreen.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/SearchFieldWidget.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/TextScaledButtonWidget.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/TooltipButtonWidget.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/YACLScreen.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/YACLTooltipPositioner.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/ActionController.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/BooleanController.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/ColorController.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/ControllerWidget.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/LabelController.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/ListEntryWidget.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/TickBoxController.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/cycling/CyclingControllerElement.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/cycling/CyclingListController.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/cycling/EnumController.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/cycling/ICyclingController.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/package-info.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/DoubleSliderController.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/FloatSliderController.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/ISliderController.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/IntegerSliderController.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/LongSliderController.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/SliderControllerElement.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/package-info.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/string/IStringController.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/string/StringController.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/string/StringControllerElement.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/DoubleFieldController.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/FloatFieldController.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/IntegerFieldController.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/LongFieldController.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/NumberFieldController.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/package-info.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/tab/ListHolderWidget.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/tab/ScrollableNavigationBar.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/tab/TabExt.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/utils/GuiUtils.java (limited to 'common/src/main/java/dev/isxander/yacl3/gui') diff --git a/common/src/main/java/dev/isxander/yacl3/gui/AbstractWidget.java b/common/src/main/java/dev/isxander/yacl3/gui/AbstractWidget.java new file mode 100644 index 0000000..8b9779c --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/gui/AbstractWidget.java @@ -0,0 +1,94 @@ +package dev.isxander.yacl3.gui; + +import dev.isxander.yacl3.api.utils.Dimension; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.Font; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.gui.components.Renderable; +import net.minecraft.client.gui.components.events.GuiEventListener; +import net.minecraft.client.gui.narration.NarratableEntry; +import net.minecraft.client.gui.narration.NarrationElementOutput; +import net.minecraft.client.resources.sounds.SimpleSoundInstance; +import net.minecraft.sounds.SoundEvents; + +import java.awt.*; + +public abstract class AbstractWidget implements GuiEventListener, Renderable, NarratableEntry { + protected final Minecraft client = Minecraft.getInstance(); + protected final Font textRenderer = client.font; + protected final int inactiveColor = 0xFFA0A0A0; + + private Dimension dim; + + public AbstractWidget(Dimension dim) { + this.dim = dim; + } + + public boolean canReset() { + return false; + } + + @Override + public boolean isMouseOver(double mouseX, double mouseY) { + if (dim == null) return false; + return this.dim.isPointInside((int) mouseX, (int) mouseY); + } + + public void setDimension(Dimension dim) { + this.dim = dim; + } + + public Dimension getDimension() { + return dim; + } + + @Override + public NarrationPriority narrationPriority() { + return NarrationPriority.NONE; + } + + public void unfocus() { + + } + + public boolean matchesSearch(String query) { + return true; + } + + @Override + public void updateNarration(NarrationElementOutput builder) { + + } + + protected void drawButtonRect(GuiGraphics graphics, int x1, int y1, int x2, int y2, boolean hovered, boolean enabled) { + if (x1 > x2) { + int xx1 = x1; + x1 = x2; + x2 = xx1; + } + if (y1 > y2) { + int yy1 = y1; + y1 = y2; + y2 = yy1; + } + int width = x2 - x1; + int height = y2 - y1; + + int i = !enabled ? 0 : hovered ? 2 : 1; + graphics.blit(net.minecraft.client.gui.components.AbstractWidget.WIDGETS_LOCATION, x1, y1, 0, 0, 46 + i * 20, width / 2, height, 256, 256); + graphics.blit(net.minecraft.client.gui.components.AbstractWidget.WIDGETS_LOCATION, x1 + width / 2, y1, 0, 200 - width / 2f, 46 + i * 20, width / 2, height, 256, 256); + } + + protected int multiplyColor(int hex, float amount) { + Color color = new Color(hex, true); + + return new Color(Math.max((int)(color.getRed() * amount), 0), + Math.max((int)(color.getGreen() * amount), 0), + Math.max((int)(color.getBlue() * amount), 0), + color.getAlpha()).getRGB(); + } + + public void playDownSound() { + Minecraft.getInstance().getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1.0F)); + } +} diff --git a/common/src/main/java/dev/isxander/yacl3/gui/DescriptionWithName.java b/common/src/main/java/dev/isxander/yacl3/gui/DescriptionWithName.java new file mode 100644 index 0000000..6ad72e8 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/gui/DescriptionWithName.java @@ -0,0 +1,11 @@ +package dev.isxander.yacl3.gui; + +import dev.isxander.yacl3.api.OptionDescription; +import net.minecraft.ChatFormatting; +import net.minecraft.network.chat.Component; + +public record DescriptionWithName(Component name, OptionDescription description) { + public static DescriptionWithName of(Component name, OptionDescription description) { + return new DescriptionWithName(name.copy().withStyle(ChatFormatting.BOLD), description); + } +} diff --git a/common/src/main/java/dev/isxander/yacl3/gui/ElementListWidgetExt.java b/common/src/main/java/dev/isxander/yacl3/gui/ElementListWidgetExt.java new file mode 100644 index 0000000..e3944ee --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/gui/ElementListWidgetExt.java @@ -0,0 +1,222 @@ +package dev.isxander.yacl3.gui; + +import com.mojang.blaze3d.platform.InputConstants; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.gui.components.AbstractWidget; +import net.minecraft.client.gui.components.ContainerObjectSelectionList; +import net.minecraft.client.gui.components.events.GuiEventListener; +import net.minecraft.client.gui.layouts.LayoutElement; +import net.minecraft.client.gui.navigation.ScreenRectangle; +import net.minecraft.util.Mth; +import org.jetbrains.annotations.Nullable; + +import java.util.function.Consumer; + +public class ElementListWidgetExt> extends ContainerObjectSelectionList implements LayoutElement { + protected int x, y; + + private double smoothScrollAmount = getScrollAmount(); + private boolean returnSmoothAmount = false; + private final boolean doSmoothScrolling; + + public ElementListWidgetExt(Minecraft client, int x, int y, int width, int height, boolean smoothScrolling) { + super(client, width, height, y, y + height, 22); + this.x = this.x0 = x; + this.y = y; + this.x1 = this.x0 + width; + this.doSmoothScrolling = smoothScrolling; + } + + @Override + public boolean mouseScrolled(double mouseX, double mouseY, double amount) { + // default implementation bases scroll step from total height of entries, this is constant + this.setScrollAmount(this.getScrollAmount() - amount * 20); + return true; + } + + @Override + protected void renderBackground(GuiGraphics graphics) { + // render transparent background if in-game. + setRenderBackground(true); + setRenderTopAndBottom(false); + } + + @Override + protected int getScrollbarPosition() { + // default implementation does not respect left/right + return this.x1 - 2; + } + + @Override + public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) { + smoothScrollAmount = Mth.lerp(Minecraft.getInstance().getDeltaFrameTime() * 0.5, smoothScrollAmount, getScrollAmount()); + returnSmoothAmount = true; + + graphics.enableScissor(x0, y0, x1, y1); + + super.render(graphics, mouseX, mouseY, delta); + + graphics.disableScissor(); + + returnSmoothAmount = false; + } + + public void updateDimensions(ScreenRectangle rectangle) { + this.x0 = rectangle.left(); + this.y0 = rectangle.top(); + this.x1 = rectangle.right(); + this.y1 = rectangle.bottom(); + this.width = rectangle.width(); + this.height = rectangle.height(); + } + + /** + * awful code to only use smooth scroll state when rendering, + * not other code that needs target scroll amount + */ + @Override + public double getScrollAmount() { + if (returnSmoothAmount && doSmoothScrolling) + return smoothScrollAmount; + + return super.getScrollAmount(); + } + + protected void resetSmoothScrolling() { + this.smoothScrollAmount = getScrollAmount(); + } + + @Nullable + @Override + protected E getEntryAtPosition(double x, double y) { + y += getScrollAmount(); + + if (x < this.x0 || x > this.x1) + return null; + + int currentY = this.y0 - headerHeight + 4; + for (E entry : children()) { + if (y >= currentY && y <= currentY + entry.getItemHeight()) { + return entry; + } + + currentY += entry.getItemHeight(); + } + + return null; + } + + /* + below code is licensed from cloth-config under LGPL3 + modified to inherit vanilla's EntryListWidget and use yarn mappings + + code is responsible for having dynamic item heights + */ + + @Override + protected int getMaxPosition() { + return children().stream().map(E::getItemHeight).reduce(0, Integer::sum) + headerHeight; + } + + @Override + protected void centerScrollOn(E entry) { + double d = (this.height) / -2d; + for (int i = 0; i < this.children().indexOf(entry) && i < this.getItemCount(); i++) + d += children().get(i).getItemHeight(); + this.setScrollAmount(d); + } + + @Override + protected int getRowTop(int index) { + int integer = y0 + 4 - (int) this.getScrollAmount() + headerHeight; + for (int i = 0; i < children().size() && i < index; i++) + integer += children().get(i).getItemHeight(); + return integer; + } + + @Override + protected void renderList(GuiGraphics graphics, int mouseX, int mouseY, float delta) { + int left = this.getRowLeft(); + int right = this.getRowWidth(); + int count = this.getItemCount(); + + for(int i = 0; i < count; ++i) { + E entry = children().get(i); + int top = this.getRowTop(i); + int bottom = top + entry.getItemHeight(); + int entryHeight = entry.getItemHeight() - 4; + if (bottom >= this.y0 && top <= this.y1) { + this.renderItem(graphics, mouseX, mouseY, delta, i, left, top, right, entryHeight); + } + } + } + + /* END cloth config code */ + + @Override + public void setX(int i) { + this.x = x0 = i; + this.x1 = x0 + width; + } + + @Override + public void setY(int i) { + this.y = y0 = i; + this.y1 = y0 + height; + } + + @Override + public int getX() { + return x; + } + + @Override + public int getY() { + return y; + } + + @Override + public int getWidth() { + return width; + } + + @Override + public int getHeight() { + return height; + } + + @Override + public void visitWidgets(Consumer consumer) { + } + + public abstract static class Entry> extends ContainerObjectSelectionList.Entry { + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + for (GuiEventListener child : this.children()) { + if (child.mouseClicked(mouseX, mouseY, button)) { + if (button == InputConstants.MOUSE_BUTTON_LEFT) + this.setDragging(true); + return true; + } + } + + return false; + } + + @Override + public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) { + if (isDragging() && button == InputConstants.MOUSE_BUTTON_LEFT) { + for (GuiEventListener child : this.children()) { + if (child.mouseDragged(mouseX, mouseY, button, deltaX, deltaY)) + return true; + } + } + return false; + } + + public int getItemHeight() { + return 22; + } + } +} diff --git a/common/src/main/java/dev/isxander/yacl3/gui/ImageRenderer.java b/common/src/main/java/dev/isxander/yacl3/gui/ImageRenderer.java new file mode 100644 index 0000000..5b5da97 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/gui/ImageRenderer.java @@ -0,0 +1,386 @@ +package dev.isxander.yacl3.gui; + +import com.mojang.blaze3d.Blaze3D; +import com.mojang.blaze3d.platform.NativeImage; +import com.twelvemonkeys.imageio.plugins.webp.WebPImageReaderSpi; +import dev.isxander.yacl3.impl.utils.YACLConstants; +import net.minecraft.CrashReport; +import net.minecraft.CrashReportCategory; +import net.minecraft.ReportedException; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.renderer.texture.DynamicTexture; +import net.minecraft.client.renderer.texture.TextureManager; +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 javax.imageio.ImageIO; +import javax.imageio.ImageReader; +import javax.imageio.metadata.IIOMetadata; +import javax.imageio.metadata.IIOMetadataNode; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.FileInputStream; +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.concurrent.ConcurrentHashMap; +import java.util.function.Supplier; +import java.util.stream.IntStream; + +public interface ImageRenderer { + int render(GuiGraphics graphics, int x, int y, int renderWidth); + + void close(); + + Map>> CACHE = new ConcurrentHashMap<>(); + + static CompletableFuture> getOrMakeAsync(ResourceLocation id, Supplier> factory) { + return CACHE.computeIfAbsent(id, key -> CompletableFuture.supplyAsync(factory, YACLConstants.SINGLE_THREAD_EXECUTOR)); + } + + static CompletableFuture> getOrMakeSync(ResourceLocation id, Supplier> 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; + private final int textureWidth, textureHeight; + private final float u, v; + + public TextureBacked(ResourceLocation location, float u, float v, int width, int height, int textureWidth, int textureHeight) { + this.location = location; + this.width = width; + this.height = height; + this.textureWidth = textureWidth; + this.textureHeight = textureHeight; + this.u = u; + this.v = v; + } + + @Override + public int render(GuiGraphics graphics, int x, int y, int renderWidth) { + float ratio = renderWidth / (float)this.width; + int targetHeight = (int) (this.height * ratio); + + graphics.pose().pushPose(); + graphics.pose().translate(x, y, 0); + graphics.pose().scale(ratio, ratio, 1); + graphics.blit(location, 0, 0, this.u, this.v, this.width, this.height, this.textureWidth, this.textureHeight); + graphics.pose().popPose(); + + return targetHeight; + } + + @Override + public void close() { + + } + } + + class NativeImageBacked implements ImageRenderer { + protected static final TextureManager textureManager = Minecraft.getInstance().getTextureManager(); + + protected NativeImage image; + protected DynamicTexture texture; + protected final ResourceLocation uniqueLocation; + protected final int width, height; + + public NativeImageBacked(NativeImage image, ResourceLocation uniqueLocation) { + this.image = image; + this.texture = new DynamicTexture(image); + this.uniqueLocation = uniqueLocation; + textureManager.register(this.uniqueLocation, this.texture); + this.width = image.getWidth(); + this.height = image.getHeight(); + } + + private NativeImageBacked(Path imagePath, ResourceLocation uniqueLocation) throws IOException { + this.uniqueLocation = uniqueLocation; + this.image = NativeImage.read(new FileInputStream(imagePath.toFile())); + this.width = image.getWidth(); + this.height = image.getHeight(); + this.texture = new DynamicTexture(image); + textureManager.register(this.uniqueLocation, this.texture); + } + + public static Optional createFromPath(Path path, ResourceLocation uniqueLocation) { + try { + return Optional.of(new NativeImageBacked(path, uniqueLocation)); + } catch (IOException e) { + e.printStackTrace(); + return Optional.empty(); + } + } + + @Override + public int render(GuiGraphics graphics, int x, int y, int renderWidth) { + if (image == null) return 0; + + float ratio = renderWidth / (float)this.width; + int targetHeight = (int) (this.height * ratio); + + graphics.pose().pushPose(); + graphics.pose().translate(x, y, 0); + graphics.pose().scale(ratio, ratio, 1); + graphics.blit(uniqueLocation, 0, 0, 0, 0, this.width, this.height, this.width, this.height); + graphics.pose().popPose(); + + return targetHeight; + } + + @Override + public void close() { + image.close(); + image = null; + texture = null; + textureManager.release(uniqueLocation); + } + } + + class AnimatedNativeImageBacked extends NativeImageBacked { + private int currentFrame; + private double lastFrameTime; + + private final double[] frameDelays; + private final int frameCount; + + private final int packCols, packRows; + private final int frameWidth, frameHeight; + + public AnimatedNativeImageBacked(NativeImage image, int frameWidth, int frameHeight, int frameCount, double[] frameDelayMS, int packCols, int packRows, ResourceLocation uniqueLocation) { + super(image, uniqueLocation); + this.frameWidth = frameWidth; + this.frameHeight = frameHeight; + this.frameCount = frameCount; + this.frameDelays = frameDelayMS; + this.packCols = packCols; + this.packRows = packRows; + } + + public static AnimatedNativeImageBacked createGIFFromTexture(ResourceLocation textureLocation) throws IOException { + ResourceManager resourceManager = Minecraft.getInstance().getResourceManager(); + Resource resource = resourceManager.getResource(textureLocation).orElseThrow(); + + return createGIF(resource.open(), textureLocation); + } + + public static AnimatedNativeImageBacked createWEBPFromTexture(ResourceLocation textureLocation) throws IOException { + ResourceManager resourceManager = Minecraft.getInstance().getResourceManager(); + Resource resource = resourceManager.getResource(textureLocation).orElseThrow(); + + return createWEBP(resource.open(), textureLocation); + } + + public static AnimatedNativeImageBacked createGIF(InputStream is, ResourceLocation uniqueLocation) { + try (is) { + ImageReader reader = ImageIO.getImageReadersBySuffix("gif").next(); + reader.setInput(ImageIO.createImageInputStream(is)); + + + + AnimFrameProvider animFrameFunction = i -> { + IIOMetadata metadata = reader.getImageMetadata(i); + String metaFormatName = metadata.getNativeMetadataFormatName(); + IIOMetadataNode root = (IIOMetadataNode) metadata.getAsTree(metaFormatName); + IIOMetadataNode graphicsControlExtensionNode = (IIOMetadataNode) root.getElementsByTagName("GraphicControlExtension").item(0); + int delay = Integer.parseInt(graphicsControlExtensionNode.getAttribute("delayTime")) * 10; + + return new AnimFrame(delay, 0, 0); + }; + + return createFromImageReader(reader, animFrameFunction, uniqueLocation); + } catch (Exception e) { + CrashReport crashReport = CrashReport.forThrowable(e, "Failed to load GIF image"); + CrashReportCategory category = crashReport.addCategory("YACL Gui"); + category.setDetail("Image identifier", uniqueLocation.toString()); + throw new ReportedException(crashReport); + } + } + + public static AnimatedNativeImageBacked createWEBP(InputStream is, ResourceLocation uniqueLocation) { + try (is) { + ImageReader reader = new WebPImageReaderSpi().createReaderInstance(); + reader.setInput(ImageIO.createImageInputStream(is)); + + int numImages = reader.getNumImages(true); // Force reading of all frames + AnimFrameProvider animFrameFunction = i -> null; + if (numImages > 1) { + // WebP reader does not expose frame delay, prepare for reflection hell + Class webpReaderClass = Class.forName("com.twelvemonkeys.imageio.plugins.webp.WebPImageReader"); + Field framesField = webpReaderClass.getDeclaredField("frames"); + framesField.setAccessible(true); + List frames = (List) framesField.get(reader); + + Class animationFrameClass = Class.forName("com.twelvemonkeys.imageio.plugins.webp.AnimationFrame"); + Field durationField = animationFrameClass.getDeclaredField("duration"); + durationField.setAccessible(true); + Field boundsField = animationFrameClass.getDeclaredField("bounds"); + boundsField.setAccessible(true); + + animFrameFunction = i -> { + Rectangle bounds = (Rectangle) boundsField.get(frames.get(i)); + return new AnimFrame((int) durationField.get(frames.get(i)), bounds.x, bounds.y); + }; + // that was fun + } + + return createFromImageReader(reader, animFrameFunction, uniqueLocation); + } catch (Throwable e) { + CrashReport crashReport = CrashReport.forThrowable(e, "Failed to load WEBP image"); + CrashReportCategory category = crashReport.addCategory("YACL Gui"); + category.setDetail("Image identifier", uniqueLocation.toString()); + throw new ReportedException(crashReport); + } + } + + private static AnimatedNativeImageBacked createFromImageReader(ImageReader reader, AnimFrameProvider animationProvider, ResourceLocation uniqueLocation) throws Exception { + int frameCount = reader.getNumImages(true); + + // Because this is being backed into a texture atlas, we need a maximum dimension + // so you can get the texture atlas size. + // Smaller frames are given black borders + int frameWidth = IntStream.range(reader.getMinIndex(), frameCount).map(i -> { + try { + return reader.getWidth(i); + } catch (IOException e) { + throw new RuntimeException(e); + } + }).max().orElseThrow(); + int frameHeight = IntStream.range(reader.getMinIndex(), frameCount).map(i -> { + try { + return reader.getHeight(i); + } catch (IOException e) { + throw new RuntimeException(e); + } + }).max().orElseThrow(); + + // Packs the frames into an optimal 1:1 texture. + // OpenGL can only have texture axis with a max of 32768 pixels, + // and packing them to that length is not efficient, apparently. + double ratio = frameWidth / (double)frameHeight; + int cols = (int)Math.ceil(Math.sqrt(frameCount) / Math.sqrt(ratio)); + int rows = (int)Math.ceil(frameCount / (double)cols); + + NativeImage image = new NativeImage(frameWidth * cols, frameHeight * rows, true); + + // Fill whole atlas with black, as each frame may have different dimensions + // that would cause borders of transparent pixels to appear around the frames + for (int x = 0; x < frameWidth * cols; x++) { + for (int y = 0; y < frameHeight * rows; y++) { + image.setPixelRGBA(x, y, 0xFF000000); + } + } + + BufferedImage bi = null; + Graphics2D graphics = null; + + // each frame may have a different delay + double[] frameDelays = new double[frameCount]; + + for (int i = reader.getMinIndex(); i < frameCount - 1; i++) { + AnimFrame frame = animationProvider.get(i); + if (frameCount > 1) // frame will be null if not animation + frameDelays[i] = frame.durationMS; + + if (bi == null) { + // first frame... + bi = reader.read(i); + graphics = bi.createGraphics(); + } else { + // WebP reader sometimes provides delta frames, (only the pixels that changed since the last frame) + // so instead of overwriting the image every frame, we draw delta frames on top of the previous frame + // to keep a complete image. + BufferedImage deltaFrame = reader.read(i); + graphics.drawImage(deltaFrame, frame.xOffset, frame.yOffset, null); + } + + // Each frame may have different dimensions, so we need to center them. + int xOffset = (frameWidth - bi.getWidth()) / 2; + int yOffset = (frameHeight - bi.getHeight()) / 2; + + for (int w = 0; w < bi.getWidth(); w++) { + for (int h = 0; h < bi.getHeight(); h++) { + int rgb = bi.getRGB(w, h); + int r = FastColor.ARGB32.red(rgb); + int g = FastColor.ARGB32.green(rgb); + int b = FastColor.ARGB32.blue(rgb); + + int col = i % cols; + int row = (int) Math.floor(i / (double)cols); + + image.setPixelRGBA( + frameWidth * col + w + xOffset, + frameHeight * row + h + yOffset, + FastColor.ABGR32.color(255, b, g, r) // NativeImage uses ABGR for some reason + ); + } + } + } + // gives the texture to GL for rendering + // usually, you create a native image with NativeImage.create, which sets the pixels and + // runs this function itself. In this case, we need to do it manually. + image.upload(0, 0, 0, false); + + graphics.dispose(); + reader.dispose(); + + return new AnimatedNativeImageBacked(image, frameWidth, frameHeight, frameCount, frameDelays, cols, rows, uniqueLocation); + } + + @Override + public int render(GuiGraphics graphics, int x, int y, int renderWidth) { + if (image == null) return 0; + + float ratio = renderWidth / (float)frameWidth; + int targetHeight = (int) (frameHeight * ratio); + + int currentCol = currentFrame % packCols; + int currentRow = (int) Math.floor(currentFrame / (double)packCols); + + graphics.pose().pushPose(); + graphics.pose().translate(x, y, 0); + graphics.pose().scale(ratio, ratio, 1); + graphics.blit( + uniqueLocation, + 0, 0, + frameWidth * currentCol, frameHeight * currentRow, + frameWidth, frameHeight, + this.width, this.height + ); + graphics.pose().popPose(); + + if (frameCount > 1) { + double timeMS = Blaze3D.getTime() * 1000; + if (lastFrameTime == 0) lastFrameTime = timeMS; + if (timeMS - lastFrameTime >= frameDelays[currentFrame]) { + currentFrame++; + lastFrameTime = timeMS; + } + if (currentFrame >= frameCount - 1) + currentFrame = 0; + } + + return targetHeight; + } + + @FunctionalInterface + private interface AnimFrameProvider { + AnimFrame get(int frame) throws Exception; + } + private record AnimFrame(int durationMS, int xOffset, int yOffset) {} + } +} diff --git a/common/src/main/java/dev/isxander/yacl3/gui/LowProfileButtonWidget.java b/common/src/main/java/dev/isxander/yacl3/gui/LowProfileButtonWidget.java new file mode 100644 index 0000000..3f5822f --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/gui/LowProfileButtonWidget.java @@ -0,0 +1,28 @@ +package dev.isxander.yacl3.gui; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.gui.components.Button; +import net.minecraft.client.gui.components.Tooltip; +import net.minecraft.network.chat.Component; + +public class LowProfileButtonWidget extends Button { + public LowProfileButtonWidget(int x, int y, int width, int height, Component message, OnPress onPress) { + super(x, y, width, height, message, onPress, DEFAULT_NARRATION); + } + + public LowProfileButtonWidget(int x, int y, int width, int height, Component message, OnPress onPress, Tooltip tooltip) { + this(x, y, width, height, message, onPress); + setTooltip(tooltip); + } + + @Override + public void renderWidget(GuiGraphics graphics, int mouseX, int mouseY, float deltaTicks) { + if (!isHoveredOrFocused() || !active) { + int j = this.active ? 0xFFFFFF : 0xA0A0A0; + this.renderString(graphics, Minecraft.getInstance().font, j); + } else { + super.renderWidget(graphics, mouseX, mouseY, deltaTicks); + } + } +} diff --git a/common/src/main/java/dev/isxander/yacl3/gui/OptionDescriptionWidget.java b/common/src/main/java/dev/isxander/yacl3/gui/OptionDescriptionWidget.java new file mode 100644 index 0000000..63371d6 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/gui/OptionDescriptionWidget.java @@ -0,0 +1,215 @@ +package dev.isxander.yacl3.gui; + +import com.mojang.blaze3d.Blaze3D; +import com.mojang.blaze3d.platform.InputConstants; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.ComponentPath; +import net.minecraft.client.gui.Font; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.gui.components.AbstractWidget; +import net.minecraft.client.gui.narration.NarratedElementType; +import net.minecraft.client.gui.narration.NarrationElementOutput; +import net.minecraft.client.gui.navigation.FocusNavigationEvent; +import net.minecraft.client.gui.navigation.ScreenRectangle; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.Style; +import net.minecraft.util.FormattedCharSequence; +import net.minecraft.util.Mth; +import org.jetbrains.annotations.Nullable; + +import java.util.List; +import java.util.function.Supplier; + +public class OptionDescriptionWidget extends AbstractWidget { + private static final int AUTO_SCROLL_TIMER = 1500; + private static final float AUTO_SCROLL_SPEED = 1; // lines per second + + private @Nullable DescriptionWithName description; + private List wrappedText; + + private static final Minecraft minecraft = Minecraft.getInstance(); + private static final Font font = minecraft.font; + + private Supplier dimensions; + + private float targetScrollAmount, currentScrollAmount; + private int maxScrollAmount; + private int descriptionY; + + private int lastInteractionTime; + private boolean scrollingBackward; + + public OptionDescriptionWidget(Supplier dimensions, @Nullable DescriptionWithName description) { + super(0, 0, 0, 0, description == null ? Component.empty() : description.name()); + this.dimensions = dimensions; + this.setOptionDescription(description); + } + + @Override + public void renderWidget(GuiGraphics graphics, int mouseX, int mouseY, float delta) { + if (description == null) return; + + currentScrollAmount = Mth.lerp(delta * 0.5f, currentScrollAmount, targetScrollAmount); + + ScreenRectangle dimensions = this.dimensions.get(); + this.setX(dimensions.left()); + this.setY(dimensions.top()); + this.width = dimensions.width(); + this.height = dimensions.height(); + + int y = getY(); + + int nameWidth = font.width(description.name()); + if (nameWidth > getWidth()) { + renderScrollingString(graphics, font, description.name(), getX(), y, getX() + getWidth(), y + font.lineHeight, -1); + } else { + graphics.drawString(font, description.name(), getX(), y, 0xFFFFFF); + } + + y += 5 + font.lineHeight; + + graphics.enableScissor(getX(), y, getX() + getWidth(), getY() + getHeight()); + + y -= (int)currentScrollAmount; + + if (description.description().image().isDone()) { + var image = description.description().image().join(); + if (image.isPresent()) { + image.get().render(graphics, getX(), y, getWidth()); + y += image.get().render(graphics, getX(), y, getWidth()) + 5; + } + } + + if (wrappedText == null) + wrappedText = font.split(description.description().text(), getWidth()); + + descriptionY = y; + for (var line : wrappedText) { + graphics.drawString(font, line, getX(), y, 0xFFFFFF); + y += font.lineHeight; + } + + graphics.disableScissor(); + + maxScrollAmount = Math.max(0, y + (int)currentScrollAmount - getY() - getHeight()); + + if (isHoveredOrFocused()) { + lastInteractionTime = currentTimeMS(); + } + Style hoveredStyle = getDescStyle(mouseX, mouseY); + if (hoveredStyle != null && hoveredStyle.getHoverEvent() != null) { + graphics.renderComponentHoverEffect(font, hoveredStyle, mouseX, mouseY); + } + + if (isFocused()) { + graphics.renderOutline(getX(), getY(), getWidth(), getHeight(), -1); + } + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + Style clickedStyle = getDescStyle((int) mouseX, (int) mouseY); + if (clickedStyle != null && clickedStyle.getClickEvent() != null) { + if (minecraft.screen.handleComponentClicked(clickedStyle)) { + playDownSound(minecraft.getSoundManager()); + return true; + } + return false; + } + + return false; + } + + @Override + public boolean mouseScrolled(double mouseX, double mouseY, double amount) { + if (isMouseOver(mouseX, mouseY)) { + targetScrollAmount = Mth.clamp(targetScrollAmount - (int) amount * 10, 0, maxScrollAmount); + lastInteractionTime = currentTimeMS(); + return true; + } + return false; + } + + @Override + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { + if (isFocused()) { + switch (keyCode) { + case InputConstants.KEY_UP -> + targetScrollAmount = Mth.clamp(targetScrollAmount - 10, 0, maxScrollAmount); + case InputConstants.KEY_DOWN -> + targetScrollAmount = Mth.clamp(targetScrollAmount + 10, 0, maxScrollAmount); + default -> { + return false; + } + } + return true; + } + return false; + } + + public void tick() { + float pxPerTick = AUTO_SCROLL_SPEED / 20f * font.lineHeight; + if (maxScrollAmount > 0 && currentTimeMS() - lastInteractionTime > AUTO_SCROLL_TIMER) { + if (scrollingBackward) { + pxPerTick *= -1; + if (targetScrollAmount + pxPerTick < 0) { + scrollingBackward = false; + lastInteractionTime = currentTimeMS(); + } + } else { + if (targetScrollAmount + pxPerTick > maxScrollAmount) { + scrollingBackward = true; + lastInteractionTime = currentTimeMS(); + } + } + + targetScrollAmount = Mth.clamp(targetScrollAmount + pxPerTick, 0, maxScrollAmount); + } + } + + private Style getDescStyle(int mouseX, int mouseY) { + if (!clicked(mouseX, mouseY)) + return null; + + int x = mouseX - getX(); + int y = mouseY - descriptionY; + + if (x < 0 || x > getX() + getWidth()) return null; + if (y < 0 || y > getY() + getHeight()) return null; + + int line = y / font.lineHeight; + + if (line >= wrappedText.size()) return null; + + return font.getSplitter().componentStyleAtWidth(wrappedText.get(line), x); + } + + @Override + protected void updateWidgetNarration(NarrationElementOutput builder) { + if (description != null) { + builder.add(NarratedElementType.TITLE, description.name()); + builder.add(NarratedElementType.HINT, description.description().text()); + } + + } + + public void setOptionDescription(DescriptionWithName description) { + this.description = description; + this.wrappedText = null; + this.targetScrollAmount = 0; + this.currentScrollAmount = 0; + this.lastInteractionTime = currentTimeMS(); + } + + private int currentTimeMS() { + return (int)(Blaze3D.getTime() * 1000); + } + + @Nullable + @Override + public ComponentPath nextFocusPath(FocusNavigationEvent event) { + // prevents focusing on this widget + return null; + } + +} diff --git a/common/src/main/java/dev/isxander/yacl3/gui/OptionListWidget.java b/common/src/main/java/dev/isxander/yacl3/gui/OptionListWidget.java new file mode 100644 index 0000000..54d58f4 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/gui/OptionListWidget.java @@ -0,0 +1,572 @@ +package dev.isxander.yacl3.gui; + +import com.google.common.collect.ImmutableList; +import dev.isxander.yacl3.api.*; +import dev.isxander.yacl3.api.utils.Dimension; +import dev.isxander.yacl3.impl.utils.YACLConstants; +import net.minecraft.ChatFormatting; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.Font; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.gui.components.MultiLineLabel; +import net.minecraft.client.gui.components.events.GuiEventListener; +import net.minecraft.client.gui.narration.NarratableEntry; +import net.minecraft.client.gui.narration.NarratedElementType; +import net.minecraft.client.gui.narration.NarrationElementOutput; +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.network.chat.Component; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.function.Consumer; + +public class OptionListWidget extends ElementListWidgetExt { + private final YACLScreen yaclScreen; + private final ConfigCategory category; + private ImmutableList viewableChildren; + private String searchQuery = ""; + private final Consumer hoverEvent; + private DescriptionWithName lastHoveredOption; + + public OptionListWidget(YACLScreen screen, ConfigCategory category, Minecraft client, int x, int y, int width, int height, Consumer hoverEvent) { + super(client, x, y, width, height, true); + this.yaclScreen = screen; + this.category = category; + this.hoverEvent = hoverEvent; + + refreshOptions(); + + for (OptionGroup group : category.groups()) { + if (group instanceof ListOption listOption) { + listOption.addRefreshListener(() -> refreshListEntries(listOption, category)); + } + } + } + + public void refreshOptions() { + clearEntries(); + + for (OptionGroup group : category.groups()) { + GroupSeparatorEntry groupSeparatorEntry; + if (!group.isRoot()) { + groupSeparatorEntry = group instanceof ListOption listOption + ? new ListGroupSeparatorEntry(listOption, yaclScreen) + : new GroupSeparatorEntry(group, yaclScreen); + addEntry(groupSeparatorEntry); + } else { + groupSeparatorEntry = null; + } + + List optionEntries = new ArrayList<>(); + + // add empty entry to make sure users know it's empty not just bugging out + if (groupSeparatorEntry instanceof ListGroupSeparatorEntry listGroupSeparatorEntry) { + if (listGroupSeparatorEntry.listOption.options().isEmpty()) { + EmptyListLabel emptyListLabel = new EmptyListLabel(listGroupSeparatorEntry, category); + addEntry(emptyListLabel); + optionEntries.add(emptyListLabel); + } + } + + for (Option option : group.options()) { + OptionEntry entry = new OptionEntry(option, category, group, groupSeparatorEntry, option.controller().provideWidget(yaclScreen, getDefaultEntryDimension())); + addEntry(entry); + optionEntries.add(entry); + } + + if (groupSeparatorEntry != null) { + groupSeparatorEntry.setChildEntries(optionEntries); + } + } + + recacheViewableChildren(); + setScrollAmount(0); + resetSmoothScrolling(); + } + + private void refreshListEntries(ListOption listOption, ConfigCategory category) { + // find group separator for group + ListGroupSeparatorEntry groupSeparator = super.children().stream().filter(e -> e instanceof ListGroupSeparatorEntry gs && gs.group == listOption).map(ListGroupSeparatorEntry.class::cast).findAny().orElse(null); + + if (groupSeparator == null) { + YACLConstants.LOGGER.warn("Can't find group seperator to refresh list option entries for list option " + listOption.name()); + return; + } + + for (Entry entry : groupSeparator.childEntries) + super.removeEntry(entry); + groupSeparator.childEntries.clear(); + + // if no entries, below loop won't run where addEntryBelow() recaches viewable children + if (listOption.options().isEmpty()) { + EmptyListLabel emptyListLabel; + addEntryBelow(groupSeparator, emptyListLabel = new EmptyListLabel(groupSeparator, category)); + groupSeparator.childEntries.add(emptyListLabel); + return; + } + + Entry lastEntry = groupSeparator; + for (ListOptionEntry listOptionEntry : listOption.options()) { + OptionEntry optionEntry = new OptionEntry(listOptionEntry, category, listOption, groupSeparator, listOptionEntry.controller().provideWidget(yaclScreen, getDefaultEntryDimension())); + addEntryBelow(lastEntry, optionEntry); + groupSeparator.childEntries.add(optionEntry); + lastEntry = optionEntry; + } + } + + public Dimension getDefaultEntryDimension() { + return Dimension.ofInt(getRowLeft(), 0, getRowWidth(), 20); + } + + public void expandAllGroups() { + for (Entry entry : super.children()) { + if (entry instanceof GroupSeparatorEntry groupSeparatorEntry) { + groupSeparatorEntry.setExpanded(true); + } + } + } + + public void updateSearchQuery(String query) { + this.searchQuery = query; + expandAllGroups(); + recacheViewableChildren(); + } + + @Override + public int getRowWidth() { + return Math.min(396, (int)(width / 1.3f)); + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + for (Entry child : children()) { + if (child != getEntryAtPosition(mouseX, mouseY) && child instanceof OptionEntry optionEntry) + optionEntry.widget.unfocus(); + } + + return super.mouseClicked(mouseX, mouseY, button); + } + + @Override + public boolean mouseScrolled(double mouseX, double mouseY, double amount) { + super.mouseScrolled(mouseX, mouseY, amount); + + for (Entry child : children()) { + if (child.mouseScrolled(mouseX, mouseY, amount)) + break; + } + + return true; + } + + @Override + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { + for (Entry child : children()) { + if (child.keyPressed(keyCode, scanCode, modifiers)) + return true; + } + + return super.keyPressed(keyCode, scanCode, modifiers); + } + + @Override + public boolean charTyped(char chr, int modifiers) { + for (Entry child : children()) { + if (child.charTyped(chr, modifiers)) + return true; + } + + return super.charTyped(chr, modifiers); + } + + @Override + protected int getScrollbarPosition() { + return x1 - (int)(width * 0.05f); + } + + public void recacheViewableChildren() { + this.viewableChildren = ImmutableList.copyOf(super.children().stream().filter(Entry::isViewable).toList()); + + // update y positions before they need to be rendered are rendered + int i = 0; + for (Entry entry : viewableChildren) { + if (entry instanceof OptionEntry optionEntry) + optionEntry.widget.setDimension(optionEntry.widget.getDimension().withY(getRowTop(i))); + i++; + } + } + + @Override + public List children() { + return viewableChildren; + } + + public void addEntry(int index, Entry entry) { + super.children().add(index, entry); + recacheViewableChildren(); + } + + public void addEntryBelow(Entry below, Entry entry) { + int idx = super.children().indexOf(below) + 1; + + if (idx == 0) + throw new IllegalStateException("The entry to insert below does not exist!"); + + addEntry(idx, entry); + } + + public void addEntryBelowWithoutScroll(Entry below, Entry entry) { + double d = (double)this.getMaxScroll() - this.getScrollAmount(); + addEntryBelow(below, entry); + setScrollAmount(getMaxScroll() - d); + } + + @Override + public boolean removeEntryFromTop(Entry entry) { + boolean ret = super.removeEntryFromTop(entry); + recacheViewableChildren(); + return ret; + } + + @Override + public boolean removeEntry(Entry entry) { + boolean ret = super.removeEntry(entry); + recacheViewableChildren(); + return ret; + } + + private void setHoverDescription(DescriptionWithName description) { + if (description != lastHoveredOption) { + lastHoveredOption = description; + hoverEvent.accept(description); + } + } + + public abstract class Entry extends ElementListWidgetExt.Entry { + public boolean isViewable() { + return true; + } + + protected boolean isHovered() { + return Objects.equals(getHovered(), this); + } + } + + public class OptionEntry extends Entry { + public final Option option; + public final ConfigCategory category; + public final OptionGroup group; + + public final @Nullable GroupSeparatorEntry groupSeparatorEntry; + + public final AbstractWidget widget; + + private final TextScaledButtonWidget resetButton; + + private final String categoryName; + private final String groupName; + + public OptionEntry(Option option, ConfigCategory category, OptionGroup group, @Nullable GroupSeparatorEntry groupSeparatorEntry, AbstractWidget widget) { + this.option = option; + this.category = category; + this.group = group; + this.groupSeparatorEntry = groupSeparatorEntry; + this.widget = widget; + this.categoryName = category.name().getString().toLowerCase(); + this.groupName = group.name().getString().toLowerCase(); + if (option.canResetToDefault() && this.widget.canReset()) { + this.widget.setDimension(this.widget.getDimension().expanded(-20, 0)); + this.resetButton = new TextScaledButtonWidget(yaclScreen, widget.getDimension().xLimit(), -50, 20, 20, 2f, Component.literal("\u21BB"), button -> { + option.requestSetDefault(); + }); + option.addListener((opt, val) -> this.resetButton.active = !opt.isPendingValueDefault() && opt.available()); + this.resetButton.active = !option.isPendingValueDefault() && option.available(); + } else { + this.resetButton = null; + } + } + + @Override + public void render(GuiGraphics graphics, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { + widget.setDimension(widget.getDimension().withY(y)); + + widget.render(graphics, mouseX, mouseY, tickDelta); + + if (resetButton != null) { + resetButton.setY(y); + resetButton.render(graphics, mouseX, mouseY, tickDelta); + } + + if (isHovered()) { + setHoverDescription(DescriptionWithName.of(option.name(), option.description())); + } + } + + @Override + public boolean mouseScrolled(double mouseX, double mouseY, double amount) { + return widget.mouseScrolled(mouseX, mouseY, amount); + } + + @Override + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { + return widget.keyPressed(keyCode, scanCode, modifiers); + } + + @Override + public boolean charTyped(char chr, int modifiers) { + return widget.charTyped(chr, modifiers); + } + + @Override + public boolean isViewable() { + return (groupSeparatorEntry == null || groupSeparatorEntry.isExpanded()) + && (searchQuery.isEmpty() + || groupName.contains(searchQuery) + || widget.matchesSearch(searchQuery)); + } + + @Override + public int getItemHeight() { + return Math.max(widget.getDimension().height(), resetButton != null ? resetButton.getHeight() : 0) + 2; + } + + @Override + public void setFocused(boolean focused) { + super.setFocused(focused); + if (focused) + setHoverDescription(DescriptionWithName.of(option.name(), option.description())); + } + + @Override + public List narratables() { + if (resetButton == null) + return ImmutableList.of(widget); + + return ImmutableList.of(widget, resetButton); + } + + @Override + public List children() { + if (resetButton == null) + return ImmutableList.of(widget); + + return ImmutableList.of(widget, resetButton); + } + } + + public class GroupSeparatorEntry extends Entry { + protected final OptionGroup group; + protected final MultiLineLabel wrappedName; + protected final MultiLineLabel wrappedTooltip; + + protected final LowProfileButtonWidget expandMinimizeButton; + + protected final Screen screen; + protected final Font font = Minecraft.getInstance().font; + + protected boolean groupExpanded; + + protected List childEntries = new ArrayList<>(); + + private int y; + + private GroupSeparatorEntry(OptionGroup group, Screen screen) { + this.group = group; + this.screen = screen; + this.wrappedName = MultiLineLabel.create(font, group.name(), getRowWidth() - 45); + this.wrappedTooltip = MultiLineLabel.create(font, group.tooltip(), screen.width / 3 * 2 - 10); + this.groupExpanded = !group.collapsed(); + this.expandMinimizeButton = new LowProfileButtonWidget(0, 0, 20, 20, Component.empty(), btn -> onExpandButtonPress()); + updateExpandMinimizeText(); + } + + @Override + public void render(GuiGraphics graphics, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { + this.y = y; + + int buttonY = y + entryHeight / 2 - expandMinimizeButton.getHeight() / 2 + 1; + + expandMinimizeButton.setY(buttonY); + expandMinimizeButton.setX(x); + expandMinimizeButton.render(graphics, mouseX, mouseY, tickDelta); + + wrappedName.renderCentered(graphics, x + entryWidth / 2, y + getYPadding()); + + if (isHovered()) { + setHoverDescription(DescriptionWithName.of(group.name(), group.description())); + } + } + + public boolean isExpanded() { + return groupExpanded; + } + + public void setExpanded(boolean expanded) { + if (this.groupExpanded == expanded) + return; + + this.groupExpanded = expanded; + updateExpandMinimizeText(); + recacheViewableChildren(); + } + + protected void onExpandButtonPress() { + setExpanded(!isExpanded()); + } + + protected void updateExpandMinimizeText() { + expandMinimizeButton.setMessage(Component.literal(isExpanded() ? "▼" : "▶")); + } + + public void setChildEntries(List childEntries) { + this.childEntries.clear(); + this.childEntries.addAll(childEntries); + } + + @Override + public boolean isViewable() { + return searchQuery.isEmpty() || childEntries.stream().anyMatch(Entry::isViewable); + } + + @Override + public int getItemHeight() { + return Math.max(wrappedName.getLineCount(), 1) * font.lineHeight + getYPadding() * 2; + } + + private int getYPadding() { + return 6; + } + + @Override + public void setFocused(boolean focused) { + super.setFocused(focused); + if (focused) + setHoverDescription(DescriptionWithName.of(group.name(), group.description())); + } + + @Override + public List narratables() { + return ImmutableList.of(new NarratableEntry() { + @Override + public NarrationPriority narrationPriority() { + return NarrationPriority.HOVERED; + } + + @Override + public void updateNarration(NarrationElementOutput builder) { + builder.add(NarratedElementType.TITLE, group.name()); + builder.add(NarratedElementType.HINT, group.tooltip()); + } + }); + } + + @Override + public List children() { + return ImmutableList.of(expandMinimizeButton); + } + } + + public class ListGroupSeparatorEntry extends GroupSeparatorEntry { + private final ListOption listOption; + private final TextScaledButtonWidget resetListButton; + private final TooltipButtonWidget addListButton; + + private ListGroupSeparatorEntry(ListOption group, Screen screen) { + super(group, screen); + this.listOption = group; + + this.resetListButton = new TextScaledButtonWidget(screen, getRowRight() - 20, -50, 20, 20, 2f, Component.literal("\u21BB"), button -> { + group.requestSetDefault(); + }); + group.addListener((opt, val) -> this.resetListButton.active = !opt.isPendingValueDefault() && opt.available()); + this.resetListButton.active = !group.isPendingValueDefault()