From 6cafd268d486a930c7e8bec229c30eb6a50fe436 Mon Sep 17 00:00:00 2001 From: DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> Date: Tue, 5 Jul 2022 16:16:23 +0200 Subject: start on new font thingies --- .../gui/elements/text/TextInputField.java | 8 +- .../cc/polyfrost/oneconfig/gui/pages/ModsPage.java | 1 + .../java/cc/polyfrost/oneconfig/hud/TextHud.java | 2 +- .../oneconfig/renderer/RenderManager.java | 48 ----- .../polyfrost/oneconfig/renderer/TextRenderer.java | 217 +++++++++++++++++++++ .../oneconfig/renderer/font/FontManager.java | 12 +- .../polyfrost/oneconfig/renderer/font/Fonts.java | 19 +- .../cc/polyfrost/oneconfig/utils/TextUtils.java | 14 -- 8 files changed, 240 insertions(+), 81 deletions(-) create mode 100644 src/main/java/cc/polyfrost/oneconfig/renderer/TextRenderer.java (limited to 'src') diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/TextInputField.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/TextInputField.java index 40463df..6b8ec6f 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/TextInputField.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/TextInputField.java @@ -129,18 +129,18 @@ public class TextInputField extends BasicElement { float width; StringBuilder s = new StringBuilder(); if (multiLine) { - wrappedText = TextUtils.wrapText(vg, input, this.width - 24, 14f, Fonts.REGULAR.font); + wrappedText = TextUtils.wrapText(vg, input, this.width - 24, 14f, Fonts.REGULAR); lines = wrappedText.size(); if (!toggled) caretPos = wrappedText.get(wrappedText.size() - 1).length(); int caretLine = (int) MathUtils.clamp(getCaretLine(caretPos), 0, wrappedText.size() - 1); - width = RenderManager.getTextWidth(vg, wrappedText.get(caretLine).substring(0, getLineCaret(caretPos, caretLine)), 14f, Fonts.REGULAR.font); + width = RenderManager.getTextWidth(vg, wrappedText.get(caretLine).substring(0, getLineCaret(caretPos, caretLine)), 14f, Fonts.REGULAR); } else if (!password) { - width = RenderManager.getTextWidth(vg, input.substring(0, caretPos), 14f, Fonts.REGULAR.font); + width = RenderManager.getTextWidth(vg, input.substring(0, caretPos), 14f, Fonts.REGULAR); } else { for (int i = 0; i < input.length(); i++) { s.append("*"); } - width = RenderManager.getTextWidth(vg, s.substring(0, caretPos), 14f, Fonts.REGULAR.font); + width = RenderManager.getTextWidth(vg, s.substring(0, caretPos), 14f, Fonts.REGULAR); } if (hovered) { while (Platform.getMousePlatform().next()) { diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/pages/ModsPage.java b/src/main/java/cc/polyfrost/oneconfig/gui/pages/ModsPage.java index 5f454f2..8a2e226 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/pages/ModsPage.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/pages/ModsPage.java @@ -9,6 +9,7 @@ import cc.polyfrost.oneconfig.internal.assets.Colors; import cc.polyfrost.oneconfig.internal.config.OneConfigConfig; import cc.polyfrost.oneconfig.internal.config.core.ConfigCore; import cc.polyfrost.oneconfig.renderer.RenderManager; +import cc.polyfrost.oneconfig.renderer.TextRenderer; import cc.polyfrost.oneconfig.renderer.font.Fonts; import cc.polyfrost.oneconfig.utils.color.ColorPalette; diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java b/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java index 381e0a8..8dd8ac3 100644 --- a/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java +++ b/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java @@ -103,7 +103,7 @@ public abstract class TextHud extends Hud { private class TickHandler { @Subscribe private void onTick(TickEvent event) { - if (event.stage != Stage.START) return; + if (event.stage != Stage.START || !isEnabled()) return; lines.clear(); if (!HudCore.editing) getLines(lines); else getExampleLines(lines); 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 @@ -280,22 +280,6 @@ public final class RenderManager { 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, Fonts font) { - drawText(vg, text, x, y, color, size, font.font); - } - /** * Draws a String with the given parameters. * @@ -319,22 +303,6 @@ public final class RenderManager { nvgColor.free(); } - /** - * 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, Fonts font) { - drawWrappedString(vg, text, x, y, width, color, size, font.font); - } - /** * Draws a String wrapped at the given width, with the given parameters. * @@ -358,18 +326,6 @@ public final class RenderManager { nvgColor.free(); } - /** - * Draw a formatted URL (a string in blue with an underline) that when clicked, opens the given text. - * - *
This does NOT scale to Minecraft's GUI scale!
- * - * @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. * @@ -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, ArrayListThis does NOT scale to Minecraft's GUI scale!
+ * + * @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