aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/gui/pages/Page.java
blob: 9b9b4d3ec7e13c52975dda6eaee603111dbd523f (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
135
136
137
138
139
140
141
142
143
144
145
/*
 * This file is part of OneConfig.
 * OneConfig - Next Generation Config Library for Minecraft: Java Edition
 * Copyright (C) 2021, 2022 Polyfrost.
 *   <https://polyfrost.cc> <https://github.com/Polyfrost/>
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 *   OneConfig is licensed under the terms of version 3 of the GNU Lesser
 * General Public License as published by the Free Software Foundation, AND
 * under the Additional Terms Applicable to OneConfig, as published by Polyfrost,
 * either version 1.0 of the Additional Terms, or (at your option) any later
 * version.
 *
 *   This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 * License.  If not, see <https://www.gnu.org/licenses/>. You should
 * have also received a copy of the Additional Terms Applicable
 * to OneConfig, as published by Polyfrost. If not, see
 * <https://polyfrost.cc/legal/oneconfig/additional-terms>
 */

package cc.polyfrost.oneconfig.gui.pages;

import cc.polyfrost.oneconfig.gui.animations.Animation;
import cc.polyfrost.oneconfig.gui.animations.ColorAnimation;
import cc.polyfrost.oneconfig.gui.animations.EaseOutQuad;
import cc.polyfrost.oneconfig.internal.assets.Colors;
import cc.polyfrost.oneconfig.platform.Platform;
import cc.polyfrost.oneconfig.renderer.RenderManager;
import cc.polyfrost.oneconfig.renderer.scissor.Scissor;
import cc.polyfrost.oneconfig.renderer.scissor.ScissorManager;
import cc.polyfrost.oneconfig.utils.InputHandler;
import cc.polyfrost.oneconfig.utils.color.ColorPalette;

import java.util.ArrayList;

/**
 * A page is a 1056x728 rectangle of the GUI. It is the main content of the gui, and can be switched back and forwards easily. All the content of OneConfig is in a page.
 */
public abstract class Page {
    protected final String title;
    protected Animation scrollAnimation;
    private final ColorAnimation colorAnimation = new ColorAnimation(new ColorPalette(Colors.TRANSPARENT, Colors.GRAY_400_60, Colors.GRAY_400_60), 200);
    protected float scrollTarget;
    private long scrollTime;
    private boolean mouseWasDown, dragging;
    private float yStart;
    protected float scroll;
    public final ArrayList<Page> parents = new ArrayList<>();

    public Page(String title) {
        this.title = title;
    }

    public abstract void draw(long vg, int x, int y, InputHandler inputHandler);

    /**
     * Use this method to draw elements that are static on the page (ignore the scrolling).
     *
     * @return the total height of the elements, so they are excluded from the scissor rectangle.
     */
    public int drawStatic(long vg, int x, int y, InputHandler inputHandler) {
        return 0;
    }

    public void finishUpAndClose() {
    }

    public void scrollWithDraw(long vg, int x, int y, InputHandler inputHandler) {
        int maxScroll = getMaxScrollHeight();
        int scissorOffset = drawStatic(vg, x, y, inputHandler);
        scroll = scrollAnimation == null ? scrollTarget : scrollAnimation.get();
        final float scrollBarLength = (728f / maxScroll) * 728f;
        Scissor scissor = ScissorManager.scissor(vg, x, y + scissorOffset, x + 1056, y + 728 - scissorOffset);
        Scissor inputScissor = inputHandler.blockInputArea(x, y,1056, scissorOffset);
        float dWheel = (float) Platform.getMousePlatform().getDWheel();
        if (dWheel != 0) {
            scrollTarget += dWheel;

            if (scrollTarget > 0f) scrollTarget = 0f;
            else if (scrollTarget < -maxScroll + 728) scrollTarget = -maxScroll + 728;

            scrollAnimation = new EaseOutQuad(150, scroll, scrollTarget, false);
            scrollTime = System.currentTimeMillis();
        } else if (scrollAnimation != null && scrollAnimation.isFinished()) scrollAnimation = null;
        if (maxScroll <= 728) {
            draw(vg, x, y, inputHandler);
            ScissorManager.resetScissor(vg, scissor);
            inputHandler.stopBlock(inputScissor);
            return;
        }
        draw(vg, x, (int) (y + scroll), inputHandler);
        if (dragging && inputHandler.isClicked(true)) {
            dragging = false;
        }

        ScissorManager.resetScissor(vg, scissor);
        inputHandler.stopBlock(inputScissor);
        if (!(scrollBarLength > 727f)) {
            final float scrollBarY = (scroll / maxScroll) * 720f;
            final boolean isMouseDown = Platform.getMousePlatform().isButtonDown(0);
            final boolean scrollHover = inputHandler.isAreaHovered(x + 1042, (int) (y - scrollBarY), 12, (int) scrollBarLength);
            final boolean scrollTimePeriod = (System.currentTimeMillis() - scrollTime < 1000);
            final boolean hovered = (scrollHover || scrollTimePeriod) && Platform.getMousePlatform().isButtonDown(0);
            if (scrollHover && isMouseDown && !mouseWasDown) {
                yStart = inputHandler.mouseY();
                dragging = true;
            }
            mouseWasDown = isMouseDown;
            if (dragging) {
                scrollTarget = -(inputHandler.mouseY() - yStart) * maxScroll / 728f;
                if (scrollTarget > 0f) scrollTarget = 0f;
                else if (scrollTarget < -maxScroll + 728) scrollTarget = -maxScroll + 728;
                scrollAnimation = new EaseOutQuad(150, scroll, scrollTarget, false);
            }
            RenderManager.drawRoundedRect(vg, x + 1044, y - scrollBarY, 8, scrollBarLength, colorAnimation.getColor(scrollHover || scrollTimePeriod, dragging), 4f);
        }
    }

    public String getTitle() {
        return title;
    }

    public void keyTyped(char key, int keyCode) {
    }

    /**
     * Overwrite this method and make it return true if you want this to always be the base in breadcrumbs
     */
    public boolean isBase() {
        return false;
    }

    /**
     * Use this method to set the maximum scroll height of the page.
     */
    public int getMaxScrollHeight() {
        return 728;
    }
}