From 3e03edff9633de35068cbb0e11c111a70585a74d Mon Sep 17 00:00:00 2001 From: isXander Date: Fri, 9 Dec 2022 20:53:19 +0000 Subject: unlimited string length in controller --- .../yacl/gui/controllers/ControllerWidget.java | 18 +--- .../string/StringControllerElement.java | 109 +++++++++++++++------ 2 files changed, 83 insertions(+), 44 deletions(-) (limited to 'src/client/java/dev/isxander/yacl/gui/controllers') diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java b/src/client/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java index cebaba7..9a16d22 100644 --- a/src/client/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java +++ b/src/client/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.gui.utils.RenderUtils; import dev.isxander.yacl.impl.utils.YACLConstants; import net.minecraft.client.font.MultilineText; import net.minecraft.client.gui.DrawableHelper; @@ -38,20 +39,7 @@ public abstract class ControllerWidget> extends Abstract hovered = isMouseOver(mouseX, mouseY); Text name = control.option().changed() ? modifiedOptionName : control.option().name(); - String nameString = name.getString(); - - boolean firstIter = true; - while (textRenderer.getWidth(nameString) > getDimension().width() - getControlWidth() - getXPadding() - 7) { - nameString = nameString.substring(0, Math.max(nameString.length() - (firstIter ? 2 : 5), 0)).trim(); - nameString += "..."; - - if (nameString.equals("...")) - break; - - firstIter = false; - } - - Text shortenedName = Text.literal(nameString).fillStyle(name.getStyle()); + Text shortenedName = Text.literal(RenderUtils.shortenString(name.getString(), textRenderer, getDimension().width() - getControlWidth() - getXPadding() - 7, "...")).fillStyle(name.getStyle()); drawButtonRect(matrices, getDimension().x(), getDimension().y(), getDimension().xLimit(), getDimension().yLimit(), isHovered(), isAvailable()); matrices.push(); @@ -67,7 +55,7 @@ public abstract class ControllerWidget> extends Abstract @Override public void postRender(MatrixStack matrices, int mouseX, int mouseY, float delta) { - if (hovered) { + if (hovered || focused) { YACLScreen.renderMultilineTooltip(matrices, textRenderer, wrappedTooltip, getDimension().centerX(), getDimension().y() - 5, getDimension().yLimit() + 5, screen.width, screen.height); } } diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/string/StringControllerElement.java b/src/client/java/dev/isxander/yacl/gui/controllers/string/StringControllerElement.java index 47a4c96..b2a324e 100644 --- a/src/client/java/dev/isxander/yacl/gui/controllers/string/StringControllerElement.java +++ b/src/client/java/dev/isxander/yacl/gui/controllers/string/StringControllerElement.java @@ -1,8 +1,10 @@ package dev.isxander.yacl.gui.controllers.string; +import com.mojang.blaze3d.systems.RenderSystem; import dev.isxander.yacl.api.utils.Dimension; import dev.isxander.yacl.gui.YACLScreen; import dev.isxander.yacl.gui.controllers.ControllerWidget; +import dev.isxander.yacl.gui.utils.RenderUtils; import net.minecraft.client.gui.DrawableHelper; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.util.InputUtil; @@ -22,6 +24,8 @@ public class StringControllerElement extends ControllerWidget 0) { if (selectionLength != 0) @@ -128,6 +153,7 @@ public class StringControllerElement extends ControllerWidget inputField.length() - renderOffset) + renderOffset = inputField.length() - caretPos; + } + @Override public boolean charTyped(char chr, int modifiers) { if (!inputFieldFocused) @@ -204,8 +249,10 @@ public class StringControllerElement extends ControllerWidget 0) { - if (modifyInput(builder -> builder.deleteCharAt(caretPos - 1))) + if (modifyInput(builder -> builder.deleteCharAt(caretPos - 1))) { caretPos--; + checkRenderOffset(); + } } } @@ -217,20 +264,18 @@ public class StringControllerElement extends ControllerWidget builder.insert(caretPos, trimmed))) { - caretPos += trimmed.length(); + if (modifyInput(builder -> builder.insert(caretPos, string))) { + caretPos += string.length(); + checkRenderOffset(); } } else { int start = getSelectionStart(); int end = getSelectionEnd(); - String trimmed = textRenderer.trimToWidth(string, getMaxLength() - textRenderer.getWidth(inputField) + textRenderer.getWidth(inputField.substring(start, end))); - - if (modifyInput(builder -> builder.replace(start, end, trimmed))) { - caretPos = start + trimmed.length(); + if (modifyInput(builder -> builder.replace(start, end, string))) { + caretPos = start + string.length(); selectionLength = 0; + checkRenderOffset(); } } } @@ -246,8 +291,8 @@ public class StringControllerElement extends ControllerWidget dim) { super.setDimension(dim); - int width = Math.max(6, textRenderer.getWidth(getValueText())); + int width = Math.max(6, Math.min(textRenderer.getWidth(getValueText()), getUnshiftedLength())); inputFieldBounds = Dimension.ofInt(dim.xLimit() - getXPadding() - width, dim.centerY() - textRenderer.fontHeight / 2, width, textRenderer.fontHeight); } @@ -311,9 +357,14 @@ public class StringControllerElement extends ControllerWidget