aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/utils/TextUtils.java
diff options
context:
space:
mode:
authorDeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com>2022-05-20 20:54:33 +0200
committerDeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com>2022-05-20 20:54:33 +0200
commit13965c418bb9429d2ff92fbcbb29fad73e8a120f (patch)
treeab744b0afa6f43de0a7a04da3bda870099fee589 /src/main/java/cc/polyfrost/oneconfig/utils/TextUtils.java
parent85a8ae69736f93a1a972b696fd9d2fba5d435884 (diff)
downloadOneConfig-13965c418bb9429d2ff92fbcbb29fad73e8a120f.tar.gz
OneConfig-13965c418bb9429d2ff92fbcbb29fad73e8a120f.tar.bz2
OneConfig-13965c418bb9429d2ff92fbcbb29fad73e8a120f.zip
some small stuff and start on multi line textbox
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/utils/TextUtils.java')
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/TextUtils.java32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/TextUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/TextUtils.java
new file mode 100644
index 0000000..f574ea1
--- /dev/null
+++ b/src/main/java/cc/polyfrost/oneconfig/utils/TextUtils.java
@@ -0,0 +1,32 @@
+package cc.polyfrost.oneconfig.utils;
+
+import cc.polyfrost.oneconfig.lwjgl.RenderManager;
+import cc.polyfrost.oneconfig.lwjgl.font.Fonts;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+public class TextUtils {
+
+ public static ArrayList<String> wrapText(long vg, String text, float maxWidth, float fontSize, Fonts font) {
+ ArrayList<String> wrappedText = new ArrayList<>();
+ List<String> split = Arrays.asList(text.split(" "));
+ for (int i = split.size(); i >= 0; i--) {
+ String textPart = String.join(" ", split.subList(0, i));
+ float textWidth = RenderManager.getTextWidth(vg, textPart, fontSize, font);
+ if (textWidth > maxWidth) continue;
+ wrappedText.add(textPart);
+ if (i != split.size())
+ wrappedText.addAll(wrapText(vg, String.join(" ", split.subList(i, split.size())), maxWidth, fontSize, font));
+ break;
+ }
+ if (text.endsWith(" ")) {
+ String lastLine = wrappedText.get(wrappedText.size() - 1);
+ lastLine += " ";
+ wrappedText.remove(wrappedText.size() - 1);
+ wrappedText.add(lastLine);
+ }
+ return wrappedText;
+ }
+}