From 33e98c7edc1404e099f9c9bcc586fd5c55cb8bdd Mon Sep 17 00:00:00 2001 From: isXander Date: Sun, 18 Sep 2022 19:14:58 +0100 Subject: 1.3.0 option tooltips now consume the pending value PlaceholderCategory: a category that when selected, just opens a screen --- .../java/dev/isxander/yacl/api/ConfigCategory.java | 2 +- src/main/java/dev/isxander/yacl/api/Option.java | 42 +++++++--- .../dev/isxander/yacl/api/PlaceholderCategory.java | 94 ++++++++++++++++++++++ 3 files changed, 127 insertions(+), 11 deletions(-) create mode 100644 src/main/java/dev/isxander/yacl/api/PlaceholderCategory.java (limited to 'src/main/java/dev/isxander/yacl/api') diff --git a/src/main/java/dev/isxander/yacl/api/ConfigCategory.java b/src/main/java/dev/isxander/yacl/api/ConfigCategory.java index 1b2a2bc..27c3e95 100644 --- a/src/main/java/dev/isxander/yacl/api/ConfigCategory.java +++ b/src/main/java/dev/isxander/yacl/api/ConfigCategory.java @@ -90,7 +90,7 @@ public interface ConfigCategory { * @see OptionGroup#isRoot() */ public Builder options(@NotNull Collection> options) { - Validate.notEmpty(options, "`options` must not be empty"); + Validate.notNull(options, "`options` must not be null"); this.rootOptions.addAll(options); return this; diff --git a/src/main/java/dev/isxander/yacl/api/Option.java b/src/main/java/dev/isxander/yacl/api/Option.java index ff72c4c..1c3b006 100644 --- a/src/main/java/dev/isxander/yacl/api/Option.java +++ b/src/main/java/dev/isxander/yacl/api/Option.java @@ -9,9 +9,11 @@ import org.apache.commons.lang3.Validate; import org.jetbrains.annotations.NotNull; import java.util.*; +import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; +import java.util.stream.Stream; public interface Option { /** @@ -95,6 +97,8 @@ public interface Option { */ void requestSetDefault(); + void addListener(BiConsumer, T> changedListener); + /** * Creates a builder to construct an {@link Option} * @@ -108,7 +112,7 @@ public interface Option { class Builder { private Text name = Text.literal("Name not specified!").formatted(Formatting.RED); - private final List tooltipLines = new ArrayList<>(); + private final List> tooltipGetters = new ArrayList<>(); private Function, Controller> controlGetter; @@ -136,6 +140,20 @@ public interface Option { return this; } + /** + * Sets the tooltip to be used by the option. + * No need to wrap the text yourself, the gui does this itself. + * + * @param tooltipGetter function to get tooltip depending on value {@link Builder#build()}. + */ + @SafeVarargs + public final Builder tooltip(@NotNull Function... tooltipGetter) { + Validate.notNull(tooltipGetter, "`tooltipGetter` cannot be null"); + + this.tooltipGetters.addAll(List.of(tooltipGetter)); + return this; + } + /** * Sets the tooltip to be used by the option. * Can be invoked twice to append more lines. @@ -146,7 +164,7 @@ public interface Option { public Builder tooltip(@NotNull Text... tooltips) { Validate.notNull(tooltips, "`tooltips` cannot be empty"); - tooltipLines.addAll(List.of(tooltips)); + this.tooltipGetters.addAll(Stream.of(tooltips).map(text -> (Function) t -> text).toList()); return this; } @@ -244,16 +262,20 @@ public interface Option { Validate.notNull(controlGetter, "`control` must not be null when building `Option`"); Validate.notNull(binding, "`binding` must not be null when building `Option`"); - MutableText concatenatedTooltip = Text.empty(); - boolean first = true; - for (Text line : tooltipLines) { - if (!first) concatenatedTooltip.append("\n"); - first = false; + Function concatenatedTooltipGetter = value -> { + MutableText concatenatedTooltip = Text.empty(); + boolean first = true; + for (Function line : tooltipGetters) { + if (!first) concatenatedTooltip.append("\n"); + first = false; + + concatenatedTooltip.append(line.apply(value)); + } - concatenatedTooltip.append(line); - } + return concatenatedTooltip; + }; - return new OptionImpl<>(name, concatenatedTooltip, controlGetter, binding, available, ImmutableSet.copyOf(flags), typeClass); + return new OptionImpl<>(name, concatenatedTooltipGetter, controlGetter, binding, available, ImmutableSet.copyOf(flags), typeClass); } } } diff --git a/src/main/java/dev/isxander/yacl/api/PlaceholderCategory.java b/src/main/java/dev/isxander/yacl/api/PlaceholderCategory.java new file mode 100644 index 0000000..de7441c --- /dev/null +++ b/src/main/java/dev/isxander/yacl/api/PlaceholderCategory.java @@ -0,0 +1,94 @@ +package dev.isxander.yacl.api; + +import dev.isxander.yacl.gui.YACLScreen; +import dev.isxander.yacl.impl.PlaceholderCategoryImpl; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.text.MutableText; +import net.minecraft.text.Text; +import org.apache.commons.lang3.Validate; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.BiFunction; + +/** + * A placeholder category that actually just opens another screen, + * instead of displaying options + */ +public interface PlaceholderCategory extends ConfigCategory { + /** + * Function to create a screen to open upon changing to this category + */ + BiFunction screen(); + + static Builder createBuilder() { + return new Builder(); + } + + class Builder { + private Text name; + + private final List tooltipLines = new ArrayList<>(); + + private BiFunction screenFunction; + + private Builder() { + + } + + /** + * Sets name of the category + * + * @see ConfigCategory#name() + */ + public Builder name(@NotNull Text name) { + Validate.notNull(name, "`name` cannot be null"); + + this.name = name; + return this; + } + + /** + * Sets the tooltip to be used by the category. + * Can be invoked twice to append more lines. + * No need to wrap the text yourself, the gui does this itself. + * + * @param tooltips text lines - merged with a new-line on {@link Builder#build()}. + */ + public Builder tooltip(@NotNull Text... tooltips) { + Validate.notEmpty(tooltips, "`tooltips` cannot be empty"); + + tooltipLines.addAll(List.of(tooltips)); + return this; + } + + /** + * Screen to open upon selecting this category + * + * @see PlaceholderCategory#screen() + */ + public Builder screen(@NotNull BiFunction screenFunction) { + Validate.notNull(screenFunction, "`screenFunction` cannot be null"); + + this.screenFunction = screenFunction; + return this; + } + + public PlaceholderCategory build() { + Validate.notNull(name, "`name` must not be null to build `ConfigCategory`"); + + MutableText concatenatedTooltip = Text.empty(); + boolean first = true; + for (Text line : tooltipLines) { + if (!first) concatenatedTooltip.append("\n"); + first = false; + + concatenatedTooltip.append(line); + } + + return new PlaceholderCategoryImpl(name, screenFunction, concatenatedTooltip); + } + } +} -- cgit