diff options
author | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-07-05 16:16:23 +0200 |
---|---|---|
committer | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-07-05 16:16:23 +0200 |
commit | 6cafd268d486a930c7e8bec229c30eb6a50fe436 (patch) | |
tree | 1dc3467b8ec6057c2c55ed1372d4ded7afc7ac9a /src/main/java/cc/polyfrost/oneconfig/renderer | |
parent | ab85af58303956e5f4436911ab9f301a215cfc15 (diff) | |
download | OneConfig-6cafd268d486a930c7e8bec229c30eb6a50fe436.tar.gz OneConfig-6cafd268d486a930c7e8bec229c30eb6a50fe436.tar.bz2 OneConfig-6cafd268d486a930c7e8bec229c30eb6a50fe436.zip |
start on new font thingies
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/renderer')
4 files changed, 234 insertions, 62 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/renderer/RenderManager.java b/src/main/java/cc/polyfrost/oneconfig/renderer/RenderManager.java index 5869ffb..b747088 100644 --- a/src/main/java/cc/polyfrost/oneconfig/renderer/RenderManager.java +++ b/src/main/java/cc/polyfrost/oneconfig/renderer/RenderManager.java @@ -292,22 +292,6 @@ public final class RenderManager { * @param font The font. * @see cc.polyfrost.oneconfig.renderer.font.Font */ - public static void drawText(long vg, String text, float x, float y, int color, float size, Fonts font) { - drawText(vg, text, x, y, color, size, font.font); - } - - /** - * Draws a String with the given parameters. - * - * @param vg The NanoVG context. - * @param text The text. - * @param x The x position. - * @param y The y position. - * @param color The color. - * @param size The size. - * @param font The font. - * @see cc.polyfrost.oneconfig.renderer.font.Font - */ public static void drawText(long vg, String text, float x, float y, int color, float size, Font font) { nvgBeginPath(vg); nvgFontSize(vg, size); @@ -331,22 +315,6 @@ public final class RenderManager { * @param size The size. * @param font The font. */ - public static void drawWrappedString(long vg, String text, float x, float y, float width, int color, float size, Fonts font) { - drawWrappedString(vg, text, x, y, width, color, size, font.font); - } - - /** - * Draws a String wrapped at the given width, with the given parameters. - * - * @param vg The NanoVG context. - * @param text The text. - * @param x The x position. - * @param y The y position. - * @param width The width. - * @param color The color. - * @param size The size. - * @param font The font. - */ public static void drawWrappedString(long vg, String text, float x, float y, float width, int color, float size, Font font) { nvgBeginPath(vg); nvgFontSize(vg, size); @@ -366,18 +334,6 @@ public final class RenderManager { * @see RenderManager#drawText(long, String, float, float, int, float, Font) * @see InputUtils#isAreaClicked(int, int, int, int) */ - public static void drawURL(long vg, String url, float x, float y, float size, Fonts font) { - drawURL(vg, url, x, y, size, font.font); - } - - /** - * Draw a formatted URL (a string in blue with an underline) that when clicked, opens the given text. - * - * <p><b>This does NOT scale to Minecraft's GUI scale!</b></p> - * - * @see RenderManager#drawText(long, String, float, float, int, float, Font) - * @see InputUtils#isAreaClicked(int, int, int, int) - */ public static void drawURL(long vg, String url, float x, float y, float size, Font font) { drawText(vg, url, x, y, Colors.PRIMARY_500, size, font); float length = getTextWidth(vg, url, size, font); @@ -487,10 +443,6 @@ public final class RenderManager { drawRoundImage(vg, filePath.filePath, x, y, width, height, radius); } - public static float getTextWidth(long vg, String text, float fontSize, Fonts font) { - return getTextWidth(vg, text, fontSize, font.font); - } - /** * Get the width of the provided String. * diff --git a/src/main/java/cc/polyfrost/oneconfig/renderer/TextRenderer.java b/src/main/java/cc/polyfrost/oneconfig/renderer/TextRenderer.java new file mode 100644 index 0000000..95071d1 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/renderer/TextRenderer.java @@ -0,0 +1,217 @@ +package cc.polyfrost.oneconfig.renderer; + +import cc.polyfrost.oneconfig.internal.assets.Colors; +import cc.polyfrost.oneconfig.renderer.font.Font; +import cc.polyfrost.oneconfig.utils.InputUtils; +import cc.polyfrost.oneconfig.utils.NetworkUtils; +import com.google.common.annotations.Beta; +import org.lwjgl.nanovg.NVGColor; + +import java.util.ArrayList; + +import static cc.polyfrost.oneconfig.renderer.RenderManager.color; +import static org.lwjgl.nanovg.NanoVG.*; + +public class TextRenderer { + /** + * Draws a String with the given parameters. + * + * @param vg The NanoVG context. + * @param text The text. + * @param x The x position. + * @param y The y position. + * @param color The color. + * @param size The size. + * @param font The font. + * @param letterSpacing The letter spacing + * @see cc.polyfrost.oneconfig.renderer.font.Font + */ + public static void drawText(long vg, String text, float x, float y, int color, float size, Font font, float letterSpacing) { + nvgBeginPath(vg); + nvgFontSize(vg, size); + nvgTextLetterSpacing(vg, letterSpacing); + nvgFontFace(vg, font.getName()); + nvgTextAlign(vg, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE); + NVGColor nvgColor = color(vg, color); + nvgText(vg, x, y, text); + nvgFill(vg); + nvgColor.free(); + } + + /** + * Draws a String with the given parameters. + * + * @param vg The NanoVG context. + * @param text The text. + * @param x The x position. + * @param y The y position. + * @param color The color. + * @param size The size. + * @param font The font. + * @see cc.polyfrost.oneconfig.renderer.font.Font + */ + public static void drawText(long vg, String text, float x, float y, int color, float size, Font font) { + drawText(vg, text, x, y, color, size, font, 0); + } + + /** + * Draws a String with the given parameters. + * + * @param vg The NanoVG context. + * @param text The text. + * @param x The x position. + * @param y The y position. + * @param color The color. + * @param size The size. + * @param font The font. + * @param lineHeight The line height + * @param letterSpacing The letter spacing + * @see cc.polyfrost.oneconfig.renderer.font.Font + */ + public static void drawText(long vg, ArrayList<String> text, float x, float y, int color, float size, Font font, float lineHeight, float letterSpacing) { + float textY = y; + for (String line : text) { + drawText(vg, line, x, textY, color, size, font, letterSpacing); + textY += lineHeight; + } + } + + /** + * Draws a String with the given parameters. + * + * @param vg The NanoVG context. + * @param text The text. + * @param x The x position. + * @param y The y position. + * @param color The color. + * @param size The size. + * @param font The font. + * @param lineHeight The line height + * @see cc.polyfrost.oneconfig.renderer.font.Font + */ + public static void drawText(long vg, ArrayList<String> text, float x, float y, int color, float size, Font font, float lineHeight) { + drawText(vg, text, x, y, color, size, font, lineHeight, 0); + } + + /** + * Draws a String wrapped at the given width, with the given parameters. + * + * @param vg The NanoVG context. + * @param text The text. + * @param x The x position. + * @param y The y position. + * @param width The width. + * @param color The color. + * @param size The size. + * @param font The font. + */ + public static void drawWrappedString(long vg, String text, float x, float y, float width, int color, float size, Font font) { + nvgBeginPath(vg); + nvgFontSize(vg, size); + nvgFontFace(vg, font.getName()); + nvgTextAlign(vg, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE); + NVGColor nvgColor = color(vg, color); + nvgTextBox(vg, x, y, width, text); + nvgFill(vg); + nvgColor.free(); + } + + /** + * Draw a formatted URL (a string in blue with an underline) that when clicked, opens the given text. + * + * <p><b>This does NOT scale to Minecraft's GUI scale!</b></p> + * + * @see RenderManager#drawText(long, String, float, float, int, float, Font) + * @see InputUtils#isAreaClicked(int, int, int, int) + */ + public static void drawURL(long vg, String url, float x, float y, float size, Font font) { + drawText(vg, url, x, y, Colors.PRIMARY_500, size, font); + float length = getTextWidth(vg, url, size, font); + RenderManager.drawRectangle(vg, x, y + size / 2, length, 1, Colors.PRIMARY_500); + if (InputUtils.isAreaClicked((int) (x - 2), (int) (y - 1), (int) (length + 4), (int) (size / 2 + 3))) { + NetworkUtils.browseLink(url); + } + } + + /** + * Get the width of the provided String. + * + * @param vg The NanoVG context. + * @param text The text. + * @param fontSize The font size. + * @param font The font. + * @param letterSpacing The letter spacing + * @return The width of the text. + */ + public static float getTextWidth(long vg, String text, float fontSize, Font font, float letterSpacing) { + float[] bounds = new float[4]; + nvgFontSize(vg, fontSize); + nvgTextLetterSpacing(vg, letterSpacing); + nvgFontFace(vg, font.getName()); + return nvgTextBounds(vg, 0, 0, text, bounds); + } + + /** + * Get the width of the provided String. + * + * @param vg The NanoVG context. + * @param text The text. + * @param fontSize The font size. + * @param font The font. + * @return The width of the text. + */ + public static float getTextWidth(long vg, String text, float fontSize, Font font) { + return getTextWidth(vg, text, fontSize, font, 0); + } + + /** + * Wraps a string into an array of lines. + * + * @param vg The NanoVG context. + * @param text The text to wrap. + * @param maxWidth The maximum width of each line. + * @param fontSize The font size. + * @param font The font to use. + * @param letterSpacing The letter spacing + * @return The array of lines. + */ + @Beta + public static ArrayList<String> wrapText(long vg, String text, float maxWidth, float fontSize, Font font, float letterSpacing) { + ArrayList<String> wrappedText = new ArrayList<>(); + text += " "; + int prevIndex = 0; + for (int i = text.indexOf(" "); i >= 0; i = text.indexOf(" ", i + 1)) { + String textPart = text.substring(0, i); + float textWidth = getTextWidth(vg, textPart, fontSize, font, letterSpacing); + if (textWidth < maxWidth) { + prevIndex = i; + continue; + } + wrappedText.add(text.substring(0, prevIndex) + " "); + wrappedText.addAll(wrapText(vg, text.substring(prevIndex + 1), maxWidth, fontSize, font, letterSpacing)); + break; + } + if (wrappedText.size() == 0) wrappedText.add(text); + String temp = wrappedText.get(wrappedText.size() - 1); + if (temp.length() != 0) { + wrappedText.remove(wrappedText.size() - 1); + wrappedText.add(temp.substring(0, temp.length() - 1)); + } + return wrappedText; + } + + /** + * Wraps a string into an array of lines. + * + * @param vg The NanoVG context. + * @param text The text to wrap. + * @param maxWidth The maximum width of each line. + * @param fontSize The font size. + * @param font The font to use. + * @return The array of lines. + */ + @Beta + public static ArrayList<String> wrapText(long vg, String text, float maxWidth, float fontSize, Font font) { + return wrapText(vg, text, maxWidth, fontSize, font, 0); + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/renderer/font/FontManager.java b/src/main/java/cc/polyfrost/oneconfig/renderer/font/FontManager.java index 388911d..79ac518 100644 --- a/src/main/java/cc/polyfrost/oneconfig/renderer/font/FontManager.java +++ b/src/main/java/cc/polyfrost/oneconfig/renderer/font/FontManager.java @@ -3,6 +3,7 @@ package cc.polyfrost.oneconfig.renderer.font; import cc.polyfrost.oneconfig.utils.IOUtils; import java.io.IOException; +import java.lang.reflect.Field; import java.nio.ByteBuffer; import static org.lwjgl.nanovg.NanoVG.nvgCreateFontMem; @@ -17,8 +18,15 @@ public class FontManager { */ public void initialize(long vg) { - for (Fonts fonts : Fonts.values()) { - loadFont(vg, fonts.font); + for (Field field : Fonts.class.getDeclaredFields()) { + try { + field.setAccessible(true); + Object font = field.get(null); + if (!(font instanceof Font)) continue; + loadFont(vg, (Font) font); + } catch (Exception e) { + throw new RuntimeException("Could not initialize fonts"); + } } } diff --git a/src/main/java/cc/polyfrost/oneconfig/renderer/font/Fonts.java b/src/main/java/cc/polyfrost/oneconfig/renderer/font/Fonts.java index 9b6193f..3120c7b 100644 --- a/src/main/java/cc/polyfrost/oneconfig/renderer/font/Fonts.java +++ b/src/main/java/cc/polyfrost/oneconfig/renderer/font/Fonts.java @@ -1,15 +1,10 @@ package cc.polyfrost.oneconfig.renderer.font; -public enum Fonts { - BOLD(new Font("inter-bold", "/assets/oneconfig/font/Bold.otf")), - SEMIBOLD(new Font("inter-semibold", "/assets/oneconfig/font/SemiBold.otf")), - MEDIUM(new Font("inter-medium", "/assets/oneconfig/font/Medium.otf")), - REGULAR(new Font("inter-regular", "/assets/oneconfig/font/Regular.otf")), - MINECRAFT_REGULAR(new Font("mc-regular", "/assets/oneconfig/font/Minecraft-Regular.otf")), - MINECRAFT_BOLD(new Font("mc-bold", "/assets/oneconfig/font/Minecraft-Bold.otf")); - public final Font font; - - Fonts(Font font) { - this.font = font; - } +public class Fonts { + public static final Font BOLD = new Font("inter-bold", "/assets/oneconfig/font/Bold.otf"); + public static final Font SEMIBOLD = new Font("inter-semibold", "/assets/oneconfig/font/SemiBold.otf"); + public static final Font MEDIUM = new Font("inter-medium", "/assets/oneconfig/font/Medium.otf"); + public static final Font REGULAR = new Font("inter-regular", "/assets/oneconfig/font/Regular.otf"); + public static final Font MINECRAFT_REGULAR = new Font("mc-regular", "/assets/oneconfig/font/Minecraft-Regular.otf"); + public static final Font MINECRAFT_BOLD = new Font("mc-bold", "/assets/oneconfig/font/Minecraft-Bold.otf"); } |