From 21728802a4ee2b65a32b626140ff01bf59a456c6 Mon Sep 17 00:00:00 2001 From: xander Date: Thu, 1 Sep 2022 12:20:05 +0100 Subject: make tickbox separate controller from on off text --- .../yacl/gui/controllers/BooleanController.java | 138 +++++++++++++++++++++ .../yacl/gui/controllers/ControllerWidget.java | 12 +- .../yacl/gui/controllers/TickBoxController.java | 39 +----- .../slider/SliderControllerElement.java | 4 +- .../dev/isxander/yacl/test/ModMenuIntegration.java | 79 ++++++++++++ 5 files changed, 231 insertions(+), 41 deletions(-) create mode 100644 src/main/java/dev/isxander/yacl/gui/controllers/BooleanController.java (limited to 'src') 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 { + + public static final Function ON_OFF_FORMATTER = (state) -> + state + ? Text.translatable("yacl.control.tickbox.on") + : Text.translatable("yacl.control.tickbox.off"); + + public static final Function TRUE_FALSE_FORMATTER = (state) -> + state + ? Text.translatable("yacl.control.tickbox.true") + : Text.translatable("yacl.control.tickbox.false"); + + public static final Function YES_NO_FORMATTER = (state) -> + state + ? Text.translatable("yacl.control.tickbox.yes") + : Text.translatable("yacl.control.tickbox.no"); + + private final Option option; + private final Function 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 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 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 option, Function valueFormatter, boolean coloured) { + this.option = option; + this.valueFormatter = valueFormatter; + this.coloured = coloured; + } + + /** + * {@inheritDoc} + */ + @Override + public Option 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 provideWidget(Screen screen, Dimension widgetDimension) { + return new BooleanControllerElement(this, screen, widgetDimension); + } + + @ApiStatus.Internal + public static class BooleanControllerElement extends ControllerWidget { + private BooleanControllerElement(BooleanController control, Screen screen, Dimension 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> 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> 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> 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> 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 { - - public static final Function 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 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 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 option; - private final Function valueFormatter; /** * Constructs a tickbox controller - * with the default value formatter of {@link TickBoxController#ON_OFF_FORMATTER} * * @param option bound option */ public TickBoxController(Option 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 option, Function valueFormatter) { this.option = option; - this.valueFormatter = valueFormatter; - } /** @@ -70,7 +39,7 @@ public class TickBoxController implements Controller { */ @Override public Text formatValue() { - return valueFormatter.apply(option().pendingValue()); + return Text.empty(); } /** @@ -103,9 +72,9 @@ public class TickBoxController implements Controller { } @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 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")) @@ -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; -- cgit