diff options
author | Xander <xander@isxander.dev> | 2023-04-25 16:28:41 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-25 16:28:41 +0100 |
commit | 13c7ba45ff201423eb8dba8a40cfb66ebb531439 (patch) | |
tree | 1e799aa9da11fbc0833bc6b7c6e6799633c2ec50 /common/src/main/java/dev/isxander/yacl/api | |
parent | 8ba7196ae990fe9aa98680aba1b387e385fff99c (diff) | |
download | YetAnotherConfigLib-13c7ba45ff201423eb8dba8a40cfb66ebb531439.tar.gz YetAnotherConfigLib-13c7ba45ff201423eb8dba8a40cfb66ebb531439.tar.bz2 YetAnotherConfigLib-13c7ba45ff201423eb8dba8a40cfb66ebb531439.zip |
Architectury! (#61)
Diffstat (limited to 'common/src/main/java/dev/isxander/yacl/api')
17 files changed, 1083 insertions, 0 deletions
diff --git a/common/src/main/java/dev/isxander/yacl/api/Binding.java b/common/src/main/java/dev/isxander/yacl/api/Binding.java new file mode 100644 index 0000000..b4cd2d0 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl/api/Binding.java @@ -0,0 +1,64 @@ +package dev.isxander.yacl.api; + +import dev.isxander.yacl.impl.GenericBindingImpl; +import dev.isxander.yacl.mixin.OptionInstanceAccessor; +import net.minecraft.client.OptionInstance; +import org.apache.commons.lang3.Validate; + +import java.util.function.Consumer; +import java.util.function.Supplier; + +/** + * Controls modifying the bound option. + * Provides the default value, a setter and a getter. + */ +public interface Binding<T> { + void setValue(T value); + + T getValue(); + + T defaultValue(); + + /** + * Creates a generic binding. + * + * @param def default value of the option, used to reset + * @param getter should return the current value of the option + * @param setter should set the option to the supplied value + */ + static <T> Binding<T> generic(T def, Supplier<T> getter, Consumer<T> setter) { + Validate.notNull(def, "`def` must not be null"); + Validate.notNull(getter, "`getter` must not be null"); + Validate.notNull(setter, "`setter` must not be null"); + + return new GenericBindingImpl<>(def, getter, setter); + } + + /** + * Creates a {@link Binding} for Minecraft's {@link OptionInstance} + */ + static <T> Binding<T> minecraft(OptionInstance<T> minecraftOption) { + Validate.notNull(minecraftOption, "`minecraftOption` must not be null"); + + return new GenericBindingImpl<>( + ((OptionInstanceAccessor<T>) (Object) minecraftOption).getInitialValue(), + minecraftOption::get, + minecraftOption::set + ); + } + + /** + * Creates an immutable binding that has no default and cannot be modified. + * + * @param value the value for the binding + */ + static <T> Binding<T> immutable(T value) { + Validate.notNull(value, "`value` must not be null"); + + return new GenericBindingImpl<>( + value, + () -> value, + changed -> {} + ); + } +} diff --git a/common/src/main/java/dev/isxander/yacl/api/ButtonOption.java b/common/src/main/java/dev/isxander/yacl/api/ButtonOption.java new file mode 100644 index 0000000..88e1c4b --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl/api/ButtonOption.java @@ -0,0 +1,66 @@ +package dev.isxander.yacl.api; + +import dev.isxander.yacl.gui.YACLScreen; +import dev.isxander.yacl.impl.ButtonOptionImpl; +import net.minecraft.network.chat.Component; +import org.jetbrains.annotations.NotNull; + +import java.util.function.BiConsumer; +import java.util.function.Consumer; +import java.util.function.Function; + +public interface ButtonOption extends Option<BiConsumer<YACLScreen, ButtonOption>> { + /** + * Action to be executed upon button press + */ + BiConsumer<YACLScreen, ButtonOption> action(); + + static dev.isxander.yacl.api.ButtonOption.Builder createBuilder() { + return new ButtonOptionImpl.BuilderImpl(); + } + + interface Builder { + /** + * Sets the name to be used by the option. + * + * @see Option#name() + */ + dev.isxander.yacl.api.ButtonOption.Builder name(@NotNull Component name); + + /** + * Sets the tooltip to be used by the option. + * 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 Option.Builder#build()}. + */ + dev.isxander.yacl.api.ButtonOption.Builder tooltip(@NotNull Component... tooltips); + + dev.isxander.yacl.api.ButtonOption.Builder action(@NotNull BiConsumer<YACLScreen, ButtonOption> action); + + /** + * Action to be executed upon button press + * + * @see ButtonOption#action() + */ + @Deprecated + dev.isxander.yacl.api.ButtonOption.Builder action(@NotNull Consumer<YACLScreen> action); + + /** + * Sets if the option can be configured + * + * @see Option#available() + */ + dev.isxander.yacl.api.ButtonOption.Builder available(boolean available); + + /** + * Sets the controller for the option. + * This is how you interact and change the options. + * + * @see dev.isxander.yacl.gui.controllers + */ + dev.isxander.yacl.api.ButtonOption.Builder controller(@NotNull Function<ButtonOption, Controller<BiConsumer<YACLScreen, ButtonOption>>> control); + + ButtonOption build(); + } +} diff --git a/common/src/main/java/dev/isxander/yacl/api/ConfigCategory.java b/common/src/main/java/dev/isxander/yacl/api/ConfigCategory.java new file mode 100644 index 0000000..7764479 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl/api/ConfigCategory.java @@ -0,0 +1,94 @@ +package dev.isxander.yacl.api; + +import com.google.common.collect.ImmutableList; +import dev.isxander.yacl.impl.ConfigCategoryImpl; +import net.minecraft.network.chat.Component; +import org.jetbrains.annotations.NotNull; + +import java.util.Collection; + +/** + * Separates {@link Option}s or {@link OptionGroup}s into multiple distinct sections. + * Served to a user as a button in the left column, + * upon pressing, the options list is filled with options contained within this category. + */ +public interface ConfigCategory { + /** + * Name of category, displayed as a button on the left column. + */ + @NotNull Component name(); + + /** + * Gets every {@link OptionGroup} in this category. + */ + @NotNull ImmutableList<OptionGroup> groups(); + + /** + * Tooltip (or description) of the category. + * Rendered on hover. + */ + @NotNull Component tooltip(); + + /** + * Creates a builder to construct a {@link ConfigCategory} + */ + static Builder createBuilder() { + return new ConfigCategoryImpl.BuilderImpl(); + } + + interface Builder extends OptionAddable { + /** + * Sets name of the category + * + * @see ConfigCategory#name() + */ + Builder name(@NotNull Component name); + + /** + * Adds an option to the root group of the category. + * To add to another group, use {@link Builder#group(OptionGroup)}. + * To construct an option, use {@link Option#createBuilder(Class)} + * + * @see ConfigCategory#groups() + * @see OptionGroup#isRoot() + */ + @Override + Builder option(@NotNull Option<?> option); + + /** + * Adds multiple options to the root group of the category. + * To add to another group, use {@link Builder#groups(Collection)}. + * To construct an option, use {@link Option#createBuilder(Class)} + * + * @see ConfigCategory#groups() + * @see OptionGroup#isRoot() + */ + @Override + Builder options(@NotNull Collection<? extends Option<?>> options); + + /** + * Adds an option group. + * To add an option to the root group, use {@link Builder#option(Option)} + * To construct a group, use {@link OptionGroup#createBuilder()} + */ + Builder group(@NotNull OptionGroup group); + + /** + * Adds multiple option groups. + * To add multiple options to the root group, use {@link Builder#options(Collection)} + * To construct a group, use {@link OptionGroup#createBuilder()} + */ + Builder groups(@NotNull Collection<OptionGroup> groups); + + /** + * 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()}. + */ + Builder tooltip(@NotNull Component... tooltips); + + ConfigCategory build(); + } +} diff --git a/common/src/main/java/dev/isxander/yacl/api/Controller.java b/common/src/main/java/dev/isxander/yacl/api/Controller.java new file mode 100644 index 0000000..0b8e2ed --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl/api/Controller.java @@ -0,0 +1,28 @@ +package dev.isxander.yacl.api; + +import dev.isxander.yacl.api.utils.Dimension; +import dev.isxander.yacl.gui.AbstractWidget; +import dev.isxander.yacl.gui.YACLScreen; +import net.minecraft.network.chat.Component; + +/** + * Provides a widget to control the option. + */ +public interface Controller<T> { + /** + * Gets the dedicated {@link Option} for this controller + */ + Option<T> option(); + + /** + * Gets the formatted value based on {@link Option#pendingValue()} + */ + Component formatValue(); + + /** + * Provides a widget to display + * + * @param screen parent screen + */ + AbstractWidget provideWidget(YACLScreen screen, Dimension<Integer> widgetDimension); +} diff --git a/common/src/main/java/dev/isxander/yacl/api/LabelOption.java b/common/src/main/java/dev/isxander/yacl/api/LabelOption.java new file mode 100644 index 0000000..05c7214 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl/api/LabelOption.java @@ -0,0 +1,41 @@ +package dev.isxander.yacl.api; + +import dev.isxander.yacl.impl.LabelOptionImpl; +import net.minecraft.network.chat.Component; +import org.jetbrains.annotations.NotNull; + +import java.util.Collection; + +/** + * A label option is an easier way of creating a label with a {@link dev.isxander.yacl.gui.controllers.LabelController}. + * This option is immutable and cannot be disabled. Tooltips are supported through + * {@link Component} styling. + */ +public interface LabelOption extends Option<Component> { + @NotNull Component label(); + + /** + * Creates a new label option with the given label, skipping a builder for ease. + */ + static LabelOption create(@NotNull Component label) { + return new LabelOptionImpl(label); + } + + static dev.isxander.yacl.api.LabelOption.Builder createBuilder() { + return new LabelOptionImpl.BuilderImpl(); + } + + interface Builder { + /** + * Appends a line to the label + */ + dev.isxander.yacl.api.LabelOption.Builder line(@NotNull Component line); + + /** + * Appends multiple lines to the label + */ + dev.isxander.yacl.api.LabelOption.Builder lines(@NotNull Collection<? extends Component> lines); + + LabelOption build(); + } +} diff --git a/common/src/main/java/dev/isxander/yacl/api/ListOption.java b/common/src/main/java/dev/isxander/yacl/api/ListOption.java new file mode 100644 index 0000000..afba8ee --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl/api/ListOption.java @@ -0,0 +1,152 @@ +package dev.isxander.yacl.api; + +import com.google.common.collect.ImmutableList; +import dev.isxander.yacl.impl.ListOptionImpl; +import net.minecraft.network.chat.Component; +import org.jetbrains.annotations.ApiStatus; +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; + +/** + * A list option that takes form as an option group for UX. + * You add this option through {@link ConfigCategory.Builder#group(OptionGroup)}. Do NOT add as an option. + * Users can add remove and reshuffle a list type. You can use any controller you wish, there are no dedicated + * controllers for list types. List options do not manipulate your list but get and set the list with a + * regular binding for simplicity. + * + * You may apply option flags like a normal option and collapse like a normal group, it is a merge of them both. + * Methods in this interface marked with {@link ApiStatus.Internal} should not be used, and could be subject to + * change at any time + * @param <T> + */ +public interface ListOption<T> extends OptionGroup, Option<List<T>> { + @Override + @NotNull ImmutableList<ListOptionEntry<T>> options(); + + /** + * Class of the entry type + */ + @NotNull Class<T> elementTypeClass(); + + @ApiStatus.Internal + ListOptionEntry<T> insertNewEntryToTop(); + + @ApiStatus.Internal + void insertEntry(int index, ListOptionEntry<?> entry); + + @ApiStatus.Internal + int indexOf(ListOptionEntry<?> entry); + + @ApiStatus.Internal + void removeEntry(ListOptionEntry<?> entry); + + @ApiStatus.Internal + void addRefreshListener(Runnable changedListener); + + static <T> Builder<T> createBuilder(Class<T> typeClass) { + return new ListOptionImpl.BuilderImpl<>(typeClass); + } + + interface Builder<T> { + /** + * Sets name of the list, for UX purposes, a name should always be given, + * but isn't enforced. + * + * @see ListOption#name() + */ + Builder<T> name(@NotNull Component name); + + /** + * Sets the tooltip to be used by the list. It is displayed like a normal + * group when you hover over the name. Entries do not allow a tooltip. + * <p> + * 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 dev.isxander.yacl.api.ListOption.Builder#build()}. + */ + Builder<T> tooltip(@NotNull Component... tooltips); + + /** + * Sets the value that is used when creating new entries + */ + Builder<T> initial(@NotNull T initialValue); + + /** + * Sets the controller for the option. + * This is how you interact and change the options. + * + * @see dev.isxander.yacl.gui.controllers + */ + Builder<T> controller(@NotNull Function<ListOptionEntry<T>, Controller<T>> control); + + /** + * Sets the binding for the option. + * Used for default, getter and setter. + * + * @see Binding + */ + Builder<T> binding(@NotNull Binding<List<T>> binding); + + /** + * Sets the binding for the option. + * Shorthand of {@link Binding#generic(Object, Supplier, Consumer)} + * + * @param def default value of the option, used to reset + * @param getter should return the current value of the option + * @param setter should set the option to the supplied value + * @see Binding + */ + Builder<T> binding(@NotNull List<T> def, @NotNull Supplier<@NotNull List<T>> getter, @NotNull Consumer<@NotNull List<T>> setter); + + /** + * Sets if the option can be configured + * + * @see Option#available() + */ + Builder<T> available(boolean available); + + /** + * Adds a flag to the option. + * Upon applying changes, all flags are executed. + * {@link Option#flags()} + */ + Builder<T> flag(@NotNull OptionFlag... flag); + + /** + * Adds a flag to the option. + * Upon applying changes, all flags are executed. + * {@link Option#flags()} + */ + Builder<T> flags(@NotNull Collection<OptionFlag> flags); + + /** + * Dictates if the group should be collapsed by default. + * If not set, it will not be collapsed by default. + * + * @see OptionGroup#collapsed() + */ + Builder<T> collapsed(boolean collapsible); + + /** + * Adds a listener to the option. Invoked upon changing any of the list's entries. + * + * @see Option#addListener(BiConsumer) + */ + ListOption.Builder<T> listener(@NotNull BiConsumer<Option<List<T>>, List<T>> listener); + + /** + * Adds multiple listeners to the option. Invoked upon changing of any of the list's entries. + * + * @see Option#addListener(BiConsumer) + */ + ListOption.Builder<T> listeners(@NotNull Collection<BiConsumer<Option<List<T>>, List<T>>> listeners); + + ListOption<T> build(); + } +} diff --git a/common/src/main/java/dev/isxander/yacl/api/ListOptionEntry.java b/common/src/main/java/dev/isxander/yacl/api/ListOptionEntry.java new file mode 100644 index 0000000..e0a3424 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl/api/ListOptionEntry.java @@ -0,0 +1,23 @@ +package dev.isxander.yacl.api; + +import com.google.common.collect.ImmutableSet; +import org.jetbrains.annotations.NotNull; + +public interface ListOptionEntry<T> extends Option<T> { + ListOption<T> parentGroup(); + + @Override + default @NotNull Class<T> typeClass() { + return parentGroup().elementTypeClass(); + } + + @Override + default @NotNull ImmutableSet<OptionFlag> flags() { + return parentGroup().flags(); + } + + @Override + default boolean available() { + return parentGroup().available(); + } +} diff --git a/common/src/main/java/dev/isxander/yacl/api/NameableEnum.java b/common/src/main/java/dev/isxander/yacl/api/NameableEnum.java new file mode 100644 index 0000000..4b04057 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl/api/NameableEnum.java @@ -0,0 +1,10 @@ +package dev.isxander.yacl.api; + +import net.minecraft.network.chat.Component; + +/** + * Used for the default value formatter of {@link dev.isxander.yacl.gui.controllers.cycling.EnumController} + */ +public interface NameableEnum { + Component getDisplayName(); +} diff --git a/common/src/main/java/dev/isxander/yacl/api/Option.java b/common/src/main/java/dev/isxander/yacl/api/Option.java new file mode 100644 index 0000000..a6c0311 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl/api/Option.java @@ -0,0 +1,227 @@ +package dev.isxander.yacl.api; + +import com.google.common.collect.ImmutableSet; +import dev.isxander.yacl.impl.OptionImpl; +import net.minecraft.network.chat.Component; +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; + +public interface Option<T> { + /** + * Name of the option + */ + @NotNull Component name(); + + /** + * Tooltip (or description) of the option. + * Rendered on hover. + */ + @NotNull Component tooltip(); + + /** + * Widget provider for a type of option. + * + * @see dev.isxander.yacl.gui.controllers + */ + @NotNull Controller<T> controller(); + + /** + * Binding for the option. + * Controls setting, getting and default value. + * + * @see Binding + */ + @NotNull Binding<T> binding(); + + /** + * If the option can be configured + */ + boolean available(); + + /** + * Sets if the option can be configured after being built + * + * @see Option#available() + */ + void setAvailable(boolean available); + + /** + * Class of the option type. + * Used by some controllers. + */ + @NotNull Class<T> typeClass(); + + /** + * Tasks that needs to be executed upon applying changes. + */ + @NotNull ImmutableSet<OptionFlag> flags(); + + /** + * Checks if the pending value is not equal to the current set value + */ + boolean changed(); + + /** + * Value in the GUI, ready to set the actual bound value or be undone. + */ + @NotNull T pendingValue(); + + /** + * Sets the pending value + */ + void requestSet(T value); + + /** + * Applies the pending value to the bound value. + * Cannot be undone. + * + * @return if there were changes to apply {@link Option#changed()} + */ + boolean applyValue(); + + /** + * Sets the pending value to the bound value. + */ + void forgetPendingValue(); + + /** + * Sets the pending value to the default bound value. + */ + void requestSetDefault(); + + /** + * Checks if the current pending value is equal to its default value + */ + boolean isPendingValueDefault(); + + default boolean canResetToDefault() { + return true; + } + + /** + * Adds a listener for when the pending value changes + */ + void addListener(BiConsumer<Option<T>, T> changedListener); + + /** + * Creates a builder to construct an {@link Option} + * + * @param <T> type of the option's value + * @param typeClass used to capture the type + */ + static <T> Builder<T> createBuilder(Class<T> typeClass) { + return new OptionImpl.BuilderImpl<>(typeClass); + } + + interface Builder<T> { + /** + * Sets the name to be used by the option. + * + * @see Option#name() + */ + Builder<T> name(@NotNull Component name); + + /** + * 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()}. + */ + Builder<T> tooltip(@NotNull Function<T, Component> tooltipGetter); + + /** + * 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()}. + */ + @Deprecated + Builder<T> tooltip(@NotNull Function<T, Component>... tooltipGetter); + + /** + * Sets the tooltip to be used by the option. + * 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()}. + */ + Builder<T> tooltip(@NotNull Component... tooltips); + + /** + * Sets the controller for the option. + * This is how you interact and change the options. + * + * @see dev.isxander.yacl.gui.controllers + */ + Builder<T> controller(@NotNull Function<Option<T>, Controller<T>> control); + + /** + * Sets the binding for the option. + * Used for default, getter and setter. + * + * @see Binding + */ + Builder<T> binding(@NotNull Binding<T> binding); + + /** + * Sets the binding for the option. + * Shorthand of {@link Binding#generic(Object, Supplier, Consumer)} + * + * @param def default value of the option, used to reset + * @param getter should return the current value of the option + * @param setter should set the option to the supplied value + * @see Binding + */ + Builder<T> binding(@NotNull T def, @NotNull Supplier<@NotNull T> getter, @NotNull Consumer<@NotNull T> setter); + + /** + * Sets if the option can be configured + * + * @see Option#available() + */ + Builder<T> available(boolean available); + + /** + * Adds a flag to the option. + * Upon applying changes, all flags are executed. + * {@link Option#flags()} + */ + Builder<T> flag(@NotNull OptionFlag... flag); + + /** + * Adds a flag to the option. + * Upon applying changes, all flags are executed. + * {@link Option#flags()} + */ + Builder<T> flags(@NotNull Collection<OptionFlag> flags); + + /** + * Instantly invokes the binder's setter when modified in the GUI. + * Prevents the user from undoing the change + * <p> + * Does not support {@link Option#flags()}! + */ + Builder<T> instant(boolean instant); + + /** + * Adds a listener to the option. Invoked upon changing the pending value. + * + * @see Option#addListener(BiConsumer) + */ + Builder<T> listener(@NotNull BiConsumer<Option<T>, T> listener); + + /** + * Adds multiple listeners to the option. Invoked upon changing the pending value. + * + * @see Option#addListener(BiConsumer) + */ + Builder<T> listeners(@NotNull Collection<BiConsumer<Option<T>, T>> listeners); + + Option<T> build(); + } +} diff --git a/common/src/main/java/dev/isxander/yacl/api/OptionAddable.java b/common/src/main/java/dev/isxander/yacl/api/OptionAddable.java new file mode 100644 index 0000000..57be06c --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl/api/OptionAddable.java @@ -0,0 +1,19 @@ +package dev.isxander.yacl.api; + +import org.jetbrains.annotations.NotNull; + +import java.util.Collection; + +public interface OptionAddable { + /** + * Adds an option to an abstract builder. + * To construct an option, use {@link Option#createBuilder(Class)} + */ + OptionAddable option(@NotNull Option<?> option); + + /** + * Adds multiple options to an abstract builder. + * To construct an option, use {@link Option#createBuilder(Class)} + */ + OptionAddable options(@NotNull Collection<? extends Option<?>> options); +} diff --git a/common/src/main/java/dev/isxander/yacl/api/OptionFlag.java b/common/src/main/java/dev/isxander/yacl/api/OptionFlag.java new file mode 100644 index 0000000..51d57e4 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl/api/OptionFlag.java @@ -0,0 +1,23 @@ +package dev.isxander.yacl.api; + +import dev.isxander.yacl.gui.RequireRestartScreen; +import net.minecraft.client.Minecraft; + +import java.util.function.Consumer; + +/** + * Code that is executed upon certain options being applied. + * Each flag is executed only once per save, no matter the amount of options with the flag. + */ +@FunctionalInterface +public interface OptionFlag extends Consumer<Minecraft> { + /** Warns the user that a game restart is required for the changes to take effect */ + OptionFlag GAME_RESTART = client -> client.setScreen(new RequireRestartScreen(client.screen)); + + /** Reloads chunks upon applying (F3+A) */ + OptionFlag RELOAD_CHUNKS = client -> client.levelRenderer.allChanged(); + + OptionFlag WORLD_RENDER_UPDATE = client -> client.levelRenderer.needsUpdate(); + + OptionFlag ASSET_RELOAD = Minecraft::delayTextureReload; +} diff --git a/common/src/main/java/dev/isxander/yacl/api/OptionGroup.java b/common/src/main/java/dev/isxander/yacl/api/OptionGroup.java new file mode 100644 index 0000000..4fe43c7 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl/api/OptionGroup.java @@ -0,0 +1,94 @@ +package dev.isxander.yacl.api; + +import com.google.common.collect.ImmutableList; +import dev.isxander.yacl.impl.OptionGroupImpl; +import net.minecraft.network.chat.Component; +import org.jetbrains.annotations.NotNull; + +import java.util.Collection; + +/** + * Serves as a separator between multiple chunks of options + * that may be too similar or too few to be placed in a separate {@link ConfigCategory}. + * Or maybe you just want your config to feel less dense. + */ +public interface OptionGroup { + /** + * Name of the option group, displayed as a separator in the option lists. + * Can be empty. + */ + Component name(); + + /** + * Tooltip displayed on hover. + */ + Component tooltip(); + + /** + * List of all options in the group + */ + @NotNull ImmutableList<? extends Option<?>> options(); + + /** + * Dictates if the group should be collapsed by default. + */ + boolean collapsed(); + + /** + * Always false when using the {@link Builder} + * used to not render the separator if true + */ + boolean isRoot(); + + /** + * Creates a builder to construct a {@link OptionGroup} + */ + static Builder createBuilder() { + return new OptionGroupImpl.BuilderImpl(); + } + + interface Builder extends OptionAddable { + /** + * Sets name of the group, can be {@link Component#empty()} to just separate options, like sodium. + * + * @see OptionGroup#name() + */ + Builder name(@NotNull Component name); + + /** + * Sets the tooltip to be used by the option group. + * Can be invoked twice to append more lines. + * No need to wrap the Component yourself, the gui does this itself. + * + * @param tooltips Component lines - merged with a new-line on {@link Builder#build()}. + */ + Builder tooltip(@NotNull Component... tooltips); + + /** + * Adds an option to group. + * To construct an option, use {@link Option#createBuilder(Class)} + * + * @see OptionGroup#options() + */ + @Override + Builder option(@NotNull Option<?> option); + + /** + * Adds multiple options to group. + * To construct an option, use {@link Option#createBuilder(Class)} + * + * @see OptionGroup#options() + */ + @Override + Builder options(@NotNull Collection<? extends Option<?>> options); + + /** + * Dictates if the group should be collapsed by default + * + * @see OptionGroup#collapsed() + */ + Builder collapsed(boolean collapsible); + + OptionGroup build(); + } +} diff --git a/common/src/main/java/dev/isxander/yacl/api/PlaceholderCategory.java b/common/src/main/java/dev/isxander/yacl/api/PlaceholderCategory.java new file mode 100644 index 0000000..3641fad --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl/api/PlaceholderCategory.java @@ -0,0 +1,52 @@ +package dev.isxander.yacl.api; + +import dev.isxander.yacl.gui.YACLScreen; +import dev.isxander.yacl.impl.PlaceholderCategoryImpl; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.network.chat.Component; +import org.jetbrains.annotations.NotNull; + +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<Minecraft, YACLScreen, Screen> screen(); + + static Builder createBuilder() { + return new PlaceholderCategoryImpl.BuilderImpl(); + } + + interface Builder { + /** + * Sets name of the category + * + * @see ConfigCategory#name() + */ + Builder name(@NotNull Component name); + + /** + * Sets the tooltip to be used by the category. + * Can be invoked twice to append more lines. + * No need to wrap the Component yourself, the gui does this itself. + * + * @param tooltips Component lines - merged with a new-line on {@link dev.isxander.yacl.api.PlaceholderCategory.Builder#build()}. + */ + Builder tooltip(@NotNull Component... tooltips); + + /** + * Screen to open upon selecting this category + * + * @see PlaceholderCategory#screen() + */ + Builder screen(@NotNull BiFunction<Minecraft, YACLScreen, Screen> screenFunction); + + PlaceholderCategory build(); + } +} diff --git a/common/src/main/java/dev/isxander/yacl/api/YetAnotherConfigLib.java b/common/src/main/java/dev/isxander/yacl/api/YetAnotherConfigLib.java new file mode 100644 index 0000000..c6da1d1 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl/api/YetAnotherConfigLib.java @@ -0,0 +1,107 @@ +package dev.isxander.yacl.api; + +import com.google.common.collect.ImmutableList; +import dev.isxander.yacl.config.ConfigInstance; +import dev.isxander.yacl.gui.YACLScreen; +import dev.isxander.yacl.impl.YetAnotherConfigLibImpl; +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.network.chat.Component; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Collection; +import java.util.function.Consumer; + +/** + * Main class of the mod. + * Contains all data and used to provide a {@link Screen} + */ +public interface YetAnotherConfigLib { + /** + * Title of the GUI. Only used for Minecraft narration. + */ + Component title(); + + /** + * Gets all config categories. + */ + ImmutableList<ConfigCategory> categories(); + + /** + * Ran when changes are saved. Can be used to save config to a file etc. + */ + Runnable saveFunction(); + + /** + * Ran every time the YACL screen initialises. Can be paired with FAPI to add custom widgets. + */ + Consumer<YACLScreen> initConsumer(); + + /** + * Generates a Screen to display based on this instance. + * + * @param parent parent screen to open once closed + */ + Screen generateScreen(@Nullable Screen parent); + + /** + * Creates a builder to construct YACL + */ + static Builder createBuilder() { + return new YetAnotherConfigLibImpl.BuilderImpl(); + } + + /** + * Creates an instance using a {@link ConfigInstance} which autofills the save() builder method. + * This also takes an easy functional interface that provides defaults and config to help build YACL bindings. + */ + static <T> YetAnotherConfigLib create(ConfigInstance<T> configInstance, ConfigBackedBuilder<T> builder) { + return builder.build(configInstance.getDefaults(), configInstance.getConfig(), createBuilder().save(configInstance::save)).build(); + } + + interface Builder { + /** + * Sets title of GUI for Minecraft narration + * + * @see YetAnotherConfigLib#title() + */ + Builder title(@NotNull Component title); + + /** + * Adds a new category. + * To create a category you need to use {@link ConfigCategory#createBuilder()} + * + * @see YetAnotherConfigLib#categories() + */ + Builder category(@NotNull ConfigCategory category); + + /** + * Adds multiple categories at once. + * To create a category you need to use {@link ConfigCategory#createBuilder()} + * + * @see YetAnotherConfigLib#categories() + */ + Builder categories(@NotNull Collection<? extends ConfigCategory> categories); + + /** + * Used to define a save function for when user clicks the Save Changes button + * + * @see YetAnotherConfigLib#saveFunction() + */ + Builder save(@NotNull Runnable saveFunction); + + /** + * Defines a consumer that is accepted every time the YACL screen initialises + * + * @see YetAnotherConfigLib#initConsumer() + */ + Builder screenInit(@NotNull Consumer<YACLScreen> initConsumer); + + YetAnotherConfigLib build(); + } + + @FunctionalInterface + interface ConfigBackedBuilder<T> { + YetAnotherConfigLib.Builder build(T defaults, T config, YetAnotherConfigLib.Builder builder); + } +} diff --git a/common/src/main/java/dev/isxander/yacl/api/utils/Dimension.java b/common/src/main/java/dev/isxander/yacl/api/utils/Dimension.java new file mode 100644 index 0000000..0de0a58 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl/api/utils/Dimension.java @@ -0,0 +1,33 @@ +package dev.isxander.yacl.api.utils; + +import dev.isxander.yacl.impl.utils.DimensionIntegerImpl; + +public interface Dimension<T extends Number> { + T x(); + T y(); + + T width(); + T height(); + + T xLimit(); + T yLimit(); + + T centerX(); + T centerY(); + + boolean isPointInside(T x, T y); + + MutableDimension<T> clone(); + + Dimension<T> withX(T x); + Dimension<T> withY(T y); + Dimension<T> withWidth(T width); + Dimension<T> withHeight(T height); + + Dimension<T> moved(T x, T y); + Dimension<T> expanded(T width, T height); + + static MutableDimension<Integer> ofInt(int x, int y, int width, int height) { + return new DimensionIntegerImpl(x, y, width, height); + } +} diff --git a/common/src/main/java/dev/isxander/yacl/api/utils/MutableDimension.java b/common/src/main/java/dev/isxander/yacl/api/utils/MutableDimension.java new file mode 100644 index 0000000..eff0186 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl/api/utils/MutableDimension.java @@ -0,0 +1,11 @@ +package dev.isxander.yacl.api.utils; + +public interface MutableDimension<T extends Number> extends Dimension<T> { + MutableDimension<T> setX(T x); + MutableDimension<T> setY(T y); + MutableDimension<T> setWidth(T width); + MutableDimension<T> setHeight(T height); + + MutableDimension<T> move(T x, T y); + MutableDimension<T> expand(T width, T height); +} diff --git a/common/src/main/java/dev/isxander/yacl/api/utils/OptionUtils.java b/common/src/main/java/dev/isxander/yacl/api/utils/OptionUtils.java new file mode 100644 index 0000000..22032bd --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl/api/utils/OptionUtils.java @@ -0,0 +1,39 @@ +package dev.isxander.yacl.api.utils; + +import dev.isxander.yacl.api.*; + +import java.util.function.Consumer; +import java.util.function.Function; + +public class OptionUtils { + /** + * Consumes all options, ignoring groups and categories. + * When consumer returns true, this function stops iterating. + */ + public static void consumeOptions(YetAnotherConfigLib yacl, Function<Option<?>, Boolean> consumer) { + for (ConfigCategory category : yacl.categories()) { + for (OptionGroup group : category.groups()) { + if (group instanceof ListOption<?> list) { + if (consumer.apply(list)) return; + } else { + for (Option<?> option : group.options()) { + if (consumer.apply(option)) return; + } + } + + } + } + } + + /** + * Consumes all options, ignoring groups and categories. + * + * @see OptionUtils#consumeOptions(YetAnotherConfigLib, Function) + */ + public static void forEachOptions(YetAnotherConfigLib yacl, Consumer<Option<?>> consumer) { + consumeOptions(yacl, (opt) -> { + consumer.accept(opt); + return false; + }); + } +} |