diff options
author | xander <xander@isxander.dev> | 2022-09-04 22:02:21 +0100 |
---|---|---|
committer | xander <xander@isxander.dev> | 2022-09-04 22:02:21 +0100 |
commit | 833189bb4d8f524c572b6dcc86a7e146cdcf979f (patch) | |
tree | bacf5fdef6b0dbb48b78fe553d5b07f743393670 /src/main/java/dev/isxander/yacl/gui/controllers/string | |
parent | 9c283303f5b7e7fff820dc2e5c0acdc4719be7a5 (diff) | |
download | YetAnotherConfigLib-833189bb4d8f524c572b6dcc86a7e146cdcf979f.tar.gz YetAnotherConfigLib-833189bb4d8f524c572b6dcc86a7e146cdcf979f.tar.bz2 YetAnotherConfigLib-833189bb4d8f524c572b6dcc86a7e146cdcf979f.zip |
ColorController, javadoc and further improvements to StringController
Diffstat (limited to 'src/main/java/dev/isxander/yacl/gui/controllers/string')
-rw-r--r-- | src/main/java/dev/isxander/yacl/gui/controllers/string/IStringController.java | 18 | ||||
-rw-r--r-- | src/main/java/dev/isxander/yacl/gui/controllers/string/StringController.java (renamed from src/main/java/dev/isxander/yacl/gui/controllers/string/BasicStringController.java) | 9 | ||||
-rw-r--r-- | src/main/java/dev/isxander/yacl/gui/controllers/string/StringControllerElement.java | 71 |
3 files changed, 68 insertions, 30 deletions
diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/string/IStringController.java b/src/main/java/dev/isxander/yacl/gui/controllers/string/IStringController.java index ae66433..41843b8 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/string/IStringController.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/string/IStringController.java @@ -1,12 +1,30 @@ package dev.isxander.yacl.gui.controllers.string; import dev.isxander.yacl.api.Controller; +import dev.isxander.yacl.api.Option; import net.minecraft.text.Text; +/** + * A controller that can be any type but can input and output a string. + */ public interface IStringController<T> extends Controller<T> { + /** + * Gets the option's pending value as a string. + * + * @see Option#pendingValue() + */ String getString(); + + /** + * Sets the option's pending value from a string. + * + * @see Option#requestSet(Object) + */ void setFromString(String value); + /** + * {@inheritDoc} + */ @Override default Text formatValue() { return Text.of(getString()); diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/string/BasicStringController.java b/src/main/java/dev/isxander/yacl/gui/controllers/string/StringController.java index edda506..0caaa93 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/string/BasicStringController.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/string/StringController.java @@ -5,15 +5,18 @@ import dev.isxander.yacl.api.utils.Dimension; import dev.isxander.yacl.gui.AbstractWidget; import dev.isxander.yacl.gui.YACLScreen; -public class BasicStringController implements IStringController<String> { +/** + * A custom text field implementation for strings. + */ +public class StringController implements IStringController<String> { private final Option<String> option; /** - * Constructs a tickbox controller + * Constructs a string controller * * @param option bound option */ - public BasicStringController(Option<String> option) { + public StringController(Option<String> option) { this.option = option; } diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/string/StringControllerElement.java b/src/main/java/dev/isxander/yacl/gui/controllers/string/StringControllerElement.java index ed066f2..f8cf802 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/string/StringControllerElement.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/string/StringControllerElement.java @@ -8,7 +8,6 @@ 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 net.minecraft.util.Pair; import org.lwjgl.glfw.GLFW; public class StringControllerElement extends ControllerWidget<IStringController<?>> { @@ -27,7 +26,6 @@ public class StringControllerElement extends ControllerWidget<IStringController< super(control, screen, dim); inputField = new StringBuilder(control.getString()); inputFieldFocused = false; - caretPos = inputField.length(); selectionLength = 0; emptyText = Text.literal("Click to type...").formatted(Formatting.GRAY); } @@ -40,7 +38,7 @@ public class StringControllerElement extends ControllerWidget<IStringController< DrawableHelper.fill(matrices, inputFieldBounds.x() + 1, inputFieldBounds.yLimit() + 1, inputFieldBounds.xLimit() + 1, inputFieldBounds.yLimit() + 2, 0xFF404040); if (inputFieldFocused || focused) { - int caretX = inputFieldBounds.x() + textRenderer.getWidth(inputField.substring(0, caretPos)) - 1; + int caretX = inputFieldBounds.x() + textRenderer.getWidth(control.getString().substring(0, caretPos)) - 1; if (inputField.isEmpty()) caretX += inputFieldBounds.width() / 2; @@ -58,18 +56,24 @@ public class StringControllerElement extends ControllerWidget<IStringController< @Override public boolean mouseClicked(double mouseX, double mouseY, int button) { if (inputFieldBounds.isPointInside((int) mouseX, (int) mouseY)) { - if (!inputFieldFocused) + if (!inputFieldFocused) { inputFieldFocused = true; - else { + caretPos = getDefaultCarotPos(); + } else { int textWidth = (int) mouseX - inputFieldBounds.x(); caretPos = textRenderer.trimToWidth(control.getString(), textWidth).length(); selectionLength = 0; } + return true; } else { inputFieldFocused = false; } - return super.mouseClicked(mouseX, mouseY, button); + return false; + } + + protected int getDefaultCarotPos() { + return inputField.length(); } @Override @@ -115,34 +119,27 @@ public class StringControllerElement extends ControllerWidget<IStringController< return true; } case GLFW.GLFW_KEY_BACKSPACE -> { - if (selectionLength != 0) { - write(""); - } else if (caretPos > 0) { - inputField.deleteCharAt(caretPos - 1); - updateControl(); - caretPos--; - } + doBackspace(); return true; } case GLFW.GLFW_KEY_DELETE -> { - if (caretPos < inputField.length()) { - inputField.deleteCharAt(caretPos); - updateControl(); - } + doDelete(); return true; } } - if (Screen.isPaste(keyCode)) { - this.write(client.keyboard.getClipboard()); - } else if (Screen.isCopy(keyCode) && selectionLength != 0) { - client.keyboard.setClipboard(getSelection()); - } else if (Screen.isCut(keyCode) && selectionLength != 0) { - client.keyboard.setClipboard(getSelection()); - this.write(""); - } else if (Screen.isSelectAll(keyCode)) { - caretPos = inputField.length(); - selectionLength = -caretPos; + if (canUseShortcuts()) { + if (Screen.isPaste(keyCode)) { + this.write(client.keyboard.getClipboard()); + } else if (Screen.isCopy(keyCode) && selectionLength != 0) { + client.keyboard.setClipboard(getSelection()); + } else if (Screen.isCut(keyCode) && selectionLength != 0) { + client.keyboard.setClipboard(getSelection()); + this.write(""); + } else if (Screen.isSelectAll(keyCode)) { + caretPos = inputField.length(); + selectionLength = -caretPos; + } } return false; @@ -158,6 +155,26 @@ public class StringControllerElement extends ControllerWidget<IStringController< return true; } + protected boolean canUseShortcuts() { + return true; + } + + protected void doBackspace() { + if (selectionLength != 0) { + write(""); + } else if (caretPos > 0) { + inputField.deleteCharAt(caretPos - 1); + caretPos--; + } + } + + protected void doDelete() { + if (caretPos < inputField.length()) { + inputField.deleteCharAt(caretPos); + updateControl(); + } + } + public void write(String string) { if (selectionLength == 0) { string = textRenderer.trimToWidth(string, getMaxLength() - textRenderer.getWidth(inputField.toString())); |