diff options
author | isXander <isxander@users.noreply.github.com> | 2022-08-30 20:04:21 +0100 |
---|---|---|
committer | isXander <isxander@users.noreply.github.com> | 2022-08-30 20:04:21 +0100 |
commit | 9d0a5e937f97c1c17d034393e01636d5241f376a (patch) | |
tree | 40b15bc97835c798d6b4489afaf8c84970c3ea0b /src/main/java/dev/isxander/yacl/api | |
parent | d643a44c866504a642f9de0055417a82585d96e5 (diff) | |
download | YetAnotherConfigLib-9d0a5e937f97c1c17d034393e01636d5241f376a.tar.gz YetAnotherConfigLib-9d0a5e937f97c1c17d034393e01636d5241f376a.tar.bz2 YetAnotherConfigLib-9d0a5e937f97c1c17d034393e01636d5241f376a.zip |
Builder API done!
gui now...
Diffstat (limited to 'src/main/java/dev/isxander/yacl/api')
6 files changed, 254 insertions, 0 deletions
diff --git a/src/main/java/dev/isxander/yacl/api/Binding.java b/src/main/java/dev/isxander/yacl/api/Binding.java new file mode 100644 index 0000000..67ff822 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/api/Binding.java @@ -0,0 +1,18 @@ +package dev.isxander.yacl.api; + +import dev.isxander.yacl.impl.GenericBindingImpl; + +import java.util.function.Consumer; +import java.util.function.Supplier; + +public interface Binding<T> { + void setValue(T value); + + T getValue(); + + void resetValue(); + + static <T> Binding<T> of(T def, Supplier<T> getter, Consumer<T> setter) { + return new GenericBindingImpl<>(def, getter, setter); + } +} diff --git a/src/main/java/dev/isxander/yacl/api/ConfigCategory.java b/src/main/java/dev/isxander/yacl/api/ConfigCategory.java new file mode 100644 index 0000000..ee2fbc7 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/api/ConfigCategory.java @@ -0,0 +1,50 @@ +package dev.isxander.yacl.api; + +import com.google.common.collect.ImmutableList; +import dev.isxander.yacl.impl.ConfigCategoryImpl; +import net.minecraft.text.Text; +import org.apache.commons.lang3.Validate; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; + +public interface ConfigCategory { + @NotNull Text name(); + + @NotNull ImmutableList<Option<?>> options(); + + static Builder createBuilder() { + return new Builder(); + } + + class Builder { + private Text name; + private final List<Option<?>> options = new ArrayList<>(); + + private Builder() { + + } + + public Builder setName(@NotNull Text name) { + Validate.notNull(name, "`name` cannot be null"); + + this.name = name; + return this; + } + + public Builder addOption(@NotNull Option<?> option) { + Validate.notNull(option, "`option` must not be null"); + + this.options.add(option); + return this; + } + + public ConfigCategory build() { + Validate.notNull(name, "`name` must not be null to build `ConfigCategory`"); + Validate.notEmpty(options, "`at least one option must be added to build `ConfigCategory`"); + + return new ConfigCategoryImpl(name, ImmutableList.copyOf(options)); + } + } +} diff --git a/src/main/java/dev/isxander/yacl/api/Control.java b/src/main/java/dev/isxander/yacl/api/Control.java new file mode 100644 index 0000000..0733c4f --- /dev/null +++ b/src/main/java/dev/isxander/yacl/api/Control.java @@ -0,0 +1,10 @@ +package dev.isxander.yacl.api; + +import dev.isxander.yacl.api.utils.Dimension; +import dev.isxander.yacl.gui.AbstractWidget; + +public interface Control<T> { + Option<T> option(); + + AbstractWidget provideWidget(Dimension<Integer> widgetDimension); +} diff --git a/src/main/java/dev/isxander/yacl/api/Option.java b/src/main/java/dev/isxander/yacl/api/Option.java new file mode 100644 index 0000000..1c7a8a1 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/api/Option.java @@ -0,0 +1,101 @@ +package dev.isxander.yacl.api; + +import dev.isxander.yacl.impl.OptionImpl; +import net.minecraft.text.MutableText; +import net.minecraft.text.Text; +import org.apache.commons.lang3.Validate; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; +import java.util.function.Supplier; + +public interface Option<T> { + @NotNull Text name(); + + @Nullable Text tooltip(); + + @NotNull Control<T> control(); + + @NotNull Binding<T> binding(); + + boolean changed(); + + void requestSet(T value); + + void applyValue(); + + static <T> Builder<T> createBuilder() { + return new Builder<>(); + } + + class Builder<T> { + private Text name; + + private final List<Text> tooltipLines = new ArrayList<>(); + + private Control<T> control; + + private Binding<T> binding; + + private Builder() { + + } + + public Builder<T> name(@NotNull Text name) { + Validate.notNull(name, "`name` cannot be null"); + + this.name = name; + return this; + } + + public Builder<T> tooltip(@NotNull Text... tooltips) { + Validate.notEmpty(tooltips, "`tooltips` cannot be empty"); + + tooltipLines.addAll(List.of(tooltips)); + return this; + } + + public Builder<T> controller(@NotNull Control<T> control) { + Validate.notNull(control, "`control` cannot be null"); + + this.control = control; + return this; + } + + public Builder<T> binding(@NotNull Binding<T> binding) { + Validate.notNull(binding, "`binding` cannot be null"); + + this.binding = binding; + return this; + } + + public Builder<T> binding(@NotNull T def, @NotNull Supplier<@NotNull T> getter, @NotNull Consumer<@NotNull T> setter) { + Validate.notNull(def, "`default` must not be null"); + Validate.notNull(getter, "`getter` must not be null"); + Validate.notNull(setter, "`setter` must not be null"); + + this.binding = Binding.of(def, getter, setter); + return this; + } + + public Option<T> build() { + Validate.notNull(name, "`name` must not be null when building `Option`"); + Validate.notNull(control, "`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; + + concatenatedTooltip.append(line); + } + + return new OptionImpl<>(name, concatenatedTooltip, control, binding); + } + } +} diff --git a/src/main/java/dev/isxander/yacl/api/YetAnotherConfigLib.java b/src/main/java/dev/isxander/yacl/api/YetAnotherConfigLib.java new file mode 100644 index 0000000..d7f8416 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/api/YetAnotherConfigLib.java @@ -0,0 +1,55 @@ +package dev.isxander.yacl.api; + +import com.google.common.collect.ImmutableList; +import dev.isxander.yacl.impl.YetAnotherConfigLibImpl; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.text.Text; +import org.apache.commons.lang3.Validate; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; + +public interface YetAnotherConfigLib { + + Text title(); + + ImmutableList<ConfigCategory> categories(); + + Screen generateScreen(); + + static Builder createBuilder(Text title) { + return new Builder(title); + } + + class Builder { + private Text title; + private final List<ConfigCategory> categories = new ArrayList<>(); + + private Builder(@NotNull Text title) { + Validate.notNull(title, "`title` cannot be null"); + this.title = title; + } + + public Builder setTitle(@NotNull Text title) { + Validate.notNull(title, "`title` cannot be null"); + + this.title = title; + return this; + } + + public Builder addCategory(@NotNull ConfigCategory category) { + Validate.notNull(category, "`category` cannot be null"); + + this.categories.add(category); + return this; + } + + public YetAnotherConfigLib build() { + Validate.notNull(title, "`title must not be null to build `YetAnotherConfigLib`"); + Validate.notEmpty(categories, "`categories` must not be empty to build `YetAnotherConfigLib`"); + + return new YetAnotherConfigLibImpl(title, ImmutableList.copyOf(categories)); + } + } +} diff --git a/src/main/java/dev/isxander/yacl/api/utils/Dimension.java b/src/main/java/dev/isxander/yacl/api/utils/Dimension.java new file mode 100644 index 0000000..cf7127a --- /dev/null +++ b/src/main/java/dev/isxander/yacl/api/utils/Dimension.java @@ -0,0 +1,20 @@ +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(); + + boolean isPointInside(T x, T y); + + static Dimension<Integer> ofInt(int x, int y, int width, int height) { + return new DimensionIntegerImpl(x, y, width, height); + } +} |