aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/gui/HudGui.java
blob: 72e8e581d038ed27bd3ba4c7bfe18b1f061aeb76 (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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package cc.polyfrost.oneconfig.gui;

import cc.polyfrost.oneconfig.config.core.ConfigCore;
import cc.polyfrost.oneconfig.hud.BasicHud;
import cc.polyfrost.oneconfig.hud.HudCore;
import cc.polyfrost.oneconfig.libs.universal.UKeyboard;
import cc.polyfrost.oneconfig.libs.universal.UMatrixStack;
import cc.polyfrost.oneconfig.libs.universal.UScreen;
import cc.polyfrost.oneconfig.lwjgl.RenderManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.awt.*;
import java.util.ArrayList;

public class HudGui extends UScreen {
    private BasicHud editingHud;
    private boolean isDragging;
    private boolean isScaling;
    private int xOffset;
    private int yOffset;

    @Override
    public void initScreen(int width, int height) {
        HudCore.editing = true;
        UKeyboard.allowRepeatEvents(true);
        super.initScreen(width, height);
    }

    @Override
    public void onDrawScreen(@NotNull UMatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
        RenderManager.drawGlRect(0, 0, this.width, this.height, new Color(80, 80, 80, 50).getRGB());

        if (isDragging) {
            setPosition(mouseX - xOffset, mouseY - yOffset, true);
        }

        for (BasicHud hud : HudCore.huds) {
            if (hud.enabled) processHud(hud, mouseX);
        }
    }

    private void processHud(BasicHud hud, int mouseX) {
        if (hud == editingHud && isScaling) {
            float xFloat = hud.getXScaled(this.width);
            float yFloat = hud.getYScaled(this.height);
            float pos = getXSnapping(mouseX, true);
            float newWidth = pos - xFloat;
            float newScale = newWidth / ((hud.getWidth(hud.scale) + hud.paddingX * hud.scale) / hud.scale);
            if (newScale > 20)
                newScale = 20;
            else if (newScale < 0.3)
                newScale = 0.3f;
            hud.scale = newScale;

            if (xFloat / this.width > 0.5)
                editingHud.xUnscaled = (xFloat + (hud.getWidth(hud.scale) + hud.paddingX * hud.scale)) / (double) this.width;
            if (yFloat / this.height > 0.5)
                editingHud.yUnscaled = (yFloat + (hud.getHeight(hud.scale) + hud.paddingY * hud.scale)) / (double) this.height;
        }

        int width = (int) (hud.getWidth(hud.scale) + hud.paddingX * hud.scale);
        int height = (int) (hud.getHeight(hud.scale) + hud.paddingY * hud.scale);
        int x = (int) hud.getXScaled(this.width);
        int y = (int) hud.getYScaled(this.height);

        if (hud.parent == null)
            hud.drawExampleAll(x, y, hud.scale, true);
        int color = new Color(215, 224, 235).getRGB();
        if (editingHud == hud) {
            color = new Color(43, 159, 235).getRGB();
            if (isDragging)
                RenderManager.drawGlRect(x, y, width, height, new Color(108, 176, 255, 60).getRGB());
        }
        int finalColor = color;
        RenderManager.setupAndDraw(true, (vg) -> {
            RenderManager.drawLine(vg, x - 2 / 4f, y, x + width + 2 / 4f, y, 1, finalColor);
            RenderManager.drawLine(vg, x, y, x, y + height, 1, finalColor);
            RenderManager.drawLine(vg, x + width, y, x + width, y + height, 1, finalColor);
            RenderManager.drawLine(vg, x - 2 / 4f, y + height, x + width + 2 / 4f, y + height, 1, finalColor);
        });

        if (hud == editingHud && !isDragging) {
            RenderManager.setupAndDraw(true, (vg) -> RenderManager.drawCircle(vg, x + width, y + height, 3, new Color(43, 159, 235).getRGB()));
        }

        if (hud.childBottom != null) processHud(hud.childBottom, mouseX);
        if (hud.childRight != null) processHud(hud.childRight, mouseX);
    }

    private void setPosition(float newX, float newY, boolean snap) {
        float width = editingHud.getWidth(editingHud.scale) + editingHud.paddingX * editingHud.scale;
        float height = editingHud.getHeight(editingHud.scale) + editingHud.paddingY * editingHud.scale;

        /* Childing disabled since it still needs some extra work
        if (editingHud.childRight != null) {
            HudCore.huds.add(editingHud.childRight);
            editingHud.childRight.parent = null;
            editingHud.childRight = null;
        }
        if (editingHud.childBottom != null) {
            HudCore.huds.add(editingHud.childBottom);
            editingHud.childBottom.parent = null;
            editingHud.childBottom = null;
        }
        if (editingHud.parent != null) {
            HudCore.huds.add(editingHud);
            if (editingHud.parent.childBottom == editingHud)
                editingHud.parent.childBottom = null;
            else if (editingHud.parent.childRight == editingHud)
                editingHud.parent.childRight = null;
            editingHud.parent = null;
        }*/

        if (newX < 0)
            newX = 0;
        else if (newX + width > this.width)
            newX = this.width - width;
        if (newY < 0)
            newY = 0;
        else if (newY + height > this.height)
            newY = this.height - height;

        if (snap) {
            float snapX = getXSnapping(newX, false);
            float snapY = getYSnapping(newY);
            if (snapX != newX || snapY != newY) {
                newX = snapX;
                newY = snapY;
                for (BasicHud hud : HudCore.huds) {
                    if (!hud.enabled) continue;
                    if (findParent(hud, snapX, snapY))
                        break;
                }
            }
        }

        if (newX / this.width <= 0.5)
            editingHud.xUnscaled = newX / (double) this.width;
        else
            editingHud.xUnscaled = (newX + width) / (double) this.width;
        if (newY / this.height <= 0.5)
            editingHud.yUnscaled = newY / (double) this.height;
        else
            editingHud.yUnscaled = (newY + height) / (double) this.height;
    }

    private boolean findParent(BasicHud hud, float snapX, float snapY) {
        int hudWidth = (int) (hud.getWidth(hud.scale) + hud.paddingX * hud.scale);
        int hudX = (int) hud.getXScaled(this.width);
        int hudHeight = (int) (hud.getHeight(hud.scale) + hud.paddingY * hud.scale);
        int hudY = (int) hud.getYScaled(this.height);
        if (hudX + hudWidth == snapX && hudY == snapY && hud.childRight == null) {
            editingHud.parent = hud;
            hud.childRight = editingHud;
            HudCore.huds.remove(editingHud);
            return true;
        } else if (hudX == snapX && hudY + hudHeight == snapY && hud.childBottom == null) {
            editingHud.parent = hud;
            hud.childBottom = editingHud;
            HudCore.huds.remove(editingHud);
            return true;
        }
        return hud.childRight != null && findParent(hud.childRight, snapX, snapY) || hud.childBottom != null && findParent(hud.childBottom, snapX, snapY);
    }

    private float getXSnapping(float pos, boolean rightOnly) {
        float width = editingHud.getWidth(editingHud.scale) + editingHud.paddingX * editingHud.scale;
        ArrayList<Float> verticalLines = new ArrayList<>();
        for (BasicHud hud : HudCore.huds) {
            if (!hud.enabled) continue;
            verticalLines.addAll(getXSnappingHud(hud));
        }
        getSpaceSnapping(verticalLines);
        verticalLines.add(this.width / 2f);
        float smallestDiff = -1;
        float smallestLine = 0;
        float smallestOffset = 0;
        for (float lineX : verticalLines) {
            for (float offset = 0; offset <= (rightOnly ? 0 : width); offset += width / 2f) {
                if (Math.abs(lineX - pos - offset) < 5 && (Math.abs(lineX - pos - offset) < smallestDiff || smallestDiff == -1)) {
                    smallestDiff = Math.abs(lineX - pos);
                    smallestLine = lineX;
                    smallestOffset = offset;
                }
            }
        }
        if (smallestDiff != -1) {
            float finalSmallestLine = smallestLine;
            RenderManager.setupAndDraw(true, (vg) -> RenderManager.drawLine(vg, finalSmallestLine, 0, finalSmallestLine, this.height, 1, new Color(255, 255, 255).getRGB()));
            return smallestLine - smallestOffset;
        }
        return pos;
    }

    private ArrayList<Float> getXSnappingHud(BasicHud hud) {
        ArrayList<Float> verticalLines = new ArrayList<>();
        if (hud == editingHud) return verticalLines;
        if (hud.childRight != null) verticalLines.addAll(getXSnappingHud(hud.childRight));
        int hudWidth = (int) (hud.getWidth(hud.scale) + hud.paddingX * hud.scale);
        int hudX = (int) hud.getXScaled(this.width);
        verticalLines.add((float) hudX);
        verticalLines.add((float) (hudX + hudWidth));
        return verticalLines;
    }

    private float getYSnapping(float pos) {
        float height = editingHud.getHeight(editingHud.scale) + editingHud.paddingY * editingHud.scale;
        ArrayList<Float> horizontalLines = new ArrayList<>();
        for (BasicHud hud : HudCore.huds) {
            if (!hud.enabled) continue;
            horizontalLines.addAll(getYSnappingHud(hud));
        }
        getSpaceSnapping(horizontalLines);
        horizontalLines.add(this.height / 2f);
        float smallestDiff = -1;
        float smallestLine = 0;
        float smallestOffset = 0;
        for (float lineY : horizontalLines) {
            for (float offset = 0; offset <= height; offset += height / 2f) {
                if (Math.abs(lineY - pos - offset) < 5 && (Math.abs(lineY - pos - offset) < smallestDiff || smallestDiff == -1)) {
                    smallestDiff = Math.abs(lineY - pos);
                    smallestLine = lineY;
                    smallestOffset = offset;
                }
            }
        }
        if (smallestDiff != -1) {
            float finalSmallestLine = smallestLine;
            RenderManager.setupAndDraw(true, (vg) -> RenderManager.drawLine(vg, 0, finalSmallestLine, this.width, finalSmallestLine, 1, new Color(255, 255, 255).getRGB()));
            return smallestLine - smallestOffset;
        }
        return pos;
    }

    private ArrayList<Float> getYSnappingHud(BasicHud hud) {
        ArrayList<Float> horizontalLines = new ArrayList<>();
        if (hud == editingHud) return horizontalLines;
        if (hud.childBottom != null) horizontalLines.addAll(getYSnappingHud(hud.childBottom));
        int hudHeight = (int) (hud.getHeight(hud.scale) + hud.paddingY * hud.scale);
        int hudY = (int) hud.getYScaled(this.height);
        horizontalLines.add((float) hudY);
        horizontalLines.add((float) (hudY + hudHeight));
        return horizontalLines;
    }

    private void getSpaceSnapping(ArrayList<Float> lines) {
        ArrayList<Float> newLines = new ArrayList<>();
        for (int i = 0; i < lines.size(); i++) {
            for (int l = i + 1; l < lines.size(); l++) {
                newLines.add(Math.max(lines.get(i), lines.get(l)) + Math.abs(lines.get(i) - lines.get(l)));
                newLines.add(Math.min(lines.get(i), lines.get(l)) - Math.abs(lines.get(i) - lines.get(l)));
            }
        }
        lines.addAll(newLines);
    }

    @Override
    public void onMouseClicked(double mouseX, double mouseY, int mouseButton) {
        super.onMouseClicked(mouseX, mouseY, mouseButton);
        if (mouseButton == 0) {
            if (editingHud != null) {
                int width = (int) (editingHud.getWidth(editingHud.scale) + editingHud.paddingX * editingHud.scale);
                int height = (int) (editingHud.getHeight(editingHud.scale) + editingHud.paddingY * editingHud.scale);
                float x = editingHud.getXScaled(this.width);
                float y = editingHud.getYScaled(this.height);
                if (mouseX >= x + width - 3 && mouseX <= x + width + 3 && mouseY >= y + height - 3 && mouseY <= y + height + 3) {
                    isScaling = true;
                    return;
                }
            }
            editingHud = null;
            for (BasicHud hud : HudCore.huds) {
                if (!hud.enabled) continue;
                if (mouseClickedHud(hud, (int) mouseX, (int) mouseY))
                    break;
            }
        }
    }

    private boolean mouseClickedHud(BasicHud hud, int mouseX, int mouseY) {
        int width = (int) (hud.getWidth(hud.scale) + hud.paddingX * hud.scale);
        int height = (int) (hud.getHeight(hud.scale) + hud.paddingY * hud.scale);
        float x = hud.getXScaled(this.width);
        float y = hud.getYScaled(this.height);
        if (mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height) {
            editingHud = hud;
            xOffset = (int) (mouseX - x);
            yOffset = (int) (mouseY - y);
            isDragging = true;
            return true;
        }
        return hud.childBottom != null && mouseClickedHud(hud.childBottom, mouseX, mouseY) || hud.childRight != null && mouseClickedHud(hud.childRight, mouseX, mouseY);
    }

    @Override
    public void onMouseReleased(double mouseX, double mouseY, int state) {
        super.onMouseReleased(mouseX, mouseY, state);
        isDragging = false;
        isScaling = false;
    }

    @Override
    public void onKeyPressed(int keyCode, char typedChar, @Nullable UKeyboard.Modifiers modifiers) {
        if (editingHud != null) {
            float x = editingHud.getXScaled(this.width);
            float y = editingHud.getYScaled(this.height);
            if (keyCode == UKeyboard.KEY_UP) {
                setPosition(x, y - 1, false);
            } else if (keyCode == UKeyboard.KEY_DOWN) {
                setPosition(x, y + 1, false);
            } else if (keyCode == UKeyboard.KEY_LEFT) {
                setPosition(x - 1, y, false);
            } else if (keyCode == UKeyboard.KEY_RIGHT) {
                setPosition(x + 1, y, false);
            }
        }
        super.onKeyPressed(keyCode, typedChar, modifiers);
    }

    @Override
    public void onScreenClose() {
        super.onScreenClose();
        HudCore.editing = false;
        UKeyboard.allowRepeatEvents(false);
        ConfigCore.saveAll();
    }

    @Override
    public boolean doesGuiPauseGame() {
        return false;
    }
}