aboutsummaryrefslogtreecommitdiff
path: root/src/client/java/dev/isxander/yacl/gui/controllers/ActionController.java
diff options
context:
space:
mode:
authorisXander <xandersmith2008@gmail.com>2022-11-27 18:17:36 +0000
committerisXander <xandersmith2008@gmail.com>2022-11-27 18:17:36 +0000
commitd163b9128d760e53e34fd6c08dbf782fa3d50c51 (patch)
treeb9ea79bb99bf52ea7de9d059cc05900270ff88d4 /src/client/java/dev/isxander/yacl/gui/controllers/ActionController.java
parentc26d3a89caae20f1a91898202d91de8dc90da5ad (diff)
downloadYetAnotherConfigLib-d163b9128d760e53e34fd6c08dbf782fa3d50c51.tar.gz
YetAnotherConfigLib-d163b9128d760e53e34fd6c08dbf782fa3d50c51.tar.bz2
YetAnotherConfigLib-d163b9128d760e53e34fd6c08dbf782fa3d50c51.zip
split sourcesets
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.java120
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..b8e2cd1
--- /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.text.Text;
+import org.lwjgl.glfw.GLFW;
+
+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 == GLFW.GLFW_KEY_ENTER || keyCode == GLFW.GLFW_KEY_SPACE || keyCode == GLFW.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);
+ }
+ }
+}