From ab7256dff5d6d37488081ba7a01b36d3ee9ef563 Mon Sep 17 00:00:00 2001 From: DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> Date: Sun, 5 Jun 2022 17:43:23 +0200 Subject: refactor (#36) * refactor * fix vig compat * fix nanovg thingy * e * finalize * gui utils package thingy --- .../cc/polyfrost/oneconfig/utils/GuiUtils.java | 62 --------- .../cc/polyfrost/oneconfig/utils/OneUIScreen.java | 146 -------------------- .../cc/polyfrost/oneconfig/utils/TextUtils.java | 6 +- .../oneconfig/utils/color/ColorPalette.java | 10 +- .../utils/commands/annotations/Command.java | 3 +- .../cc/polyfrost/oneconfig/utils/gui/GuiUtils.java | 63 +++++++++ .../polyfrost/oneconfig/utils/gui/OneUIScreen.java | 147 +++++++++++++++++++++ 7 files changed, 220 insertions(+), 217 deletions(-) delete mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/OneUIScreen.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/gui/GuiUtils.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/gui/OneUIScreen.java (limited to 'src/main/java/cc/polyfrost/oneconfig/utils') diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java deleted file mode 100644 index 29c26fb..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java +++ /dev/null @@ -1,62 +0,0 @@ -package cc.polyfrost.oneconfig.utils; - -import cc.polyfrost.oneconfig.events.EventManager; -import cc.polyfrost.oneconfig.events.event.RenderEvent; -import cc.polyfrost.oneconfig.events.event.Stage; -import gg.essential.universal.UMinecraft; -import gg.essential.universal.UScreen; -import me.kbrewster.eventbus.Subscribe; -import net.minecraft.client.gui.GuiScreen; - -/** - * A class containing utility methods for working with GuiScreens. - */ -public final class GuiUtils { - private static long time = -1L; - private static long deltaTime = 17L; - - static { - EventManager.INSTANCE.register(new GuiUtils()); - } - - /** - * Displays a screen after a tick, preventing mouse sync issues. - * - * @param screen the screen to display. - */ - public static void displayScreen(GuiScreen screen) { - new TickDelay(() -> UScreen.displayScreen(screen), 1); - } - - /** - * Close the current open GUI screen. - */ - public static void closeScreen() { - UScreen.displayScreen(null); - } - - /** - * Gets the delta time (in milliseconds) between frames. - *

- * Not to be confused with Minecraft deltaTicks / renderPartialTicks, which can be gotten via - * {@link cc.polyfrost.oneconfig.events.event.TimerUpdateEvent} - *

- * - * @return the delta time. - */ - public static float getDeltaTime() { - return deltaTime; - } - - @Subscribe - private void onRenderEvent(RenderEvent event) { - if (event.stage == Stage.START) { - if (time == -1) time = UMinecraft.getTime(); - else { - long currentTime = UMinecraft.getTime(); - deltaTime = currentTime - time; - time = currentTime; - } - } - } -} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/OneUIScreen.java b/src/main/java/cc/polyfrost/oneconfig/utils/OneUIScreen.java deleted file mode 100644 index dac995d..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/utils/OneUIScreen.java +++ /dev/null @@ -1,146 +0,0 @@ -package cc.polyfrost.oneconfig.utils; - -import cc.polyfrost.oneconfig.lwjgl.RenderManager; -import gg.essential.universal.UMatrixStack; -import gg.essential.universal.UScreen; -import net.minecraft.client.gui.GuiScreen; -import org.jetbrains.annotations.NotNull; -import org.lwjgl.input.Mouse; - -/** - *

OneUIScreen

- * OneUIScreen is a GUI that can be used to render things on the client's screen. - * It contains many handy methods for rendering, including {@link #draw(long, float)} for drawing using OneConfig's {@link RenderManager}. - *

It also contains methods for mouse input. (see {@link InputUtils} for more utils). - *

- * Use {@link GuiUtils#displayScreen(GuiScreen)} to display a screen; and {@link GuiUtils#closeScreen()} to close it. - */ -public abstract class OneUIScreen extends UScreen { - private boolean mouseDown; - private boolean blockClicks; - - /** - * Create a new OneUIScreen. - * - * @param restoreGuiOnClose use this to declare weather or not to open the Gui that was open before it when this screen is closed. - */ - public OneUIScreen(boolean restoreGuiOnClose) { - super(restoreGuiOnClose); - } - - /** - * Create a new OneUIScreen. - */ - public OneUIScreen() { - super(false); - } - - @Override - public void onDrawScreen(@NotNull UMatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) { - super.onDrawScreen(matrixStack, mouseX, mouseY, partialTicks); - RenderManager.setupAndDraw(ignoreMinecraftScale(), vg -> draw(vg, partialTicks)); - mouseDown = Mouse.isButtonDown(0); - } - - /** - * This method is called when the screen is first opened. You can use it to set variables, initialize things, etc. - */ - public abstract void onScreenOpen(); - - /** - * Use this method to draw things on the screen. It is called every render tick, and has a handy vg (NanoVG context) that can be used with the {@link RenderManager} to draw things. - *

- * For example: {@link RenderManager#drawRoundedRect(long, float, float, float, float, int, float)} - * - * @param vg the NanoVG context you can use to render things with - * @param partialTicks the time between ticks (You can use this as a deltaTime equivalent) - */ - public abstract void draw(long vg, float partialTicks); - - /** - * This method is called when the screen is closed. You can use it to clean up things, etc. - */ - @Override - public abstract void onScreenClose(); - - @Override - public void initScreen(int width, int height) { - onScreenOpen(); - } - - /** - * Use this method to set weather or not to use the Minecraft scale on the GUI. Its default is true, and that is recommended for the NanoVG rendering. - */ - public boolean ignoreMinecraftScale() { - return true; - } - - /** - * Use this method to declare weather or not this Screen pauses the game when it is open. (Single-player only) Its default is false. - */ - public boolean doesScreenPauseGame() { - return false; - } - - @Override - public boolean doesGuiPauseGame() { - return doesScreenPauseGame(); - } - - - /** - * Get the current x position of the mouse. - */ - public int getMouseX() { - return InputUtils.mouseX(); - } - - /** - * Get the current y position of the mouse. - */ - public int getMouseY() { - return InputUtils.mouseY(); - } - - @Override - public void onMouseClicked(double mouseX, double mouseY, int mouseButton) { - super.onMouseClicked(mouseX, mouseY, mouseButton); - } - - /** - * Retrieve the click status of the mouse. This method uses a boolean to store the status of the mouse, so it will only return true once per click. (very useful) - * - * @param ignoreBlockClicks whether to ignore the current click blocker. - */ - public boolean isClicked(boolean ignoreBlockClicks) { - return mouseDown && !Mouse.isButtonDown(0) && (!blockClicks || ignoreBlockClicks); - } - - /** - * Retrieve the click status of the mouse. This method uses a boolean to store the status of the mouse, so it will only return true once per click. (very useful) - */ - public boolean isClicked() { - return isClicked(false); - } - - /** - * Retrieve weather or not the mouse is currently down. Will constantly return true if its clicked. See {@link #isClicked()} for a method that only executes once per tick. - */ - public boolean isMouseDown() { - return Mouse.isButtonDown(0); - } - - /** - * Click blocking can be useful when you are drawing buttons for example over the top of other elements, so a click blocker can be used to ensure that the mouse doesn't click through things. - */ - public void shouldBlockClicks(boolean state) { - blockClicks = state; - } - - /** - * Click blocking can be useful when you are drawing buttons for example over the top of other elements, so a click blocker can be used to ensure that the mouse doesn't click through things. - */ - public boolean isBlockingClicks() { - return blockClicks; - } -} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/TextUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/TextUtils.java index df37aae..51b42d8 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/TextUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/TextUtils.java @@ -1,8 +1,8 @@ package cc.polyfrost.oneconfig.utils; -import cc.polyfrost.oneconfig.lwjgl.RenderManager; -import cc.polyfrost.oneconfig.lwjgl.font.Font; -import cc.polyfrost.oneconfig.lwjgl.font.Fonts; +import cc.polyfrost.oneconfig.renderer.RenderManager; +import cc.polyfrost.oneconfig.renderer.font.Font; +import cc.polyfrost.oneconfig.renderer.font.Fonts; import java.util.ArrayList; diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/color/ColorPalette.java b/src/main/java/cc/polyfrost/oneconfig/utils/color/ColorPalette.java index a73e9ff..dc15e92 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/color/ColorPalette.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/color/ColorPalette.java @@ -1,18 +1,18 @@ package cc.polyfrost.oneconfig.utils.color; -import cc.polyfrost.oneconfig.config.OneConfigConfig; +import cc.polyfrost.oneconfig.gui.Colors; import cc.polyfrost.oneconfig.config.core.OneColor; import java.awt.*; -import static cc.polyfrost.oneconfig.config.OneConfigConfig.*; +import static cc.polyfrost.oneconfig.gui.Colors.*; public class ColorPalette { /** * Always returns transparent. */ - public static final ColorPalette TRANSPARENT = new ColorPalette(OneConfigConfig.TRANSPARENT, OneConfigConfig.TRANSPARENT, OneConfigConfig.TRANSPARENT); + public static final ColorPalette TRANSPARENT = new ColorPalette(Colors.TRANSPARENT, Colors.TRANSPARENT, Colors.TRANSPARENT); /** *

Primary Color Scheme

Normal: Primary 600,
Hover: Primary 700,
Clicked: Primary 700 (80%) */ @@ -20,7 +20,7 @@ public class ColorPalette { /** *

Secondary Color Scheme

Normal: Gray 500,
Hover: Gray 400,
Clicked: Gray 400 (80%) */ - public static final ColorPalette SECONDARY = new ColorPalette(OneConfigConfig.GRAY_500, OneConfigConfig.GRAY_400, OneConfigConfig.GRAY_400_80); + public static final ColorPalette SECONDARY = new ColorPalette(Colors.GRAY_500, Colors.GRAY_400, Colors.GRAY_400_80); /** *

Tertiary Color Scheme

Normal: Transparent (Text=White 90%),
Hover: Transparent (Text=White 100%),
Clicked: Transparent (Text=White 80%) *

NOTICE this returns the text colors as it is always transparent.

@@ -33,7 +33,7 @@ public class ColorPalette { /** *

Secondary Destructive Color Scheme

Normal: Gray 500,
Hover: Error 800,
Clicked: Error 800 (80%) */ - public static final ColorPalette SECONDARY_DESTRUCTIVE = new ColorPalette(OneConfigConfig.GRAY_500, ERROR_800, ERROR_800_80); + public static final ColorPalette SECONDARY_DESTRUCTIVE = new ColorPalette(Colors.GRAY_500, ERROR_800, ERROR_800_80); /** *

Tertiary Destructive Color Scheme

Normal: Transparent (Text=White 90%),
Hover: Transparent (Text=Error 300),
Clicked: Transparent (Text=Error 300 80%) *

NOTICE this returns the text colors as it is always transparent.

diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Command.java b/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Command.java index deec7f1..41909c0 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Command.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Command.java @@ -1,5 +1,6 @@ package cc.polyfrost.oneconfig.utils.commands.annotations; +import cc.polyfrost.oneconfig.internal.command.OneConfigCommand; import cc.polyfrost.oneconfig.utils.commands.CommandManager; import cc.polyfrost.oneconfig.utils.commands.arguments.ArgumentParser; @@ -78,7 +79,7 @@ import java.lang.annotation.Target; * Note: if you're viewing this in IntelliJ or just see the @literal tag everywhere, please ignore that. *

* - * @see cc.polyfrost.oneconfig.command.OneConfigCommand + * @see OneConfigCommand * @see Main * @see CommandManager */ diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/gui/GuiUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/gui/GuiUtils.java new file mode 100644 index 0000000..973e003 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/gui/GuiUtils.java @@ -0,0 +1,63 @@ +package cc.polyfrost.oneconfig.utils.gui; + +import cc.polyfrost.oneconfig.events.EventManager; +import cc.polyfrost.oneconfig.events.event.RenderEvent; +import cc.polyfrost.oneconfig.events.event.Stage; +import cc.polyfrost.oneconfig.utils.TickDelay; +import gg.essential.universal.UMinecraft; +import gg.essential.universal.UScreen; +import me.kbrewster.eventbus.Subscribe; +import net.minecraft.client.gui.GuiScreen; + +/** + * A class containing utility methods for working with GuiScreens. + */ +public final class GuiUtils { + private static long time = -1L; + private static long deltaTime = 17L; + + static { + EventManager.INSTANCE.register(new GuiUtils()); + } + + /** + * Displays a screen after a tick, preventing mouse sync issues. + * + * @param screen the screen to display. + */ + public static void displayScreen(GuiScreen screen) { + new TickDelay(() -> UScreen.displayScreen(screen), 1); + } + + /** + * Close the current open GUI screen. + */ + public static void closeScreen() { + UScreen.displayScreen(null); + } + + /** + * Gets the delta time (in milliseconds) between frames. + *

+ * Not to be confused with Minecraft deltaTicks / renderPartialTicks, which can be gotten via + * {@link cc.polyfrost.oneconfig.events.event.TimerUpdateEvent} + *

+ * + * @return the delta time. + */ + public static float getDeltaTime() { + return deltaTime; + } + + @Subscribe + private void onRenderEvent(RenderEvent event) { + if (event.stage == Stage.START) { + if (time == -1) time = UMinecraft.getTime(); + else { + long currentTime = UMinecraft.getTime(); + deltaTime = currentTime - time; + time = currentTime; + } + } + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/gui/OneUIScreen.java b/src/main/java/cc/polyfrost/oneconfig/utils/gui/OneUIScreen.java new file mode 100644 index 0000000..c06f6f3 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/gui/OneUIScreen.java @@ -0,0 +1,147 @@ +package cc.polyfrost.oneconfig.utils.gui; + +import cc.polyfrost.oneconfig.renderer.RenderManager; +import cc.polyfrost.oneconfig.utils.InputUtils; +import gg.essential.universal.UMatrixStack; +import gg.essential.universal.UScreen; +import net.minecraft.client.gui.GuiScreen; +import org.jetbrains.annotations.NotNull; +import org.lwjgl.input.Mouse; + +/** + *

OneUIScreen

+ * OneUIScreen is a GUI that can be used to render things on the client's screen. + * It contains many handy methods for rendering, including {@link #draw(long, float)} for drawing using OneConfig's {@link RenderManager}. + *

It also contains methods for mouse input. (see {@link InputUtils} for more utils). + *

+ * Use {@link GuiUtils#displayScreen(GuiScreen)} to display a screen; and {@link GuiUtils#closeScreen()} to close it. + */ +public abstract class OneUIScreen extends UScreen { + private boolean mouseDown; + private boolean blockClicks; + + /** + * Create a new OneUIScreen. + * + * @param restoreGuiOnClose use this to declare weather or not to open the Gui that was open before it when this screen is closed. + */ + public OneUIScreen(boolean restoreGuiOnClose) { + super(restoreGuiOnClose); + } + + /** + * Create a new OneUIScreen. + */ + public OneUIScreen() { + super(false); + } + + @Override + public void onDrawScreen(@NotNull UMatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) { + super.onDrawScreen(matrixStack, mouseX, mouseY, partialTicks); + RenderManager.setupAndDraw(ignoreMinecraftScale(), vg -> draw(vg, partialTicks)); + mouseDown = Mouse.isButtonDown(0); + } + + /** + * This method is called when the screen is first opened. You can use it to set variables, initialize things, etc. + */ + public abstract void onScreenOpen(); + + /** + * Use this method to draw things on the screen. It is called every render tick, and has a handy vg (NanoVG context) that can be used with the {@link RenderManager} to draw things. + *

+ * For example: {@link RenderManager#drawRoundedRect(long, float, float, float, float, int, float)} + * + * @param vg the NanoVG context you can use to render things with + * @param partialTicks the time between ticks (You can use this as a deltaTime equivalent) + */ + public abstract void draw(long vg, float partialTicks); + + /** + * This method is called when the screen is closed. You can use it to clean up things, etc. + */ + @Override + public abstract void onScreenClose(); + + @Override + public void initScreen(int width, int height) { + onScreenOpen(); + } + + /** + * Use this method to set weather or not to use the Minecraft scale on the GUI. Its default is true, and that is recommended for the NanoVG rendering. + */ + public boolean ignoreMinecraftScale() { + return true; + } + + /** + * Use this method to declare weather or not this Screen pauses the game when it is open. (Single-player only) Its default is false. + */ + public boolean doesScreenPauseGame() { + return false; + } + + @Override + public boolean doesGuiPauseGame() { + return doesScreenPauseGame(); + } + + + /** + * Get the current x position of the mouse. + */ + public int getMouseX() { + return InputUtils.mouseX(); + } + + /** + * Get the current y position of the mouse. + */ + public int getMouseY() { + return InputUtils.mouseY(); + } + + @Override + public void onMouseClicked(double mouseX, double mouseY, int mouseButton) { + super.onMouseClicked(mouseX, mouseY, mouseButton); + } + + /** + * Retrieve the click status of the mouse. This method uses a boolean to store the status of the mouse, so it will only return true once per click. (very useful) + * + * @param ignoreBlockClicks whether to ignore the current click blocker. + */ + public boolean isClicked(boolean ignoreBlockClicks) { + return mouseDown && !Mouse.isButtonDown(0) && (!blockClicks || ignoreBlockClicks); + } + + /** + * Retrieve the click status of the mouse. This method uses a boolean to store the status of the mouse, so it will only return true once per click. (very useful) + */ + public boolean isClicked() { + return isClicked(false); + } + + /** + * Retrieve weather or not the mouse is currently down. Will constantly return true if its clicked. See {@link #isClicked()} for a method that only executes once per tick. + */ + public boolean isMouseDown() { + return Mouse.isButtonDown(0); + } + + /** + * Click blocking can be useful when you are drawing buttons for example over the top of other elements, so a click blocker can be used to ensure that the mouse doesn't click through things. + */ + public void shouldBlockClicks(boolean state) { + blockClicks = state; + } + + /** + * Click blocking can be useful when you are drawing buttons for example over the top of other elements, so a click blocker can be used to ensure that the mouse doesn't click through things. + */ + public boolean isBlockingClicks() { + return blockClicks; + } +} -- cgit