diff options
author | isXander <xandersmith2008@gmail.com> | 2022-12-09 20:53:19 +0000 |
---|---|---|
committer | isXander <xandersmith2008@gmail.com> | 2022-12-09 20:53:19 +0000 |
commit | 3e03edff9633de35068cbb0e11c111a70585a74d (patch) | |
tree | 0c2cb9f71121ba0e989681d669406f9de222ba9d /src/client/java/dev/isxander | |
parent | 62b1450df690c4b22c0eb42f02a6b0c507111686 (diff) | |
download | YetAnotherConfigLib-3e03edff9633de35068cbb0e11c111a70585a74d.tar.gz YetAnotherConfigLib-3e03edff9633de35068cbb0e11c111a70585a74d.tar.bz2 YetAnotherConfigLib-3e03edff9633de35068cbb0e11c111a70585a74d.zip |
unlimited string length in controller
Diffstat (limited to 'src/client/java/dev/isxander')
5 files changed, 121 insertions, 47 deletions
diff --git a/src/client/java/dev/isxander/yacl/gui/CategoryListWidget.java b/src/client/java/dev/isxander/yacl/gui/CategoryListWidget.java index 4dbcb11..974dc2c 100644 --- a/src/client/java/dev/isxander/yacl/gui/CategoryListWidget.java +++ b/src/client/java/dev/isxander/yacl/gui/CategoryListWidget.java @@ -3,6 +3,7 @@ package dev.isxander.yacl.gui; import com.google.common.collect.ImmutableList; import com.mojang.blaze3d.systems.RenderSystem; import dev.isxander.yacl.api.ConfigCategory; +import dev.isxander.yacl.gui.utils.RenderUtils; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.Element; import net.minecraft.client.gui.Selectable; @@ -27,8 +28,7 @@ public class CategoryListWidget extends ElementListWidgetExt<CategoryListWidget. @Override public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { - double d = this.client.getWindow().getScaleFactor(); - RenderSystem.enableScissor(0, (int)((yaclScreen.height - bottom) * d), (int)(width * d), (int)(height * d)); + RenderUtils.enableScissor(0, 0, width, height); super.render(matrices, mouseX, mouseY, delta); RenderSystem.disableScissor(); } diff --git a/src/client/java/dev/isxander/yacl/gui/OptionListWidget.java b/src/client/java/dev/isxander/yacl/gui/OptionListWidget.java index 7ea275b..976d796 100644 --- a/src/client/java/dev/isxander/yacl/gui/OptionListWidget.java +++ b/src/client/java/dev/isxander/yacl/gui/OptionListWidget.java @@ -302,7 +302,7 @@ public class OptionListWidget extends ElementListWidgetExt<OptionListWidget.Entr @Override public void postRender(MatrixStack matrices, int mouseX, int mouseY, float delta) { - if (isHovered() && !expandMinimizeButton.isMouseOver(mouseX, mouseY)) { + if ((isHovered() && !expandMinimizeButton.isMouseOver(mouseX, mouseY)) || expandMinimizeButton.isFocused()) { YACLScreen.renderMultilineTooltip(matrices, textRenderer, wrappedTooltip, getRowLeft() + getRowWidth() / 2, y - 3, y + getItemHeight() + 3, screen.width, screen.height); } } 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<T extends Controller<?>> 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<T extends Controller<?>> 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<IStringController< protected int caretPos; protected int selectionLength; + protected int renderOffset; + protected float ticks; private final Text emptyText; @@ -39,27 +43,47 @@ public class StringControllerElement extends ControllerWidget<IStringController< @Override protected void drawHoveredControl(MatrixStack matrices, int mouseX, int mouseY, float delta) { - ticks += delta; - String text = getValueText().getString(); + } + + @Override + protected void drawValueText(MatrixStack matrices, int mouseX, int mouseY, float delta) { + Text valueText = getValueText(); + if (!isHovered()) valueText = Text.literal(RenderUtils.shortenString(valueText.getString(), textRenderer, getDimension().width() / 2, "...")).setStyle(valueText.getStyle()); - DrawableHelper.fill(matrices, inputFieldBounds.x(), inputFieldBounds.yLimit(), inputFieldBounds.xLimit(), inputFieldBounds.yLimit() + 1, -1); - DrawableHelper.fill(matrices, inputFieldBounds.x() + 1, inputFieldBounds.yLimit() + 1, inputFieldBounds.xLimit() + 1, inputFieldBounds.yLimit() + 2, 0xFF404040); + int overflowWidth = textRenderer.getWidth(inputField.substring(inputField.length() - renderOffset)); - if (inputFieldFocused || focused) { - int caretX = inputFieldBounds.x() + textRenderer.getWidth(text.substring(0, caretPos)) - 1; - if (text.isEmpty()) - caretX += inputFieldBounds.width() / 2; + matrices.push(); + int textX = getDimension().xLimit() - textRenderer.getWidth(valueText) + overflowWidth - getXPadding(); + matrices.translate(textX, getTextY(), 0); + RenderUtils.enableScissor(inputFieldBounds.x(), inputFieldBounds.y(), inputFieldBounds.width() + 1, inputFieldBounds.height() + 2); + textRenderer.drawWithShadow(matrices, valueText, 0, 0, getValueColor()); + matrices.pop(); - if (ticks % 20 <= 10) { - DrawableHelper.fill(matrices, caretX, inputFieldBounds.y(), caretX + 1, inputFieldBounds.yLimit(), -1); - } + if (isHovered()) { + ticks += delta; + + String text = getValueText().getString(); - if (selectionLength != 0) { - int selectionX = inputFieldBounds.x() + textRenderer.getWidth(text.substring(0, caretPos + selectionLength)); - DrawableHelper.fill(matrices, caretX, inputFieldBounds.y() - 1, selectionX, inputFieldBounds.yLimit(), 0x803030FF); + DrawableHelper.fill(matrices, inputFieldBounds.x(), inputFieldBounds.yLimit(), inputFieldBounds.xLimit(), inputFieldBounds.yLimit() + 1, -1); + DrawableHelper.fill(matrices, inputFieldBounds.x() + 1, inputFieldBounds.yLimit() + 1, inputFieldBounds.xLimit() + 1, inputFieldBounds.yLimit() + 2, 0xFF404040); + + if (inputFieldFocused || focused) { + int caretX = textX + textRenderer.getWidth(text.substring(0, caretPos)) - 1; + if (text.isEmpty()) + caretX = inputFieldBounds.x() + inputFieldBounds.width() / 2; + + if (ticks % 20 <= 10) { + DrawableHelper.fill(matrices, caretX, inputFieldBounds.y(), caretX + 1, inputFieldBounds.yLimit(), -1); + } + + if (selectionLength != 0) { + int selectionX = textX + textRenderer.getWidth(text.substring(0, caretPos + selectionLength)); + DrawableHelper.fill(matrices, caretX, inputFieldBounds.y() - 1, selectionX, inputFieldBounds.yLimit(), 0x803030FF); + } } } + RenderSystem.disableScissor(); } @Override @@ -70,8 +94,8 @@ public class StringControllerElement extends ControllerWidget<IStringController< if (!inputFieldBounds.isPointInside((int) mouseX, (int) mouseY)) { caretPos = getDefaultCarotPos(); } else { - // gets the appropriate carot position for where you click - int textX = (int) mouseX - inputFieldBounds.x(); + // gets the appropriate caret position for where you click + int textX = (int) mouseX - (inputFieldBounds.xLimit() - textRenderer.getWidth(getValueText())); int pos = -1; int currentWidth = 0; for (char ch : inputField.toCharArray()) { @@ -121,6 +145,7 @@ public class StringControllerElement extends ControllerWidget<IStringController< caretPos--; selectionLength += 1; } + checkRenderOffset(); } else { if (caretPos > 0) { if (selectionLength != 0) @@ -128,6 +153,7 @@ public class StringControllerElement extends ControllerWidget<IStringController< else caretPos--; } + checkRenderOffset(); selectionLength = 0; } @@ -143,12 +169,14 @@ public class StringControllerElement extends ControllerWidget<IStringController< caretPos++; selectionLength -= 1; } + checkRenderOffset(); } else { if (caretPos < inputField.length()) { if (selectionLength != 0) caretPos += Math.max(selectionLength, 0); else caretPos++; + checkRenderOffset(); } selectionLength = 0; } @@ -178,6 +206,7 @@ public class StringControllerElement extends ControllerWidget<IStringController< return true; } else if (Screen.isSelectAll(keyCode)) { caretPos = inputField.length(); + checkRenderOffset(); selectionLength = -caretPos; return true; } @@ -186,6 +215,22 @@ public class StringControllerElement extends ControllerWidget<IStringController< return false; } + protected void checkRenderOffset() { + if (textRenderer.getWidth(inputField) < getUnshiftedLength()) { + renderOffset = 0; + return; + } + + String inp = inputField.substring(0, inputField.length() - renderOffset); + int obstructionWidth = textRenderer.getWidth(inp) - getUnshiftedLength(); + int length = inp.length() - textRenderer.trimToWidth(inp, obstructionWidth).length(); + + if (caretPos < inputField.length() - renderOffset - length) + renderOffset = inputField.length() - caretPos - length; + if (caretPos > 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<IStringController< if (selectionLength != 0) { write(""); } else if (caretPos > 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<IStringController< public void write(String string) { if (selectionLength == 0) { - String trimmed = textRenderer.trimToWidth(string, getMaxLength() - textRenderer.getWidth(inputField)); - - if (modifyInput(builder -> 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<IStringController< return true; } - public int getMaxLength() { - return getDimension().width() / 8 * 7; + public int getUnshiftedLength() { + return getDimension().width() / 8 * 5; } public int getSelectionStart() { @@ -291,6 +336,7 @@ public class StringControllerElement extends ControllerWidget<IStringController< public void unfocus() { super.unfocus(); inputFieldFocused = false; + renderOffset = 0; if (!instantApply) updateControl(); } @@ -298,7 +344,7 @@ public class StringControllerElement extends ControllerWidget<IStringController< public void setDimension(Dimension<Integer> 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); } @@ -312,8 +358,13 @@ public class StringControllerElement extends ControllerWidget<IStringController< } @Override + protected int getUnhoveredControlWidth() { + return !isHovered() ? Math.min(getHoveredControlWidth(), getDimension().width() / 2) : getHoveredControlWidth(); + } + + @Override protected int getHoveredControlWidth() { - return getUnhoveredControlWidth(); + return Math.min(textRenderer.getWidth(getValueText()), getUnshiftedLength()); } @Override diff --git a/src/client/java/dev/isxander/yacl/gui/utils/RenderUtils.java b/src/client/java/dev/isxander/yacl/gui/utils/RenderUtils.java new file mode 100644 index 0000000..222517b --- /dev/null +++ b/src/client/java/dev/isxander/yacl/gui/utils/RenderUtils.java @@ -0,0 +1,35 @@ +package dev.isxander.yacl.gui.utils; + +import com.mojang.blaze3d.systems.RenderSystem; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.font.TextRenderer; +import net.minecraft.client.util.TextCollector; +import net.minecraft.client.util.Window; +import net.minecraft.text.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +public class RenderUtils { + public static void enableScissor(int x, int y, int width, int height) { + Window window = MinecraftClient.getInstance().getWindow(); + double d = window.getScaleFactor(); + RenderSystem.enableScissor((int)(x * d), (int)((window.getScaledHeight() - y - height) * d), (int)(width * d), (int)(height * d)); + } + + public static String shortenString(String string, TextRenderer textRenderer, int maxWidth, String suffix) { + boolean firstIter = true; + while (textRenderer.getWidth(string) > maxWidth) { + string = string.substring(0, Math.max(string.length() - 1 - (firstIter ? 1 : suffix.length() + 1), 0)).trim(); + string += suffix; + + if (string.equals(suffix)) + break; + + firstIter = false; + } + + return string; + } +} |