aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/skyblock/fancybars/BarGrid.java
blob: b85f85e1215593848c08ecf416a3dd27fa04a42c (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
package de.hysky.skyblocker.skyblock.fancybars;

import java.util.LinkedList;
import java.util.List;

public class BarGrid {
    private final LinkedList<LinkedList<StatusBar>> top = new LinkedList<>();
    private final LinkedList<LinkedList<StatusBar>> bottomLeft = new LinkedList<>();
    private final LinkedList<LinkedList<StatusBar>> bottomRight = new LinkedList<>();

    public BarGrid() {}

    public void add(int row, StatusBar bar, boolean right) {
        if (row > 0)
            top.get(row).add(bar);
        else if (row < 0) {
            if (right) {
                bottomRight.get(Math.abs(row)).add(bar);
            } else {
                bottomLeft.get(Math.abs(row)).add(bar);
            }
        }
    }

    public void add(int x, int y, StatusBar bar) {
        if (y > 0) {
            if (x < 1) throw new IllegalArgumentException("x can't be negative, x: " + x);
            LinkedList<StatusBar> statusBars = top.get(y-1);
            statusBars.add(Math.min(x-1, statusBars.size()), bar);
            bar.gridY = y;
            bar.gridX = statusBars.indexOf(bar)+1;
        } else if (y < 0) {
            LinkedList<StatusBar> statusBars = (x < 0? bottomLeft: bottomRight).get(Math.abs(y)-1);
            statusBars.add(Math.min(Math.abs(x)-1, statusBars.size()), bar);
            bar.gridY = y;
            bar.gridX = -(statusBars.indexOf(bar)+1);
        }
    }

    public List<StatusBar> getRow(int row, boolean right) {
        if (row > 0) {
            return top.get(row-1);
        } else {
            return (right ? bottomRight: bottomLeft).get(Math.abs(row)-1);
        }
    }

    public void addRow(int row, boolean right) {
        if (row>0) {
            top.add(row-1, new LinkedList<>());
        } else if (row<0) {
            (right ? bottomRight: bottomLeft).add(Math.abs(row)-1, new LinkedList<>());
        }
    }

    public void remove(int x, int y) {
        if (y > 0) {
            if (x < 1) throw new IllegalArgumentException("x can't be negative, x: " + x);
            LinkedList<StatusBar> statusBars = top.get(y-1);
            StatusBar remove = statusBars.remove(x-1);
            for (int i = x-1; i < statusBars.size(); i++) {
                statusBars.get(i).gridX--;
            }
            remove.gridX = 0;
            remove.gridY = 0;
            if (statusBars.isEmpty()) {
                top.remove(y - 1);
                for (int i = y-1; i < top.size(); i++) {
                    for (StatusBar bar : top.get(i)) {
                        bar.gridY--;
                    }
                }
            }
        } else if (y < 0) {
            LinkedList<LinkedList<StatusBar>> bottom = x < 0 ? bottomLeft : bottomRight;
            LinkedList<StatusBar> statusBars = bottom.get(Math.abs(y)-1);
            StatusBar remove = statusBars.remove(Math.abs(x) - 1);
            for (int i = Math.abs(x)-1; i < statusBars.size(); i++) {
                statusBars.get(i).gridX--;
            }
            remove.gridX = 0;
            remove.gridY = 0;
            if (statusBars.isEmpty()) {
                bottom.remove(Math.abs(y) - 1);
                for (int i = Math.abs(y)-1; i < bottom.size(); i++) {
                    for (StatusBar bar : bottom.get(i)) {
                        bar.gridY--;
                    }
                }
            }
        }
    }
}