From 6f8ef7daaafd71090b2c334c10eadc8dedc738d9 Mon Sep 17 00:00:00 2001 From: xander Date: Thu, 1 Sep 2022 08:57:59 +0100 Subject: GUI Implementation Added groups Added button "option" Added test mod --- .../dev/isxander/yacl/impl/ButtonOptionImpl.java | 105 +++++++++++++++++++++ .../dev/isxander/yacl/impl/ConfigCategoryImpl.java | 4 +- .../dev/isxander/yacl/impl/GenericBindingImpl.java | 5 +- .../dev/isxander/yacl/impl/OptionGroupImpl.java | 10 ++ .../java/dev/isxander/yacl/impl/OptionImpl.java | 39 ++++++-- .../yacl/impl/YetAnotherConfigLibImpl.java | 9 +- .../yacl/impl/utils/DimensionIntegerImpl.java | 96 ++++++++++++++++++- 7 files changed, 250 insertions(+), 18 deletions(-) create mode 100644 src/main/java/dev/isxander/yacl/impl/ButtonOptionImpl.java create mode 100644 src/main/java/dev/isxander/yacl/impl/OptionGroupImpl.java (limited to 'src/main/java/dev/isxander/yacl/impl') 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 control; + private final Binding binding; + + public ButtonOptionImpl( + @NotNull Text name, + @Nullable Text tooltip, + @NotNull Runnable action, + @NotNull Function> 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 control() { + return control; + } + + @Override + public @NotNull Binding 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 { + @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> options) implements ConfigCategory { +public record ConfigCategoryImpl(Text name, ImmutableList 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 implements Binding { } @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> 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 implements Option { private final Text name; @@ -15,16 +17,19 @@ public class OptionImpl implements Option { private final Control control; private final Binding binding; - private @Nullable T changedValue = null; + private T pendingValue; - public OptionImpl(@NotNull Text name, - @Nullable Text tooltip, - @NotNull Control control, - @NotNull Binding binding) { + public OptionImpl( + @NotNull Text name, + @Nullable Text tooltip, + @NotNull Function, Control> controlGetter, + @NotNull Binding 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 implements Option { @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 categories) implements YetAnotherConfigLib { +public record YetAnotherConfigLibImpl(Text title, ImmutableList categories, Runnable saveFunction, Consumer 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 { +public class DimensionIntegerImpl implements Dimension { + 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; @@ -15,8 +45,72 @@ public record DimensionIntegerImpl(Integer x, Integer y, Integer width, Integer return y + height; } + @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 clone() { + return new DimensionIntegerImpl(x, y, width, height); + } + + @Override public Dimension setX(Integer x) { this.x = x; return this; } + @Override public Dimension setY(Integer y) { this.y = y; return this; } + @Override public Dimension setWidth(Integer width) { this.width = width; return this; } + @Override public Dimension setHeight(Integer height) { this.height = height; return this; } + + @Override + public Dimension withX(Integer x) { + return clone().setX(x); + } + + @Override + public Dimension withY(Integer y) { + return clone().setY(y); + } + + @Override + public Dimension withWidth(Integer width) { + return clone().withWidth(width); + } + + @Override + public Dimension withHeight(Integer height) { + return clone().withHeight(height); + } + + @Override + public Dimension move(Integer x, Integer y) { + this.x += x; + this.y += y; + return this; + } + + @Override + public Dimension expand(Integer width, Integer height) { + this.width += width; + this.height += height; + return this; + } + + @Override + public Dimension moved(Integer x, Integer y) { + return clone().move(x, y); + } + + @Override + public Dimension expanded(Integer width, Integer height) { + return clone().expand(width, height); + } } -- cgit