aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/utils/gui/OneUIScreen.java
blob: 2dd961ee130c3796a581aace8b602691e47a2e31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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.UScreen;
import cc.polyfrost.oneconfig.platform.Platform;
import cc.polyfrost.oneconfig.renderer.RenderManager;
import cc.polyfrost.oneconfig.utils.InputUtils;
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).
 * <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;

    /**
     * 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 = Platform.getMousePlatform().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;
    }

    /**
     * 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 && !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)
     */
    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 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.
     */
    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;
    }
}