diff options
author | nextdaydelivery <12willettsh@gmail.com> | 2022-02-20 12:13:58 +0000 |
---|---|---|
committer | nextdaydelivery <12willettsh@gmail.com> | 2022-02-20 12:14:29 +0000 |
commit | dfa65f6236c226eb88f4e3761e10e80e5f37c22b (patch) | |
tree | 33937f0cadf5fb012aae57e6db87e56a32b4e265 /src/main/java/io/polyfrost/oneconfig/renderer | |
parent | 4f8e90571e3a270b54244d0fef985d5e0ca04b40 (diff) | |
download | OneConfig-dfa65f6236c226eb88f4e3761e10e80e5f37c22b.tar.gz OneConfig-dfa65f6236c226eb88f4e3761e10e80e5f37c22b.tar.bz2 OneConfig-dfa65f6236c226eb88f4e3761e10e80e5f37c22b.zip |
package moves and a split string renderer
Diffstat (limited to 'src/main/java/io/polyfrost/oneconfig/renderer')
-rw-r--r-- | src/main/java/io/polyfrost/oneconfig/renderer/TrueTypeFont.java | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/src/main/java/io/polyfrost/oneconfig/renderer/TrueTypeFont.java b/src/main/java/io/polyfrost/oneconfig/renderer/TrueTypeFont.java index 452bc48..01901eb 100644 --- a/src/main/java/io/polyfrost/oneconfig/renderer/TrueTypeFont.java +++ b/src/main/java/io/polyfrost/oneconfig/renderer/TrueTypeFont.java @@ -13,7 +13,9 @@ import java.awt.image.DataBufferInt; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.IntBuffer; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; @@ -248,8 +250,8 @@ public class TrueTypeFont { public int getWidth(String text) { int totalWidth = 0; - IntObject intObject = null; - int currentChar = 0; + IntObject intObject; + int currentChar; for (int i = 0; i < text.length(); i++) { currentChar = text.charAt(i); if (currentChar < 256) { @@ -272,6 +274,40 @@ public class TrueTypeFont { drawString(text, x, y, scaleX, scaleY, ALIGN_LEFT, color); } + public void drawSplitString(String text, float x, float y, int wrapWidth, int color) { + try { // time taken: 0.035ms to do complete cycle + wrapWidth += 140; // it needs this extra to work properly (why?) + List<String> splitString = new ArrayList<>(); + String[] words = text.split("\\W+"); + int totalWidth = 0; + String line = ""; + for(String word : words) { + int width = getWidth(word); + word += " "; // add the space + totalWidth += width; + line += word; + if(totalWidth >= wrapWidth) { // wrap line if it is too long + splitString.add(line); + totalWidth = 0; + line = ""; + } + } + if(!line.equals("")) { // add extra if there is any (last line) + splitString.add(line); + } + int i1 = 0; + for (String string : splitString) { + drawString(string, x, y + i1, 1f, 1f, color); // draw it + i1 += getHeight(); + } + } catch (Exception e) { // be safe kids + e.printStackTrace(); + } + } + + + + public void drawString(String text, float x, float y, float scaleX, float scaleY, int format, int color) { float f = (float) (color >> 16 & 255) / 255.0F; |