diff options
author | nextdaydelivery <79922345+nxtdaydelivery@users.noreply.github.com> | 2022-05-30 15:09:02 +0100 |
---|---|---|
committer | nextdaydelivery <79922345+nxtdaydelivery@users.noreply.github.com> | 2022-05-30 15:09:02 +0100 |
commit | b745fa5c04a421cc8d8979b2e4cbf6288dc4684a (patch) | |
tree | 256137076b066310c9f439028dde15bde2729f0a /src/main/java/cc/polyfrost/oneconfig | |
parent | c305d7b4b3b446ed85343e5248862fc157a13e5b (diff) | |
download | OneConfig-b745fa5c04a421cc8d8979b2e4cbf6288dc4684a.tar.gz OneConfig-b745fa5c04a421cc8d8979b2e4cbf6288dc4684a.tar.bz2 OneConfig-b745fa5c04a421cc8d8979b2e4cbf6288dc4684a.zip |
OneUIScreen implementation
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig')
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java | 5 | ||||
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/utils/OneUIScreen.java | 146 |
2 files changed, 151 insertions, 0 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java index 3c710c3..92df1b1 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java @@ -16,4 +16,9 @@ public final class GuiUtils { 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); + } } diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/OneUIScreen.java b/src/main/java/cc/polyfrost/oneconfig/utils/OneUIScreen.java new file mode 100644 index 0000000..6aaec37 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/OneUIScreen.java @@ -0,0 +1,146 @@ +package cc.polyfrost.oneconfig.utils; + +import cc.polyfrost.oneconfig.libs.universal.UMatrixStack; +import cc.polyfrost.oneconfig.libs.universal.UScreen; +import cc.polyfrost.oneconfig.lwjgl.RenderManager; +import net.minecraft.client.gui.GuiScreen; +import org.jetbrains.annotations.NotNull; +import org.lwjgl.input.Mouse; + +/** + * <h1>OneUIScreen</h1> + * 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}. + * <p> It also contains methods for mouse input. (see {@link InputUtils} for more utils). + * <p></p> + * 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 <code>vg</code> (NanoVG context) that can be used with the {@link RenderManager} to draw things. + * <p></p> + * For example: <d> <code>{@link RenderManager#drawRoundedRect(long, float, float, float, float, int, float)} </code> + * + * @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; + } +} |