File size: 6,577 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package net.minecraft.client.gui.layouts;

import com.mojang.math.Divisor;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;
import net.minecraft.Util;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class EqualSpacingLayout extends AbstractLayout {
    private final EqualSpacingLayout.Orientation orientation;
    private final List<EqualSpacingLayout.ChildContainer> children = new ArrayList<>();
    private final LayoutSettings defaultChildLayoutSettings = LayoutSettings.defaults();

    public EqualSpacingLayout(int p_298986_, int p_300198_, EqualSpacingLayout.Orientation p_297766_) {
        this(0, 0, p_298986_, p_300198_, p_297766_);
    }

    public EqualSpacingLayout(int p_300144_, int p_297966_, int p_301171_, int p_299404_, EqualSpacingLayout.Orientation p_300279_) {
        super(p_300144_, p_297966_, p_301171_, p_299404_);
        this.orientation = p_300279_;
    }

    @Override
    public void arrangeElements() {
        super.arrangeElements();
        if (!this.children.isEmpty()) {
            int i = 0;
            int j = this.orientation.getSecondaryLength(this);

            for (EqualSpacingLayout.ChildContainer equalspacinglayout$childcontainer : this.children) {
                i += this.orientation.getPrimaryLength(equalspacinglayout$childcontainer);
                j = Math.max(j, this.orientation.getSecondaryLength(equalspacinglayout$childcontainer));
            }

            int k = this.orientation.getPrimaryLength(this) - i;
            int l = this.orientation.getPrimaryPosition(this);
            Iterator<EqualSpacingLayout.ChildContainer> iterator = this.children.iterator();
            EqualSpacingLayout.ChildContainer equalspacinglayout$childcontainer1 = iterator.next();
            this.orientation.setPrimaryPosition(equalspacinglayout$childcontainer1, l);
            l += this.orientation.getPrimaryLength(equalspacinglayout$childcontainer1);
            if (this.children.size() >= 2) {
                Divisor divisor = new Divisor(k, this.children.size() - 1);

                while (divisor.hasNext()) {
                    l += divisor.nextInt();
                    EqualSpacingLayout.ChildContainer equalspacinglayout$childcontainer2 = iterator.next();
                    this.orientation.setPrimaryPosition(equalspacinglayout$childcontainer2, l);
                    l += this.orientation.getPrimaryLength(equalspacinglayout$childcontainer2);
                }
            }

            int i1 = this.orientation.getSecondaryPosition(this);

            for (EqualSpacingLayout.ChildContainer equalspacinglayout$childcontainer3 : this.children) {
                this.orientation.setSecondaryPosition(equalspacinglayout$childcontainer3, i1, j);
            }

            switch (this.orientation) {
                case HORIZONTAL:
                    this.height = j;
                    break;
                case VERTICAL:
                    this.width = j;
            }
        }
    }

    @Override
    public void visitChildren(Consumer<LayoutElement> p_299750_) {
        this.children.forEach(p_298818_ -> p_299750_.accept(p_298818_.child));
    }

    public LayoutSettings newChildLayoutSettings() {
        return this.defaultChildLayoutSettings.copy();
    }

    public LayoutSettings defaultChildLayoutSetting() {
        return this.defaultChildLayoutSettings;
    }

    public <T extends LayoutElement> T addChild(T p_301351_) {
        return this.addChild(p_301351_, this.newChildLayoutSettings());
    }

    public <T extends LayoutElement> T addChild(T p_299596_, LayoutSettings p_301383_) {
        this.children.add(new EqualSpacingLayout.ChildContainer(p_299596_, p_301383_));
        return p_299596_;
    }

    public <T extends LayoutElement> T addChild(T p_298517_, Consumer<LayoutSettings> p_297917_) {
        return this.addChild(p_298517_, Util.make(this.newChildLayoutSettings(), p_297917_));
    }

    @OnlyIn(Dist.CLIENT)
    static class ChildContainer extends AbstractLayout.AbstractChildWrapper {
        protected ChildContainer(LayoutElement p_298955_, LayoutSettings p_298136_) {
            super(p_298955_, p_298136_);
        }
    }

    @OnlyIn(Dist.CLIENT)
    public static enum Orientation {
        HORIZONTAL,
        VERTICAL;

        int getPrimaryLength(LayoutElement p_299718_) {
            return switch (this) {
                case HORIZONTAL -> p_299718_.getWidth();
                case VERTICAL -> p_299718_.getHeight();
            };
        }

        int getPrimaryLength(EqualSpacingLayout.ChildContainer p_298044_) {
            return switch (this) {
                case HORIZONTAL -> p_298044_.getWidth();
                case VERTICAL -> p_298044_.getHeight();
            };
        }

        int getSecondaryLength(LayoutElement p_301361_) {
            return switch (this) {
                case HORIZONTAL -> p_301361_.getHeight();
                case VERTICAL -> p_301361_.getWidth();
            };
        }

        int getSecondaryLength(EqualSpacingLayout.ChildContainer p_297317_) {
            return switch (this) {
                case HORIZONTAL -> p_297317_.getHeight();
                case VERTICAL -> p_297317_.getWidth();
            };
        }

        void setPrimaryPosition(EqualSpacingLayout.ChildContainer p_298745_, int p_300763_) {
            switch (this) {
                case HORIZONTAL:
                    p_298745_.setX(p_300763_, p_298745_.getWidth());
                    break;
                case VERTICAL:
                    p_298745_.setY(p_300763_, p_298745_.getHeight());
            }
        }

        void setSecondaryPosition(EqualSpacingLayout.ChildContainer p_299676_, int p_297698_, int p_297461_) {
            switch (this) {
                case HORIZONTAL:
                    p_299676_.setY(p_297698_, p_297461_);
                    break;
                case VERTICAL:
                    p_299676_.setX(p_297698_, p_297461_);
            }
        }

        int getPrimaryPosition(LayoutElement p_299240_) {
            return switch (this) {
                case HORIZONTAL -> p_299240_.getX();
                case VERTICAL -> p_299240_.getY();
            };
        }

        int getSecondaryPosition(LayoutElement p_299036_) {
            return switch (this) {
                case HORIZONTAL -> p_299036_.getY();
                case VERTICAL -> p_299036_.getX();
            };
        }
    }
}