diff options
author | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-08-17 17:43:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-17 17:43:23 +0200 |
commit | 97f788ecd4be15b1556ee1f3d8bd057bdf06bf5f (patch) | |
tree | 2bccc96007025fbccf1758ac66d4665a97d6c315 /src/main/java/cc/polyfrost/oneconfig/utils | |
parent | e18629af6aee276b0be6cec473e4099cca9100f1 (diff) | |
download | OneConfig-97f788ecd4be15b1556ee1f3d8bd057bdf06bf5f.tar.gz OneConfig-97f788ecd4be15b1556ee1f3d8bd057bdf06bf5f.tar.bz2 OneConfig-97f788ecd4be15b1556ee1f3d8bd057bdf06bf5f.zip |
Input revamp (#93)
* hud fix
* api
* things
* stuff
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/utils')
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/utils/InputHandler.java (renamed from src/main/java/cc/polyfrost/oneconfig/utils/InputUtils.java) | 70 | ||||
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/utils/gui/GuiUtils.java | 10 | ||||
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/utils/gui/OneUIScreen.java | 97 |
3 files changed, 80 insertions, 97 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/InputUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/InputHandler.java index 3fb32f7..6bd8207 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/InputUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/InputHandler.java @@ -26,10 +26,10 @@ package cc.polyfrost.oneconfig.utils; -import cc.polyfrost.oneconfig.gui.OneConfigGui; import cc.polyfrost.oneconfig.libs.universal.UResolution; import cc.polyfrost.oneconfig.platform.Platform; import cc.polyfrost.oneconfig.renderer.scissor.Scissor; +import cc.polyfrost.oneconfig.utils.gui.GuiUtils; import java.util.ArrayList; @@ -40,15 +40,37 @@ import java.util.ArrayList; * For scaled values, see {@link cc.polyfrost.oneconfig.libs.universal.UMouse}. * </p> */ -public final class InputUtils { - private static final ArrayList<Scissor> blockScissors = new ArrayList<>(); +public class InputHandler { + private final ArrayList<Scissor> blockScissors = new ArrayList<>(); + private double scaleX = 1d; + private double scaleY = 1d; + + /** + * Push a scale for the input utils to use + * + * @param scaleX X scale + * @param scaleY Y scale + */ + public void scale(double scaleX, double scaleY) { + this.scaleX = scaleX; + this.scaleY = scaleY; + } + + /** + * Reset the scale input utils uses + */ + public void resetScale() { + scaleX = 1d; + scaleY = 1d; + } + /** * function to determine weather the mouse is currently over a specific region. Uses the current nvgScale to fix to any scale. * * @return true if mouse is over region, false if not. */ - public static boolean isAreaHovered(float x, float y, float width, float height, boolean ignoreBlock) { + public boolean isAreaHovered(float x, float y, float width, float height, boolean ignoreBlock) { float mouseX = mouseX(); float mouseY = mouseY(); return (ignoreBlock || blockScissors.size() == 0 || !shouldBlock(mouseX, mouseY)) && mouseX > x && mouseY > y && mouseX < x + width && mouseY < y + height; @@ -59,7 +81,7 @@ public final class InputUtils { * * @return true if mouse is over region, false if not. */ - public static boolean isAreaHovered(float x, float y, float width, float height) { + public boolean isAreaHovered(float x, float y, float width, float height) { return isAreaHovered(x, y, width, height, false); } @@ -72,9 +94,9 @@ public final class InputUtils { * @param height the height of the region * @param ignoreBlock if true, will ignore * @return true if the mouse is clicked and is over the region, false if not - * @see InputUtils#isAreaHovered(float, float, float, float) + * @see InputHandler#isAreaHovered(float, float, float, float) */ - public static boolean isAreaClicked(float x, float y, float width, float height, boolean ignoreBlock) { + public boolean isAreaClicked(float x, float y, float width, float height, boolean ignoreBlock) { return isAreaHovered(x, y, width, height, ignoreBlock) && isClicked(false); } @@ -86,9 +108,9 @@ public final class InputUtils { * @param width the width of the region * @param height the height of the region * @return true if the mouse is clicked and is over the region, false if not - * @see InputUtils#isAreaClicked(float, float, float, float, boolean) + * @see InputHandler#isAreaClicked(float, float, float, float, boolean) */ - public static boolean isAreaClicked(float x, float y, float width, float height) { + public boolean isAreaClicked(float x, float y, float width, float height) { return isAreaClicked(x, y, width, height, false); } @@ -98,17 +120,17 @@ public final class InputUtils { * @param ignoreBlock if true, will ignore * @return true if the mouse is clicked, false if not */ - public static boolean isClicked(boolean ignoreBlock) { - return OneConfigGui.INSTANCE != null && OneConfigGui.INSTANCE.mouseDown && !Platform.getMousePlatform().isButtonDown(0) && (ignoreBlock || blockScissors.size() == 0 || !shouldBlock(mouseX(), mouseY())); + public boolean isClicked(boolean ignoreBlock) { + return GuiUtils.wasMouseDown() && !Platform.getMousePlatform().isButtonDown(0) && (ignoreBlock || blockScissors.size() == 0 || !shouldBlock(mouseX(), mouseY())); } /** * Checks whether the mouse is clicked or not. * * @return true if the mouse is clicked, false if not - * @see InputUtils#isClicked(boolean) + * @see InputHandler#isClicked(boolean) */ - public static boolean isClicked() { + public boolean isClicked() { return isClicked(false); } @@ -121,9 +143,8 @@ public final class InputUtils { * * @return the current mouse X position */ - public static float mouseX() { - if (OneConfigGui.INSTANCE == null) return (float) Platform.getMousePlatform().getMouseX(); - return (float) (Platform.getMousePlatform().getMouseX() / OneConfigGui.INSTANCE.getScaleFactor()); + public float mouseX() { + return (float) (Platform.getMousePlatform().getMouseX() / scaleX); } /** @@ -135,9 +156,8 @@ public final class InputUtils { * * @return the current mouse Y position */ - public static float mouseY() { - if (OneConfigGui.INSTANCE == null) return (float) (UResolution.getWindowHeight() - Math.abs(Platform.getMousePlatform().getMouseY())); - return (float) ((UResolution.getWindowHeight() - Math.abs(Platform.getMousePlatform().getMouseY())) / OneConfigGui.INSTANCE.getScaleFactor()); + public float mouseY() { + return (float) ((UResolution.getWindowHeight() - Math.abs(Platform.getMousePlatform().getMouseY())) / scaleY); } /** @@ -148,7 +168,7 @@ public final class InputUtils { * @param width Width * @param height Height */ - public static Scissor blockInputArea(float x, float y, float width, float height) { + public Scissor blockInputArea(float x, float y, float width, float height) { Scissor scissor = new Scissor(new Scissor(x, y, width, height)); blockScissors.add(scissor); return scissor; @@ -157,7 +177,7 @@ public final class InputUtils { /** * Should be used if there is something above other components and you don't want it clicking trough */ - public static Scissor blockAllInput() { + public Scissor blockAllInput() { return blockInputArea(0, 0, 1920, 1080); } @@ -166,14 +186,14 @@ public final class InputUtils { * * @param scissor The scissor area */ - public static void stopBlock(Scissor scissor) { + public void stopBlock(Scissor scissor) { blockScissors.remove(scissor); } /** * Clears all blocking areas */ - public static void stopBlockingInput() { + public void stopBlockingInput() { blockScissors.clear(); } @@ -182,11 +202,11 @@ public final class InputUtils { * * @return true if clicks are blocked, false if not */ - public static boolean isBlockingInput() { + public boolean isBlockingInput() { return blockScissors.size() > 0; } - private static boolean shouldBlock(float x, float y) { + private boolean shouldBlock(float x, float y) { for (Scissor block : blockScissors) { if (block.isInScissor(x, y)) return true; } diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/gui/GuiUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/gui/GuiUtils.java index e3dffb6..71bba26 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/gui/GuiUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/gui/GuiUtils.java @@ -41,6 +41,7 @@ import cc.polyfrost.oneconfig.utils.TickDelay; public final class GuiUtils { private static long time = -1L; private static long deltaTime = 17L; + private static boolean wasMouseDown = false; static { EventManager.INSTANCE.register(new GuiUtils()); @@ -86,6 +87,13 @@ public final class GuiUtils { return deltaTime; } + /** + * @return If the mouse was down last frame + */ + public static boolean wasMouseDown() { + return wasMouseDown; + } + @Subscribe private void onRenderEvent(RenderEvent event) { if (event.stage == Stage.START) { @@ -95,6 +103,8 @@ public final class GuiUtils { deltaTime = currentTime - time; time = currentTime; } + } else if (event.stage == Stage.END) { + wasMouseDown = Platform.getMousePlatform().isButtonDown(0); } } } diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/gui/OneUIScreen.java b/src/main/java/cc/polyfrost/oneconfig/utils/gui/OneUIScreen.java index 5f93c7b..45242b2 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/gui/OneUIScreen.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/gui/OneUIScreen.java @@ -28,115 +28,68 @@ package cc.polyfrost.oneconfig.utils.gui; import cc.polyfrost.oneconfig.gui.GuiPause; import cc.polyfrost.oneconfig.libs.universal.UMatrixStack; +import cc.polyfrost.oneconfig.libs.universal.UResolution; import cc.polyfrost.oneconfig.libs.universal.UScreen; -import cc.polyfrost.oneconfig.platform.Platform; import cc.polyfrost.oneconfig.renderer.RenderManager; -import cc.polyfrost.oneconfig.utils.InputUtils; +import cc.polyfrost.oneconfig.utils.InputHandler; import org.jetbrains.annotations.NotNull; /** * <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). + * It contains many handy methods for rendering, including {@link #draw(long, float, InputHandler)} for drawing using OneConfig's {@link RenderManager}. + * <p> It also contains methods for mouse input. (see {@link InputHandler} for more utils). * <p></p> * Use GuiUtils to display a screen; and GuiUtils.closeScreen to close it. */ public abstract class OneUIScreen extends UScreen implements GuiPause { - private boolean mouseDown; - private boolean blockClicks; + private final boolean useMinecraftScale; + private final InputHandler inputHandler = new InputHandler(); /** * Create a new OneUIScreen. * + * @param useMinecraftScale wether to use Minecraft scale * @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) { + public OneUIScreen(boolean useMinecraftScale, boolean restoreGuiOnClose) { super(restoreGuiOnClose); + this.useMinecraftScale = useMinecraftScale; + } + + /** + * Create a new OneUIScreen. + * + * @param useMinecraftScale wether to use Minecraft scale + */ + public OneUIScreen(boolean useMinecraftScale) { + this(useMinecraftScale, false); } /** * Create a new OneUIScreen. */ public OneUIScreen() { - super(false); + this(false, false); } @Override public final 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 = Platform.getMousePlatform().isButtonDown(0); + if (useMinecraftScale) inputHandler.scale(UResolution.getScaleFactor(), UResolution.getScaleFactor()); + RenderManager.setupAndDraw(useMinecraftScale, vg -> draw(vg, partialTicks, inputHandler)); } - /** * 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); - - /** - * Use this method to set whether 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; - } - - /** - * Get the current x position of the mouse. - */ - protected float getMouseX() { - return InputUtils.mouseX(); - } - - /** - * Get the current y position of the mouse. - */ - protected float getMouseY() { - return InputUtils.mouseY(); - } - - /** - * 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. - */ - protected boolean isClicked(boolean ignoreBlockClicks) { - return mouseDown && !Platform.getMousePlatform().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) - */ - protected 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. - */ - protected boolean isMouseDown() { - return Platform.getMousePlatform().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. + * @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) + * @param inputHandler The input handler */ - 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; - } + public abstract void draw(long vg, float partialTicks, InputHandler inputHandler); @Override public boolean doesGuiPauseGame() { |