From 77f8b7924ad61ebb8334d37a0a2ae2c8a297b400 Mon Sep 17 00:00:00 2001 From: nextdaydelivery <79922345+nxtdaydelivery@users.noreply.github.com> Date: Wed, 4 May 2022 18:36:37 +0100 Subject: change some files and start on scrolling --- .../cc/polyfrost/oneconfig/gui/OneConfigGui.java | 9 ++-- .../oneconfig/gui/elements/ColorSelector.java | 28 ++++++++--- .../oneconfig/gui/elements/TextInputField.java | 4 +- .../gui/elements/config/ConfigUniSelector.java | 4 +- .../oneconfig/gui/pages/ModConfigPage.java | 5 ++ .../cc/polyfrost/oneconfig/gui/pages/Page.java | 28 +++++++++++ .../cc/polyfrost/oneconfig/lwjgl/BlurHandler.java | 2 +- .../java/cc/polyfrost/oneconfig/lwjgl/IOUtil.java | 56 ---------------------- .../oneconfig/lwjgl/Lwjgl2FunctionProvider.java | 39 --------------- .../java/cc/polyfrost/oneconfig/lwjgl/Scissor.java | 22 --------- .../polyfrost/oneconfig/lwjgl/ScissorManager.java | 40 ---------------- .../oneconfig/lwjgl/font/FontManager.java | 4 +- .../oneconfig/lwjgl/image/ImageLoader.java | 4 +- .../oneconfig/lwjgl/plugin/ClassTransformer.java | 4 +- .../lwjgl/plugin/Lwjgl2FunctionProvider.java | 39 +++++++++++++++ .../polyfrost/oneconfig/lwjgl/scissor/Scissor.java | 22 +++++++++ .../oneconfig/lwjgl/scissor/ScissorManager.java | 41 ++++++++++++++++ .../java/cc/polyfrost/oneconfig/utils/IOUtils.java | 54 +++++++++++++++++++++ 18 files changed, 228 insertions(+), 177 deletions(-) delete mode 100644 src/main/java/cc/polyfrost/oneconfig/lwjgl/IOUtil.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/lwjgl/Lwjgl2FunctionProvider.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/lwjgl/Scissor.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/lwjgl/ScissorManager.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/lwjgl/plugin/Lwjgl2FunctionProvider.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/lwjgl/scissor/Scissor.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/lwjgl/scissor/ScissorManager.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/IOUtils.java (limited to 'src/main/java/cc/polyfrost') diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java b/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java index 72cd8c0..2c0816c 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java @@ -7,8 +7,8 @@ import cc.polyfrost.oneconfig.gui.elements.TextInputField; import cc.polyfrost.oneconfig.gui.pages.HomePage; import cc.polyfrost.oneconfig.gui.pages.Page; import cc.polyfrost.oneconfig.lwjgl.RenderManager; -import cc.polyfrost.oneconfig.lwjgl.Scissor; -import cc.polyfrost.oneconfig.lwjgl.ScissorManager; +import cc.polyfrost.oneconfig.lwjgl.scissor.Scissor; +import cc.polyfrost.oneconfig.lwjgl.scissor.ScissorManager; import cc.polyfrost.oneconfig.lwjgl.font.Fonts; import cc.polyfrost.oneconfig.lwjgl.image.Images; import cc.polyfrost.oneconfig.utils.InputUtils; @@ -125,7 +125,10 @@ public class OneConfigGui extends GuiScreen { pageProgress = -224f; } } else { - currentPage.draw(vg, (int) (x - pageProgress), y + 72); + if(currentPage.getMaxScrollHeight() == 728) { + currentPage.draw(vg, (int) (x - pageProgress), y + 72); + } else currentPage.scrollWithDraw(vg, (int) (x - pageProgress), y + 72); + } ScissorManager.resetScissor(vg, scissor); if (currentColorSelector != null) { diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/ColorSelector.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/ColorSelector.java index 83d0f7c..fcd96b2 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/ColorSelector.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/ColorSelector.java @@ -9,6 +9,22 @@ import java.awt.*; import java.util.ArrayList; public class ColorSelector { + + public ColorSelector(Color color, int mouseX, int mouseY) { + + } + + public void draw(long vg) { + + } + + public Color getColor() { + + return null; + } +} + +/*public class ColorSelector { private Color color; private final int x, y; private final int width = 416; @@ -147,7 +163,7 @@ public class ColorSelector { public abstract void drawBox(long vg, int x, int y); /** draw the color selector contents, including the box, and the input fields. If it is clicked, getColorAtPos is called. updateElements is also called to update all the input fields. */ - public void draw(long vg, int x, int y) { + /*public void draw(long vg, int x, int y) { drawBox(vg, x + 16, y + 120); if(InputUtils.isAreaHovered(x + 16, y + 120, 384, 288) && Mouse.isButtonDown(0)) { selectedX = InputUtils.mouseX() - x - 16; @@ -170,18 +186,18 @@ public class ColorSelector { RenderManager.drawRoundedRect(vg, x + 16, y + 488, 384, 40, color1.getRGB(), 12f); } - /** called to set the color of the color selector box based on the values of the input fields. */ + /** called to set the color of the color selector box based on the values of the input fields. */ /* public abstract void setColor(Color color); - /** return an array of two ints of the start color of the gradient and the end color of the gradient. */ + /** return an array of two ints of the start color of the gradient and the end color of the gradient. */ /* public abstract int[] drawTopSlider(); - /** return an array of two ints of the start color of the gradient and the end color of the gradient. */ + /** return an array of two ints of the start color of the gradient and the end color of the gradient. */ /* public abstract int[] drawBottomSlider(); /** * This method is called when the color selector is clicked. It needs to return color at the clicked position. * @return color at the clicked position as a float[] rgba. - */ + */ /* public abstract float[] getColorAtPos(int clickX, int clickY); public float getRed() { @@ -246,5 +262,5 @@ public class ColorSelector { } } } - +*/ diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/TextInputField.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/TextInputField.java index 3058643..196c447 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/TextInputField.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/TextInputField.java @@ -2,8 +2,8 @@ package cc.polyfrost.oneconfig.gui.elements; import cc.polyfrost.oneconfig.config.OneConfigConfig; import cc.polyfrost.oneconfig.lwjgl.RenderManager; -import cc.polyfrost.oneconfig.lwjgl.Scissor; -import cc.polyfrost.oneconfig.lwjgl.ScissorManager; +import cc.polyfrost.oneconfig.lwjgl.scissor.Scissor; +import cc.polyfrost.oneconfig.lwjgl.scissor.ScissorManager; import cc.polyfrost.oneconfig.lwjgl.font.Fonts; import cc.polyfrost.oneconfig.utils.InputUtils; import net.minecraft.client.gui.GuiScreen; diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigUniSelector.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigUniSelector.java index 0623f1e..5fad0a4 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigUniSelector.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigUniSelector.java @@ -3,8 +3,8 @@ package cc.polyfrost.oneconfig.gui.elements.config; import cc.polyfrost.oneconfig.config.OneConfigConfig; import cc.polyfrost.oneconfig.config.interfaces.BasicOption; import cc.polyfrost.oneconfig.lwjgl.RenderManager; -import cc.polyfrost.oneconfig.lwjgl.Scissor; -import cc.polyfrost.oneconfig.lwjgl.ScissorManager; +import cc.polyfrost.oneconfig.lwjgl.scissor.Scissor; +import cc.polyfrost.oneconfig.lwjgl.scissor.ScissorManager; import cc.polyfrost.oneconfig.lwjgl.font.Fonts; import cc.polyfrost.oneconfig.lwjgl.image.Images; import cc.polyfrost.oneconfig.utils.InputUtils; diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/pages/ModConfigPage.java b/src/main/java/cc/polyfrost/oneconfig/gui/pages/ModConfigPage.java index f335e4d..d0e809e 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/pages/ModConfigPage.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/pages/ModConfigPage.java @@ -149,4 +149,9 @@ public class ModConfigPage extends Page { button.setToggled(false); } } + + @Override + public int getMaxScrollHeight() { + return 1600; + } } diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/pages/Page.java b/src/main/java/cc/polyfrost/oneconfig/gui/pages/Page.java index ba31fa8..2f2fa36 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/pages/Page.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/pages/Page.java @@ -1,9 +1,17 @@ package cc.polyfrost.oneconfig.gui.pages; +import cc.polyfrost.oneconfig.config.OneConfigConfig; +import cc.polyfrost.oneconfig.lwjgl.RenderManager; +import cc.polyfrost.oneconfig.utils.MathUtils; +import org.lwjgl.input.Mouse; + /** * A page is a 1056x728 rectangle of the GUI. It is the main content of the gui, and can be switched back and forwards easily. All the content of OneConfig is in a page. */ public abstract class Page { + private float scrollPercent = 0f; + private float yDiff, scrollAmount; + protected final String title; Page(String title) { @@ -15,6 +23,21 @@ public abstract class Page { public void finishUpAndClose() { } + public void scrollWithDraw(long vg, int x, int y) { + int dWheel = Mouse.getDWheel(); + scrollAmount += dWheel / 120f; + scrollPercent = MathUtils.easeOut(scrollPercent, scrollAmount, 20f); + + + int currentScroll = (int) yDiff + (int) (scrollPercent * 100); + if(currentScroll > 0) { + currentScroll = 0; + scrollPercent = 0; + scrollAmount = 0; + } + draw(vg, x, y + currentScroll); + } + public String getTitle() { return title; } @@ -28,4 +51,9 @@ public abstract class Page { public boolean isBase() { return false; } + + /** Use this method to set the maximum scroll height of the page. */ + public int getMaxScrollHeight() { + return 728; + } } diff --git a/src/main/java/cc/polyfrost/oneconfig/lwjgl/BlurHandler.java b/src/main/java/cc/polyfrost/oneconfig/lwjgl/BlurHandler.java index 914c483..285875a 100644 --- a/src/main/java/cc/polyfrost/oneconfig/lwjgl/BlurHandler.java +++ b/src/main/java/cc/polyfrost/oneconfig/lwjgl/BlurHandler.java @@ -109,7 +109,7 @@ public class BlurHandler { su.set(progress); } } catch (IllegalArgumentException ex) { - this.logger.error("An error occurred while updating ToggleChat's blur. Please report this!", ex); + this.logger.error("An error occurred while updating OneConfig's blur. Please report this!", ex); } this.mc.mcProfiler.endSection(); diff --git a/src/main/java/cc/polyfrost/oneconfig/lwjgl/IOUtil.java b/src/main/java/cc/polyfrost/oneconfig/lwjgl/IOUtil.java deleted file mode 100644 index 1394239..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/lwjgl/IOUtil.java +++ /dev/null @@ -1,56 +0,0 @@ -package cc.polyfrost.oneconfig.lwjgl; - -import org.apache.commons.io.IOUtils; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.nio.Buffer; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.nio.file.Files; - -public final class IOUtil { - - private IOUtil() { - } - - /** - * Taken from legui under MIT License - * https://github.com/SpinyOwl/legui/blob/develop/LICENSE - */ - public static ByteBuffer resourceToByteBuffer(String path) throws IOException { - byte[] bytes; - path = path.trim(); - if (path.startsWith("http")) { - bytes = IOUtils.toByteArray(new URL(path)); - } else { - InputStream stream; - File file = new File(path); - if (file.exists() && file.isFile()) { - stream = Files.newInputStream(file.toPath()); - } else { - stream = IOUtil.class.getResourceAsStream(path); - } - if (stream == null) { - throw new FileNotFoundException(path); - } - bytes = IOUtils.toByteArray(stream); - } - ByteBuffer data = ByteBuffer.allocateDirect(bytes.length).order(ByteOrder.nativeOrder()) - .put(bytes); - ((Buffer) data).flip(); - return data; - } - - public static ByteBuffer resourceToByteBufferNullable(String path) { - try { - return resourceToByteBuffer(path); - } catch (Exception ignored) { - return null; - } - } - -} \ No newline at end of file diff --git a/src/main/java/cc/polyfrost/oneconfig/lwjgl/Lwjgl2FunctionProvider.java b/src/main/java/cc/polyfrost/oneconfig/lwjgl/Lwjgl2FunctionProvider.java deleted file mode 100644 index e00570e..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/lwjgl/Lwjgl2FunctionProvider.java +++ /dev/null @@ -1,39 +0,0 @@ -package cc.polyfrost.oneconfig.lwjgl; - -import org.lwjgl.opengl.GLContext; -import org.lwjgl.system.FunctionProvider; - -import java.lang.reflect.Method; -import java.nio.ByteBuffer; - -/** - * Taken from LWJGLTwoPointFive under The Unlicense - * https://github.com/DJtheRedstoner/LWJGLTwoPointFive/blob/master/LICENSE/ - */ -public class Lwjgl2FunctionProvider implements FunctionProvider { - - private final Method m_getFunctionAddress; - - public Lwjgl2FunctionProvider() { - try { - m_getFunctionAddress = GLContext.class.getDeclaredMethod("getFunctionAddress", String.class); - m_getFunctionAddress.setAccessible(true); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - @Override - public long getFunctionAddress(CharSequence functionName) { - try { - return (long) m_getFunctionAddress.invoke(null, functionName.toString()); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - @Override - public long getFunctionAddress(ByteBuffer byteBuffer) { - throw new UnsupportedOperationException(); - } -} \ No newline at end of file diff --git a/src/main/java/cc/polyfrost/oneconfig/lwjgl/Scissor.java b/src/main/java/cc/polyfrost/oneconfig/lwjgl/Scissor.java deleted file mode 100644 index 5709823..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/lwjgl/Scissor.java +++ /dev/null @@ -1,22 +0,0 @@ -package cc.polyfrost.oneconfig.lwjgl; - -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/lwjgl/ScissorManager.java b/src/main/java/cc/polyfrost/oneconfig/lwjgl/ScissorManager.java deleted file mode 100644 index db85e86..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/lwjgl/ScissorManager.java +++ /dev/null @@ -1,40 +0,0 @@ -package cc.polyfrost.oneconfig.lwjgl; - -import org.lwjgl.nanovg.NanoVG; - -import java.util.ArrayList; - -public class ScissorManager { - private static final ArrayList scissors = new ArrayList<>(); - - 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; - } - - public static void resetScissor(long vg, Scissor scissor) { - if (scissors.contains(scissor)) { - scissors.remove(scissor); - applyScissors(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); - } -} diff --git a/src/main/java/cc/polyfrost/oneconfig/lwjgl/font/FontManager.java b/src/main/java/cc/polyfrost/oneconfig/lwjgl/font/FontManager.java index 0974019..534795b 100644 --- a/src/main/java/cc/polyfrost/oneconfig/lwjgl/font/FontManager.java +++ b/src/main/java/cc/polyfrost/oneconfig/lwjgl/font/FontManager.java @@ -1,6 +1,6 @@ package cc.polyfrost.oneconfig.lwjgl.font; -import cc.polyfrost.oneconfig.lwjgl.IOUtil; +import cc.polyfrost.oneconfig.utils.IOUtils; import java.io.IOException; import java.nio.ByteBuffer; @@ -15,7 +15,7 @@ public class FontManager { Font font = fonts.font; int loaded = -1; try { - ByteBuffer buffer = IOUtil.resourceToByteBuffer(font.getFileName()); + ByteBuffer buffer = IOUtils.resourceToByteBuffer(font.getFileName()); loaded = nvgCreateFontMem(vg, font.getName(), buffer, 0); font.setBuffer(buffer); } catch (IOException e) { diff --git a/src/main/java/cc/polyfrost/oneconfig/lwjgl/image/ImageLoader.java b/src/main/java/cc/polyfrost/oneconfig/lwjgl/image/ImageLoader.java index 8e72828..479104c 100644 --- a/src/main/java/cc/polyfrost/oneconfig/lwjgl/image/ImageLoader.java +++ b/src/main/java/cc/polyfrost/oneconfig/lwjgl/image/ImageLoader.java @@ -1,6 +1,6 @@ package cc.polyfrost.oneconfig.lwjgl.image; -import cc.polyfrost.oneconfig.lwjgl.IOUtil; +import cc.polyfrost.oneconfig.utils.IOUtils; import org.lwjgl.nanovg.NanoVG; import org.lwjgl.stb.STBImage; @@ -17,7 +17,7 @@ public class ImageLoader { int[] height = {0}; int[] channels = {0}; - ByteBuffer image = IOUtil.resourceToByteBufferNullable(fileName); + ByteBuffer image = IOUtils.resourceToByteBufferNullable(fileName); if (image == null) { return false; } diff --git a/src/main/java/cc/polyfrost/oneconfig/lwjgl/plugin/ClassTransformer.java b/src/main/java/cc/polyfrost/oneconfig/lwjgl/plugin/ClassTransformer.java index c34f3b8..858d615 100644 --- a/src/main/java/cc/polyfrost/oneconfig/lwjgl/plugin/ClassTransformer.java +++ b/src/main/java/cc/polyfrost/oneconfig/lwjgl/plugin/ClassTransformer.java @@ -23,11 +23,11 @@ public class ClassTransformer implements IClassTransformer { InsnList list = new InsnList(); list.add(new VarInsnNode(Opcodes.LLOAD, 0)); - list.add(new TypeInsnNode(Opcodes.NEW, "cc/polyfrost/oneconfig/lwjgl/Lwjgl2FunctionProvider")); + list.add(new TypeInsnNode(Opcodes.NEW, "cc/polyfrost/oneconfig/lwjgl/plugin/Lwjgl2FunctionProvider")); list.add(new InsnNode(Opcodes.DUP)); list.add(new MethodInsnNode( Opcodes.INVOKESPECIAL, - "cc/polyfrost/oneconfig/lwjgl/Lwjgl2FunctionProvider", + "cc/polyfrost/oneconfig/lwjgl/plugin/Lwjgl2FunctionProvider", "", "()V", false diff --git a/src/main/java/cc/polyfrost/oneconfig/lwjgl/plugin/Lwjgl2FunctionProvider.java b/src/main/java/cc/polyfrost/oneconfig/lwjgl/plugin/Lwjgl2FunctionProvider.java new file mode 100644 index 0000000..4cef17c --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/lwjgl/plugin/Lwjgl2FunctionProvider.java @@ -0,0 +1,39 @@ +package cc.polyfrost.oneconfig.lwjgl.plugin; + +import org.lwjgl.opengl.GLContext; +import org.lwjgl.system.FunctionProvider; + +import java.lang.reflect.Method; +import java.nio.ByteBuffer; + +/** + * Taken from LWJGLTwoPointFive under The Unlicense + * https://github.com/DJtheRedstoner/LWJGLTwoPointFive/blob/master/LICENSE/ + */ +public class Lwjgl2FunctionProvider implements FunctionProvider { + + private final Method m_getFunctionAddress; + + public Lwjgl2FunctionProvider() { + try { + m_getFunctionAddress = GLContext.class.getDeclaredMethod("getFunctionAddress", String.class); + m_getFunctionAddress.setAccessible(true); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public long getFunctionAddress(CharSequence functionName) { + try { + return (long) m_getFunctionAddress.invoke(null, functionName.toString()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public long getFunctionAddress(ByteBuffer byteBuffer) { + throw new UnsupportedOperationException(); + } +} \ No newline at end of file diff --git a/src/main/java/cc/polyfrost/oneconfig/lwjgl/scissor/Scissor.java b/src/main/java/cc/polyfrost/oneconfig/lwjgl/scissor/Scissor.java new file mode 100644 index 0000000..9523865 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/lwjgl/scissor/Scissor.java @@ -0,0 +1,22 @@ +package cc.polyfrost.oneconfig.lwjgl.scissor; + +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/lwjgl/scissor/ScissorManager.java b/src/main/java/cc/polyfrost/oneconfig/lwjgl/scissor/ScissorManager.java new file mode 100644 index 0000000..a3f3780 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/lwjgl/scissor/ScissorManager.java @@ -0,0 +1,41 @@ +package cc.polyfrost.oneconfig.lwjgl.scissor; + +import cc.polyfrost.oneconfig.lwjgl.scissor.Scissor; +import org.lwjgl.nanovg.NanoVG; + +import java.util.ArrayList; + +public class ScissorManager { + private static final ArrayList scissors = new ArrayList<>(); + + 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; + } + + public static void resetScissor(long vg, Scissor scissor) { + if (scissors.contains(scissor)) { + scissors.remove(scissor); + applyScissors(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); + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/IOUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/IOUtils.java new file mode 100644 index 0000000..6264451 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/IOUtils.java @@ -0,0 +1,54 @@ +package cc.polyfrost.oneconfig.utils; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.nio.Buffer; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.file.Files; + +public final class IOUtils { + + private IOUtils() { + } + + /** + * Taken from legui under MIT License + * https://github.com/SpinyOwl/legui/blob/develop/LICENSE + */ + public static ByteBuffer resourceToByteBuffer(String path) throws IOException { + byte[] bytes; + path = path.trim(); + if (path.startsWith("http")) { + bytes = org.apache.commons.io.IOUtils.toByteArray(new URL(path)); + } else { + InputStream stream; + File file = new File(path); + if (file.exists() && file.isFile()) { + stream = Files.newInputStream(file.toPath()); + } else { + stream = IOUtils.class.getResourceAsStream(path); + } + if (stream == null) { + throw new FileNotFoundException(path); + } + bytes = org.apache.commons.io.IOUtils.toByteArray(stream); + } + ByteBuffer data = ByteBuffer.allocateDirect(bytes.length).order(ByteOrder.nativeOrder()) + .put(bytes); + ((Buffer) data).flip(); + return data; + } + + public static ByteBuffer resourceToByteBufferNullable(String path) { + try { + return resourceToByteBuffer(path); + } catch (Exception ignored) { + return null; + } + } + +} \ No newline at end of file -- cgit