diff options
author | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-06-05 17:43:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-05 17:43:23 +0200 |
commit | ab7256dff5d6d37488081ba7a01b36d3ee9ef563 (patch) | |
tree | 8207341e6c402848cdbe7b2f2297f5f975e0e083 /src/main/java/cc/polyfrost/oneconfig/renderer/scissor | |
parent | a903cfc4d3f76cf3db24749b65156d126fa714e7 (diff) | |
download | OneConfig-ab7256dff5d6d37488081ba7a01b36d3ee9ef563.tar.gz OneConfig-ab7256dff5d6d37488081ba7a01b36d3ee9ef563.tar.bz2 OneConfig-ab7256dff5d6d37488081ba7a01b36d3ee9ef563.zip |
refactor (#36)
* refactor
* fix vig compat
* fix nanovg thingy
* e
* finalize
* gui utils package thingy
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/renderer/scissor')
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/renderer/scissor/Scissor.java | 27 | ||||
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/renderer/scissor/ScissorManager.java | 69 |
2 files changed, 96 insertions, 0 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/renderer/scissor/Scissor.java b/src/main/java/cc/polyfrost/oneconfig/renderer/scissor/Scissor.java new file mode 100644 index 0000000..179a260 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/renderer/scissor/Scissor.java @@ -0,0 +1,27 @@ +package cc.polyfrost.oneconfig.renderer.scissor; + +/** + * A class that represents a scissor rectangle. + * + * @see ScissorManager + */ +public class Scissor { + public float x; + public float y; + public float width; + public float height; + + public Scissor(float x, float y, float width, float height) { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + } + + public Scissor(Scissor scissor) { + this.x = scissor.x; + this.y = scissor.y; + this.width = scissor.width; + this.height = scissor.height; + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/renderer/scissor/ScissorManager.java b/src/main/java/cc/polyfrost/oneconfig/renderer/scissor/ScissorManager.java new file mode 100644 index 0000000..1f7801b --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/renderer/scissor/ScissorManager.java @@ -0,0 +1,69 @@ +package cc.polyfrost.oneconfig.renderer.scissor; + +import org.lwjgl.nanovg.NanoVG; + +import java.util.ArrayList; + +/** + * Provides an easy way to manage and group scissor rectangles. + */ +public class ScissorManager { + private static final ArrayList<Scissor> scissors = new ArrayList<>(); + + /** + * Adds and applies a scissor rectangle to the list of scissor rectangles. + * + * @param vg The NanoVG context. + * @param x The x coordinate of the scissor rectangle. + * @param y The y coordinate of the scissor rectangle. + * @param width The width of the scissor rectangle. + * @param height The height of the scissor rectangle. + * @return The scissor rectangle. + */ + public static Scissor scissor(long vg, float x, float y, float width, float height) { + Scissor scissor = new Scissor(x, y, width, height); + if (scissors.contains(scissor)) return scissor; + scissors.add(scissor); + applyScissors(vg); + return scissor; + } + + /** + * Resets the scissor rectangle provided. + * + * @param vg The NanoVG context. + * @param scissor The scissor rectangle to reset. + */ + public static void resetScissor(long vg, Scissor scissor) { + if (scissors.contains(scissor)) { + scissors.remove(scissor); + applyScissors(vg); + } + } + + /** + * Clear all scissor rectangles. + * + * @param vg The NanoVG context. + */ + public static void clearScissors(long vg) { + scissors.clear(); + NanoVG.nvgResetScissor(vg); + } + + private static void applyScissors(long vg) { + NanoVG.nvgResetScissor(vg); + if (scissors.size() <= 0) return; + Scissor finalScissor = new Scissor(scissors.get(0)); + for (int i = 1; i < scissors.size(); i++) { + Scissor scissor = scissors.get(i); + float rightX = Math.min(scissor.x + scissor.width, finalScissor.x + finalScissor.width); + float rightY = Math.min(scissor.y + scissor.height, finalScissor.y + finalScissor.height); + finalScissor.x = Math.max(finalScissor.x, scissor.x); + finalScissor.y = Math.max(finalScissor.y, scissor.y); + finalScissor.width = rightX - finalScissor.x; + finalScissor.height = rightY - finalScissor.y; + } + NanoVG.nvgScissor(vg, finalScissor.x, finalScissor.y, finalScissor.width, finalScissor.height); + } +} |