aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/isxander/yacl3/gui/controllers/ActionController.java
diff options
context:
space:
mode:
authorisxander <xander@isxander.dev>2024-04-11 18:43:06 +0100
committerisxander <xander@isxander.dev>2024-04-11 18:43:06 +0100
commit04fe933f4c24817100f3101f088accf55a621f8a (patch)
treefeff94ca3ab4484160e69a24f4ee38522381950e /src/main/java/dev/isxander/yacl3/gui/controllers/ActionController.java
parent831b894fdb7fe3e173d81387c8f6a2402b8ccfa9 (diff)
downloadYetAnotherConfigLib-04fe933f4c24817100f3101f088accf55a621f8a.tar.gz
YetAnotherConfigLib-04fe933f4c24817100f3101f088accf55a621f8a.tar.bz2
YetAnotherConfigLib-04fe933f4c24817100f3101f088accf55a621f8a.zip
Extremely fragile and broken multiversion build with stonecutter
Diffstat (limited to 'src/main/java/dev/isxander/yacl3/gui/controllers/ActionController.java')
-rw-r--r--src/main/java/dev/isxander/yacl3/gui/controllers/ActionController.java120
1 files changed, 120 insertions, 0 deletions
diff --git a/src/main/java/dev/isxander/yacl3/gui/controllers/ActionController.java b/src/main/java/dev/isxander/yacl3/gui/controllers/ActionController.java
new file mode 100644
index 0000000..77938f6
--- /dev/null
+++ b/src/main/java/dev/isxander/yacl3/gui/controllers/ActionController.java
@@ -0,0 +1,120 @@
+package dev.isxander.yacl3.gui.controllers;
+
+import com.mojang.blaze3d.platform.InputConstants;
+import dev.isxander.yacl3.api.ButtonOption;
+import dev.isxander.yacl3.api.Controller;
+import dev.isxander.yacl3.api.utils.Dimension;
+import dev.isxander.yacl3.gui.AbstractWidget;
+import dev.isxander.yacl3.gui.YACLScreen;
+import net.minecraft.network.chat.Component;
+
+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 Component DEFAULT_TEXT = Component.translatable("yacl.control.action.execute");
+
+ private final ButtonOption option;
+ private final Component 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, Component text) {
+ this.option = option;
+ this.text = text;
+
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public ButtonOption option() {
+ return option;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Component 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 == InputConstants.KEY_RETURN || keyCode == InputConstants.KEY_SPACE || keyCode == InputConstants.KEY_NUMPADENTER) {
+ 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);
+ }
+ }
+}