diff options
author | xander <xander@isxander.dev> | 2022-09-01 12:20:05 +0100 |
---|---|---|
committer | xander <xander@isxander.dev> | 2022-09-01 12:20:05 +0100 |
commit | 21728802a4ee2b65a32b626140ff01bf59a456c6 (patch) | |
tree | b20264e89856295df3d679a8a47574597e429408 /src | |
parent | 4d977cc9764ecf0073650f126700f6ff638fa06b (diff) | |
download | YetAnotherConfigLib-21728802a4ee2b65a32b626140ff01bf59a456c6.tar.gz YetAnotherConfigLib-21728802a4ee2b65a32b626140ff01bf59a456c6.tar.bz2 YetAnotherConfigLib-21728802a4ee2b65a32b626140ff01bf59a456c6.zip |
make tickbox separate controller from on off text
Diffstat (limited to 'src')
5 files changed, 231 insertions, 41 deletions
diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/BooleanController.java b/src/main/java/dev/isxander/yacl/gui/controllers/BooleanController.java new file mode 100644 index 0000000..4a14799 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/BooleanController.java @@ -0,0 +1,138 @@ +package dev.isxander.yacl.gui.controllers; + +import dev.isxander.yacl.api.Controller; +import dev.isxander.yacl.api.Option; +import dev.isxander.yacl.api.utils.Dimension; +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 org.jetbrains.annotations.ApiStatus; + +import java.util.function.Function; + +/** + * This controller renders a simple formatted {@link Text} + */ +public class BooleanController implements Controller<Boolean> { + + public static final Function<Boolean, Text> ON_OFF_FORMATTER = (state) -> + state + ? Text.translatable("yacl.control.tickbox.on") + : Text.translatable("yacl.control.tickbox.off"); + + public static final Function<Boolean, Text> TRUE_FALSE_FORMATTER = (state) -> + state + ? Text.translatable("yacl.control.tickbox.true") + : Text.translatable("yacl.control.tickbox.false"); + + public static final Function<Boolean, Text> YES_NO_FORMATTER = (state) -> + state + ? Text.translatable("yacl.control.tickbox.yes") + : Text.translatable("yacl.control.tickbox.no"); + + private final Option<Boolean> option; + private final Function<Boolean, Text> valueFormatter; + private final boolean coloured; + + /** + * Constructs a tickbox controller + * with the default value formatter of {@link BooleanController#ON_OFF_FORMATTER} + * + * @param option bound option + */ + public BooleanController(Option<Boolean> option) { + this(option, ON_OFF_FORMATTER, true); + } + + /** + * Constructs a tickbox controller + * with the default value formatter of {@link BooleanController#ON_OFF_FORMATTER} + * + * @param option bound option + * @param coloured value format is green or red depending on the state + */ + public BooleanController(Option<Boolean> option, boolean coloured) { + this(option, ON_OFF_FORMATTER, coloured); + } + + /** + * Constructs a tickbox controller + * + * @param option bound option + * @param valueFormatter format value into any {@link Text} + * @param coloured value format is green or red depending on the state + */ + public BooleanController(Option<Boolean> option, Function<Boolean, Text> valueFormatter, boolean coloured) { + this.option = option; + this.valueFormatter = valueFormatter; + this.coloured = coloured; + } + + /** + * {@inheritDoc} + */ + @Override + public Option<Boolean> option() { + return option; + } + + /** + * {@inheritDoc} + */ + @Override + public Text formatValue() { + return valueFormatter.apply(option().pendingValue()); + } + + /** + * Value format is green or red depending on the state + */ + public boolean coloured() { + return coloured; + } + + /** + * {@inheritDoc} + */ + @Override + public ControllerWidget<BooleanController> provideWidget(Screen screen, Dimension<Integer> widgetDimension) { + return new BooleanControllerElement(this, screen, widgetDimension); + } + + @ApiStatus.Internal + public static class BooleanControllerElement extends ControllerWidget<BooleanController> { + private BooleanControllerElement(BooleanController control, Screen screen, Dimension<Integer> dim) { + super(control, screen, dim); + } + + @Override + protected void drawHoveredControl(MatrixStack matrices, int mouseX, int mouseY, float delta) { + + } + + @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 getUnhoveredControlWidth(); + } + + @Override + protected Text getValueText() { + if (control.coloured()) { + return super.getValueText().copy().formatted(control.option().pendingValue() ? Formatting.GREEN : Formatting.RED); + } + + return super.getValueText(); + } + } +} diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java b/src/main/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java index 416ee42..216c945 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java @@ -57,7 +57,7 @@ public abstract class ControllerWidget<T extends Controller<?>> extends Abstract textRenderer.drawWithShadow(matrices, shortenedName, 0, 0, -1); matrices.pop(); - drawValueText(matrices); + drawValueText(matrices, mouseX, mouseY, delta); if (hovered) { drawHoveredControl(matrices, mouseX, mouseY, delta); } @@ -71,8 +71,8 @@ public abstract class ControllerWidget<T extends Controller<?>> extends Abstract } - protected void drawValueText(MatrixStack matrices) { - Text valueText = control.formatValue(); + protected void drawValueText(MatrixStack matrices, int mouseX, int mouseY, float delta) { + Text valueText = getValueText(); matrices.push(); matrices.translate(dim.xLimit() - textRenderer.getWidth(valueText) - getXPadding(), getTextY(), 0); textRenderer.drawWithShadow(matrices, valueText, 0, 0, -1); @@ -91,7 +91,7 @@ public abstract class ControllerWidget<T extends Controller<?>> extends Abstract protected abstract int getHoveredControlWidth(); protected int getUnhoveredControlWidth() { - return textRenderer.getWidth(control.formatValue()); + return textRenderer.getWidth(getValueText()); } protected int getXPadding() { @@ -102,6 +102,10 @@ public abstract class ControllerWidget<T extends Controller<?>> extends Abstract return 2; } + protected Text getValueText() { + return control.formatValue(); + } + 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); diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/TickBoxController.java b/src/main/java/dev/isxander/yacl/gui/controllers/TickBoxController.java index 3bc5c22..00df45b 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/TickBoxController.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/TickBoxController.java @@ -7,54 +7,23 @@ 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 org.jetbrains.annotations.ApiStatus; import java.util.function.Function; /** - * On hover, this controller renders a tickbox, otherwise, formatted {@link Text} + * This controller renders a tickbox */ public class TickBoxController implements Controller<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; /** * Constructs a tickbox controller - * with the default value formatter of {@link TickBoxController#ON_OFF_FORMATTER} * * @param option bound option */ public TickBoxController(Option<Boolean> option) { - this(option, ON_OFF_FORMATTER); - } - - /** - * Constructs a tickbox controller - * - * @param option bound option - * @param valueFormatter format value into any {@link Text} - */ - public TickBoxController(Option<Boolean> option, Function<Boolean, Text> valueFormatter) { this.option = option; - this.valueFormatter = valueFormatter; - } /** @@ -70,7 +39,7 @@ public class TickBoxController implements Controller<Boolean> { */ @Override public Text formatValue() { - return valueFormatter.apply(option().pendingValue()); + return Text.empty(); } /** @@ -103,9 +72,9 @@ public class TickBoxController implements Controller<Boolean> { } @Override - protected void drawValueText(MatrixStack matrices) { + protected void drawValueText(MatrixStack matrices, int mouseX, int mouseY, float delta) { if (!hovered) - super.drawValueText(matrices); + drawHoveredControl(matrices, mouseX, mouseY, delta); } @Override diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java index 139ef39..5c5e20e 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java @@ -48,11 +48,11 @@ public class SliderControllerElement extends ControllerWidget<ISliderController< } @Override - protected void drawValueText(MatrixStack matrices) { + protected void drawValueText(MatrixStack matrices, int mouseX, int mouseY, float delta) { matrices.push(); if (hovered) matrices.translate(-(sliderBounds.width() + 6 + getThumbWidth() / 2f), 0, 0); - super.drawValueText(matrices); + super.drawValueText(matrices, mouseX, mouseY, delta); matrices.pop(); } diff --git a/src/testmod/java/dev/isxander/yacl/test/ModMenuIntegration.java b/src/testmod/java/dev/isxander/yacl/test/ModMenuIntegration.java index e7fa794..0c39ff6 100644 --- a/src/testmod/java/dev/isxander/yacl/test/ModMenuIntegration.java +++ b/src/testmod/java/dev/isxander/yacl/test/ModMenuIntegration.java @@ -4,6 +4,7 @@ import com.terraformersmc.modmenu.api.ConfigScreenFactory; import com.terraformersmc.modmenu.api.ModMenuApi; import dev.isxander.yacl.api.*; import dev.isxander.yacl.gui.controllers.ActionController; +import dev.isxander.yacl.gui.controllers.BooleanController; import dev.isxander.yacl.gui.controllers.EnumController; import dev.isxander.yacl.gui.controllers.TickBoxController; import dev.isxander.yacl.gui.controllers.slider.DoubleSliderController; @@ -22,6 +23,15 @@ public class ModMenuIntegration implements ModMenuApi { .category(ConfigCategory.createBuilder() .name(Text.of("Control Examples")) .option(Option.createBuilder(boolean.class) + .name(Text.of("Boolean Toggle")) + .binding( + false, + () -> TestSettings.booleanToggle, + (value) -> TestSettings.booleanToggle = value + ) + .controller(BooleanController::new) + .build()) + .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( @@ -127,10 +137,79 @@ public class ModMenuIntegration implements ModMenuApi { .build()) .build()) .build()) + .category(ConfigCategory.createBuilder() + .name(Text.of("Scroll Test")) + .option(ButtonOption.createBuilder() + .name(Text.of("Option")) + .action(() -> {}) + .controller(ActionController::new) + .build()) + .option(ButtonOption.createBuilder() + .name(Text.of("Option")) + .action(() -> {}) + .controller(ActionController::new) + .build()) + .option(ButtonOption.createBuilder() + .name(Text.of("Option")) + .action(() -> {}) + .controller(ActionController::new) + .build()) + .option(ButtonOption.createBuilder() + .name(Text.of("Option")) + .action(() -> {}) + .controller(ActionController::new) + .build()) + .option(ButtonOption.createBuilder() + .name(Text.of("Option")) + .action(() -> {}) + .controller(ActionController::new) + .build()) + .option(ButtonOption.createBuilder() + .name(Text.of("Option")) + .action(() -> {}) + .controller(ActionController::new) + .build()) + .option(ButtonOption.createBuilder() + .name(Text.of("Option")) + .action(() -> {}) + .controller(ActionController::new) + .build()) + .option(ButtonOption.createBuilder() + .name(Text.of("Option")) + .action(() -> {}) + .controller(ActionController::new) + .build()) + .option(ButtonOption.createBuilder() + .name(Text.of("Option")) + .action(() -> {}) + .controller(ActionController::new) + .build()) + .option(ButtonOption.createBuilder() + .name(Text.of("Option")) + .action(() -> {}) + .controller(ActionController::new) + .build()) + .option(ButtonOption.createBuilder() + .name(Text.of("Option")) + .action(() -> {}) + .controller(ActionController::new) + .build()) + .option(ButtonOption.createBuilder() + .name(Text.of("Option")) + .action(() -> {}) + .controller(ActionController::new) + .build()) + .option(ButtonOption.createBuilder() + .name(Text.of("Option")) + .action(() -> {}) + .controller(ActionController::new) + .build()) + .build()) .build().generateScreen(parent); } private static class TestSettings { + private static boolean booleanToggle = false; private static boolean tickbox = false; private static int intSlider = 0; private static double doubleSlider = 0; |