aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/mbgui/MBGuiGroupFloating.java
blob: 91cca2d73db1f93404970bf495781bd2fa8cc8c8 (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
package io.github.moulberry.notenoughupdates.mbgui;

import org.lwjgl.util.vector.Vector2f;

import java.util.*;

public class MBGuiGroupFloating extends MBGuiGroup {

    private LinkedHashMap<MBGuiElement, MBAnchorPoint> children;

    public MBGuiGroupFloating(int width, int height, LinkedHashMap<MBGuiElement, MBAnchorPoint> children) {
        this.width = width;
        this.height = height;
        this.children = children;
        recalculate();
    }

    public Map<MBGuiElement, MBAnchorPoint> getChildrenMap() {
        return Collections.unmodifiableMap(children);
    }

    @Override
    public void recalculate() {
        for(MBGuiElement child : children.keySet()) {
            child.recalculate();
        }

        for(Map.Entry<MBGuiElement, MBAnchorPoint> entry : children.entrySet()) {
            MBGuiElement child = entry.getKey();
            MBAnchorPoint anchorPoint = entry.getValue();
            float x = anchorPoint.anchorPoint.x * width - anchorPoint.anchorPoint.x * child.getWidth() + anchorPoint.offset.x;
            float y = anchorPoint.anchorPoint.y * height - anchorPoint.anchorPoint.y * child.getHeight() + anchorPoint.offset.y;
            childrenPosition.put(child, new Vector2f(x, y));
        }
    }

    @Override
    public Collection<MBGuiElement> getChildren() {
        return children.keySet();
    }

}