From 30df3e578cef533cacedf737449bbd35d4ea5d11 Mon Sep 17 00:00:00 2001 From: Wyvest <45589059+Wyvest@users.noreply.github.com> Date: Fri, 15 Apr 2022 18:20:30 +0900 Subject: fix shadow and add nanovg support --- .../oneconfig/command/OneConfigCommand.java | 29 ++---- .../java/io/polyfrost/oneconfig/lwjgl/IOUtil.java | 65 ++++++++++++++ .../oneconfig/lwjgl/Lwjgl2FunctionProvider.java | 39 ++++++++ .../io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java | 99 +++++++++++++++++++++ .../oneconfig/lwjgl/plugin/ClassTransformer.java | 55 ++++++++++++ .../oneconfig/lwjgl/plugin/LoadingPlugin.java | 52 +++++++++++ .../io/polyfrost/oneconfig/test/TestNanoVGGui.java | 19 ++++ .../io/polyfrost/oneconfig/utils/TickDelay.java | 2 +- .../assets/oneconfig/font/Roboto-Regular.ttf | Bin 0 -> 145348 bytes 9 files changed, 338 insertions(+), 22 deletions(-) create mode 100644 src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java create mode 100644 src/main/java/io/polyfrost/oneconfig/lwjgl/Lwjgl2FunctionProvider.java create mode 100644 src/main/java/io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java create mode 100644 src/main/java/io/polyfrost/oneconfig/lwjgl/plugin/ClassTransformer.java create mode 100644 src/main/java/io/polyfrost/oneconfig/lwjgl/plugin/LoadingPlugin.java create mode 100644 src/main/java/io/polyfrost/oneconfig/test/TestNanoVGGui.java create mode 100644 src/main/resources/assets/oneconfig/font/Roboto-Regular.ttf (limited to 'src/main') diff --git a/src/main/java/io/polyfrost/oneconfig/command/OneConfigCommand.java b/src/main/java/io/polyfrost/oneconfig/command/OneConfigCommand.java index df727a0..c2e6447 100644 --- a/src/main/java/io/polyfrost/oneconfig/command/OneConfigCommand.java +++ b/src/main/java/io/polyfrost/oneconfig/command/OneConfigCommand.java @@ -2,20 +2,19 @@ package io.polyfrost.oneconfig.command; import io.polyfrost.oneconfig.gui.Window; import io.polyfrost.oneconfig.hud.gui.HudGui; +import io.polyfrost.oneconfig.test.TestNanoVGGui; import io.polyfrost.oneconfig.themes.Themes; import io.polyfrost.oneconfig.utils.TickDelay; import net.minecraft.client.Minecraft; -import net.minecraft.command.ICommand; +import net.minecraft.command.CommandBase; import net.minecraft.command.ICommandSender; -import net.minecraft.util.BlockPos; import net.minecraft.util.ChatComponentText; -import org.jetbrains.annotations.NotNull; import java.io.File; import java.util.ArrayList; import java.util.List; -public class OneConfigCommand implements ICommand { +public class OneConfigCommand extends CommandBase { private static final Minecraft mc = Minecraft.getMinecraft(); @@ -49,27 +48,15 @@ public class OneConfigCommand implements ICommand { mc.thePlayer.addChatMessage(new ChatComponentText("reloading theme!")); Themes.openTheme(new File("OneConfig/themes/one.zip").getAbsoluteFile()); break; + case "lwjgl": + new TickDelay(() -> mc.displayGuiScreen(new TestNanoVGGui()), 1); + break; } } } @Override - public boolean canCommandSenderUseCommand(ICommandSender sender) { - return true; - } - - @Override - public List addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos) { - return null; - } - - @Override - public boolean isUsernameIndex(String[] args, int index) { - return false; - } - - @Override - public int compareTo(@NotNull ICommand o) { - return 0; + public int getRequiredPermissionLevel() { + return -1; } } diff --git a/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java b/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java new file mode 100644 index 0000000..d0f54f7 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java @@ -0,0 +1,65 @@ +package io.polyfrost.oneconfig.lwjgl; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.Buffer; +import java.nio.ByteBuffer; +import java.nio.channels.Channels; +import java.nio.channels.ReadableByteChannel; +import java.nio.channels.SeekableByteChannel; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import static org.lwjgl.BufferUtils.createByteBuffer; +import static org.lwjgl.system.MemoryUtil.memSlice; + +final class IOUtil { + + private IOUtil() { + } + + private static ByteBuffer resizeBuffer(ByteBuffer buffer, int newCapacity) { + ByteBuffer newBuffer = createByteBuffer(newCapacity); + buffer.flip(); + newBuffer.put(buffer); + return newBuffer; + } + + static ByteBuffer resourceToByteBuffer(String resource, int bufferSize) throws IOException { + ByteBuffer buffer; + + Path path = Paths.get(resource); + if (Files.isReadable(path)) { + try (SeekableByteChannel fc = Files.newByteChannel(path)) { + buffer = createByteBuffer((int)fc.size() + 1); + while (fc.read(buffer) != -1) { + //noinspection UnnecessarySemicolon + ; + } + } + } else { + try ( + InputStream source = IOUtil.class.getResourceAsStream(resource); + ReadableByteChannel rbc = Channels.newChannel(source) + ) { + buffer = createByteBuffer(bufferSize); + + while (true) { + int bytes = rbc.read(buffer); + if (bytes == -1) { + break; + } + if (buffer.remaining() == 0) { + buffer = resizeBuffer(buffer, buffer.capacity() * 3 / 2); // 50% + } + } + } + } + + //noinspection RedundantCast + ((Buffer) buffer).flip(); + return memSlice(buffer); + } + +} \ No newline at end of file diff --git a/src/main/java/io/polyfrost/oneconfig/lwjgl/Lwjgl2FunctionProvider.java b/src/main/java/io/polyfrost/oneconfig/lwjgl/Lwjgl2FunctionProvider.java new file mode 100644 index 0000000..ea1a44b --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/lwjgl/Lwjgl2FunctionProvider.java @@ -0,0 +1,39 @@ +package io.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/io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java b/src/main/java/io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java new file mode 100644 index 0000000..c9d0615 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java @@ -0,0 +1,99 @@ +package io.polyfrost.oneconfig.lwjgl; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.client.shader.Framebuffer; +import org.lwjgl.nanovg.NVGColor; +import org.lwjgl.opengl.Display; +import org.lwjgl.opengl.GL11; +import org.lwjgl.system.MemoryStack; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.function.LongConsumer; + +import static org.lwjgl.nanovg.NanoVG.*; +import static org.lwjgl.nanovg.NanoVGGL2.*; + +public final class NanoVGUtils { + private NanoVGUtils() { + + } + private static long vg = -1; + private static int font = -1; + + public static void setupAndDraw(LongConsumer consumer) { + if (vg == -1) { + vg = nvgCreate(NVG_ANTIALIAS); + if (vg == -1) { + throw new RuntimeException("Failed to create nvg context"); + } + } + if (font == -1) { + try { + font = nvgCreateFontMem(vg, "custom-font", IOUtil.resourceToByteBuffer("/assets/oneconfig/font/Roboto-Regular.ttf", 150 * 1024), 0); + } catch (IOException e) { + e.printStackTrace(); + } + if (font == -1) { + throw new RuntimeException("Failed to create custom font"); + } + } + + Framebuffer fb = Minecraft.getMinecraft().getFramebuffer(); + if (!fb.isStencilEnabled()) { + fb.enableStencil(); + } + GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS); + + nvgBeginFrame(vg, Display.getWidth(), Display.getHeight(), 1); + + consumer.accept(vg); + + nvgEndFrame(vg); + + GlStateManager.popAttrib(); + } + + public static void drawRect(long vg, float x, float y, float width, float height, int color) { + nvgBeginPath(vg); + nvgRect(vg, x, y, width, height); + color(vg, color); + nvgFill(vg); + } + + public static void drawRoundedRect(long vg, float x, float y, float width, float height, int color, float radius) { + nvgBeginPath(vg); + nvgRoundedRect(vg, x, y, width, height, radius); + color(vg, color); + nvgFill(vg); + } + + public static void drawCircle(long vg, float x, float y, float radius, int color) { + nvgBeginPath(vg); + nvgCircle(vg, x, y, radius); + color(vg, color); + nvgFill(vg); + } + + public static void drawString(long vg, String text, float x, float y, int color, float size) { + nvgBeginPath(vg); + nvgFontSize(vg, size); + nvgFontFace(vg, "custom-font"); + nvgTextAlign(vg, NVG_ALIGN_CENTER | NVG_ALIGN_MIDDLE); + try (MemoryStack stack = MemoryStack.stackPush()) { + ByteBuffer textByte = stack.ASCII(text, false); + nvgFontBlur(vg, 0); + color(vg, color); + nvgText(vg, x, y, textByte); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void color(long vg, int color) { + NVGColor nvgColor = NVGColor.create(); + nvgRGBA((byte) (color >> 16 & 0xFF), (byte) (color >> 8 & 0xFF), (byte) (color & 0xFF), (byte) (color >> 24 & 0xFF), nvgColor); + nvgFillColor(vg, nvgColor); + } +} diff --git a/src/main/java/io/polyfrost/oneconfig/lwjgl/plugin/ClassTransformer.java b/src/main/java/io/polyfrost/oneconfig/lwjgl/plugin/ClassTransformer.java new file mode 100644 index 0000000..066677b --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/lwjgl/plugin/ClassTransformer.java @@ -0,0 +1,55 @@ +package io.polyfrost.oneconfig.lwjgl.plugin; + +import net.minecraft.launchwrapper.IClassTransformer; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.tree.*; + +/** + * Taken from LWJGLTwoPointFive under The Unlicense + * https://github.com/DJtheRedstoner/LWJGLTwoPointFive/blob/master/LICENSE/ + */ +public class ClassTransformer implements IClassTransformer { + @Override + public byte[] transform(String name, String transformedName, byte[] basicClass) { + if (name.equals("org.lwjgl.nanovg.NanoVGGLConfig")) { + ClassReader reader = new ClassReader(basicClass); + ClassNode node = new ClassNode(); + reader.accept(node, ClassReader.EXPAND_FRAMES); + + for (MethodNode method : node.methods) { + if (method.name.equals("configGL")) { + InsnList list = new InsnList(); + + list.add(new VarInsnNode(Opcodes.LLOAD, 0)); + list.add(new TypeInsnNode(Opcodes.NEW, "io/polyfrost/oneconfig/lwjgl/Lwjgl2FunctionProvider")); + list.add(new InsnNode(Opcodes.DUP)); + list.add(new MethodInsnNode( + Opcodes.INVOKESPECIAL, + "io/polyfrost/oneconfig/lwjgl/Lwjgl2FunctionProvider", + "", + "()V", + false + )); + list.add(new MethodInsnNode( + Opcodes.INVOKESTATIC, + "org/lwjgl/nanovg/NanoVGGLConfig", + "config", + "(JLorg/lwjgl/system/FunctionProvider;)V", + false + )); + list.add(new InsnNode(Opcodes.RETURN)); + + method.instructions.clear(); + method.instructions.insert(list); + } + } + + ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); + node.accept(cw); + return cw.toByteArray(); + } + return basicClass; + } +} \ No newline at end of file diff --git a/src/main/java/io/polyfrost/oneconfig/lwjgl/plugin/LoadingPlugin.java b/src/main/java/io/polyfrost/oneconfig/lwjgl/plugin/LoadingPlugin.java new file mode 100644 index 0000000..1f48135 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/lwjgl/plugin/LoadingPlugin.java @@ -0,0 +1,52 @@ +package io.polyfrost.oneconfig.lwjgl.plugin; + +import net.minecraft.launchwrapper.Launch; +import net.minecraft.launchwrapper.LaunchClassLoader; +import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin; + +import java.lang.reflect.Field; +import java.util.Map; +import java.util.Set; + +/** + * Taken from LWJGLTwoPointFive under The Unlicense + * https://github.com/DJtheRedstoner/LWJGLTwoPointFive/blob/master/LICENSE/ + */ +public class LoadingPlugin implements IFMLLoadingPlugin { + + public LoadingPlugin() { + try { + Field f_exceptions = LaunchClassLoader.class.getDeclaredField("classLoaderExceptions"); + f_exceptions.setAccessible(true); + Set exceptions = (Set) f_exceptions.get(Launch.classLoader); + exceptions.remove("org.lwjgl."); + } catch (Exception e) { + throw new RuntimeException("e"); + } + } + + @Override + public String[] getASMTransformerClass() { + return new String[]{"io.polyfrost.oneconfig.lwjgl.plugin.ClassTransformer"}; + } + + @Override + public String getModContainerClass() { + return null; + } + + @Override + public String getSetupClass() { + return null; + } + + @Override + public void injectData(Map data) { + + } + + @Override + public String getAccessTransformerClass() { + return null; + } +} \ No newline at end of file diff --git a/src/main/java/io/polyfrost/oneconfig/test/TestNanoVGGui.java b/src/main/java/io/polyfrost/oneconfig/test/TestNanoVGGui.java new file mode 100644 index 0000000..74d6a1e --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/test/TestNanoVGGui.java @@ -0,0 +1,19 @@ +package io.polyfrost.oneconfig.test; + +import io.polyfrost.oneconfig.lwjgl.NanoVGUtils; +import net.minecraft.client.gui.GuiScreen; + +import java.awt.*; + +public class TestNanoVGGui extends GuiScreen { + + @Override + public void drawScreen(int mouseX, int mouseY, float partialTicks) { + super.drawScreen(mouseX, mouseY, partialTicks); + NanoVGUtils.setupAndDraw((vg) -> { + NanoVGUtils.drawRect(vg, 0, 0, 300, 300, Color.BLUE.getRGB()); + NanoVGUtils.drawRoundedRect(vg, 305, 305, 100, 100, Color.BLACK.getRGB(), 8); + NanoVGUtils.drawString(vg, "Hello!", 500, 500, Color.BLACK.getRGB(), 50); + }); + } +} diff --git a/src/main/java/io/polyfrost/oneconfig/utils/TickDelay.java b/src/main/java/io/polyfrost/oneconfig/utils/TickDelay.java index 33b02fe..b28a20a 100644 --- a/src/main/java/io/polyfrost/oneconfig/utils/TickDelay.java +++ b/src/main/java/io/polyfrost/oneconfig/utils/TickDelay.java @@ -6,7 +6,7 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.TickEvent; public class TickDelay { - Integer delay; + int delay; Runnable function; public TickDelay(Runnable functionName, int ticks) { diff --git a/src/main/resources/assets/oneconfig/font/Roboto-Regular.ttf b/src/main/resources/assets/oneconfig/font/Roboto-Regular.ttf new file mode 100644 index 0000000..3e6e2e7 Binary files /dev/null and b/src/main/resources/assets/oneconfig/font/Roboto-Regular.ttf differ -- cgit From fff43b4d2a89ae50aa1d315fc4dad65055e654be Mon Sep 17 00:00:00 2001 From: Wyvest <45589059+Wyvest@users.noreply.github.com> Date: Sat, 16 Apr 2022 01:55:58 +0900 Subject: misc things --- .../java/io/polyfrost/oneconfig/lwjgl/IOUtil.java | 4 +- .../io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java | 20 ++--- .../io/polyfrost/oneconfig/test/TestNanoVGGui.java | 5 +- .../resources/assets/oneconfig/font/Inter-Bold.ttf | Bin 0 -> 316100 bytes .../assets/oneconfig/font/OFL-INTER-BOLD.txt | 93 +++++++++++++++++++++ .../assets/oneconfig/font/Roboto-Regular.ttf | Bin 145348 -> 0 bytes 6 files changed, 105 insertions(+), 17 deletions(-) create mode 100644 src/main/resources/assets/oneconfig/font/Inter-Bold.ttf create mode 100644 src/main/resources/assets/oneconfig/font/OFL-INTER-BOLD.txt delete mode 100644 src/main/resources/assets/oneconfig/font/Roboto-Regular.ttf (limited to 'src/main') diff --git a/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java b/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java index d0f54f7..eb830df 100644 --- a/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java +++ b/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java @@ -14,6 +14,7 @@ import java.nio.file.Paths; import static org.lwjgl.BufferUtils.createByteBuffer; import static org.lwjgl.system.MemoryUtil.memSlice; +@SuppressWarnings("RedundantCast") final class IOUtil { private IOUtil() { @@ -21,7 +22,7 @@ final class IOUtil { private static ByteBuffer resizeBuffer(ByteBuffer buffer, int newCapacity) { ByteBuffer newBuffer = createByteBuffer(newCapacity); - buffer.flip(); + ((Buffer) buffer).flip(); newBuffer.put(buffer); return newBuffer; } @@ -57,7 +58,6 @@ final class IOUtil { } } - //noinspection RedundantCast ((Buffer) buffer).flip(); return memSlice(buffer); } diff --git a/src/main/java/io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java b/src/main/java/io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java index c9d0615..f8dcafb 100644 --- a/src/main/java/io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java +++ b/src/main/java/io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java @@ -6,14 +6,13 @@ import net.minecraft.client.shader.Framebuffer; import org.lwjgl.nanovg.NVGColor; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.GL11; -import org.lwjgl.system.MemoryStack; import java.io.IOException; -import java.nio.ByteBuffer; import java.util.function.LongConsumer; import static org.lwjgl.nanovg.NanoVG.*; -import static org.lwjgl.nanovg.NanoVGGL2.*; +import static org.lwjgl.nanovg.NanoVGGL2.NVG_ANTIALIAS; +import static org.lwjgl.nanovg.NanoVGGL2.nvgCreate; public final class NanoVGUtils { private NanoVGUtils() { @@ -31,7 +30,7 @@ public final class NanoVGUtils { } if (font == -1) { try { - font = nvgCreateFontMem(vg, "custom-font", IOUtil.resourceToByteBuffer("/assets/oneconfig/font/Roboto-Regular.ttf", 150 * 1024), 0); + font = nvgCreateFontMem(vg, "custom-font", IOUtil.resourceToByteBuffer("/assets/oneconfig/font/Inter-Bold.ttf", 200 * 1024), 0); } catch (IOException e) { e.printStackTrace(); } @@ -80,15 +79,10 @@ public final class NanoVGUtils { nvgBeginPath(vg); nvgFontSize(vg, size); nvgFontFace(vg, "custom-font"); - nvgTextAlign(vg, NVG_ALIGN_CENTER | NVG_ALIGN_MIDDLE); - try (MemoryStack stack = MemoryStack.stackPush()) { - ByteBuffer textByte = stack.ASCII(text, false); - nvgFontBlur(vg, 0); - color(vg, color); - nvgText(vg, x, y, textByte); - } catch (Exception e) { - e.printStackTrace(); - } + nvgTextAlign(vg, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE); + color(vg, color); + nvgText(vg, x, y, text); + nvgFill(vg); } public static void color(long vg, int color) { diff --git a/src/main/java/io/polyfrost/oneconfig/test/TestNanoVGGui.java b/src/main/java/io/polyfrost/oneconfig/test/TestNanoVGGui.java index 74d6a1e..67e0a68 100644 --- a/src/main/java/io/polyfrost/oneconfig/test/TestNanoVGGui.java +++ b/src/main/java/io/polyfrost/oneconfig/test/TestNanoVGGui.java @@ -10,10 +10,11 @@ public class TestNanoVGGui extends GuiScreen { @Override public void drawScreen(int mouseX, int mouseY, float partialTicks) { super.drawScreen(mouseX, mouseY, partialTicks); + drawRect(0, 0, width, height, Color.BLACK.getRGB()); NanoVGUtils.setupAndDraw((vg) -> { NanoVGUtils.drawRect(vg, 0, 0, 300, 300, Color.BLUE.getRGB()); - NanoVGUtils.drawRoundedRect(vg, 305, 305, 100, 100, Color.BLACK.getRGB(), 8); - NanoVGUtils.drawString(vg, "Hello!", 500, 500, Color.BLACK.getRGB(), 50); + NanoVGUtils.drawRoundedRect(vg, 305, 305, 100, 100, Color.YELLOW.getRGB(), 8); + NanoVGUtils.drawString(vg, "Hello!", 500, 500, Color.WHITE.getRGB(), 50); }); } } diff --git a/src/main/resources/assets/oneconfig/font/Inter-Bold.ttf b/src/main/resources/assets/oneconfig/font/Inter-Bold.ttf new file mode 100644 index 0000000..76a215c Binary files /dev/null and b/src/main/resources/assets/oneconfig/font/Inter-Bold.ttf differ diff --git a/src/main/resources/assets/oneconfig/font/OFL-INTER-BOLD.txt b/src/main/resources/assets/oneconfig/font/OFL-INTER-BOLD.txt new file mode 100644 index 0000000..b525cbf --- /dev/null +++ b/src/main/resources/assets/oneconfig/font/OFL-INTER-BOLD.txt @@ -0,0 +1,93 @@ +Copyright 2020 The Inter Project Authors (https://github.com/rsms/inter) + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/src/main/resources/assets/oneconfig/font/Roboto-Regular.ttf b/src/main/resources/assets/oneconfig/font/Roboto-Regular.ttf deleted file mode 100644 index 3e6e2e7..0000000 Binary files a/src/main/resources/assets/oneconfig/font/Roboto-Regular.ttf and /dev/null differ -- cgit From f10f1165a7c2ea88ce7bb265d51b52eeaa64d8f8 Mon Sep 17 00:00:00 2001 From: Wyvest <45589059+Wyvest@users.noreply.github.com> Date: Sat, 16 Apr 2022 12:33:00 +0900 Subject: nanovg optimizations + image renderer --- .../java/io/polyfrost/oneconfig/lwjgl/IOUtil.java | 86 ++++++++++------------ .../io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java | 39 ++++++++-- .../io/polyfrost/oneconfig/lwjgl/image/Image.java | 20 +++++ .../oneconfig/lwjgl/image/ImageLoader.java | 39 ++++++++++ .../io/polyfrost/oneconfig/test/TestNanoVGGui.java | 1 + 5 files changed, 130 insertions(+), 55 deletions(-) create mode 100644 src/main/java/io/polyfrost/oneconfig/lwjgl/image/Image.java create mode 100644 src/main/java/io/polyfrost/oneconfig/lwjgl/image/ImageLoader.java (limited to 'src/main') diff --git a/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java b/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java index eb830df..f5105fb 100644 --- a/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java +++ b/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java @@ -1,65 +1,53 @@ package io.polyfrost.oneconfig.lwjgl; -import java.io.IOException; -import java.io.InputStream; +import org.apache.commons.io.IOUtils; + +import java.io.*; +import java.net.URL; import java.nio.Buffer; import java.nio.ByteBuffer; -import java.nio.channels.Channels; -import java.nio.channels.ReadableByteChannel; -import java.nio.channels.SeekableByteChannel; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; - -import static org.lwjgl.BufferUtils.createByteBuffer; -import static org.lwjgl.system.MemoryUtil.memSlice; +import java.nio.ByteOrder; -@SuppressWarnings("RedundantCast") -final class IOUtil { +public final class IOUtil { private IOUtil() { } - private static ByteBuffer resizeBuffer(ByteBuffer buffer, int newCapacity) { - ByteBuffer newBuffer = createByteBuffer(newCapacity); - ((Buffer) buffer).flip(); - newBuffer.put(buffer); - return newBuffer; - } - - static ByteBuffer resourceToByteBuffer(String resource, int bufferSize) throws IOException { - ByteBuffer buffer; - - Path path = Paths.get(resource); - if (Files.isReadable(path)) { - try (SeekableByteChannel fc = Files.newByteChannel(path)) { - buffer = createByteBuffer((int)fc.size() + 1); - while (fc.read(buffer) != -1) { - //noinspection UnnecessarySemicolon - ; - } - } + /** + * Taken from legui under MIT License + * https://github.com/SpinyOwl/legui/blob/develop/LICENSE + */ + @SuppressWarnings("RedundantCast") + public static ByteBuffer resourceToByteBuffer(String path) throws IOException { + byte[] bytes; + path = path.trim(); + if (path.startsWith("http")) { + bytes = IOUtils.toByteArray(new URL(path)); } else { - try ( - InputStream source = IOUtil.class.getResourceAsStream(resource); - ReadableByteChannel rbc = Channels.newChannel(source) - ) { - buffer = createByteBuffer(bufferSize); - - while (true) { - int bytes = rbc.read(buffer); - if (bytes == -1) { - break; - } - if (buffer.remaining() == 0) { - buffer = resizeBuffer(buffer, buffer.capacity() * 3 / 2); // 50% - } - } + InputStream stream; + File file = new File(path); + if (file.exists() && file.isFile()) { + stream = new FileInputStream(file); + } 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; + } - ((Buffer) buffer).flip(); - return memSlice(buffer); + 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/io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java b/src/main/java/io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java index f8dcafb..fc556fe 100644 --- a/src/main/java/io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java +++ b/src/main/java/io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java @@ -1,13 +1,18 @@ package io.polyfrost.oneconfig.lwjgl; +import io.polyfrost.oneconfig.lwjgl.image.Image; +import io.polyfrost.oneconfig.lwjgl.image.ImageLoader; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.shader.Framebuffer; import org.lwjgl.nanovg.NVGColor; +import org.lwjgl.nanovg.NVGPaint; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.GL11; import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; import java.util.function.LongConsumer; import static org.lwjgl.nanovg.NanoVG.*; @@ -20,6 +25,7 @@ public final class NanoVGUtils { } private static long vg = -1; private static int font = -1; + private static final ArrayList fontBuffers = new ArrayList<>(); public static void setupAndDraw(LongConsumer consumer) { if (vg == -1) { @@ -30,7 +36,9 @@ public final class NanoVGUtils { } if (font == -1) { try { - font = nvgCreateFontMem(vg, "custom-font", IOUtil.resourceToByteBuffer("/assets/oneconfig/font/Inter-Bold.ttf", 200 * 1024), 0); + ByteBuffer buffer = IOUtil.resourceToByteBuffer("/assets/oneconfig/font/Inter-Bold.ttf"); + font = nvgCreateFontMem(vg, "custom-font", buffer, 0); + fontBuffers.add(buffer); } catch (IOException e) { e.printStackTrace(); } @@ -57,22 +65,26 @@ public final class NanoVGUtils { public static void drawRect(long vg, float x, float y, float width, float height, int color) { nvgBeginPath(vg); nvgRect(vg, x, y, width, height); - color(vg, color); + NVGColor nvgColor = color(vg, color); nvgFill(vg); + nvgColor.free(); } public static void drawRoundedRect(long vg, float x, float y, float width, float height, int color, float radius) { nvgBeginPath(vg); nvgRoundedRect(vg, x, y, width, height, radius); color(vg, color); + NVGColor nvgColor = color(vg, color); nvgFill(vg); + nvgColor.free(); } public static void drawCircle(long vg, float x, float y, float radius, int color) { nvgBeginPath(vg); nvgCircle(vg, x, y, radius); - color(vg, color); + NVGColor nvgColor = color(vg, color); nvgFill(vg); + nvgColor.free(); } public static void drawString(long vg, String text, float x, float y, int color, float size) { @@ -80,14 +92,29 @@ public final class NanoVGUtils { nvgFontSize(vg, size); nvgFontFace(vg, "custom-font"); nvgTextAlign(vg, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE); - color(vg, color); + NVGColor nvgColor = color(vg, color); nvgText(vg, x, y, text); nvgFill(vg); + nvgColor.free(); + } + + public static void drawImage(long vg, String fileName, float x, float y, float width, float height) { + if (ImageLoader.INSTANCE.loadImage(vg, fileName)) { + NVGPaint imagePaint = NVGPaint.calloc(); + Image image = ImageLoader.INSTANCE.getImage(fileName); + nvgBeginPath(vg); + nvgImagePattern(vg, x, y, width, height, 0, image.getReference(), 1, imagePaint); + nvgRect(vg, x, y, width, height); + nvgFillPaint(vg, imagePaint); + nvgFill(vg); + imagePaint.free(); + } } - public static void color(long vg, int color) { - NVGColor nvgColor = NVGColor.create(); + public static NVGColor color(long vg, int color) { + NVGColor nvgColor = NVGColor.calloc(); nvgRGBA((byte) (color >> 16 & 0xFF), (byte) (color >> 8 & 0xFF), (byte) (color & 0xFF), (byte) (color >> 24 & 0xFF), nvgColor); nvgFillColor(vg, nvgColor); + return nvgColor; } } diff --git a/src/main/java/io/polyfrost/oneconfig/lwjgl/image/Image.java b/src/main/java/io/polyfrost/oneconfig/lwjgl/image/Image.java new file mode 100644 index 0000000..2ed7276 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/lwjgl/image/Image.java @@ -0,0 +1,20 @@ +package io.polyfrost.oneconfig.lwjgl.image; + +import java.nio.ByteBuffer; + +public class Image { + private final int reference; + private final ByteBuffer buffer; + public Image(int reference, ByteBuffer buffer) { + this.reference = reference; + this.buffer = buffer; + } + + public ByteBuffer getBuffer() { + return buffer; + } + + public int getReference() { + return reference; + } +} diff --git a/src/main/java/io/polyfrost/oneconfig/lwjgl/image/ImageLoader.java b/src/main/java/io/polyfrost/oneconfig/lwjgl/image/ImageLoader.java new file mode 100644 index 0000000..b0cb6d4 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/lwjgl/image/ImageLoader.java @@ -0,0 +1,39 @@ +package io.polyfrost.oneconfig.lwjgl.image; + +import io.polyfrost.oneconfig.lwjgl.IOUtil; +import org.lwjgl.nanovg.NanoVG; +import org.lwjgl.stb.STBImage; + +import java.nio.ByteBuffer; +import java.util.HashMap; + +public class ImageLoader { + private final HashMap imageHashMap = new HashMap<>(); + public static ImageLoader INSTANCE = new ImageLoader(); + + public boolean loadImage(long vg, String fileName) { + if (!imageHashMap.containsKey(fileName)) { + int[] width = {0}; + int[] height = {0}; + int[] channels = {0}; + + ByteBuffer image = IOUtil.resourceToByteBufferNullable(fileName); + if (image == null) { + return false; + } + + ByteBuffer buffer = STBImage.stbi_load_from_memory(image, width, height, channels, 4); + if (buffer == null) { + return false; + } + + imageHashMap.put(fileName, new Image(NanoVG.nvgCreateImageRGBA(vg, width[0], height[0], NanoVG.NVG_IMAGE_REPEATX | NanoVG.NVG_IMAGE_REPEATY | NanoVG.NVG_IMAGE_GENERATE_MIPMAPS, buffer), buffer)); + return true; + } + return true; + } + + public Image getImage(String fileName) { + return imageHashMap.get(fileName); + } +} diff --git a/src/main/java/io/polyfrost/oneconfig/test/TestNanoVGGui.java b/src/main/java/io/polyfrost/oneconfig/test/TestNanoVGGui.java index 67e0a68..3f2fa27 100644 --- a/src/main/java/io/polyfrost/oneconfig/test/TestNanoVGGui.java +++ b/src/main/java/io/polyfrost/oneconfig/test/TestNanoVGGui.java @@ -15,6 +15,7 @@ public class TestNanoVGGui extends GuiScreen { NanoVGUtils.drawRect(vg, 0, 0, 300, 300, Color.BLUE.getRGB()); NanoVGUtils.drawRoundedRect(vg, 305, 305, 100, 100, Color.YELLOW.getRGB(), 8); NanoVGUtils.drawString(vg, "Hello!", 500, 500, Color.WHITE.getRGB(), 50); + NanoVGUtils.drawImage(vg, "/assets/oneconfig/textures/hudsettings.png", 10, 10, 400, 400); }); } } -- cgit From cf164be0c8f43c3d1387c9c9f7ae73c2cf1b3a02 Mon Sep 17 00:00:00 2001 From: Wyvest <45589059+Wyvest@users.noreply.github.com> Date: Sat, 16 Apr 2022 14:51:58 +0900 Subject: merge rendering files + remove themes --- .../java/io/polyfrost/oneconfig/OneConfig.java | 2 - .../oneconfig/command/OneConfigCommand.java | 10 +- .../java/io/polyfrost/oneconfig/gui/Window.java | 111 ----- .../polyfrost/oneconfig/gui/elements/OCBlock.java | 270 ------------ .../polyfrost/oneconfig/gui/elements/OCButton.java | 105 ----- .../oneconfig/gui/elements/OCColorPicker.java | 4 - .../oneconfig/gui/elements/OCSelector.java | 4 - .../oneconfig/gui/elements/OCStoreBlock.java | 35 -- .../oneconfig/gui/elements/OCTextField.java | 4 - .../oneconfig/gui/pages/BasicWindowPage.java | 4 - .../io/polyfrost/oneconfig/gui/pages/HomePage.java | 4 - .../io/polyfrost/oneconfig/gui/pages/ModsPage.java | 4 - .../oneconfig/gui/pages/SettingsPage.java | 4 - .../polyfrost/oneconfig/gui/pages/StorePage.java | 4 - .../java/io/polyfrost/oneconfig/hud/HudCore.java | 3 +- .../io/polyfrost/oneconfig/hud/gui/HudGui.java | 20 +- .../oneconfig/hud/interfaces/BasicHud.java | 4 +- .../oneconfig/hud/interfaces/TextHud.java | 4 +- .../io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java | 120 ------ .../polyfrost/oneconfig/lwjgl/RenderManager.java | 194 +++++++++ .../io/polyfrost/oneconfig/lwjgl/font/Font.java | 39 ++ .../oneconfig/lwjgl/font/FontManager.java | 34 ++ .../io/polyfrost/oneconfig/renderer/Renderer.java | 189 --------- .../polyfrost/oneconfig/renderer/TrueTypeFont.java | 451 --------------------- .../java/io/polyfrost/oneconfig/test/TestHud.java | 4 +- .../io/polyfrost/oneconfig/test/TestNanoVGGui.java | 15 +- .../java/io/polyfrost/oneconfig/themes/Theme.java | 310 -------------- .../java/io/polyfrost/oneconfig/themes/Themes.java | 53 --- .../oneconfig/themes/textures/TextureManager.java | 113 ------ .../oneconfig/themes/textures/ThemeElement.java | 48 --- .../oneconfig/themes/textures/TickableTexture.java | 90 ---- .../io/polyfrost/oneconfig/utils/MathUtils.java | 15 + .../assets/oneconfig/font/Minecraft-Regular.otf | Bin 0 -> 11016 bytes 33 files changed, 309 insertions(+), 1962 deletions(-) delete mode 100644 src/main/java/io/polyfrost/oneconfig/gui/Window.java delete mode 100644 src/main/java/io/polyfrost/oneconfig/gui/elements/OCBlock.java delete mode 100644 src/main/java/io/polyfrost/oneconfig/gui/elements/OCButton.java delete mode 100644 src/main/java/io/polyfrost/oneconfig/gui/elements/OCColorPicker.java delete mode 100644 src/main/java/io/polyfrost/oneconfig/gui/elements/OCSelector.java delete mode 100644 src/main/java/io/polyfrost/oneconfig/gui/elements/OCStoreBlock.java delete mode 100644 src/main/java/io/polyfrost/oneconfig/gui/elements/OCTextField.java delete mode 100644 src/main/java/io/polyfrost/oneconfig/gui/pages/BasicWindowPage.java delete mode 100644 src/main/java/io/polyfrost/oneconfig/gui/pages/HomePage.java delete mode 100644 src/main/java/io/polyfrost/oneconfig/gui/pages/ModsPage.java delete mode 100644 src/main/java/io/polyfrost/oneconfig/gui/pages/SettingsPage.java delete mode 100644 src/main/java/io/polyfrost/oneconfig/gui/pages/StorePage.java delete mode 100644 src/main/java/io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java create mode 100644 src/main/java/io/polyfrost/oneconfig/lwjgl/RenderManager.java create mode 100644 src/main/java/io/polyfrost/oneconfig/lwjgl/font/Font.java create mode 100644 src/main/java/io/polyfrost/oneconfig/lwjgl/font/FontManager.java delete mode 100644 src/main/java/io/polyfrost/oneconfig/renderer/Renderer.java delete mode 100644 src/main/java/io/polyfrost/oneconfig/renderer/TrueTypeFont.java delete mode 100644 src/main/java/io/polyfrost/oneconfig/themes/Theme.java delete mode 100644 src/main/java/io/polyfrost/oneconfig/themes/Themes.java delete mode 100644 src/main/java/io/polyfrost/oneconfig/themes/textures/TextureManager.java delete mode 100644 src/main/java/io/polyfrost/oneconfig/themes/textures/ThemeElement.java delete mode 100644 src/main/java/io/polyfrost/oneconfig/themes/textures/TickableTexture.java create mode 100644 src/main/java/io/polyfrost/oneconfig/utils/MathUtils.java create mode 100644 src/main/resources/assets/oneconfig/font/Minecraft-Regular.otf (limited to 'src/main') diff --git a/src/main/java/io/polyfrost/oneconfig/OneConfig.java b/src/main/java/io/polyfrost/oneconfig/OneConfig.java index 7f29c39..5d20af3 100644 --- a/src/main/java/io/polyfrost/oneconfig/OneConfig.java +++ b/src/main/java/io/polyfrost/oneconfig/OneConfig.java @@ -4,7 +4,6 @@ import io.polyfrost.oneconfig.command.OneConfigCommand; import io.polyfrost.oneconfig.config.OneConfigConfig; import io.polyfrost.oneconfig.hud.HudCore; import io.polyfrost.oneconfig.test.TestConfig; -import io.polyfrost.oneconfig.themes.Themes; import net.minecraft.client.Minecraft; import net.minecraftforge.client.ClientCommandHandler; import net.minecraftforge.common.MinecraftForge; @@ -37,6 +36,5 @@ public class OneConfig { ClientCommandHandler.instance.registerCommand(new OneConfigCommand()); MinecraftForge.EVENT_BUS.register(this); MinecraftForge.EVENT_BUS.register(new HudCore()); - Themes.openTheme(new File("OneConfig/themes/one.zip").getAbsoluteFile()); } } diff --git a/src/main/java/io/polyfrost/oneconfig/command/OneConfigCommand.java b/src/main/java/io/polyfrost/oneconfig/command/OneConfigCommand.java index c2e6447..b210146 100644 --- a/src/main/java/io/polyfrost/oneconfig/command/OneConfigCommand.java +++ b/src/main/java/io/polyfrost/oneconfig/command/OneConfigCommand.java @@ -1,16 +1,12 @@ package io.polyfrost.oneconfig.command; -import io.polyfrost.oneconfig.gui.Window; import io.polyfrost.oneconfig.hud.gui.HudGui; import io.polyfrost.oneconfig.test.TestNanoVGGui; -import io.polyfrost.oneconfig.themes.Themes; import io.polyfrost.oneconfig.utils.TickDelay; import net.minecraft.client.Minecraft; import net.minecraft.command.CommandBase; import net.minecraft.command.ICommandSender; -import net.minecraft.util.ChatComponentText; -import java.io.File; import java.util.ArrayList; import java.util.List; @@ -38,16 +34,12 @@ public class OneConfigCommand extends CommandBase { @Override public void processCommand(ICommandSender sender, String[] args) { - if (args.length == 0) new TickDelay(() -> mc.displayGuiScreen(new Window()), 1); + if (args.length == 0) ; //new TickDelay(() -> mc.displayGuiScreen(new Window()), 1); else { switch (args[0]) { case "hud": new TickDelay(() -> mc.displayGuiScreen(new HudGui()), 1); break; - case "theme": - mc.thePlayer.addChatMessage(new ChatComponentText("reloading theme!")); - Themes.openTheme(new File("OneConfig/themes/one.zip").getAbsoluteFile()); - break; case "lwjgl": new TickDelay(() -> mc.displayGuiScreen(new TestNanoVGGui()), 1); break; diff --git a/src/main/java/io/polyfrost/oneconfig/gui/Window.java b/src/main/java/io/polyfrost/oneconfig/gui/Window.java deleted file mode 100644 index fa672d5..0000000 --- a/src/main/java/io/polyfrost/oneconfig/gui/Window.java +++ /dev/null @@ -1,111 +0,0 @@ -package io.polyfrost.oneconfig.gui; - -import io.polyfrost.oneconfig.gui.elements.OCBlock; -import io.polyfrost.oneconfig.gui.elements.OCStoreBlock; -import io.polyfrost.oneconfig.themes.Theme; -import io.polyfrost.oneconfig.themes.Themes; -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.GuiScreen; -import net.minecraft.client.gui.ScaledResolution; -import net.minecraft.util.ResourceLocation; - -import java.awt.*; - -import static io.polyfrost.oneconfig.renderer.Renderer.clamp; -import static io.polyfrost.oneconfig.renderer.Renderer.easeOut; - -public class Window extends GuiScreen { - private float currentProgress = 0f; - public static Window currentWindow; - private final Theme t = Themes.getActiveTheme(); - private final int guiScaleToRestore; - long secondCounter = System.currentTimeMillis(); - long prevTime = System.currentTimeMillis(); - int frames = 0; - OCBlock block = new OCBlock(() -> { - t.getFont().drawString("hi", 10, 10, 1f, 1f, -1); - },750, 144); - ResourceLocation example = new ResourceLocation("oneconfig", "textures/hudsettings.png"); - OCStoreBlock storeBlock = new OCStoreBlock("OneConfig Theme", "OneConfig default theme with the default look you love.", example, new Color(27,27,27,255).getRGB()); - public static ScaledResolution resolution = new ScaledResolution(Minecraft.getMinecraft()); - - public Window() { - super.initGui(); - currentWindow = this; - guiScaleToRestore = Minecraft.getMinecraft().gameSettings.guiScale; - Minecraft.getMinecraft().gameSettings.guiScale = 1; - } - - public boolean doesGuiPauseGame() { - return false; - } - - public void drawScreen(int mouseX, int mouseY, float partialTicks) { - resolution = new ScaledResolution(Minecraft.getMinecraft()); - super.drawScreen(mouseX, mouseY, partialTicks); - currentProgress = clamp(easeOut(currentProgress, 1f)); - int alphaVal = (int) (50 * currentProgress); - drawGradientRect(0, 0, super.width, super.height, new Color(80, 80, 80, alphaVal).getRGB(), new Color(80, 80, 80, alphaVal + 10).getRGB()); - long secondDelta = System.currentTimeMillis() - secondCounter; - long deltaTime = System.currentTimeMillis() - prevTime; - //if(deltaTime >= 15) { - // prevTime = System.currentTimeMillis(); - // frames++; - // drawWindow(); - //} - if(secondDelta >= 1000) { - secondCounter = System.currentTimeMillis(); - //System.out.println(frames + "FPS"); - //Minecraft.getMinecraft().thePlayer.sendChatMessage(frames + "FPS"); - frames = 0; - } - drawWindow(); - } - - public void drawWindow() { - Color testingColor = new Color(127, 144, 155, 255); - //System.out.println(testingColor.getRGB()); - int middleX = this.width / 2; - int middleY = this.height / 2; - int left = middleX - 800; - int right = (int) (1600 * currentProgress); - int top = middleY - 512; - int bottom = (int) (1024 * currentProgress); - //Gui.drawRect(left - 1, top - 1, right + 1, bottom + 1, testingColor.getRGB()); - //new Color(16, 17, 19, 255).getRGB() - //t.getTextureManager().draw(ThemeElement.BACKGROUND, left, top, right, bottom); - //t.getTextureManager().draw(ThemeElement.BUTTON_OFF, left + 480, top + 40, 640, 48); - //t.getTextureManager().draw(ThemeElement.SEARCH, left + 504, top + 48, 32, 32); - //t.getFont().drawString("Search all of OneConfig", left + 548, top + 48, 1.1f, 1f, new Color(242,242,242,255).getRGB()); - - //t.getTextureManager().draw(ThemeElement.BUTTON_OFF, left + 1504, top + 32, 64, 64); - //t.getTextureManager().draw(ThemeElement.BUTTON_OFF, left + 1424, top + 32, 64, 64); - //t.getTextureManager().draw(ThemeElement.BUTTON_OFF, left + 1344, top + 32, 64, 64); - //block.draw(200, 300); - block.draw(100,200); - //t.getTextureManager().draw(ThemeElement.CLOSE, left + 1504, top + 32, 64, 64); - //t.getTextureManager().draw(ThemeElement.BUTTON_OFF, left + 100, top + 100, 296, 64); - //t.getTextureManager().draw(ThemeElement.CLOSE); - - //Renderer.drawRoundRect(left,top,right,bottom,30, testingColor.getRGB()); - //Renderer.drawRoundRect(left + 1,top + 1,right - 2,bottom - 2,30, t.getBaseColor().getRGB()); - //t.getTextureManager().draw(ThemeElement.LOGO, left + 24, top + 24, 64, 64); // 0.875 - //t.getBoldFont().drawString("OneConfig", left + 93f, top + 25, 1f,1f); - //Gui.drawRect(left, top, right, bottom, t.getBaseColor().getRGB()); - - //Gui.drawRect(left, top, right, top + 100, t.getTitleBarColor().getRGB()); - //Gui.drawRect(left, top + 100, right, top + 101, testingColor.getRGB()); - - - //font.drawString("OneConfig is pog!\nWow, this font renderer actually works :D", 50, 50, 1f, 1f); - } - - public static Window getWindow() { - return currentWindow; - } - - @Override - public void onGuiClosed() { - Minecraft.getMinecraft().gameSettings.guiScale = guiScaleToRestore; - } -} diff --git a/src/main/java/io/polyfrost/oneconfig/gui/elements/OCBlock.java b/src/main/java/io/polyfrost/oneconfig/gui/elements/OCBlock.java deleted file mode 100644 index abcd5ca..0000000 --- a/src/main/java/io/polyfrost/oneconfig/gui/elements/OCBlock.java +++ /dev/null @@ -1,270 +0,0 @@ -package io.polyfrost.oneconfig.gui.elements; - -import io.polyfrost.oneconfig.renderer.Renderer; -import io.polyfrost.oneconfig.themes.Theme; -import io.polyfrost.oneconfig.themes.Themes; -import io.polyfrost.oneconfig.themes.textures.ThemeElement; -import net.minecraft.client.Minecraft; -import net.minecraft.client.renderer.GlStateManager; -import org.jetbrains.annotations.NotNull; -import org.lwjgl.input.Keyboard; -import org.lwjgl.input.Mouse; - -import java.awt.*; - -import static io.polyfrost.oneconfig.gui.Window.resolution; - -/** - * Default simple block for all the GUI elements. If you are making custom ones, your class should extend this one. - */ -@SuppressWarnings("unused") -public class OCBlock { - public static final Theme theme = Themes.getActiveTheme(); - private static final Minecraft mc = Minecraft.getMinecraft(); - private final boolean bold; - private final Color elementColor = theme.getElementColor(); - private final Color hoverColor = theme.getHoverColor(); - private final Runnable draw; - /** - * Width of the element in pixels. - */ - public int width; - /** - * Height of the element in pixels. - */ - public int height; - private Color color; - private String text; - private ThemeElement element; - private boolean clicked = false; - private boolean rightClicked = false; - private int mouseX, mouseY; - private boolean hovered; - private float percentHoveredRed = 0f; - private float percentHoveredGreen = 0f; - private float percentHoveredBlue = 0f; - private float percentHoveredAlpha = 0f; - - /** - * Create a basic element. - */ - public OCBlock(int width, int height) { - this(null, false, theme.getElementColor().getRGB(), width, height); - } - - /** - * Create a new basic element. - * - * @param color color of the element - * @param width width of the element - * @param height height of the element - * @deprecated This method DOES NOT respect the theme colors for the element. Use of {@link #OCBlock(int, int)} is recommended instead. - */ - @Deprecated() - public OCBlock(int color, int width, int height) { - this(null, false, color, width, height); - } - - /** - * Create a new element with the specified text, and automatic width/height + padding. - * - * @param text text to use - * @param bold weather or not to use bold text - * @param color color for the text - */ - public OCBlock(@NotNull String text, boolean bold, int color) { - this(text, bold, color, theme.getFont().getWidth(text) + 6, theme.getFont().getHeight() + 4); - } - - /** - * Create a new element with the specified text, and custom width/height. - * - * @param text text to use - * @param bold weather or not to use bold text - * @param color color for the text (use {@link Theme#getTextColor()} or {@link Theme#getAccentTextColor()} for default colors) - */ - public OCBlock(String text, boolean bold, int color, int width, int height) { - this.draw = null; - this.text = text; - this.bold = bold; - this.color = Renderer.getColorFromInt(color); - this.width = width; - this.height = height; - } - - /** - * Create a new Element with the specified image. - * - * @param element element to use - * @param colorMask color mast to use (-1 for default) - */ - public OCBlock(ThemeElement element, int colorMask, int width, int height) { - this.draw = null; - this.element = element; - this.color = Renderer.getColorFromInt(colorMask); - this.width = width; - this.height = height; - this.bold = false; - } - - /** - * Create a new Element with a custom render script. The {@link Runnable} should ONLY contain #draw() calls or equivalent. - * - * @param whatToDraw a {@link Runnable}, containing draw scripts for elements. You will need to instantiate the objects first, if they are sub-elements. - */ - public OCBlock(Runnable whatToDraw, int width, int height) { - this.draw = whatToDraw; - this.bold = false; - this.width = width; - this.height = height; - } - - /** - * Draw the element at the specified coordinates. - */ - public void draw(int x, int y) { - GlStateManager.enableBlend(); - percentHoveredRed = smooth(percentHoveredRed, elementColor.getRed() / 255f, hoverColor.getRed() / 255f); - percentHoveredGreen = smooth(percentHoveredGreen, elementColor.getGreen() / 255f, hoverColor.getGreen() / 255f); - percentHoveredBlue = smooth(percentHoveredBlue, elementColor.getBlue() / 255f, hoverColor.getBlue() / 255f); - percentHoveredAlpha = smooth(percentHoveredAlpha, elementColor.getAlpha() / 255f, hoverColor.getAlpha() / 255f); - GlStateManager.color(percentHoveredRed, percentHoveredGreen, percentHoveredBlue, percentHoveredAlpha); - update(x, y); - if (draw != null) { - draw.run(); - } - if (element != null) { - theme.getTextureManager().draw(ThemeElement.BUTTON, x, y, width, height); - GlStateManager.color(color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f, color.getAlpha() / 255f); - theme.getTextureManager().draw(element, x, y, width, height); - } - if (text == null) { - theme.getTextureManager().draw(ThemeElement.BUTTON, x, y, width, height); - } else { - theme.getTextureManager().draw(ThemeElement.BUTTON, x, y, width, height); - if (bold) { - theme.getBoldFont().drawString(text, x + 3, y + 2, 1f, 1f, color.getRGB()); - } else { - theme.getFont().drawString(text, x + 3, y + 2, 1.1f, 1f, color.getRGB()); - } - } - GlStateManager.disableBlend(); - - - } - - /** - * Update this elements click, key and hover status. Call this method at the end of your 'draw' function, if overridden. - */ - public void update(int x, int y) { - int mouseX = Mouse.getX() / resolution.getScaleFactor(); - int mouseY = Math.abs((Mouse.getY() / resolution.getScaleFactor()) - resolution.getScaledHeight()); - hovered = mouseX > x && mouseY > y && mouseX < x + width && mouseY < y + height; - if (hovered) { - onHover(); - if (Mouse.isButtonDown(0) && !clicked) { - onClick(0); - } - clicked = Mouse.isButtonDown(0); - - if (Mouse.isButtonDown(1) && !rightClicked) { - onClick(1); - } - rightClicked = Mouse.isButtonDown(1); - onKeyPress(Keyboard.getEventKey()); - } - if (clicked) { - Renderer.color(theme.getClickColor()); - } - if (!hovered && clicked) clicked = false; - } - - /** - * Draw the element with the specified coordinates, width and height. - */ - public void draw(int x, int y, int width, int height) { - this.width = width; - this.height = height; - draw(x, y); - } - - - /** - * Override this method to set a function when a key is pressed while this element is hovered. - * - * @param keyCode key code that was pressed (check org.lwjgl.Keyboard for keymap) - */ - public void onKeyPress(int keyCode) { - - } - - /** - * Override this method to set a function when the element is hovered. - * - * @param button the button that was pressed (0 is left, 1 is right) - */ - public void onClick(int button) { - - } - - - /** - * Override this method to set a function when the element is hovered. - */ - public void onHover() { - - } - - - private float smooth(float current, float min, float max) { - current = Renderer.easeOut(current, isHovered() ? 1f : 0f); - if (current <= min) { - current = min; - } - - if (current >= max) { - current = max; - } - return current; - } - - public int getHeight() { - return height; - } - - public void setHeight(int height) { - this.height = height; - } - - public Color getColor() { - return color; - } - - public void setColor(Color color) { - this.color = color; - } - - public int getWidth() { - return width; - } - - public void setWidth(int width) { - this.width = width; - } - - public String getText() { - return text; - } - - public void setText(String text) { - this.text = text; - } - - public boolean isHovered() { - return hovered; - } - - public boolean isClicked() { - return clicked; - } -} diff --git a/src/main/java/io/polyfrost/oneconfig/gui/elements/OCButton.java b/src/main/java/io/polyfrost/oneconfig/gui/elements/OCButton.java deleted file mode 100644 index 5f78baa..0000000 --- a/src/main/java/io/polyfrost/oneconfig/gui/elements/OCButton.java +++ /dev/null @@ -1,105 +0,0 @@ -package io.polyfrost.oneconfig.gui.elements; - -import io.polyfrost.oneconfig.renderer.Renderer; -import io.polyfrost.oneconfig.themes.textures.ThemeElement; -import net.minecraft.client.renderer.GlStateManager; -import org.jetbrains.annotations.NotNull; - -import java.awt.*; - -public class OCButton extends OCBlock { - private final Color elementColor = theme.getElementColor(); - private final Color hoverColor = theme.getHoverColor(); - private float percentHoveredRed = 0f; - private float percentHoveredGreen = 0f; - private float percentHoveredBlue = 0f; - private float percentHoveredAlpha = 0f; - private float percentDescription = 0f; - private ThemeElement element; - private boolean alwaysShowDesc = true; - private String title, description; - - /** - * Create an empty button. - */ - public OCButton(int width, int height) { - super(width, height); - } - - /** - * Create a new button with the specified texture. - */ - public OCButton(ThemeElement element) { - super(element.size + 2, element.size + 2); - this.element = element; - } - - public OCButton(@NotNull String title, @NotNull String description, ThemeElement icon, boolean alwaysShowDesc) { - super(icon.size + theme.getBoldFont().getWidth(title) + 20, icon.size + 10); - this.element = icon; - this.title = title; - this.description = description; - this.alwaysShowDesc = alwaysShowDesc; - } - - - public OCButton(@NotNull String title, @NotNull String description, ThemeElement icon, boolean alwaysShowDesc, int width, int height) { - super(width, height); - this.element = icon; - this.title = title; - this.description = description; - this.alwaysShowDesc = alwaysShowDesc; - } - - public void draw(int x, int y) { - super.update(x, y); - - percentHoveredRed = smooth(percentHoveredRed, elementColor.getRed() / 255f, hoverColor.getRed() / 255f); - percentHoveredGreen = smooth(percentHoveredGreen, elementColor.getGreen() / 255f, hoverColor.getGreen() / 255f); - percentHoveredBlue = smooth(percentHoveredBlue, elementColor.getBlue() / 255f, hoverColor.getBlue() / 255f); - percentHoveredAlpha = smooth(percentHoveredAlpha, elementColor.getAlpha() / 255f, hoverColor.getAlpha() / 255f); - if (!alwaysShowDesc) { - percentDescription = Renderer.clamp(Renderer.easeOut(percentDescription, isHovered() ? 1f : 0f)); - } - GlStateManager.color(percentHoveredRed, percentHoveredGreen, percentHoveredBlue, percentHoveredAlpha); - if (isClicked()) { - //Renderer.setGlColor(theme.getClickColor()); - } - - theme.getTextureManager().draw(ThemeElement.BUTTON, x, y, width, height); - if (element != null) { - GlStateManager.color(1f, 1f, 1f, isClicked() ? 0.6f : 1f); - theme.getTextureManager().draw(element, x + 19, y + 8, element.size, element.size); - if (title != null) { - if (alwaysShowDesc) { - theme.getBoldFont().drawString(title, x + element.size + 25, y + 30, 1.2f, 1.2f, isClicked() ? theme.getTextColor().darker().getRGB() : theme.getTextColor().getRGB()); - theme.getFont().drawString(description, x + element.size + 25, y + theme.getBoldFont().getHeight() + 37, 1.2f,