From 9d0a5e937f97c1c17d034393e01636d5241f376a Mon Sep 17 00:00:00 2001 From: isXander Date: Tue, 30 Aug 2022 20:04:21 +0100 Subject: Builder API done! gui now... --- .../java/dev/isxander/fabricmodtemplate/.gitkeep | 0 .../dev/isxander/fabricmodtemplate/mixins/.gitkeep | 0 src/main/java/dev/isxander/yacl/api/Binding.java | 18 ++++ .../java/dev/isxander/yacl/api/ConfigCategory.java | 50 ++++++++++ src/main/java/dev/isxander/yacl/api/Control.java | 10 ++ src/main/java/dev/isxander/yacl/api/Option.java | 101 +++++++++++++++++++++ .../dev/isxander/yacl/api/YetAnotherConfigLib.java | 55 +++++++++++ .../dev/isxander/yacl/api/utils/Dimension.java | 20 ++++ .../java/dev/isxander/yacl/gui/AbstractWidget.java | 8 ++ .../dev/isxander/yacl/impl/ConfigCategoryImpl.java | 12 +++ .../dev/isxander/yacl/impl/GenericBindingImpl.java | 36 ++++++++ .../java/dev/isxander/yacl/impl/OptionImpl.java | 65 +++++++++++++ .../yacl/impl/YetAnotherConfigLibImpl.java | 16 ++++ .../yacl/impl/utils/DimensionIntegerImpl.java | 22 +++++ 14 files changed, 413 insertions(+) delete mode 100644 src/main/java/dev/isxander/fabricmodtemplate/.gitkeep delete mode 100644 src/main/java/dev/isxander/fabricmodtemplate/mixins/.gitkeep create mode 100644 src/main/java/dev/isxander/yacl/api/Binding.java create mode 100644 src/main/java/dev/isxander/yacl/api/ConfigCategory.java create mode 100644 src/main/java/dev/isxander/yacl/api/Control.java create mode 100644 src/main/java/dev/isxander/yacl/api/Option.java create mode 100644 src/main/java/dev/isxander/yacl/api/YetAnotherConfigLib.java create mode 100644 src/main/java/dev/isxander/yacl/api/utils/Dimension.java create mode 100644 src/main/java/dev/isxander/yacl/gui/AbstractWidget.java create mode 100644 src/main/java/dev/isxander/yacl/impl/ConfigCategoryImpl.java create mode 100644 src/main/java/dev/isxander/yacl/impl/GenericBindingImpl.java create mode 100644 src/main/java/dev/isxander/yacl/impl/OptionImpl.java create mode 100644 src/main/java/dev/isxander/yacl/impl/YetAnotherConfigLibImpl.java create mode 100644 src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java (limited to 'src/main/java/dev') diff --git a/src/main/java/dev/isxander/fabricmodtemplate/.gitkeep b/src/main/java/dev/isxander/fabricmodtemplate/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/dev/isxander/fabricmodtemplate/mixins/.gitkeep b/src/main/java/dev/isxander/fabricmodtemplate/mixins/.gitkeep deleted file mode 100644 index e69de29..0000000 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 { + void setValue(T value); + + T getValue(); + + void resetValue(); + + static Binding of(T def, Supplier getter, Consumer 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> options(); + + static Builder createBuilder() { + return new Builder(); + } + + class Builder { + private Text name; + private final List> 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 { + Option option(); + + AbstractWidget provideWidget(Dimension 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 { + @NotNull Text name(); + + @Nullable Text tooltip(); + + @NotNull Control control(); + + @NotNull Binding binding(); + + boolean changed(); + + void requestSet(T value); + + void applyValue(); + + static Builder createBuilder() { + return new Builder<>(); + } + + class Builder { + private Text name; + + private final List tooltipLines = new ArrayList<>(); + + private Control control; + + private Binding binding; + + private Builder() { + + } + + public Builder name(@NotNull Text name) { + Validate.notNull(name, "`name` cannot be null"); + + this.name = name; + return this; + } + + public Builder tooltip(@NotNull Text... tooltips) { + Validate.notEmpty(tooltips, "`tooltips` cannot be empty"); + + tooltipLines.addAll(List.of(tooltips)); + return this; + } + + public Builder controller(@NotNull Control control) { + Validate.notNull(control, "`control` cannot be null"); + + this.control = control; + return this; + } + + public Builder binding(@NotNull Binding binding) { + Validate.notNull(binding, "`binding` cannot be null"); + + this.binding = binding; + return this; + } + + public Builder 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 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 categories(); + + Screen generateScreen(); + + static Builder createBuilder(Text title) { + return new Builder(title); + } + + class Builder { + private Text title; + private final List 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 x(); + T y(); + + T width(); + T height(); + + T xLimit(); + T yLimit(); + + boolean isPointInside(T x, T y); + + static Dimension ofInt(int x, int y, int width, int height) { + return new DimensionIntegerImpl(x, y, width, height); + } +} diff --git a/src/main/java/dev/isxander/yacl/gui/AbstractWidget.java b/src/main/java/dev/isxander/yacl/gui/AbstractWidget.java new file mode 100644 index 0000000..2a8a519 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/AbstractWidget.java @@ -0,0 +1,8 @@ +package dev.isxander.yacl.gui; + +import net.minecraft.client.gui.Drawable; +import net.minecraft.client.gui.Element; +import net.minecraft.client.gui.Selectable; + +public abstract class AbstractWidget implements Element, Drawable, Selectable { +} diff --git a/src/main/java/dev/isxander/yacl/impl/ConfigCategoryImpl.java b/src/main/java/dev/isxander/yacl/impl/ConfigCategoryImpl.java new file mode 100644 index 0000000..0f6f33b --- /dev/null +++ b/src/main/java/dev/isxander/yacl/impl/ConfigCategoryImpl.java @@ -0,0 +1,12 @@ +package dev.isxander.yacl.impl; + +import com.google.common.collect.ImmutableList; +import dev.isxander.yacl.api.ConfigCategory; +import dev.isxander.yacl.api.Option; +import net.minecraft.text.Text; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal +public record ConfigCategoryImpl(Text name, ImmutableList> options) implements ConfigCategory { + +} diff --git a/src/main/java/dev/isxander/yacl/impl/GenericBindingImpl.java b/src/main/java/dev/isxander/yacl/impl/GenericBindingImpl.java new file mode 100644 index 0000000..f2321c7 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/impl/GenericBindingImpl.java @@ -0,0 +1,36 @@ +package dev.isxander.yacl.impl; + +import dev.isxander.yacl.api.Binding; +import org.jetbrains.annotations.ApiStatus; + +import java.util.function.Consumer; +import java.util.function.Supplier; + +@ApiStatus.Internal +public class GenericBindingImpl implements Binding { + private final T def; + private final Supplier getter; + private final Consumer setter; + + public GenericBindingImpl(T def, Supplier getter, Consumer setting) { + this.def = def; + this.getter = getter; + this.setter = setting; + } + + + @Override + public void setValue(T value) { + setter.accept(value); + } + + @Override + public T getValue() { + return getter.get(); + } + + @Override + public void resetValue() { + setValue(def); + } +} diff --git a/src/main/java/dev/isxander/yacl/impl/OptionImpl.java b/src/main/java/dev/isxander/yacl/impl/OptionImpl.java new file mode 100644 index 0000000..3f6e5c2 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/impl/OptionImpl.java @@ -0,0 +1,65 @@ +package dev.isxander.yacl.impl; + +import dev.isxander.yacl.api.Binding; +import dev.isxander.yacl.api.Control; +import dev.isxander.yacl.api.Option; +import net.minecraft.text.Text; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +@ApiStatus.Internal +public class OptionImpl implements Option { + private final Text name; + private final Text tooltip; + private final Control control; + private final Binding binding; + + private @Nullable T changedValue = null; + + public OptionImpl(@NotNull Text name, + @Nullable Text tooltip, + @NotNull Control control, + @NotNull Binding binding) { + this.name = name; + this.tooltip = tooltip; + this.control = control; + this.binding = binding; + } + + @Override + public @NotNull Text name() { + return name; + } + + @Override + public @Nullable Text tooltip() { + return tooltip; + } + + @Override + public @NotNull Control control() { + return control; + } + + @Override + public @NotNull Binding binding() { + return binding; + } + + @Override + public boolean changed() { + return !binding().getValue().equals(changedValue); + } + + @Override + public void requestSet(T value) { + this.changedValue = value; + } + + @Override + public void applyValue() { + if (changedValue != null) + binding().setValue(changedValue); + } +} diff --git a/src/main/java/dev/isxander/yacl/impl/YetAnotherConfigLibImpl.java b/src/main/java/dev/isxander/yacl/impl/YetAnotherConfigLibImpl.java new file mode 100644 index 0000000..0cef49f --- /dev/null +++ b/src/main/java/dev/isxander/yacl/impl/YetAnotherConfigLibImpl.java @@ -0,0 +1,16 @@ +package dev.isxander.yacl.impl; + +import com.google.common.collect.ImmutableList; +import dev.isxander.yacl.api.ConfigCategory; +import dev.isxander.yacl.api.YetAnotherConfigLib; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.text.Text; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal +public record YetAnotherConfigLibImpl(Text title, ImmutableList categories) implements YetAnotherConfigLib { + @Override + public Screen generateScreen() { + return null; + } +} diff --git a/src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java b/src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java new file mode 100644 index 0000000..908157d --- /dev/null +++ b/src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java @@ -0,0 +1,22 @@ +package dev.isxander.yacl.impl.utils; + +import dev.isxander.yacl.api.utils.Dimension; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal +public record DimensionIntegerImpl(Integer x, Integer y, Integer width, Integer height) implements Dimension { + @Override + public Integer xLimit() { + return x + width; + } + + @Override + public Integer yLimit() { + return y + height; + } + + @Override + public boolean isPointInside(Integer x, Integer y) { + return x >= x() && x <= xLimit() && y >= y() && y <= yLimit(); + } +} -- cgit