diff options
author | Xander <xander@isxander.dev> | 2022-12-09 16:31:25 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-09 16:31:25 +0000 |
commit | e4856a17133b0567d09cb6db3821674491d57e64 (patch) | |
tree | 0c59597708b3ea9f402ba119490537b5c18fdb93 /src/client/java/dev/isxander/yacl/gui/controllers/ActionController.java | |
parent | e1f6d190d862dd86c251fdd5726efe99f8ec1baf (diff) | |
parent | 49ff470de36e719d5b963de405de891eca2b69d1 (diff) | |
download | YetAnotherConfigLib-e4856a17133b0567d09cb6db3821674491d57e64.tar.gz YetAnotherConfigLib-e4856a17133b0567d09cb6db3821674491d57e64.tar.bz2 YetAnotherConfigLib-e4856a17133b0567d09cb6db3821674491d57e64.zip |
Merge pull request #38 from isXander/update/1.19.3
Diffstat (limited to 'src/client/java/dev/isxander/yacl/gui/controllers/ActionController.java')
-rw-r--r-- | src/client/java/dev/isxander/yacl/gui/controllers/ActionController.java | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/ActionController.java b/src/client/java/dev/isxander/yacl/gui/controllers/ActionController.java new file mode 100644 index 0000000..7666dff --- /dev/null +++ b/src/client/java/dev/isxander/yacl/gui/controllers/ActionController.java @@ -0,0 +1,120 @@ +package dev.isxander.yacl.gui.controllers; + +import dev.isxander.yacl.api.ButtonOption; +import dev.isxander.yacl.api.Controller; +import dev.isxander.yacl.api.utils.Dimension; +import dev.isxander.yacl.gui.AbstractWidget; +import dev.isxander.yacl.gui.YACLScreen; +import net.minecraft.client.util.InputUtil; +import net.minecraft.text.Text; + +import java.util.function.BiConsumer; + +/** + * Simple controller that simply runs the button action on press + * and renders a {@link} Text on the right. + */ +public class ActionController implements Controller<BiConsumer<YACLScreen, ButtonOption>> { + public static final Text DEFAULT_TEXT = Text.translatable("yacl.control.action.execute"); + + private final ButtonOption option; + private final Text text; + + /** + * Constructs an action controller + * with the default formatter of {@link ActionController#DEFAULT_TEXT} + * + * @param option bound option + */ + public ActionController(ButtonOption option) { + this(option, DEFAULT_TEXT); + } + + /** + * Constructs an action controller + * + * @param option bound option + * @param text text to display + */ + public ActionController(ButtonOption option, Text text) { + this.option = option; + this.text = text; + + } + + /** + * {@inheritDoc} + */ + @Override + public ButtonOption option() { + return option; + } + + /** + * {@inheritDoc} + */ + @Override + public Text formatValue() { + return text; + } + + /** + * {@inheritDoc} + */ + @Override + public AbstractWidget provideWidget(YACLScreen screen, Dimension<Integer> widgetDimension) { + return new ActionControllerElement(this, screen, widgetDimension); + } + + public static class ActionControllerElement extends ControllerWidget<ActionController> { + private final String buttonString; + + public ActionControllerElement(ActionController control, YACLScreen screen, Dimension<Integer> dim) { + super(control, screen, dim); + buttonString = control.formatValue().getString().toLowerCase(); + } + + public void executeAction() { + playDownSound(); + control.option().action().accept(screen, control.option()); + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + if (isMouseOver(mouseX, mouseY) && isAvailable()) { + executeAction(); + return true; + } + return false; + } + + @Override + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { + if (!focused) { + return false; + } + + if (keyCode == InputUtil.GLFW_KEY_ENTER || keyCode == InputUtil.GLFW_KEY_SPACE || keyCode == InputUtil.GLFW_KEY_KP_ENTER) { + executeAction(); + return true; + } + + return false; + } + + @Override + protected int getHoveredControlWidth() { + return getUnhoveredControlWidth(); + } + + @Override + public boolean canReset() { + return false; + } + + @Override + public boolean matchesSearch(String query) { + return super.matchesSearch(query) || buttonString.contains(query); + } + } +} |