From 268e64acec2a9e556b575103631ddddfac698b3c Mon Sep 17 00:00:00 2001 From: DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> Date: Thu, 18 Aug 2022 18:04:48 +0200 Subject: Add ability to use image flags (#95) add image flags --- api/OneConfig.api | 9 ++++ .../polyfrost/oneconfig/renderer/AssetLoader.java | 60 ++++++++++++++++++++-- .../cc/polyfrost/oneconfig/renderer/Image.java | 8 ++- .../oneconfig/renderer/RenderManager.java | 38 +++++++++++--- .../java/cc/polyfrost/oneconfig/renderer/SVG.java | 8 ++- 5 files changed, 109 insertions(+), 14 deletions(-) diff --git a/api/OneConfig.api b/api/OneConfig.api index 553b87c..32318e8 100644 --- a/api/OneConfig.api +++ b/api/OneConfig.api @@ -1132,21 +1132,28 @@ public abstract interface class cc/polyfrost/oneconfig/platform/ServerPlatform { } public final class cc/polyfrost/oneconfig/renderer/AssetLoader { + public static final field DEFAULT_FLAGS I public static field INSTANCE Lcc/polyfrost/oneconfig/renderer/AssetLoader; public fun clearImages (J)V public fun clearSVGs (J)V public fun getImage (Ljava/lang/String;)I public fun getSVG (Ljava/lang/String;FF)I public fun imageToIntBuffer (Ljava/lang/String;)Ljava/nio/IntBuffer; + public fun loadImage (JLcc/polyfrost/oneconfig/renderer/Image;)Z public fun loadImage (JLjava/lang/String;)Z + public fun loadImage (JLjava/lang/String;I)Z + public fun loadSVG (JLcc/polyfrost/oneconfig/renderer/SVG;FF)Z public fun loadSVG (JLjava/lang/String;FF)Z + public fun loadSVG (JLjava/lang/String;FFI)Z public fun removeImage (JLjava/lang/String;)V public fun removeSVG (JLjava/lang/String;FF)V } public class cc/polyfrost/oneconfig/renderer/Image { public final field filePath Ljava/lang/String; + public final field flags I public fun (Ljava/lang/String;)V + public fun (Ljava/lang/String;I)V } public final class cc/polyfrost/oneconfig/renderer/RenderManager { @@ -1197,7 +1204,9 @@ public final class cc/polyfrost/oneconfig/renderer/RenderManager$TextType : java public class cc/polyfrost/oneconfig/renderer/SVG { public final field filePath Ljava/lang/String; + public final field flags I public fun (Ljava/lang/String;)V + public fun (Ljava/lang/String;I)V } public class cc/polyfrost/oneconfig/renderer/TextRenderer { diff --git a/src/main/java/cc/polyfrost/oneconfig/renderer/AssetLoader.java b/src/main/java/cc/polyfrost/oneconfig/renderer/AssetLoader.java index 6c90c5b..4fe8c0b 100644 --- a/src/main/java/cc/polyfrost/oneconfig/renderer/AssetLoader.java +++ b/src/main/java/cc/polyfrost/oneconfig/renderer/AssetLoader.java @@ -58,6 +58,7 @@ public final class AssetLoader { } + public static final int DEFAULT_FLAGS = NanoVG.NVG_IMAGE_REPEATX | NanoVG.NVG_IMAGE_REPEATY | NanoVG.NVG_IMAGE_GENERATE_MIPMAPS; private final HashMap imageHashMap = new HashMap<>(); private final HashMap svgHashMap = new HashMap<>(); public static AssetLoader INSTANCE = new AssetLoader(); @@ -67,9 +68,10 @@ public final class AssetLoader { * * @param vg The NanoVG context. * @param fileName The name of the file to load. + * @param flags The image flags * @return Whether the asset was loaded successfully. */ - public boolean loadImage(long vg, String fileName) { + public boolean loadImage(long vg, String fileName, int flags) { if (!imageHashMap.containsKey(fileName)) { int[] width = {0}; int[] height = {0}; @@ -85,12 +87,34 @@ public final class AssetLoader { return false; } - imageHashMap.put(fileName, NanoVG.nvgCreateImageRGBA(vg, width[0], height[0], NanoVG.NVG_IMAGE_REPEATX | NanoVG.NVG_IMAGE_REPEATY | NanoVG.NVG_IMAGE_GENERATE_MIPMAPS, buffer)); + imageHashMap.put(fileName, NanoVG.nvgCreateImageRGBA(vg, width[0], height[0], flags, buffer)); return true; } return true; } + /** + * Loads an assets from resources. + * + * @param vg The NanoVG context. + * @param image The Image + * @return Whether the asset was loaded successfully. + */ + public boolean loadImage(long vg, Image image) { + return loadImage(vg, image.filePath, image.flags); + } + + /** + * Loads an assets from resources. + * + * @param vg The NanoVG context. + * @param fileName The name of the file to load. + * @return Whether the asset was loaded successfully. + */ + public boolean loadImage(long vg, String fileName) { + return loadImage(vg, fileName, DEFAULT_FLAGS); + } + /** * Loads an SVG from resources. * @@ -98,9 +122,10 @@ public final class AssetLoader { * @param fileName The name of the file to load. * @param width The width of the SVG. * @param height The height of the SVG. + * @param flags The image flags * @return Whether the SVG was loaded successfully. */ - public boolean loadSVG(long vg, String fileName, float width, float height) { + public boolean loadSVG(long vg, String fileName, float width, float height, int flags) { String name = fileName + "-" + width + "-" + height; if (!svgHashMap.containsKey(name)) { try { @@ -130,7 +155,7 @@ public final class AssetLoader { NanoSVG.nsvgDeleteRasterizer(rasterizer); NanoSVG.nsvgDelete(svg); - svgHashMap.put(name, NanoVG.nvgCreateImageRGBA(vg, w, h, NanoVG.NVG_IMAGE_REPEATX | NanoVG.NVG_IMAGE_REPEATY | NanoVG.NVG_IMAGE_GENERATE_MIPMAPS, image)); + svgHashMap.put(name, NanoVG.nvgCreateImageRGBA(vg, w, h, flags, image)); return true; } catch (Exception e) { System.err.println("Failed to parse SVG file"); @@ -141,6 +166,32 @@ public final class AssetLoader { return true; } + /** + * Loads an assets from resources. + * + * @param vg The NanoVG context. + * @param svg The SVG + * @param width The width of the SVG. + * @param height The height of the SVG. + * @return Whether the asset was loaded successfully. + */ + public boolean loadSVG(long vg, SVG svg, float width, float height) { + return loadSVG(vg, svg.filePath, width, height, svg.flags); + } + + /** + * Loads an SVG from resources. + * + * @param vg The NanoVG context. + * @param fileName The name of the file to load. + * @param width The width of the SVG. + * @param height The height of the SVG. + * @return Whether the SVG was loaded successfully. + */ + public boolean loadSVG(long vg, String fileName, float width, float height) { + return loadSVG(vg, fileName, width, height, DEFAULT_FLAGS); + } + /** * Get a loaded assets from the cache. *

Requires the assets to have been loaded first.

@@ -224,6 +275,7 @@ public final class AssetLoader { /** * Convert the given image (as a quantified path) to an IntBuffer, of its pixels, in order, stored as integers in ARGB format. * Mostly an internal method; used by LWJGL. + * * @param fileName quantified path to the image * @return intBuffer of the image's pixels in ARGB format */ diff --git a/src/main/java/cc/polyfrost/oneconfig/renderer/Image.java b/src/main/java/cc/polyfrost/oneconfig/renderer/Image.java index 8020c68..2e5ca2a 100644 --- a/src/main/java/cc/polyfrost/oneconfig/renderer/Image.java +++ b/src/main/java/cc/polyfrost/oneconfig/renderer/Image.java @@ -33,8 +33,14 @@ package cc.polyfrost.oneconfig.renderer; */ public class Image { public final String filePath; + public final int flags; - public Image(String filePath) { + public Image(String filePath, int flags) { this.filePath = filePath; + this.flags = flags; + } + + public Image(String filePath) { + this(filePath, AssetLoader.DEFAULT_FLAGS); } } diff --git a/src/main/java/cc/polyfrost/oneconfig/renderer/RenderManager.java b/src/main/java/cc/polyfrost/oneconfig/renderer/RenderManager.java index 5e9c996..cfdb4ad 100644 --- a/src/main/java/cc/polyfrost/oneconfig/renderer/RenderManager.java +++ b/src/main/java/cc/polyfrost/oneconfig/renderer/RenderManager.java @@ -402,8 +402,10 @@ public final class RenderManager { * * @see RenderManager#drawImage(long, String, float, float, float, float) */ - public static void drawImage(long vg, Image filePath, float x, float y, float width, float height) { - drawImage(vg, filePath.filePath, x, y, width, height); + public static void drawImage(long vg, Image image, float x, float y, float width, float height) { + if (AssetLoader.INSTANCE.loadImage(vg, image)) { + drawImage(vg, image.filePath, x, y, width, height); + } } /** @@ -411,8 +413,10 @@ public final class RenderManager { * * @see RenderManager#drawImage(long, String, float, float, float, float, int) */ - public static void drawImage(long vg, Image filePath, float x, float y, float width, float height, int color) { - drawImage(vg, filePath.filePath, x, y, width, height, color); + public static void drawImage(long vg, Image image, float x, float y, float width, float height, int color) { + if (AssetLoader.INSTANCE.loadImage(vg, image)) { + drawImage(vg, image.filePath, x, y, width, height, color); + } } /** @@ -444,8 +448,10 @@ public final class RenderManager { * * @see RenderManager#drawRoundImage(long, String, float, float, float, float, float) */ - public static void drawRoundImage(long vg, Image filePath, float x, float y, float width, float height, float radius) { - drawRoundImage(vg, filePath.filePath, x, y, width, height, radius); + public static void drawRoundImage(long vg, Image image, float x, float y, float width, float height, float radius) { + if (AssetLoader.INSTANCE.loadImage(vg, image)) { + drawRoundImage(vg, image.filePath, x, y, width, height, radius); + } } /** @@ -636,7 +642,15 @@ public final class RenderManager { * @see RenderManager#drawSvg(long, String, float, float, float, float) */ public static void drawSvg(long vg, SVG svg, float x, float y, float width, float height) { - drawSvg(vg, svg.filePath, x, y, width, height); + float w = width; + float h = height; + if (OneConfigGui.INSTANCE != null) { + w *= OneConfigGui.INSTANCE.getScaleFactor(); + h *= OneConfigGui.INSTANCE.getScaleFactor(); + } + if (AssetLoader.INSTANCE.loadSVG(vg, svg, w, h)) { + drawSvg(vg, svg.filePath, x, y, width, height); + } } /** @@ -645,7 +659,15 @@ public final class RenderManager { * @see RenderManager#drawSvg(long, String, float, float, float, float, int) */ public static void drawSvg(long vg, SVG svg, float x, float y, float width, float height, int color) { - drawSvg(vg, svg.filePath, x, y, width, height, color); + float w = width; + float h = height; + if (OneConfigGui.INSTANCE != null) { + w *= OneConfigGui.INSTANCE.getScaleFactor(); + h *= OneConfigGui.INSTANCE.getScaleFactor(); + } + if (AssetLoader.INSTANCE.loadSVG(vg, svg, w, h)) { + drawSvg(vg, svg.filePath, x, y, width, height, color); + } } /** diff --git a/src/main/java/cc/polyfrost/oneconfig/renderer/SVG.java b/src/main/java/cc/polyfrost/oneconfig/renderer/SVG.java index 17a55c9..83e5749 100644 --- a/src/main/java/cc/polyfrost/oneconfig/renderer/SVG.java +++ b/src/main/java/cc/polyfrost/oneconfig/renderer/SVG.java @@ -33,8 +33,14 @@ package cc.polyfrost.oneconfig.renderer; */ public class SVG { public final String filePath; + public final int flags; - public SVG(String filePath) { + public SVG(String filePath, int flags) { this.filePath = filePath; + this.flags = flags; + } + + public SVG(String filePath) { + this(filePath, AssetLoader.DEFAULT_FLAGS); } } -- cgit