aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/kr/syeyoung/dungeonsguide/gui/MPanel.java
blob: 84cccdf55098fa033562778c3c838f85d92a8bcc (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 *     Dungeons Guide - The most intelligent Hypixel Skyblock Dungeons Mod
 *     Copyright (C) 2021  cyoung06
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Affero General Public License as published
 *     by the Free Software Foundation, either version 3 of the License, 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 Affero General Public License for more details.
 *
 *     You should have received a copy of the GNU Affero General Public License
 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package kr.syeyoung.dungeonsguide.gui;

import kr.syeyoung.dungeonsguide.gui.elements.MTooltip;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.renderer.GlStateManager;
import org.lwjgl.opengl.GL11;

import java.awt.*;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@Getter
public class MPanel {
    protected Rectangle bounds = new Rectangle(0,0,0,0); // relative to parent

    protected List<MPanel> childComponents = new CopyOnWriteArrayList<MPanel>();

    protected Color backgroundColor = new Color(0,0,0,0);

    protected Rectangle lastAbsClip = new Rectangle(0,0,0,0);

    @Getter(AccessLevel.PUBLIC)
    protected boolean isFocused;

    @Getter
    @Setter
    protected MPanel parent;

    private boolean debug = false;

    public void setBackgroundColor(Color c) {
        if (c == null) return;
        this.backgroundColor = c;
    }

    public void setPosition(Point pt) {
        this.setBounds(new Rectangle(pt.x, pt.y, getBounds().width, getBounds().height));
    }

    public void setSize(Dimension dim) {
        this.setBounds(new Rectangle(getBounds().x, getBounds().y, dim.width, dim.height));
    }

    public Dimension getSize() {
        return getBounds().getSize();
    }

    public Dimension getPreferredSize() { return getSize(); }

    public void setBounds(Rectangle bounds) {
        if (bounds == null) return;
        this.bounds.x = bounds.x;
        this.bounds.y = bounds.y;
        this.bounds.width = bounds.width;
        this.bounds.height = bounds.height;

        for (MPanel childComponent : childComponents) {
            childComponent.resize0(getBounds().width, getBounds().height);
        }
        onBoundsUpdate();
    }

    public void onBoundsUpdate() {

    }

    public void add(MPanel child) {
        if (child.parent != null) throw new IllegalArgumentException("What have you done");
        this.childComponents.add(child);
        child.setParent(this);
    }

    public void openTooltip(MTooltip mPanel) {
        parent.openTooltip(mPanel);
    }
    public int getTooltipsOpen() {
        return parent.getTooltipsOpen();
    }

    public void remove(MPanel panel) {
        if (panel != null)
        panel.setParent(null);
        this.childComponents.remove(panel);
    }

    protected Point lastParentPoint;

    @Getter
    @Setter
    private boolean ignoreBoundOnClip;

    public void render0(double scale, Point parentPoint, Rectangle parentClip, int absMousex, int absMousey, int relMousex0, int relMousey0, float partialTicks) { // 0,0 - a a

        lastParentPoint = parentPoint;
        int relMousex = relMousex0 - getBounds().x;
        int relMousey = relMousey0 - getBounds().y;

        GlStateManager.translate(getBounds().x, getBounds().y, 5);
        GlStateManager.color(1,1,1,0);


        Rectangle absBound = getBounds().getBounds();
        absBound.setLocation(absBound.x + parentPoint.x, absBound.y + parentPoint.y);
        Rectangle clip;
        if (ignoreBoundOnClip) clip = parentClip;
        else clip = determineClip(parentClip, absBound);
        lastAbsClip = clip;
        if (clip.getSize().height * clip.getSize().width == 0) return;

        this.scale = scale;
        clip(clip.x, clip.y, clip.width, clip.height);
        GlStateManager.pushAttrib();
        GL11.glEnable(GL11.GL_SCISSOR_TEST);

        GlStateManager.pushAttrib();
        GuiScreen.drawRect(0,0, getBounds().width, getBounds().height,  backgroundColor.getRGB());
        GlStateManager.enableBlend();
        GlStateManager.popAttrib();

        GlStateManager.pushMatrix();
        GlStateManager.pushAttrib();

        GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
        GlStateManager.enableBlend();
        GlStateManager.enableAlpha();
        GlStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0);

        render(absMousex, absMousey, relMousex, relMousey, partialTicks, clip);
        GlStateManager.popAttrib();
        GlStateManager.popMatrix();
        if (debug && lastAbsClip.contains(absMousex, absMousey)) {
            Gui.drawRect(0, 0, getBounds().width, getBounds().height, 0x2200FF00);
            GL11.glDisable(GL11.GL_SCISSOR_TEST);
//            Gui.drawRect(0, 0, getPreferredSize().width, getPreferredSize().height, 0x220000FF);
        }

        GL11.glDisable(GL11.GL_SCISSOR_TEST);
        GlStateManager.popAttrib();

        Point newPt = new Point(parentPoint.x + getBounds().x, parentPoint.y + getBounds().y);

        for (MPanel mPanel : getChildComponents()){
            GlStateManager.pushMatrix();
            GlStateManager.pushAttrib();
            mPanel.render0(scale, newPt, clip, absMousex, absMousey, relMousex, relMousey, partialTicks);
            GlStateManager.popAttrib();
            GlStateManager.popMatrix();
        }
    }
    protected double scale;
    public void clip(int x, int y, int width, int height) {
        if (width < 0 || height < 0) return;

        GL11.glScissor((int) (x  * scale), Minecraft.getMinecraft().displayHeight - (int) ((y + height) * scale), (int)(width* scale), (int) (height * scale));
    }

    protected Rectangle determineClip(Rectangle rect1, Rectangle rect2) {
        int minX = Math.max(rect1.x, rect2.x);
        int minY = Math.max(rect1.y, rect2.y);
        int maxX = Math.min(rect1.x + rect1.width, rect2.x + rect2.width);
        int maxY = Math.min(rect1.y + rect1.height, rect2.y +rect2.height);
        if (minX > maxX) return new Rectangle(0,0,0,0);
        if (minY > maxY) return new Rectangle(0,0,0,0);
        return new Rectangle(minX, minY, maxX - minX, maxY - minY);
    }

    public void render(int absMousex, int absMousey, int relMousex0, int relMousey0, float partialTicks, Rectangle scissor) {}

    public void resize0(int parentWidth, int parentHeight) {
        resize(parentWidth, parentHeight);
    }

    public void resize(int parentWidth, int parentHeight) {}


    public void keyTyped0(char typedChar, int keyCode) {
        for (MPanel childComponent  : getChildComponents()) {
            childComponent.keyTyped0(typedChar, keyCode);
        }

        if (isFocused)
            keyTyped(typedChar, keyCode);
    }
    public void keyTyped(char typedChar, int keyCode) {}

    public boolean mouseClicked0(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0, int mouseButton) {
        int relMousex = relMouseX0 - getBounds().x;
        int relMousey = relMouseY0 - getBounds().y;

        boolean noClip = true;
        boolean focusedOverall = false;
        for (MPanel childComponent  : getChildComponents()) {
            if (childComponent.mouseClicked0(absMouseX, absMouseY, relMousex, relMousey, mouseButton)) {
                noClip = false;
                focusedOverall = true;
            }
        }

        if (getBounds().contains(relMouseX0, relMouseY0) && noClip) {
            isFocused = true;
            focusedOverall = true;
        } else {
            isFocused = false;
        }

        mouseClicked(absMouseX, absMouseY, relMousex, relMousey, mouseButton);
        return focusedOverall;
    }

    public void mouseClicked(int absMouseX, int absMouseY, int relMouseX, int relMouseY, int mouseButton) {}

    public void mouseReleased0(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0, int state) {
        int relMousex = relMouseX0 - getBounds().x;
        int relMousey = relMouseY0 - getBounds().y;

        for (MPanel childComponent : getChildComponents()) {
            childComponent.mouseReleased0(absMouseX, absMouseY, relMousex, relMousey, state);
        }
        mouseReleased(absMouseX, absMouseY, relMousex, relMousey, state);
    }
    public void mouseReleased(int absMouseX, int absMouseY, int relMouseX, int relMouseY, int state) {}

    public void mouseClickMove0(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0, int clickedMouseButton, long timeSinceLastClick) {
        int relMousex = relMouseX0 - getBounds().x;
        int relMousey = relMouseY0 - getBounds().y;

        for (MPanel childComponent  : getChildComponents()) {
            childComponent.mouseClickMove0(absMouseX, absMouseY, relMousex, relMousey, clickedMouseButton, timeSinceLastClick);
        }
        mouseClickMove(absMouseX, absMouseY, relMousex, relMousey, clickedMouseButton, timeSinceLastClick);
    }
    public void mouseClickMove(int absMouseX, int absMouseY, int relMouseX, int relMouseY, int clickedMouseButton, long timeSinceLastClick) {}

    public void mouseScrolled0(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0, int scrollAmount) {
        int relMousex = relMouseX0 - getBounds().x;
        int relMousey = relMouseY0 - getBounds().y;

        for (MPanel childComponent  : getChildComponents()) {
            childComponent.mouseScrolled0(absMouseX, absMouseY, relMousex, relMousey, scrollAmount);
        }
        mouseScrolled(absMouseX, absMouseY, relMousex, relMousey, scrollAmount);
    }
    public void mouseScrolled(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0, int scrollAmount) {}
}