diff options
author | xander <xander@isxander.dev> | 2022-09-01 08:57:59 +0100 |
---|---|---|
committer | xander <xander@isxander.dev> | 2022-09-01 08:57:59 +0100 |
commit | 6f8ef7daaafd71090b2c334c10eadc8dedc738d9 (patch) | |
tree | d4054a65d99070c944132be83d25e109750dc5f9 /src/main/java/dev/isxander/yacl/impl | |
parent | 9d0a5e937f97c1c17d034393e01636d5241f376a (diff) | |
download | YetAnotherConfigLib-6f8ef7daaafd71090b2c334c10eadc8dedc738d9.tar.gz YetAnotherConfigLib-6f8ef7daaafd71090b2c334c10eadc8dedc738d9.tar.bz2 YetAnotherConfigLib-6f8ef7daaafd71090b2c334c10eadc8dedc738d9.zip |
GUI Implementation
Added groups
Added button "option"
Added test mod
Diffstat (limited to 'src/main/java/dev/isxander/yacl/impl')
7 files changed, 250 insertions, 18 deletions
diff --git a/src/main/java/dev/isxander/yacl/impl/ButtonOptionImpl.java b/src/main/java/dev/isxander/yacl/impl/ButtonOptionImpl.java new file mode 100644 index 0000000..516a5ee --- /dev/null +++ b/src/main/java/dev/isxander/yacl/impl/ButtonOptionImpl.java @@ -0,0 +1,105 @@ +package dev.isxander.yacl.impl; + +import dev.isxander.yacl.api.Binding; +import dev.isxander.yacl.api.ButtonOption; +import dev.isxander.yacl.api.Control; +import net.minecraft.text.Text; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.function.Function; + +@ApiStatus.Internal +public class ButtonOptionImpl implements ButtonOption { + private final Text name; + private final Text tooltip; + private final Runnable action; + private final Control<Runnable> control; + private final Binding<Runnable> binding; + + public ButtonOptionImpl( + @NotNull Text name, + @Nullable Text tooltip, + @NotNull Runnable action, + @NotNull Function<ButtonOption, Control<Runnable>> controlGetter + ) { + this.name = name; + this.tooltip = tooltip; + this.action = action; + this.control = controlGetter.apply(this); + this.binding = new EmptyBinderImpl(); + } + + @Override + public @NotNull Text name() { + return name; + } + + @Override + public @Nullable Text tooltip() { + return tooltip; + } + + @Override + public Runnable action() { + return action; + } + + @Override + public @NotNull Control<Runnable> control() { + return control; + } + + @Override + public @NotNull Binding<Runnable> binding() { + return binding; + } + + @Override + public boolean changed() { + return false; + } + + @Override + public Runnable pendingValue() { + throw new UnsupportedOperationException(); + } + + @Override + public void requestSet(Runnable value) { + throw new UnsupportedOperationException(); + } + + @Override + public void applyValue() { + + } + + @Override + public void forgetPendingValue() { + + } + + @Override + public void requestSetDefault() { + + } + + private static class EmptyBinderImpl implements Binding<Runnable> { + @Override + public void setValue(Runnable value) { + + } + + @Override + public Runnable getValue() { + throw new UnsupportedOperationException(); + } + + @Override + public Runnable defaultValue() { + throw new UnsupportedOperationException(); + } + } +} diff --git a/src/main/java/dev/isxander/yacl/impl/ConfigCategoryImpl.java b/src/main/java/dev/isxander/yacl/impl/ConfigCategoryImpl.java index 0f6f33b..27bd60c 100644 --- a/src/main/java/dev/isxander/yacl/impl/ConfigCategoryImpl.java +++ b/src/main/java/dev/isxander/yacl/impl/ConfigCategoryImpl.java @@ -2,11 +2,11 @@ package dev.isxander.yacl.impl; import com.google.common.collect.ImmutableList; import dev.isxander.yacl.api.ConfigCategory; -import dev.isxander.yacl.api.Option; +import dev.isxander.yacl.api.OptionGroup; import net.minecraft.text.Text; import org.jetbrains.annotations.ApiStatus; @ApiStatus.Internal -public record ConfigCategoryImpl(Text name, ImmutableList<Option<?>> options) implements ConfigCategory { +public record ConfigCategoryImpl(Text name, ImmutableList<OptionGroup> groups) implements ConfigCategory { } diff --git a/src/main/java/dev/isxander/yacl/impl/GenericBindingImpl.java b/src/main/java/dev/isxander/yacl/impl/GenericBindingImpl.java index f2321c7..9dfe2b0 100644 --- a/src/main/java/dev/isxander/yacl/impl/GenericBindingImpl.java +++ b/src/main/java/dev/isxander/yacl/impl/GenericBindingImpl.java @@ -30,7 +30,8 @@ public class GenericBindingImpl<T> implements Binding<T> { } @Override - public void resetValue() { - setValue(def); + public T defaultValue() { + return def; } + } diff --git a/src/main/java/dev/isxander/yacl/impl/OptionGroupImpl.java b/src/main/java/dev/isxander/yacl/impl/OptionGroupImpl.java new file mode 100644 index 0000000..73bff07 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/impl/OptionGroupImpl.java @@ -0,0 +1,10 @@ +package dev.isxander.yacl.impl; + +import com.google.common.collect.ImmutableList; +import dev.isxander.yacl.api.Option; +import dev.isxander.yacl.api.OptionGroup; +import net.minecraft.text.Text; +import org.jetbrains.annotations.Nullable; + +public record OptionGroupImpl(@Nullable Text name, ImmutableList<Option<?>> options, boolean isRoot) implements OptionGroup { +} diff --git a/src/main/java/dev/isxander/yacl/impl/OptionImpl.java b/src/main/java/dev/isxander/yacl/impl/OptionImpl.java index 3f6e5c2..a62726f 100644 --- a/src/main/java/dev/isxander/yacl/impl/OptionImpl.java +++ b/src/main/java/dev/isxander/yacl/impl/OptionImpl.java @@ -8,6 +8,8 @@ import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.function.Function; + @ApiStatus.Internal public class OptionImpl<T> implements Option<T> { private final Text name; @@ -15,16 +17,19 @@ public class OptionImpl<T> implements Option<T> { private final Control<T> control; private final Binding<T> binding; - private @Nullable T changedValue = null; + private T pendingValue; - public OptionImpl(@NotNull Text name, - @Nullable Text tooltip, - @NotNull Control<T> control, - @NotNull Binding<T> binding) { + public OptionImpl( + @NotNull Text name, + @Nullable Text tooltip, + @NotNull Function<Option<T>, Control<T>> controlGetter, + @NotNull Binding<T> binding + ) { this.name = name; this.tooltip = tooltip; - this.control = control; + this.control = controlGetter.apply(this); this.binding = binding; + this.pendingValue = binding().getValue(); } @Override @@ -49,17 +54,31 @@ public class OptionImpl<T> implements Option<T> { @Override public boolean changed() { - return !binding().getValue().equals(changedValue); + return !binding().getValue().equals(pendingValue); + } + + @Override + public T pendingValue() { + return pendingValue; } @Override public void requestSet(T value) { - this.changedValue = value; + pendingValue = value; } @Override public void applyValue() { - if (changedValue != null) - binding().setValue(changedValue); + binding().setValue(pendingValue); + } + + @Override + public void forgetPendingValue() { + pendingValue = binding().getValue(); + } + + @Override + public void requestSetDefault() { + pendingValue = binding().defaultValue(); } } diff --git a/src/main/java/dev/isxander/yacl/impl/YetAnotherConfigLibImpl.java b/src/main/java/dev/isxander/yacl/impl/YetAnotherConfigLibImpl.java index 0cef49f..5938bad 100644 --- a/src/main/java/dev/isxander/yacl/impl/YetAnotherConfigLibImpl.java +++ b/src/main/java/dev/isxander/yacl/impl/YetAnotherConfigLibImpl.java @@ -3,14 +3,17 @@ package dev.isxander.yacl.impl; import com.google.common.collect.ImmutableList; import dev.isxander.yacl.api.ConfigCategory; import dev.isxander.yacl.api.YetAnotherConfigLib; +import dev.isxander.yacl.gui.YACLScreen; import net.minecraft.client.gui.screen.Screen; import net.minecraft.text.Text; import org.jetbrains.annotations.ApiStatus; +import java.util.function.Consumer; + @ApiStatus.Internal -public record YetAnotherConfigLibImpl(Text title, ImmutableList<ConfigCategory> categories) implements YetAnotherConfigLib { +public record YetAnotherConfigLibImpl(Text title, ImmutableList<ConfigCategory> categories, Runnable saveFunction, Consumer<YACLScreen> initConsumer) implements YetAnotherConfigLib { @Override - public Screen generateScreen() { - return null; + public Screen generateScreen(Screen parent) { + return new YACLScreen(this, parent); } } diff --git a/src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java b/src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java index 908157d..4cb1c0e 100644 --- a/src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java +++ b/src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java @@ -4,7 +4,37 @@ 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<Integer> { +public class DimensionIntegerImpl implements Dimension<Integer> { + private int x, y; + private int width, height; + + public DimensionIntegerImpl(int x, int y, int width, int height) { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + } + + @Override + public Integer x() { + return x; + } + + @Override + public Integer y() { + return y; + } + + @Override + public Integer width() { + return width; + } + + @Override + public Integer height() { + return height; + } + @Override public Integer xLimit() { return x + width; @@ -16,7 +46,71 @@ public record DimensionIntegerImpl(Integer x, Integer y, Integer width, Integer } @Override + public Integer centerX() { + return x + width / 2; + } + + @Override + public Integer centerY() { + return y + height / 2; + } + + @Override public boolean isPointInside(Integer x, Integer y) { return x >= x() && x <= xLimit() && y >= y() && y <= yLimit(); } + + @Override + public Dimension<Integer> clone() { + return new DimensionIntegerImpl(x, y, width, height); + } + + @Override public Dimension<Integer> setX(Integer x) { this.x = x; return this; } + @Override public Dimension<Integer> setY(Integer y) { this.y = y; return this; } + @Override public Dimension<Integer> setWidth(Integer width) { this.width = width; return this; } + @Override public Dimension<Integer> setHeight(Integer height) { this.height = height; return this; } + + @Override + public Dimension<Integer> withX(Integer x) { + return clone().setX(x); + } + + @Override + public Dimension<Integer> withY(Integer y) { + return clone().setY(y); + } + + @Override + public Dimension<Integer> withWidth(Integer width) { + return clone().withWidth(width); + } + + @Override + public Dimension<Integer> withHeight(Integer height) { + return clone().withHeight(height); + } + + @Override + public Dimension<Integer> move(Integer x, Integer y) { + this.x += x; + this.y += y; + return this; + } + + @Override + public Dimension<Integer> expand(Integer width, Integer height) { + this.width += width; + this.height += height; + return this; + } + + @Override + public Dimension<Integer> moved(Integer x, Integer y) { + return clone().move(x, y); + } + + @Override + public Dimension<Integer> expanded(Integer width, Integer height) { + return clone().expand(width, height); + } } |