aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/internal/gui/HudGui.java
blob: 1710832b0507a85221d06850d28258d9dfc6a1d0 (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
package cc.polyfrost.oneconfig.internal.gui;

import cc.polyfrost.oneconfig.gui.GuiPause;
import cc.polyfrost.oneconfig.gui.OneConfigGui;
import cc.polyfrost.oneconfig.hud.Hud;
import cc.polyfrost.oneconfig.hud.Position;
import cc.polyfrost.oneconfig.internal.hud.utils.GrabOffset;
import cc.polyfrost.oneconfig.internal.hud.utils.SnappingLine;
import cc.polyfrost.oneconfig.internal.config.core.ConfigCore;
import cc.polyfrost.oneconfig.internal.hud.HudCore;
import cc.polyfrost.oneconfig.libs.universal.UKeyboard;
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.renderer.RenderManager;
import cc.polyfrost.oneconfig.utils.MathUtils;
import cc.polyfrost.oneconfig.utils.gui.GuiUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

public class HudGui extends UScreen implements GuiPause {
    private static final int SNAPPING_DISTANCE = 10;
    private final HashMap<Hud, GrabOffset> editingHuds = new HashMap<>();
    private boolean isDragging;
    private boolean isSelecting;
    private float selectX;
    private float selectY;
    private boolean isScaling;

    public HudGui() {
        super();
    }

    @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) {
        int lineWidth = Math.max(1, Math.round(Math.min(UResolution.getWindowWidth() / 1920f, UResolution.getWindowHeight() / 1080f)));
        if (isDragging) {
            RenderManager.setupAndDraw(vg -> setHudPositions(vg, mouseX, mouseY, true, true, lineWidth));
        } else if (isSelecting) {
            getHudsInRegion(selectX, selectY, mouseX, mouseY);
        } else if (isScaling && editingHuds.size() == 1) {
            RenderManager.setupAndDraw(vg -> {
                Hud hud = (Hud) editingHuds.keySet().toArray()[0];
                Position position = hud.position;
                float scaleX = getXSnapping(vg, lineWidth, mouseX, position.getWidth(), false);
                hud.setScale(MathUtils.clamp((scaleX - position.getX()) / (position.getWidth() / hud.getScale()), 0.3f, 10f), true);
            });
        }

        float scaleFactor = (float) UResolution.getScaleFactor();
        for (Hud hud : HudCore.huds) {
            if (!hud.isEnabled()) continue;
            Position position = hud.position;
            hud.drawAll(matrixStack, true);
            if (editingHuds.containsKey(hud))
                RenderManager.setupAndDraw(true, vg -> RenderManager.drawRect(vg, position.getX(), position.getY(), position.getWidth(), position.getHeight(), new Color(0, 128, 128, 60).getRGB()));
            RenderManager.setupAndDraw(vg -> {
                RenderManager.drawLine(vg, position.getX() * scaleFactor - lineWidth / 2f, position.getY() * scaleFactor - lineWidth / 2f, position.getRightX() * scaleFactor + lineWidth / 2f, position.getY() * scaleFactor - lineWidth / 2f, lineWidth, new Color(255, 255, 255).getRGB());
                RenderManager.drawLine(vg, position.getX() * scaleFactor - lineWidth / 2f, position.getBottomY() * scaleFactor + lineWidth / 2f, position.getRightX() * scaleFactor + lineWidth / 2f, position.getBottomY() * scaleFactor + lineWidth / 2f, lineWidth, new Color(255, 255, 255).getRGB());
                RenderManager.drawLine(vg, position.getX() * scaleFactor - lineWidth / 2f, position.getY() * scaleFactor - lineWidth / 2f, position.getX() * scaleFactor - lineWidth / 2f, position.getBottomY() * scaleFactor + lineWidth / 2f, lineWidth, new Color(255, 255, 255).getRGB());
                RenderManager.drawLine(vg, position.getRightX() * scaleFactor + lineWidth / 2f, position.getY() * scaleFactor - lineWidth / 2f, position.getRightX() * scaleFactor + lineWidth / 2f, position.getBottomY() * scaleFactor + lineWidth / 2f, lineWidth, new Color(255, 255, 255).getRGB());
            });
            if (editingHuds.containsKey(hud) && editingHuds.size() == 1)
                RenderManager.setupAndDraw(true, vg -> RenderManager.drawRect(vg, position.getRightX() - 4, position.getBottomY() - 4, 8, 8, new Color(0, 128, 128, 200).getRGB()));
        }

        if (isSelecting)
            RenderManager.setupAndDraw(true, vg -> RenderManager.drawRect(vg, selectX, selectY, mouseX - selectX, mouseY - selectY, new Color(0, 0, 255, 100).getRGB()));
    }

    @Override
    public void onMouseClicked(double mouseX, double mouseY, int mouseButton) {
        super.onMouseClicked(mouseX, mouseY, mouseButton);
        if (mouseButton != 0) return;
        isDragging = false;
        isSelecting = false;
        isScaling = false;
        if (editingHuds.size() == 1) {
            Position position = ((Hud) editingHuds.keySet().toArray()[0]).position;
            if (mouseX >= position.getRightX() - 7 && mouseX <= position.getRightX() + 7 && mouseY >= position.getBottomY() - 7 && mouseY <= position.getBottomY() + 7) {
                isScaling = true;
                return;
            }
        }
        for (Hud hud : HudCore.huds) {
            if (!hud.isEnabled() || !mouseClickedHud(hud, (float) mouseX, (float) mouseY)) continue;
            if (!editingHuds.containsKey(hud)) {
                if (!UKeyboard.isCtrlKeyDown()) editingHuds.clear();
                editingHuds.put(hud, new GrabOffset());
            }
            isDragging = true;
            editingHuds.forEach((hud2, grabOffset) -> grabOffset.setOffset((float) (mouseX - hud2.position.getX()), (float) (mouseY - hud2.position.getY())));
            return;
        }
        isSelecting = true;
        selectX = (float) mouseX;
        selectY = (float) mouseY;
    }

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

    @Override
    public void onKeyPressed(int keyCode, char typedChar, @Nullable UKeyboard.Modifiers modifiers) {
        if (isDragging) return;
        editingHuds.forEach((hud, grabOffset) -> grabOffset.setOffset(-hud.position.getX(), -hud.position.getY()));
        if (keyCode == UKeyboard.KEY_UP) {
            setHudPositions(0f, -1f, false);
        } else if (keyCode == UKeyboard.KEY_DOWN) {
            setHudPositions(0f, 1f, false);
        } else if (keyCode == UKeyboard.KEY_LEFT) {
            setHudPositions(-1f, 0f, false);
        } else if (keyCode == UKeyboard.KEY_RIGHT) {
            setHudPositions(1f, 0f, false);
        }
        super.onKeyPressed(keyCode, typedChar, modifiers);
    }

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

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

    private void getHudsInRegion(float x1, float y1, float x2, float y2) {
        if (x1 > x2) {
            float temp = x1;
            x1 = x2;
            x2 = temp;
        }
        if (y1 > y2) {
            float temp = y1;
            y1 = y2;
            y2 = temp;
        }

        editingHuds.clear();
        for (Hud hud : HudCore.huds) {
            if (!hud.isEnabled()) continue;
            Position pos = hud.position;
            if ((x1 <= pos.getX() && x2 >= pos.getX() || x1 <= pos.getRightX() && x2 >= pos.getRightX())
                    && (y1 <= pos.getY() && y2 >= pos.getY() || y1 <= pos.getBottomY() && y2 >= pos.getBottomY()))
                editingHuds.put(hud, new GrabOffset());
        }
    }

    private void setHudPositions(long vg, float mouseX, float mouseY, boolean snap, boolean locked, int lineWidth) {
        for (Hud hud : editingHuds.keySet()) {
            GrabOffset grabOffset = editingHuds.get(hud);
            Position position = hud.position;
            float x = mouseX - grabOffset.getX();
            float y = mouseY - grabOffset.getY();

            if (editingHuds.size() == 1 && snap) {
                x = getXSnapping(vg, lineWidth, x, position.getWidth(), true);
                y = getYSnapping(vg, lineWidth, y, position.getHeight(), true);
            }

            if (locked) {
                if (x < 0) x = 0;
                else if (x + position.getWidth() > UResolution.getScaledWidth())
                    x = UResolution.getScaledWidth() - position.getWidth();
                if (y < 0) y = 0;
                else if (y + position.getHeight() > UResolution.getScaledHeight())
                    y = UResolution.getScaledHeight() - position.getHeight();
            }

            position.setPosition(x, y);
        }
    }

    private void setHudPositions(float mouseX, float mouseY, boolean locked) {
        setHudPositions(0, mouseX, mouseY, false, locked, 0);
    }

    private boolean mouseClickedHud(Hud hud, float mouseX, float mouseY) {
        Position position = hud.position;
        return mouseX >= position.getX() && mouseX <= position.getRightX() &&
                mouseY >= position.getY() && mouseY <= position.getBottomY();
    }

    private float getXSnapping(long vg, float lineWidth, float x, float width, boolean multipleSides) {
        ArrayList<Float> lines = getXSnappingLines();
        ArrayList<SnappingLine> snappingLines = new ArrayList<>();
        float closest = (float) (SNAPPING_DISTANCE / UResolution.getScaleFactor());
        for (Float line : lines) {
            SnappingLine snappingLine = new SnappingLine(line, x, width, multipleSides);
            if (Math.round(snappingLine.getDistance()) == Math.round(closest)) snappingLines.add(snappingLine);
            else if (snappingLine.getDistance() < closest) {
                closest = snappingLine.getDistance();
                snappingLines.clear();
                snappingLines.add(snappingLine);
            }
        }
        if (snappingLines.isEmpty()) return x;
        for (SnappingLine snappingLine : snappingLines) {
            snappingLine.drawLine(vg, lineWidth, true);
        }
        return snappingLines.get(0).getPosition();
    }

    private ArrayList<Float> getXSnappingLines() {
        ArrayList<Float> lines = new ArrayList<>();
        lines.add(UResolution.getScaledWidth() / 2f);
        for (Hud hud : HudCore.huds) {
            if (!hud.isEnabled() || editingHuds.containsKey(hud)) continue;
            lines.add(hud.position.getX());
            lines.add(hud.position.getCenterX());
            lines.add(hud.position.getRightX());
        }
        return lines;
    }

    private float getYSnapping(long vg, float lineWidth, float y, float height, boolean multipleSides) {
        ArrayList<Float> lines = getYSnappingLines();
        ArrayList<SnappingLine> snappingLines = new ArrayList<>();
        float closest = (float) (SNAPPING_DISTANCE / UResolution.getScaleFactor());
        for (Float line : lines) {
            SnappingLine snappingLine = new SnappingLine(line, y, height, multipleSides);
            if (Math.round(snappingLine.getDistance()) == Math.round(closest)) snappingLines.add(snappingLine);
            else if (snappingLine.getDistance() < closest) {
                closest = snappingLine.getDistance();
                snappingLines.clear();
                snappingLines.add(snappingLine);
            }
        }
        if (snappingLines.isEmpty()) return y;
        for (SnappingLine snappingLine : snappingLines) {
            snappingLine.drawLine(vg, lineWidth, false);
        }
        return snappingLines.get(0).getPosition();
    }

    private ArrayList<Float> getYSnappingLines() {
        ArrayList<Float> lines = new ArrayList<>();
        lines.add(UResolution.getScaledHeight() / 2f);
        for (Hud hud : HudCore.huds) {
            if (!hud.isEnabled() || editingHuds.containsKey(hud)) continue;
            lines.add(hud.position.getY());
            lines.add(hud.position.getCenterY());
            lines.add(hud.position.getBottomY());
        }
        return lines;
    }
}