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 | |
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
33 files changed, 1718 insertions, 40 deletions
diff --git a/build.gradle.kts b/build.gradle.kts index db614c7..c4562de 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,8 +14,27 @@ plugins { group = "dev.isxander" version = "1.0.0" +val testmod by sourceSets.registering { + compileClasspath += sourceSets.main.get().compileClasspath + runtimeClasspath += sourceSets.main.get().runtimeClasspath +} + +loom { + runs { + register("testmod") { + client() + ideConfigGenerated(true) + name("Test Mod") + source(testmod.get()) + } + } + + createRemapConfigurations(testmod.get()) +} + repositories { mavenCentral() + maven("https://maven.terraformersmc.com") } val minecraftVersion: String by project @@ -24,8 +43,12 @@ val fabricLoaderVersion: String by project dependencies { minecraft("com.mojang:minecraft:$minecraftVersion") mappings("net.fabricmc:yarn:$minecraftVersion+build.+:v2") - modImplementation("net.fabricmc:fabric-loader:$fabricLoaderVersion") + + modImplementation(fabricApi.module("fabric-resource-loader-v0", "0.60.0+1.19.2")) + + "testmodImplementation"(sourceSets.main.get().output) + "modTestmodImplementation"("com.terraformersmc:modmenu:4.0.6") } tasks { diff --git a/src/main/java/dev/isxander/yacl/api/Binding.java b/src/main/java/dev/isxander/yacl/api/Binding.java index 67ff822..74120a0 100644 --- a/src/main/java/dev/isxander/yacl/api/Binding.java +++ b/src/main/java/dev/isxander/yacl/api/Binding.java @@ -10,7 +10,7 @@ public interface Binding<T> { T getValue(); - void resetValue(); + T defaultValue(); 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/ButtonOption.java b/src/main/java/dev/isxander/yacl/api/ButtonOption.java new file mode 100644 index 0000000..08436b3 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/api/ButtonOption.java @@ -0,0 +1,75 @@ +package dev.isxander.yacl.api; + +import dev.isxander.yacl.impl.ButtonOptionImpl; +import net.minecraft.text.MutableText; +import net.minecraft.text.Text; +import org.apache.commons.lang3.Validate; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Function; + +public interface ButtonOption extends Option<Runnable> { + Runnable action(); + + static Builder createBuilder() { + return new Builder(); + } + + class Builder { + private Text name; + private final List<Text> tooltipLines = new ArrayList<>(); + private Function<ButtonOption, Control<Runnable>> controlGetter; + private Runnable action; + + 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 action(@NotNull Runnable action) { + Validate.notNull(action, "`action` cannot be null"); + + this.action = action; + return this; + } + + public Builder controller(@NotNull Function<ButtonOption, Control<Runnable>> control) { + Validate.notNull(control, "`control` cannot be null"); + + this.controlGetter = control; + return this; + } + + public ButtonOption build() { + Validate.notNull(name, "`name` must not be null when building `Option`"); + Validate.notNull(controlGetter, "`control` must not be null when building `Option`"); + Validate.notNull(action, "`action` 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 ButtonOptionImpl(name, concatenatedTooltip, action, controlGetter); + } + } +} diff --git a/src/main/java/dev/isxander/yacl/api/ConfigCategory.java b/src/main/java/dev/isxander/yacl/api/ConfigCategory.java index ee2fbc7..13552d6 100644 --- a/src/main/java/dev/isxander/yacl/api/ConfigCategory.java +++ b/src/main/java/dev/isxander/yacl/api/ConfigCategory.java @@ -1,18 +1,22 @@ package dev.isxander.yacl.api; import com.google.common.collect.ImmutableList; +import dev.isxander.yacl.gui.YACLScreen; import dev.isxander.yacl.impl.ConfigCategoryImpl; +import dev.isxander.yacl.impl.OptionGroupImpl; +import net.minecraft.client.gui.widget.ClickableWidget; import net.minecraft.text.Text; import org.apache.commons.lang3.Validate; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.List; +import java.util.function.Function; public interface ConfigCategory { @NotNull Text name(); - @NotNull ImmutableList<Option<?>> options(); + @NotNull ImmutableList<OptionGroup> groups(); static Builder createBuilder() { return new Builder(); @@ -20,31 +24,44 @@ public interface ConfigCategory { class Builder { private Text name; - private final List<Option<?>> options = new ArrayList<>(); + private final List<Option<?>> rootOptions = new ArrayList<>(); + + private final List<OptionGroup> groups = new ArrayList<>(); private Builder() { } - public Builder setName(@NotNull Text name) { + public Builder name(@NotNull Text name) { Validate.notNull(name, "`name` cannot be null"); this.name = name; return this; } - public Builder addOption(@NotNull Option<?> option) { + public Builder option(@NotNull Option<?> option) { Validate.notNull(option, "`option` must not be null"); - this.options.add(option); + this.rootOptions.add(option); + return this; + } + + public Builder group(@NotNull OptionGroup group) { + Validate.notNull(group, "`group` must not be null"); + + this.groups.add(group); 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`"); + Validate.notEmpty(rootOptions, "`at least one option must be added to build `ConfigCategory`"); + + List<OptionGroup> combinedGroups = new ArrayList<>(); + combinedGroups.add(new OptionGroupImpl(Text.empty(), ImmutableList.copyOf(rootOptions), true)); + combinedGroups.addAll(groups); - return new ConfigCategoryImpl(name, ImmutableList.copyOf(options)); + return new ConfigCategoryImpl(name, ImmutableList.copyOf(combinedGroups)); } } } diff --git a/src/main/java/dev/isxander/yacl/api/Control.java b/src/main/java/dev/isxander/yacl/api/Control.java index 0733c4f..242b2c8 100644 --- a/src/main/java/dev/isxander/yacl/api/Control.java +++ b/src/main/java/dev/isxander/yacl/api/Control.java @@ -1,10 +1,14 @@ package dev.isxander.yacl.api; import dev.isxander.yacl.api.utils.Dimension; -import dev.isxander.yacl.gui.AbstractWidget; +import dev.isxander.yacl.gui.controllers.ControlWidget; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.text.Text; public interface Control<T> { Option<T> option(); - AbstractWidget provideWidget(Dimension<Integer> widgetDimension); + Text formatValue(); + + ControlWidget<?> provideWidget(Screen screen, Dimension<Integer> widgetDimension); } diff --git a/src/main/java/dev/isxander/yacl/api/NameableEnum.java b/src/main/java/dev/isxander/yacl/api/NameableEnum.java new file mode 100644 index 0000000..12a58c3 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/api/NameableEnum.java @@ -0,0 +1,7 @@ +package dev.isxander.yacl.api; + +import net.minecraft.text.Text; + +public interface NameableEnum { + Text getDisplayName(); +} diff --git a/src/main/java/dev/isxander/yacl/api/Option.java b/src/main/java/dev/isxander/yacl/api/Option.java index 1c7a8a1..5a98d50 100644 --- a/src/main/java/dev/isxander/yacl/api/Option.java +++ b/src/main/java/dev/isxander/yacl/api/Option.java @@ -10,6 +10,7 @@ import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; +import java.util.function.Function; import java.util.function.Supplier; public interface Option<T> { @@ -23,11 +24,17 @@ public interface Option<T> { boolean changed(); + T pendingValue(); + void requestSet(T value); void applyValue(); - static <T> Builder<T> createBuilder() { + void forgetPendingValue(); + + void requestSetDefault(); + + static <T> Builder<T> createBuilder(Class<T> clazz) { return new Builder<>(); } @@ -36,7 +43,7 @@ public interface Option<T> { private final List<Text> tooltipLines = new ArrayList<>(); - private Control<T> control; + private Function<Option<T>, Control<T>> controlGetter; private Binding<T> binding; @@ -58,10 +65,10 @@ public interface Option<T> { return this; } - public Builder<T> controller(@NotNull Control<T> control) { + public Builder<T> controller(@NotNull Function<Option<T>, Control<T>> control) { Validate.notNull(control, "`control` cannot be null"); - this.control = control; + this.controlGetter = control; return this; } @@ -73,7 +80,7 @@ public interface Option<T> { } 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(def, "`def` must not be null"); Validate.notNull(getter, "`getter` must not be null"); Validate.notNull(setter, "`setter` must not be null"); @@ -83,7 +90,7 @@ public interface Option<T> { 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(controlGetter, "`control` must not be null when building `Option`"); Validate.notNull(binding, "`binding` must not be null when building `Option`"); MutableText concatenatedTooltip = Text.empty(); @@ -95,7 +102,7 @@ public interface Option<T> { concatenatedTooltip.append(line); } - return new OptionImpl<>(name, concatenatedTooltip, control, binding); + return new OptionImpl<>(name, concatenatedTooltip, controlGetter, binding); } } } diff --git a/src/main/java/dev/isxander/yacl/api/OptionGroup.java b/src/main/java/dev/isxander/yacl/api/OptionGroup.java new file mode 100644 index 0000000..bedbc82 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/api/OptionGroup.java @@ -0,0 +1,52 @@ +package dev.isxander.yacl.api; + +import com.google.common.collect.ImmutableList; +import dev.isxander.yacl.impl.OptionGroupImpl; +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; + +public interface OptionGroup { + Text name(); + + @NotNull ImmutableList<Option<?>> options(); + + boolean isRoot(); + + static Builder createBuilder() { + return new Builder(); + } + + class Builder { + private Text name = Text.empty(); + private final List<Option<?>> options = new ArrayList<>(); + + private Builder() { + + } + + public Builder name(@NotNull Text name) { + Validate.notNull(name, "`name` must not be null"); + + this.name = name; + return this; + } + + public Builder option(@NotNull Option<?> option) { + Validate.notNull(option, "`option` must not be null"); + + this.options.add(option); + return this; + } + + public OptionGroup build() { + Validate.notEmpty(options, "`options` must not be empty to build `OptionGroup`"); + + return new OptionGroupImpl(name, ImmutableList.copyOf(options), false); + } + } +} diff --git a/src/main/java/dev/isxander/yacl/api/YetAnotherConfigLib.java b/src/main/java/dev/isxander/yacl/api/YetAnotherConfigLib.java index d7f8416..a598e27 100644 --- a/src/main/java/dev/isxander/yacl/api/YetAnotherConfigLib.java +++ b/src/main/java/dev/isxander/yacl/api/YetAnotherConfigLib.java @@ -1,14 +1,17 @@ package dev.isxander.yacl.api; import com.google.common.collect.ImmutableList; +import dev.isxander.yacl.gui.YACLScreen; 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 org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; +import java.util.function.Consumer; public interface YetAnotherConfigLib { @@ -16,7 +19,11 @@ public interface YetAnotherConfigLib { ImmutableList<ConfigCategory> categories(); - Screen generateScreen(); + Runnable saveFunction(); + + Consumer<YACLScreen> initConsumer(); + + Screen generateScreen(@Nullable Screen parent); static Builder createBuilder(Text title) { return new Builder(title); @@ -25,31 +32,47 @@ public interface YetAnotherConfigLib { class Builder { private Text title; private final List<ConfigCategory> categories = new ArrayList<>(); + private Runnable saveFunction = () -> {}; + private Consumer<YACLScreen> initConsumer = screen -> {}; private Builder(@NotNull Text title) { Validate.notNull(title, "`title` cannot be null"); this.title = title; } - public Builder setTitle(@NotNull Text title) { + public Builder title(@NotNull Text title) { Validate.notNull(title, "`title` cannot be null"); this.title = title; return this; } - public Builder addCategory(@NotNull ConfigCategory category) { + public Builder category(@NotNull ConfigCategory category) { Validate.notNull(category, "`category` cannot be null"); this.categories.add(category); return this; } + public Builder save(@NotNull Runnable saveFunction) { + Validate.notNull(saveFunction, "`saveFunction` cannot be null"); + + this.saveFunction = saveFunction; + return this; + } + + public Builder screenInit(@NotNull Consumer<YACLScreen> initConsumer) { + Validate.notNull(initConsumer, "`initConsumer` cannot be null"); + + this.initConsumer = initConsumer; + 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)); + return new YetAnotherConfigLibImpl(title, ImmutableList.copyOf(categories), saveFunction, initConsumer); } } } diff --git a/src/main/java/dev/isxander/yacl/api/utils/Dimension.java b/src/main/java/dev/isxander/yacl/api/utils/Dimension.java index cf7127a..69958b1 100644 --- a/src/main/java/dev/isxander/yacl/api/utils/Dimension.java +++ b/src/main/java/dev/isxander/yacl/api/utils/Dimension.java @@ -12,8 +12,29 @@ public interface Dimension<T extends Number> { T xLimit(); T yLimit(); + T centerX(); + T centerY(); + boolean isPointInside(T x, T y); + Dimension<T> clone(); + + Dimension<T> setX(T x); + Dimension<T> setY(T y); + Dimension<T> setWidth(T width); + Dimension<T> setHeight(T height); + + Dimension<T> withX(T x); + Dimension<T> withY(T y); + Dimension<T> withWidth(T width); + Dimension<T> withHeight(T height); + + Dimension<T> move(T x, T y); + Dimension<T> expand(T width, T height); + + Dimension<T> moved(T x, T y); + Dimension<T> expanded(T width, T height); + static Dimension<Integer> 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/api/utils/OptionUtils.java b/src/main/java/dev/isxander/yacl/api/utils/OptionUtils.java new file mode 100644 index 0000000..ed51683 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/api/utils/OptionUtils.java @@ -0,0 +1,28 @@ +package dev.isxander.yacl.api.utils; + +import dev.isxander.yacl.api.ConfigCategory; +import dev.isxander.yacl.api.Option; +import dev.isxander.yacl.api.OptionGroup; +import dev.isxander.yacl.api.YetAnotherConfigLib; + +import java.util.function.Consumer; +import java.util.function.Function; + +public class OptionUtils { + public static void consumeOptions(YetAnotherConfigLib yacl, Function<Option<?>, Boolean> consumer) { + for (ConfigCategory category : yacl.categories()) { + for (OptionGroup group : category.groups()) { + for (Option<?> option : group.options()) { + if (!consumer.apply(option)) return; + } + } + } + } + + public static void forEachOptions(YetAnotherConfigLib yacl, Consumer<Option<?>> consumer) { + consumeOptions(yacl, (opt) -> { + consumer.accept(opt); + return true; + }); + } +} diff --git a/src/main/java/dev/isxander/yacl/gui/AbstractWidget.java b/src/main/java/dev/isxander/yacl/gui/AbstractWidget.java index 2a8a519..7affbd4 100644 --- a/src/main/java/dev/isxander/yacl/gui/AbstractWidget.java +++ b/src/main/java/dev/isxander/yacl/gui/AbstractWidget.java @@ -1,8 +1,53 @@ package dev.isxander.yacl.gui; +import com.mojang.blaze3d.systems.RenderSystem; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.Drawable; +import net.minecraft.client.gui.DrawableHelper; import net.minecraft.client.gui.Element; import net.minecraft.client.gui.Selectable; +import net.minecraft.client.gui.widget.ClickableWidget; +import net.minecraft.client.render.GameRenderer; +import net.minecraft.client.sound.PositionedSoundInstance; +import net.minecraft.client.sound.SoundManager; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.sound.SoundEvents; public abstract class AbstractWidget implements Element, Drawable, Selectable { + protected final MinecraftClient client = MinecraftClient.getInstance(); + protected final TextRenderer textRenderer = client.textRenderer; + + public void tick() { + + } + + protected void drawButtonRect(MatrixStack matrices, int x1, int y1, int x2, int y2, boolean hovered) { + if (x1 > x2) { + int xx1 = x1; + x1 = x2; + x2 = xx1; + } + if (y1 > y2) { + int yy1 = y1; + y1 = y2; + y2 = yy1; + } + int width = x2 - x1; + int height = y2 - y1; + + RenderSystem.setShader(GameRenderer::getPositionTexShader); + RenderSystem.setShaderTexture(0, ClickableWidget.WIDGETS_TEXTURE); + RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); + int i = hovered ? 2 : 1; + RenderSystem.enableBlend(); + RenderSystem.defaultBlendFunc(); + RenderSystem.enableDepthTest(); + DrawableHelper.drawTexture(matrices, x1, y1, 0, 0, 46 + i * 20, width / 2, height, 256, 256); + DrawableHelper.drawTexture(matrices, x1 + width / 2, y1, 0, 200 - width / 2f, 46 + i * 20, width / 2, height, 256, 256); + } + + public void playDownSound() { + MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F)); + } } diff --git a/src/main/java/dev/isxander/yacl/gui/OptionListWidget.java b/src/main/java/dev/isxander/yacl/gui/OptionListWidget.java new file mode 100644 index 0000000..6cb7090 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/OptionListWidget.java @@ -0,0 +1,90 @@ +package dev.isxander.yacl.gui; + +import dev.isxander.yacl.api.ConfigCategory; +import dev.isxander.yacl.api.Option; +import dev.isxander.yacl.api.OptionGroup; +import dev.isxander.yacl.api.utils.Dimension; +import dev.isxander.yacl.gui.controllers.ControlWidget; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.font.TextRenderer; +import net.minecraft.client.gui.Element; +import net.minecraft.client.gui.Selectable; +import net.minecraft.client.gui.widget.ElementListWidget; +import net.minecraft.client.util.math.MatrixStack; + +import java.util.List; + +public class OptionListWidget extends ElementListWidget<OptionListWidget.Entry> { + + public OptionListWidget(ConfigCategory category, YACLScreen screen, MinecraftClient client, int width, int height) { + super(client, width / 3 * 2, height, 0, height, 22); + left = width - this.width; + right = width; + + for (OptionGroup group : category.groups()) { + if (!group.isRoot()) + addEntry(new GroupSeparatorEntry(group)); + for (Option<?> option : group.options()) { + addEntry(new OptionEntry(option.control().provideWidget(screen, null))); + } + } + } + + @Override + protected int getScrollbarPositionX() { + return left + super.getScrollbarPositionX(); + } + + public static abstract class Entry extends ElementListWidget.Entry<Entry> { + + } + + private static class OptionEntry extends Entry { + private final ControlWidget<?> widget; + + public OptionEntry(ControlWidget<?> widget) { + this.widget = widget; + } + + @Override + public void render(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { + widget.dim = Dimension.ofInt(x, y, entryWidth, 20); + + widget.render(matrices, mouseX, mouseY, tickDelta); + } + + @Override + public List<? extends Selectable> selectableChildren() { + return List.of(widget); + } + + @Override + public List<? extends Element> children() { + return List.of(widget); + } + } + + private static class GroupSeparatorEntry extends Entry { + private final OptionGroup group; + + public GroupSeparatorEntry(OptionGroup group) { + this.group = group; + } + + @Override + public void render(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { + TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer; + drawCenteredText(matrices, textRenderer, group.name(), x + entryWidth / 2, y + entryHeight / 2 - textRenderer.fontHeight / 2, -1); + } + + @Override + public List<? extends Selectable> selectableChildren() { + return List.of(); + } + + @Override + public List<? extends Element> children() { + return List.of(); + } + } +} diff --git a/src/main/java/dev/isxander/yacl/gui/YACLScreen.java b/src/main/java/dev/isxander/yacl/gui/YACLScreen.java new file mode 100644 index 0000000..97cc8ed --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/YACLScreen.java @@ -0,0 +1,150 @@ +package dev.isxander.yacl.gui; + +import dev.isxander.yacl.api.ConfigCategory; +import dev.isxander.yacl.api.Option; +import dev.isxander.yacl.api.YetAnotherConfigLib; +import dev.isxander.yacl.api.utils.Dimension; +import dev.isxander.yacl.api.utils.OptionUtils; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.text.Text; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; + +public class YACLScreen extends Screen { + public final YetAnotherConfigLib config; + public int currentCategoryIdx; + + private final Screen parent; + + public OptionListWidget optionList; + public final List<ButtonWidget> categoryButtons; + public ButtonWidget finishedSaveButton, cancelResetButton, undoButton; + + public YACLScreen(YetAnotherConfigLib config, Screen parent) { + super(config.title()); + this.config = config; + this.parent = parent; + this.categoryButtons = new ArrayList<>(); + this.currentCategoryIdx = 0; + } + + @Override + protected void init() { + categoryButtons.clear(); + int columnWidth = width / 3; + int padding = columnWidth / 20; + Dimension<Integer> categoryDim = Dimension.ofInt(padding, padding, columnWidth - padding * 2, 20); + int idx = 0; + for (ConfigCategory category : config.categories()) { + ButtonWidget categoryWidget = new ButtonWidget( + categoryDim.x(), categoryDim.y(), + categoryDim.width(), categoryDim.height(), + category.name(), + (btn) -> changeCategory(categoryButtons.indexOf(btn)) + ); + if (idx == currentCategoryIdx) + categoryWidget.active = false; + categoryButtons.add(categoryWidget); + addDrawableChild(categoryWidget); + + idx++; + categoryDim = categoryDim.moved(0, 21); + } + + Dimension<Integer> actionDim = Dimension.ofInt(padding, height - padding - 20, columnWidth - padding * 2, 20); + finishedSaveButton = new ButtonWidget(actionDim.x(), actionDim.y(), actionDim.width(), actionDim.height(), Text.empty(), (btn) -> { + if (pendingChanges()) { + OptionUtils.forEachOptions(config, Option::applyValue); + config.saveFunction().run(); + } else close(); + }); + actionDim = actionDim.moved(0, -22).expanded(-actionDim.width() / 2 - 2, 0); + cancelResetButton = new ButtonWidget(actionDim.x(), actionDim.y(), actionDim.width(), actionDim.height(), Text.translatable("yacl.gui.cancel"), (btn) -> { + if (pendingChanges()) { + OptionUtils.forEachOptions(config, Option::forgetPendingValue); + close(); + } else { + OptionUtils.forEachOptions(config, Option::requestSetDefault); + } + + }); + actionDim = actionDim.moved(actionDim.width() + 4, 0); + undoButton = new ButtonWidget(actionDim.x(), actionDim.y(), actionDim.width(), actionDim.height(), Text.translatable("yacl.gui.undo"), (btn) -> { + OptionUtils.forEachOptions(config, Option::forgetPendingValue); + }); + + updateActionAvailability(); + addDrawableChild(finishedSaveButton); + addDrawableChild(cancelResetButton); + addDrawableChild(undoButton); + + ConfigCategory currentCategory = config.categories().get(currentCategoryIdx); + optionList = new OptionListWidget(currentCategory, this, client, width, height); + addSelectableChild(optionList); + + config.initConsumer().accept(this); + } + + @Override + public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { + renderBackground(matrices); + + super.render(matrices, mouseX, mouseY, delta); + + optionList.render(matrices, mouseX, mouseY, delta); + } + + @Override + public void tick() { + updateActionAvailability(); + } + + private void changeCategory(int idx) { + int currentIndex = 0; + for (ButtonWidget categoryWidget : categoryButtons) { + categoryWidget.active = currentIndex != idx; + currentIndex++; + } + currentCategoryIdx = idx; + refreshGUI(); + } + + private boolean pendingChanges() { + AtomicBoolean pendingChanges = new AtomicBoolean(false); + OptionUtils.consumeOptions(config, (option) -> { + if (option.changed()) { + pendingChanges.set(true); + return false; + } + return true; + }); + + return pendingChanges.get(); + } + + private void updateActionAvailability() { + boolean pendingChanges = pendingChanges(); + + undoButton.active = pendingChanges; + finishedSaveButton.setMessage(pendingChanges ? Text.translatable("yacl.gui.save") : Text.translatable("yacl.gui.finished")); + cancelResetButton.setMessage(pendingChanges ? Text.translatable("yacl.gui.cancel") : Text.translatable("yacl.gui.reset")); + } + + private void refreshGUI() { + init(client, width, height); + } + + @Override + public boolean shouldCloseOnEsc() { + return !undoButton.active; + } + + @Override + public void close() { + client.setScreen(parent); + } +} diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/ActionControl.java b/src/main/java/dev/isxander/yacl/gui/controllers/ActionControl.java new file mode 100644 index 0000000..66e8e35 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/ActionControl.java @@ -0,0 +1,58 @@ +package dev.isxander.yacl.gui.controllers; + +import dev.isxander.yacl.api.ButtonOption; +import dev.isxander.yacl.api.Control; +import dev.isxander.yacl.api.utils.Dimension; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.text.Text; + +public class ActionControl implements Control<Runnable> { + private final ButtonOption option; + private final Text executeText; + + public ActionControl(ButtonOption option) { + this(option, Text.translatable("yacl.control.action.execute")); + } + + public ActionControl(ButtonOption option, Text executeText) { + this.option = option; + this.executeText = executeText; + + } + + @Override + public ButtonOption option() { + return option; + } + + @Override + public Text formatValue() { + return executeText; + } + + @Override + public ControlWidget<ActionControl> provideWidget(Screen screen, Dimension<Integer> widgetDimension) { + return new ActionControlElement(this, screen, widgetDimension); + } + + public static class ActionControlElement extends ControlWidget<ActionControl> { + public ActionControlElement(ActionControl control, Screen screen, Dimension<Integer> dim) { + super(control, screen, dim); + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + if (isMouseOver(mouseX, mouseY)) { + playDownSound(); + control.option().action().run(); + return true; + } + return false; + } + + @Override + protected int getHoveredControlWidth() { + return getUnhoveredControlWidth(); + } + } +} diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/ControlWidget.java b/src/main/java/dev/isxander/yacl/gui/controllers/ControlWidget.java new file mode 100644 index 0000000..be05ba4 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/ControlWidget.java @@ -0,0 +1,125 @@ +package dev.isxander.yacl.gui.controllers; + +import dev.isxander.yacl.api.Control; +import dev.isxander.yacl.api.utils.Dimension; +import dev.isxander.yacl.gui.AbstractWidget; +import net.minecraft.client.gui.DrawableHelper; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.text.OrderedText; +import net.minecraft.text.Text; + +import java.util.List; + +public abstract class ControlWidget<T extends Control<?>> extends AbstractWidget { + protected final T control; + protected final List<OrderedText> wrappedDescription; + + public Dimension<Integer> dim; + protected final Screen screen; + + protected boolean hovered = false; + protected float hoveredTicks = 0; + + public ControlWidget(T control, Screen screen, Dimension<Integer> dim) { + this.control = control; + this.dim = dim; + this.screen = screen; + this.wrappedDescription = textRenderer.wrapLines(control.option().tooltip(), screen.width / 2); + } + + @Override + public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { + hovered = isMouseOver(mouseX, mouseY); + if (hovered) { + hoveredTicks += delta; + } else { + hoveredTicks = 0; + } + + Text name = control.option().name(); + String nameString = name.getString(); + + boolean firstIter = true; + while (textRenderer.getWidth(nameString) > dim.width() - getControlWidth() - getXPadding() - 7) { + nameString = nameString.substring(0, nameString.length() - (firstIter ? 2 : 5)).trim(); + nameString += "..."; + + firstIter = false; + } + + Text shortenedName = Text.literal(nameString).fillStyle(name.getStyle()); + + drawButtonRect(matrices, dim.x(), dim.y(), dim.xLimit(), dim.yLimit(), hovered); + matrices.push(); + matrices.translate(dim.x() + getXPadding(), getTextY(), 0); + textRenderer.drawWithShadow(matrices, shortenedName, 0, 0, -1); + matrices.pop(); + + drawValueText(matrices); + if (hovered) { + drawHoveredControl(matrices, mouseX, mouseY, delta); + } + + if (hoveredTicks > 40) { + screen.renderOrderedTooltip(matrices, wrappedDescription, mouseX, mouseY); + } + } + + protected void drawHoveredControl(MatrixStack matrices, int mouseX, int mouseY, float delta) { + + } + + protected void drawValueText(MatrixStack matrices) { + Text valueText = control.formatValue(); + matrices.push(); + matrices.translate(dim.xLimit() - textRenderer.getWidth(valueText) - getXPadding(), getTextY(), 0); + textRenderer.drawWithShadow(matrices, valueText, 0, 0, -1); + matrices.pop(); + } + + @Override + public boolean isMouseOver(double mouseX, double mouseY) { + return this.dim.isPointInside((int) mouseX, (int) mouseY); + } + + protected int getControlWidth() { + return hovered ? getHoveredControlWidth() : getUnhoveredControlWidth(); + } + + protected abstract int getHoveredControlWidth(); + + protected int getUnhoveredControlWidth() { + return textRenderer.getWidth(control.formatValue()); + } + + protected int getXPadding() { + return 5; + } + + protected int getYPadding() { + return 2; + } + + protected void drawOutline(MatrixStack matrices, int x1, int y1, int x2, int y2, int width, int color) { + DrawableHelper.fill(matrices, x1, y1, x2, y1 + width, color); + DrawableHelper.fill(matrices, x2, y1, x2 - width, y2, color); + DrawableHelper.fill(matrices, x1, y2, x2, y2 - width, color); + DrawableHelper.fill(matrices, x1, y1, x1 + width, y2, color); + } + + protected float getTextY() { + return dim.y() + dim.height() / 2f - textRenderer.fontHeight / 2f; + } + + @Override + public SelectionType getType() { + return hovered ? SelectionType.HOVERED : SelectionType.NONE; + } + + @Override + public void appendNarrations(NarrationMessageBuilder builder) { + + } +} diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/EnumControl.java b/src/main/java/dev/isxander/yacl/gui/controllers/EnumControl.java new file mode 100644 index 0000000..d032937 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/EnumControl.java @@ -0,0 +1,77 @@ +package dev.isxander.yacl.gui.controllers; + +import dev.isxander.yacl.api.Control; +import dev.isxander.yacl.api.NameableEnum; +import dev.isxander.yacl.api.Option; +import dev.isxander.yacl.api.utils.Dimension; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.text.Text; + +import java.util.function.Function; + +public class EnumControl<T extends Enum<T>> implements Control<T> { + private final Option<T> option; + private final Function<T, Text> valueFormatter; + private final Class<T> enumClass; + + public EnumControl(Option<T> option, Class<T> enumClass) { + this(option, enumClass, value -> { + if (value instanceof NameableEnum nameableEnum) + return nameableEnum.getDisplayName(); + return Text.of(value.name()); + }); + } + + public EnumControl(Option<T> option, Class<T> enumClass, Function<T, Text> valueFormatter) { + this.option = option; + this.valueFormatter = valueFormatter; + this.enumClass = enumClass; + } + + @Override + public Option<T> option() { + return option; + } + + @Override + public Text formatValue() { + return valueFormatter.apply(option().pendingValue()); + } + + @Override + public ControlWidget<EnumControl<T>> provideWidget(Screen screen, Dimension<Integer> widgetDimension) { + return new EnumControlElement<>(this, screen, widgetDimension, enumClass.getEnumConstants()); + } + + public static class EnumControlElement<T extends Enum<T>> extends ControlWidget<EnumControl<T>> { + private final T[] values; + + public EnumControlElement(EnumControl<T> control, Screen screen, Dimension<Integer> dim, T[] values) { + super(control, screen, dim); + this.values = values; + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + if (!isMouseOver(mouseX, mouseY) || (button != 0 && button != 1)) + return false; + + playDownSound(); + + int change = button == 1 || Screen.hasShiftDown() ? -1 : 1; + int targetIdx = control.option().pendingValue().ordinal() + change; + if (targetIdx >= values.length) { + targetIdx -= values.length; + } else if (targetIdx < 0) { + targetIdx += values.length; + } + control.option().requestSet(values[targetIdx]); + return true; + } + + @Override + protected int getHoveredControlWidth() { + return getUnhoveredControlWidth(); + } + } +} diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/TickBoxControl.java b/src/main/java/dev/isxander/yacl/gui/controllers/TickBoxControl.java new file mode 100644 index 0000000..fb0e595 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/TickBoxControl.java @@ -0,0 +1,100 @@ +package dev.isxander.yacl.gui.controllers; + +import dev.isxander.yacl.api.Control; +import dev.isxander.yacl.api.Option; +import dev.isxander.yacl.api.utils.Dimension; +import net.minecraft.client.gui.DrawableHelper; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; + +import java.util.function.Function; + +public class TickBoxControl implements Control<Boolean> { + + public static final Function<Boolean, Text> ON_OFF_FORMATTER = (state) -> + state + ? Text.translatable("yacl.control.tickbox.on").formatted(Formatting.GREEN) + : Text.translatable("yacl.control.tickbox.off").formatted(Formatting.RED); + + public static final Function<Boolean, Text> TRUE_FALSE_FORMATTER = (state) -> + state + ? Text.translatable("yacl.control.tickbox.true").formatted(Formatting.GREEN) + : Text.translatable("yacl.control.tickbox.false").formatted(Formatting.RED); + + public static final Function<Boolean, Text> YES_NO_FORMATTER = (state) -> + state + ? Text.translatable("yacl.control.tickbox.yes").formatted(Formatting.GREEN) + : Text.translatable("yacl.control.tickbox.no").formatted(Formatting.RED); + + private final Option<Boolean> option; + private final Function<Boolean, Text> valueFormatter; + + public TickBoxControl(Option<Boolean> option) { + this(option, ON_OFF_FORMATTER); + } + + public TickBoxControl(Option<Boolean> option, Function<Boolean, Text> valueFormatter) { + this.option = option; + this.valueFormatter = valueFormatter; + + } + + @Override + public Option<Boolean> option() { + return option; + } + + @Override + public Text formatValue() { + return valueFormatter.apply(option().pendingValue()); + } + + @Override + public ControlWidget<TickBoxControl> provideWidget(Screen screen, Dimension<Integer> widgetDimension) { + return new TickBoxControlElement(this, screen, widgetDimension); + } + + public static class TickBoxControlElement extends ControlWidget<TickBoxControl> { + private TickBoxControlElement(TickBoxControl control, Screen screen, Dimension<Integer> dim) { + super(control, screen, dim); + } + + @Override + protected void drawHoveredControl(MatrixStack matrices, int mouseX, int mouseY, float delta) { + int outlineSize = 10; + int outlineX1 = dim.xLimit() - getXPadding() - outlineSize; + int outlineY1 = dim.centerY() - outlineSize / 2; + int outlineX2 = dim.xLimit() - getXPadding(); + int outlineY2 = dim.centerY() + outlineSize / 2; + drawOutline(matrices, outlineX1 + 1, outlineY1 + 1, outlineX2 + 1, outlineY2 + 1, 1, 0xFF404040); + drawOutline(matrices, outlineX1, outlineY1, outlineX2, outlineY2, 1, -1); + if (control.option().pendingValue()) { + DrawableHelper.fill(matrices, outlineX1 + 3, outlineY1 + 3, outlineX2 - 1, outlineY2 - 1, 0xFF404040); + DrawableHelper.fill(matrices, outlineX1 + 2, outlineY1 + 2, outlineX2 - 2, outlineY2 - 2, -1); + } + } + + @Override + protected void drawValueText(MatrixStack matrices) { + if (!hovered) + super.drawValueText(matrices); + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + if (!isMouseOver(mouseX, mouseY)) + return false; + + control.option().requestSet(!control.option().pendingValue()); + playDownSound(); + return true; + } + + @Override + protected int getHoveredControlWidth() { + return 10; + } + } +} diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderControl.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderControl.java new file mode 100644 index 0000000..5aaa6c4 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderControl.java @@ -0,0 +1,72 @@ +package dev.isxander.yacl.gui.controllers.slider; + +import dev.isxander.yacl.api.Option; +import net.minecraft.text.Text; +import org.apache.commons.lang3.Validate; + +import java.util.function.Function; + +public class DoubleSliderControl implements ISliderControl<Double> { + public static final Function<Double, Text> DEFAULT_FORMATTER = value -> Text.of(String.format("%.2f", value)); + + private final Option<Double> option; + + private final double min, max, interval; + + private final Function<Double, Text> valueFormatter; + + public DoubleSliderControl(Option<Double> option, double min, double max, double interval) { + this(option, min, max, interval, DEFAULT_FORMATTER); + } + + public DoubleSliderControl(Option<Double> option, double min, double max, double interval, Function<Double, Text> valueFormatter) { + Validate.isTrue(max > min, "`max` cannot be smaller than `min`"); + Validate.isTrue(interval > 0, "`interval` must be more than 0"); + + this.option = option; + this.min = min; + this.max = max; + this.interval = interval; + this.valueFormatter = valueFormatter; + } + + @Override + public Option<Double> option() { + return option; + } + + @Override + public Text formatValue() { + return valueFormatter.apply(option().pendingValue()); + } + + @Override + public double min() { + return min; + } + + @Override + public double max() { + return max; + } + + @Override + public double interval() { + return interval; + } + + @Override + public void setPendingValue(double value) { + option().requestSet(value); + } + + @Override + public double pendingValue() { + return option().pendingValue(); + } + + @Override + public Text getValueText(double value) { + return valueFormatter.apply(value); + } +} diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderControl.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderControl.java new file mode 100644 index 0000000..d74ec33 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderControl.java @@ -0,0 +1,72 @@ +package dev.isxander.yacl.gui.controllers.slider; + +import dev.isxander.yacl.api.Option; +import net.minecraft.text.Text; +import org.apache.commons.lang3.Validate; + +import java.util.function.Function; + +public class FloatSliderControl implements ISliderControl<Float> { + public static final Function<Float, Text> DEFAULT_FORMATTER = value -> Text.of(String.format("%.1f", value)); + + private final Option<Float> option; + + private final float min, max, interval; + + private final Function<Float, Text> valueFormatter; + + public FloatSliderControl(Option<Float> option, float min, float max, float interval) { + this(option, min, max, interval, DEFAULT_FORMATTER); + } + + public FloatSliderControl(Option<Float> option, float min, float max, float interval, Function<Float, Text> valueFormatter) { + Validate.isTrue(max > min, "`max` cannot be smaller than `min`"); + Validate.isTrue(interval > 0, "`interval` must be more than 0"); + + this.option = option; + this.min = min; + this.max = max; + this.interval = interval; + this.valueFormatter = valueFormatter; + } + + @Override + public Option<Float> option() { + return option; + } + + @Override + public Text formatValue() { + return valueFormatter.apply(option().pendingValue()); + } + + @Override + public double min() { + return min; + } + + @Override + public double max() { + return max; + } + + @Override + public double interval() { + return interval; + } + + @Override + public void setPendingValue(double value) { + option().requestSet((float) value); + } + + @Override + public double pendingValue() { + return option().pendingValue(); + } + + @Override + public Text getValueText(double value) { + return valueFormatter.apply((float) value); + } +} diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/ISliderControl.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/ISliderControl.java new file mode 100644 index 0000000..8c1cdf8 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/ISliderControl.java @@ -0,0 +1,29 @@ +package dev.isxander.yacl.gui.controllers.slider; + +import dev.isxander.yacl.api.Control; +import dev.isxander.yacl.api.utils.Dimension; +import dev.isxander.yacl.gui.controllers.ControlWidget; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.text.Text; + +public interface ISliderControl<T extends Number> extends Control<T> { + double min(); + + double max(); + + double interval(); + + default double range() { + return max() - min(); + } + + void setPendingValue(double value); + double pendingValue(); + + Text getValueText(double value); + + @Override + default ControlWidget<?> provideWidget(Screen screen, Dimension<Integer> widgetDimension) { + return new SliderControlElement(this, screen, widgetDimension, min(), max(), interval()); + } +} diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderControl.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderControl.java new file mode 100644 index 0000000..99c8137 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderControl.java @@ -0,0 +1,72 @@ +package dev.isxander.yacl.gui.controllers.slider; + +import dev.isxander.yacl.api.Option; +import net.minecraft.text.Text; +import org.apache.commons.lang3.Validate; + +import java.util.function.Function; + +public class IntegerSliderControl implements ISliderControl<Integer> { + public static final Function<Integer, Text> DEFAULT_FORMATTER = value -> Text.of(String.valueOf(value)); + + private final Option<Integer> option; + + private final int min, max, interval; + + private final Function<Integer, Text> valueFormatter; + + public IntegerSliderControl(Option<Integer> option, int min, int max, int interval) { + this(option, min, max, interval, DEFAULT_FORMATTER); + } + + public IntegerSliderControl(Option<Integer> option, int min, int max, int interval, Function<Integer, Text> valueFormatter) { + Validate.isTrue(max > min, "`max` cannot be smaller than `min`"); + Validate.isTrue(interval > 0, "`interval` must be more than 0"); + + this.option = option; + this.min = min; + this.max = max; + this.interval = interval; + this.valueFormatter = valueFormatter; + } + + @Override + public Option<Integer> option() { + return option; + } + + @Override + public Text formatValue() { + return valueFormatter.apply(option().pendingValue()); + } + + @Override + public double min() { + return min; + } + + @Override + public double max() { + return max; + } + + @Override + public double interval() { + return interval; + } + + @Override + public void setPendingValue(double value) { + option().requestSet((int) value); + } + + @Override + public double pendingValue() { + return option().pendingValue(); + } + + @Override + public Text getValueText(double value) { + return valueFormatter.apply((int) value); + } +} diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControlElement.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControlElement.java new file mode 100644 index 0000000..b8dab71 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControlElement.java @@ -0,0 +1,125 @@ +package dev.isxander.yacl.gui.controllers.slider; + +import dev.isxander.yacl.api.utils.Dimension; +import dev.isxander.yacl.gui.controllers.ControlWidget; +import net.minecraft.client.gui.DrawableHelper; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.text.Text; +import net.minecraft.util.math.MathHelper; + +public class SliderControlElement extends ControlWidget<ISliderControl<?>> { + private final double min, max, interval; + + private float interpolation; + + private Dimension<Integer> sliderBounds; + + private boolean mouseDown = false; + + public SliderControlElement(ISliderControl<?> option, Screen screen, Dimension<Integer> dim, double min, double max, double interval) { + super(option, screen, dim); + this.min = min; + this.max = max; + this.interval = interval; + } + + @Override + public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { + super.render(matrices, mouseX, mouseY, delta); + + if (sliderBounds == null) + sliderBounds = Dimension.ofInt(dim.xLimit() - getXPadding() - getThumbWidth() / 2 - dim.width() / 3, dim.centerY() - 4, dim.width() / 3, 8); + calculateInterpolation(); + } + + @Override + protected void drawHoveredControl(MatrixStack matrices, int mouseX, int mouseY, float delta) { + // track + DrawableHelper.fill(matrices, sliderBounds.x(), sliderBounds.centerY() - 1, sliderBounds.xLimit(), sliderBounds.centerY(), -1); + // track shadow + DrawableHelper.fill(matrices, sliderBounds.x() + 1, sliderBounds.centerY(), sliderBounds.xLimit() + 1, sliderBounds.centerY() + 1, 0xFF404040); + + // thumb shadow + DrawableHelper.fill(matrices, getThumbX() - getThumbWidth() / 2 + 1, sliderBounds.y() + 1, getThumbX() + getThumbWidth() / 2 + 1, sliderBounds.yLimit() + 1, 0xFF404040); + // thumb + DrawableHelper.fill(matrices, getThumbX() - getThumbWidth() / 2, sliderBounds.y(), getThumbX() + getThumbWidth() / 2, sliderBounds.yLimit(), -1); + } + + @Override + protected void drawValueText(MatrixStack matrices) { + matrices.push(); + if (hovered) + matrices.translate(-(sliderBounds.width() + 6 + getThumbWidth() / 2f), 0, 0); + super.drawValueText(matrices); + matrices.pop(); + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + if (button != 0 || !sliderBounds.isPointInside((int) mouseX, (int) mouseY)) + return false; + + mouseDown = true; + + setValueFromMouse(mouseX); + return true; + } + + @Override + public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) { + if (button != 0 || !sliderBounds.isPointInside((int) mouseX, (int) mouseY)) + return false; + + setValueFromMouse(mouseX); + return true; + } + + @Override + public boolean mouseScrolled(double mouseX, double mouseY, double amount) { + if (!isMouseOver(mouseX, mouseY) || !Screen.hasShiftDown()) + return false; + + control.setPendingValue(MathHelper.clamp(control.pendingValue() + interval * amount, min, max)); + calculateInterpolation(); + return true; + } + + @Override + public boolean mouseReleased(double mouseX, double mouseY, int button) { + if (mouseDown) + playDownSound(); + mouseDown = false; + + return super.mouseReleased(mouseX, mouseY, button); + } + + private void setValueFromMouse(double mouseX) { + double value = (mouseX - sliderBounds.x()) / sliderBounds.width() * control.range(); + double roundedValue = min + (interval * Math.round(value / interval)); + control.setPendingValue(roundedValue); + calculateInterpolation(); + } + + @Override + protected int getHoveredControlWidth() { + int textWidth = textRenderer.getWidth(getValueText()); + return hovered ? sliderBounds.width() + textWidth + 6 + getThumbWidth() / 2 : textWidth ; + } + + private void calculateInterpolation() { + interpolation = (float) ((control.pendingValue() - control.min()) * 1 / control.range()); + } + + private Text getValueText() { + return control.getValueText(control.pendingValue()); + } + + private int getThumbX() { + return (int) (sliderBounds.x() + sliderBounds.width() * interpolation); + } + + private int getThumbWidth() { + return 4; + } +} 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); + } } diff --git a/src/main/resources/assets/yet-another-config-lib/lang/en_us.json b/src/main/resources/assets/yet-another-config-lib/lang/en_us.json new file mode 100644 index 0000000..7f609d3 --- /dev/null +++ b/src/main/resources/assets/yet-another-config-lib/lang/en_us.json @@ -0,0 +1,16 @@ +{ + "yacl.control.tickbox.on": "ON", + "yacl.control.tickbox.off": "OFF", + "yacl.control.tickbox.true": "True", + "yacl.control.tickbox.false": "False", + "yacl.control.tickbox.yes": "Yes", + "yacl.control.tickbox.no": "No", + + "yacl.control.action.execute": "EXECUTE", + + "yacl.gui.save": "Save Changes", + "yacl.gui.finished": "Finished", + "yacl.gui.cancel": "Cancel", + "yacl.gui.reset": "Reset", + "yacl.gui.undo": "Undo" +} diff --git a/src/testmod/java/dev/isxander/yacl/test/ModMenuIntegration.java b/src/testmod/java/dev/isxander/yacl/test/ModMenuIntegration.java new file mode 100644 index 0000000..f328ab4 --- /dev/null +++ b/src/testmod/java/dev/isxander/yacl/test/ModMenuIntegration.java @@ -0,0 +1,137 @@ +package dev.isxander.yacl.test; + +import com.terraformersmc.modmenu.api.ConfigScreenFactory; +import com.terraformersmc.modmenu.api.ModMenuApi; +import dev.isxander.yacl.api.*; +import dev.isxander.yacl.gui.controllers.ActionControl; +import dev.isxander.yacl.gui.controllers.EnumControl; +import dev.isxander.yacl.gui.controllers.TickBoxControl; +import dev.isxander.yacl.gui.controllers.slider.DoubleSliderControl; +import dev.isxander.yacl.gui.controllers.slider.FloatSliderControl; +import dev.isxander.yacl.gui.controllers.slider.IntegerSliderControl; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.text.Text; + +public class ModMenuIntegration implements ModMenuApi { + @Override + public ConfigScreenFactory<?> getModConfigScreenFactory() { + return (parent) -> YetAnotherConfigLib.createBuilder(Text.of("Test GUI")) + .category(ConfigCategory.createBuilder() + .name(Text.of("Control Examples")) + .option(Option.createBuilder(boolean.class) + .name(Text.of("Tick Box")) + .tooltip(Text.of("Super long tooltip that is very descriptive to show off the text wrapping features of the thingy yes whwowwoow")) + .binding( + false, + () -> TestSettings.tickbox, + (value) -> TestSettings.tickbox = value + ) + .controller(TickBoxControl::new) + .build()) + .option(Option.createBuilder(int.class) + .name(Text.of("Int Slider that is cut off because the slider")) + .binding( + 0, + () -> TestSettings.intSlider, + (value) -> TestSettings.intSlider = value + ) + .controller(opt -> new IntegerSliderControl(opt, 0, 3, 1)) + .build()) + .option(Option.createBuilder(double.class) + .name(Text.of("Double Slider")) + .binding( + 0.0, + () -> TestSettings.doubleSlider, + (value) -> TestSettings.doubleSlider = value + ) + .controller(opt -> new DoubleSliderControl(opt, 0, 3, 0.05)) + .build()) + .option(Option.createBuilder(float.class) + .name(Text.of("Float Slider")) + .binding( + 0f, + () -> TestSettings.floatSlider, + (value) -> TestSettings.floatSlider = value + ) + .controller(opt -> new FloatSliderControl(opt, 0, 3, 0.1f)) + .build()) + .option(Option.createBuilder(TestSettings.Alphabet.class) + .name(Text.of("Enum Cycler")) + .binding( + TestSettings.Alphabet.A, + () -> TestSettings.enumOption, + (value) -> TestSettings.enumOption = value + ) + .controller(opt -> new EnumControl<>(opt, TestSettings.Alphabet.class)) + .build()) + .option(ButtonOption.createBuilder() + .name(Text.of("Button \"Option\"")) + .action(() -> System.out.println("aha!")) + .controller(ActionControl::new) + .build()) + .build()) + .category(ConfigCategory.createBuilder() + .name(Text.of("Group Test")) + .option(Option.createBuilder(boolean.class) + .name(Text.of("Root Test")) + .binding( + false, + () -> TestSettings.groupTestRoot, + value -> TestSettings.groupTestRoot = value + ) + .controller(TickBoxControl::new) + .build()) + .group(OptionGroup.createBuilder() + .name(Text.of("First Group")) + .option(Option.createBuilder(boolean.class) + .name(Text.of("First Group Test 1")) + .binding( + false, + () -> TestSettings.groupTestFirstGroup, + value -> TestSettings.groupTestFirstGroup = value + ) + .controller(TickBoxControl::new) + .build()) + .option(Option.createBuilder(boolean.class) + .name(Text.of("First Group Test 2")) + .binding( + false, + () -> TestSettings.groupTestFirstGroup2, + value -> TestSettings.groupTestFirstGroup2 = value + ) + .controller(TickBoxControl::new) + .build()) + .build()) + .group(OptionGroup.createBuilder() + .name(Text.empty()) + .option(Option.createBuilder(boolean.class) + .name(Text.of("Second Group Test")) + .binding( + false, + () -> TestSettings.groupTestSecondGroup, + value -> TestSettings.groupTestSecondGroup = value + ) + .controller(TickBoxControl::new) + .build()) + .build()) + .build()) + .build().generateScreen(parent); + } + + private static class TestSettings { + private static boolean tickbox = false; + private static int intSlider = 0; + private static double doubleSlider = 0; + private static float floatSlider = 0; + private static Alphabet enumOption = Alphabet.A; + + private static boolean groupTestRoot = false; + private static boolean groupTestFirstGroup = false; + private static boolean groupTestFirstGroup2 = false; + private static boolean groupTestSecondGroup = false; + + public enum Alphabet { + A, B, C + } + } +} diff --git a/src/testmod/resources/fabric.mod.json b/src/testmod/resources/fabric.mod.json new file mode 100644 index 0000000..0d78804 --- /dev/null +++ b/src/testmod/resources/fabric.mod.json @@ -0,0 +1,21 @@ +{ + "schemaVersion": 1, + "id": "test-mod", + "version": "1", + "name": "YACL Test", + "authors": [ + "isXander" + ], + "license": "LGPL-3.0-or-later", + "environment": "client", + "entrypoints": { + "modmenu": [ + "dev.isxander.yacl.test.ModMenuIntegration" + ] + }, + "depends": { + "fabricloader": ">=0.14.0", + "minecraft": "1.19.x", + "java": ">=17" + } +} |