From 7f88ddf36ba7804211e8aae6b3723bd8f37c82c5 Mon Sep 17 00:00:00 2001 From: xander Date: Fri, 2 Sep 2022 22:04:35 +0100 Subject: implement keyboard-operation for all controllers fix crash when focusing on option list with tab add tooltip to groups check that pending values actually applied on save, if not, error in log and the save button displays an error when trying to escape with unsaved changes, save button text goes green and bold YACLConstants file to change certain behaviours, could evolve into its own settings! update icon --- .../yacl/gui/controllers/ActionController.java | 20 +++++++++++-- .../yacl/gui/controllers/BooleanController.java | 20 +++++++++++-- .../yacl/gui/controllers/ControllerWidget.java | 20 +++++++++---- .../yacl/gui/controllers/EnumController.java | 35 +++++++++++++++++----- .../yacl/gui/controllers/TickBoxController.java | 21 +++++++++++-- .../slider/SliderControllerElement.java | 26 +++++++++++++--- 6 files changed, 118 insertions(+), 24 deletions(-) (limited to 'src/main/java/dev/isxander/yacl/gui/controllers') diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/ActionController.java b/src/main/java/dev/isxander/yacl/gui/controllers/ActionController.java index 92ef3d5..d198c96 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/ActionController.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/ActionController.java @@ -6,6 +6,7 @@ import dev.isxander.yacl.api.utils.Dimension; import dev.isxander.yacl.gui.YACLScreen; import net.minecraft.text.Text; import org.jetbrains.annotations.ApiStatus; +import org.lwjgl.glfw.GLFW; import java.util.function.Consumer; @@ -71,16 +72,31 @@ public class ActionController implements Controller> { super(control, screen, dim); } + public void executeAction() { + playDownSound(); + control.option().action().accept(screen); + } + @Override public boolean mouseClicked(double mouseX, double mouseY, int button) { if (isMouseOver(mouseX, mouseY)) { - playDownSound(); - control.option().action().accept(screen); + executeAction(); return true; } return false; } + @Override + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { + if (keyCode != GLFW.GLFW_KEY_ENTER && keyCode != GLFW.GLFW_KEY_SPACE && keyCode != GLFW.GLFW_KEY_KP_ENTER) { + return false; + } + + executeAction(); + + return true; + } + @Override protected int getHoveredControlWidth() { return getUnhoveredControlWidth(); diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/BooleanController.java b/src/main/java/dev/isxander/yacl/gui/controllers/BooleanController.java index 6bf0b80..ac8754f 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/BooleanController.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/BooleanController.java @@ -9,6 +9,7 @@ import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.Text; import net.minecraft.util.Formatting; import org.jetbrains.annotations.ApiStatus; +import org.lwjgl.glfw.GLFW; import java.util.function.Function; @@ -117,8 +118,7 @@ public class BooleanController implements Controller { if (!isMouseOver(mouseX, mouseY)) return false; - control.option().requestSet(!control.option().pendingValue()); - playDownSound(); + toggleSetting(); return true; } @@ -127,6 +127,11 @@ public class BooleanController implements Controller { return getUnhoveredControlWidth(); } + public void toggleSetting() { + control.option().requestSet(!control.option().pendingValue()); + playDownSound(); + } + @Override protected Text getValueText() { if (control.coloured()) { @@ -135,5 +140,16 @@ public class BooleanController implements Controller { return super.getValueText(); } + + @Override + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { + if (keyCode != GLFW.GLFW_KEY_ENTER && keyCode != GLFW.GLFW_KEY_SPACE && keyCode != GLFW.GLFW_KEY_KP_ENTER) { + return false; + } + + toggleSetting(); + + return true; + } } } 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 77ac9ca..d712e56 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java @@ -4,6 +4,7 @@ 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 dev.isxander.yacl.impl.YACLConstants; import net.minecraft.client.gui.DrawableHelper; import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; import net.minecraft.client.util.math.MatrixStack; @@ -19,6 +20,7 @@ public abstract class ControllerWidget> extends Abstract protected Dimension dim; protected final YACLScreen screen; + protected boolean focused = false; protected boolean hovered = false; protected float hoveredTicks = 0; @@ -34,7 +36,7 @@ public abstract class ControllerWidget> extends Abstract @Override public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { hovered = isMouseOver(mouseX, mouseY); - if (hovered && mouseX == prevMouseX && mouseY == prevMouseY) { + if (hovered && (!YACLConstants.HOVER_MOUSE_RESET || (mouseX == prevMouseX && mouseY == prevMouseY))) { hoveredTicks += delta; } else { hoveredTicks = 0; @@ -53,18 +55,18 @@ public abstract class ControllerWidget> extends Abstract Text shortenedName = Text.literal(nameString).fillStyle(name.getStyle()); - drawButtonRect(matrices, dim.x(), dim.y(), dim.xLimit(), dim.yLimit(), hovered); + drawButtonRect(matrices, dim.x(), dim.y(), dim.xLimit(), dim.yLimit(), hovered || focused); matrices.push(); matrices.translate(dim.x() + getXPadding(), getTextY(), 0); textRenderer.drawWithShadow(matrices, shortenedName, 0, 0, -1); matrices.pop(); drawValueText(matrices, mouseX, mouseY, delta); - if (hovered) { + if (hovered || focused) { drawHoveredControl(matrices, mouseX, mouseY, delta); } - if (hoveredTicks > 30) { + if (hoveredTicks > YACLConstants.HOVER_TICKS) { screen.renderOrderedTooltip(matrices, wrappedTooltip, mouseX, mouseY); } @@ -90,7 +92,7 @@ public abstract class ControllerWidget> extends Abstract } protected int getControlWidth() { - return hovered ? getHoveredControlWidth() : getUnhoveredControlWidth(); + return hovered || focused ? getHoveredControlWidth() : getUnhoveredControlWidth(); } protected abstract int getHoveredControlWidth(); @@ -126,9 +128,15 @@ public abstract class ControllerWidget> extends Abstract this.dim = dim; } + @Override + public boolean changeFocus(boolean lookForwards) { + this.focused = !this.focused; + return this.focused; + } + @Override public SelectionType getType() { - return hovered ? SelectionType.HOVERED : SelectionType.NONE; + return focused ? SelectionType.FOCUSED : hovered ? SelectionType.HOVERED : SelectionType.NONE; } @Override diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java b/src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java index f4c4006..af52255 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java @@ -8,6 +8,7 @@ import dev.isxander.yacl.gui.YACLScreen; import net.minecraft.client.gui.screen.Screen; import net.minecraft.text.Text; import org.jetbrains.annotations.ApiStatus; +import org.lwjgl.glfw.GLFW; import java.util.function.Function; @@ -85,21 +86,41 @@ public class EnumController> implements Controller { this.values = values; } + public void cycleValue(int increment) { + int targetIdx = control.option().pendingValue().ordinal() + increment; + if (targetIdx >= values.length) { + targetIdx -= values.length; + } else if (targetIdx < 0) { + targetIdx += values.length; + } + control.option().requestSet(values[targetIdx]); + } + @Override public boolean mouseClicked(double mouseX, double mouseY, int button) { if (!isMouseOver(mouseX, mouseY) || (button != 0 && button != 1)) return false; playDownSound(); + cycleValue(button == 1 || Screen.hasShiftDown() || Screen.hasControlDown() ? -1 : 1); - int change = button == 1 || Screen.hasShiftDown() ? -1 : 1; - int targetIdx = control.option().pendingValue().ordinal() + change; - if (targetIdx >= values.length) { - targetIdx -= values.length; - } else if (targetIdx < 0) { - targetIdx += values.length; + return true; + } + + @Override + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { + switch (keyCode) { + case GLFW.GLFW_KEY_LEFT, GLFW.GLFW_KEY_DOWN -> + cycleValue(-1); + case GLFW.GLFW_KEY_RIGHT, GLFW.GLFW_KEY_UP -> + cycleValue(1); + case GLFW.GLFW_KEY_ENTER, GLFW.GLFW_KEY_SPACE, GLFW.GLFW_KEY_KP_ENTER -> + cycleValue(Screen.hasControlDown() || Screen.hasShiftDown() ? -1 : 1); + default -> { + return false; + } } - control.option().requestSet(values[targetIdx]); + return true; } 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 1d78c78..a1c0095 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/TickBoxController.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/TickBoxController.java @@ -5,10 +5,10 @@ import dev.isxander.yacl.api.Option; import dev.isxander.yacl.api.utils.Dimension; import dev.isxander.yacl.gui.YACLScreen; 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 org.jetbrains.annotations.ApiStatus; +import org.lwjgl.glfw.GLFW; /** * This controller renders a tickbox @@ -81,8 +81,7 @@ public class TickBoxController implements Controller { if (!isMouseOver(mouseX, mouseY)) return false; - control.option().requestSet(!control.option().pendingValue()); - playDownSound(); + toggleSetting(); return true; } @@ -90,5 +89,21 @@ public class TickBoxController implements Controller { protected int getHoveredControlWidth() { return 10; } + + public void toggleSetting() { + control.option().requestSet(!control.option().pendingValue()); + playDownSound(); + } + + @Override + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { + if (keyCode != GLFW.GLFW_KEY_ENTER && keyCode != GLFW.GLFW_KEY_SPACE && keyCode != GLFW.GLFW_KEY_KP_ENTER) { + return false; + } + + toggleSetting(); + + return true; + } } } 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 8af2433..a6f046a 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 @@ -8,6 +8,7 @@ import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.util.math.MathHelper; import org.jetbrains.annotations.ApiStatus; +import org.lwjgl.glfw.GLFW; @ApiStatus.Internal public class SliderControllerElement extends ControllerWidget> { @@ -49,7 +50,7 @@ public class SliderControllerElement extends ControllerWidget incrementValue(-1); + case GLFW.GLFW_KEY_RIGHT, GLFW.GLFW_KEY_UP -> incrementValue(1); + default -> { + return false; + } + } + + return true; + } + @Override public boolean isMouseOver(double mouseX, double mouseY) { return super.isMouseOver(mouseX, mouseY) || mouseDown; -- cgit