File size: 8,525 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
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
package net.minecraft.client.renderer.texture;

import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class Stitcher<T extends Stitcher.Entry> {
    private static final Comparator<Stitcher.Holder<?>> HOLDER_COMPARATOR = Comparator.<Stitcher.Holder<?>, Integer>comparing(p_118201_ -> -p_118201_.height)
        .thenComparing(p_118199_ -> -p_118199_.width)
        .thenComparing(p_247945_ -> p_247945_.entry.name());
    private final int mipLevel;
    private final List<Stitcher.Holder<T>> texturesToBeStitched = new ArrayList<>();
    private final List<Stitcher.Region<T>> storage = new ArrayList<>();
    private int storageX;
    private int storageY;
    private final int maxWidth;
    private final int maxHeight;

    public Stitcher(int p_118171_, int p_118172_, int p_118173_) {
        this.mipLevel = p_118173_;
        this.maxWidth = p_118171_;
        this.maxHeight = p_118172_;
    }

    public int getWidth() {
        return this.storageX;
    }

    public int getHeight() {
        return this.storageY;
    }

    public void registerSprite(T p_249253_) {
        Stitcher.Holder<T> holder = new Stitcher.Holder<>(p_249253_, this.mipLevel);
        this.texturesToBeStitched.add(holder);
    }

    public void stitch() {
        List<Stitcher.Holder<T>> list = new ArrayList<>(this.texturesToBeStitched);
        list.sort(HOLDER_COMPARATOR);

        for (Stitcher.Holder<T> holder : list) {
            if (!this.addToStorage(holder)) {
                throw new StitcherException(holder.entry, list.stream().map(p_247946_ -> p_247946_.entry).collect(ImmutableList.toImmutableList()));
            }
        }
    }

    public void gatherSprites(Stitcher.SpriteLoader<T> p_118181_) {
        for (Stitcher.Region<T> region : this.storage) {
            region.walk(p_118181_);
        }
    }

    static int smallestFittingMinTexel(int p_118189_, int p_118190_) {
        return (p_118189_ >> p_118190_) + ((p_118189_ & (1 << p_118190_) - 1) == 0 ? 0 : 1) << p_118190_;
    }

    private boolean addToStorage(Stitcher.Holder<T> p_118179_) {
        for (Stitcher.Region<T> region : this.storage) {
            if (region.add(p_118179_)) {
                return true;
            }
        }

        return this.expand(p_118179_);
    }

    private boolean expand(Stitcher.Holder<T> p_118192_) {
        int i = Mth.smallestEncompassingPowerOfTwo(this.storageX);
        int j = Mth.smallestEncompassingPowerOfTwo(this.storageY);
        int k = Mth.smallestEncompassingPowerOfTwo(this.storageX + p_118192_.width);
        int l = Mth.smallestEncompassingPowerOfTwo(this.storageY + p_118192_.height);
        boolean flag1 = k <= this.maxWidth;
        boolean flag2 = l <= this.maxHeight;
        if (!flag1 && !flag2) {
            return false;
        } else {
            boolean flag3 = flag1 && i != k;
            boolean flag4 = flag2 && j != l;
            boolean flag;
            if (flag3 ^ flag4) {
                flag = flag3;
            } else {
                flag = flag1 && i <= j;
            }

            Stitcher.Region<T> region;
            if (flag) {
                if (this.storageY == 0) {
                    this.storageY = l;
                }

                region = new Stitcher.Region<>(this.storageX, 0, k - this.storageX, this.storageY);
                this.storageX = k;
            } else {
                region = new Stitcher.Region<>(0, this.storageY, this.storageX, l - this.storageY);
                this.storageY = l;
            }

            region.add(p_118192_);
            this.storage.add(region);
            return true;
        }
    }

    @OnlyIn(Dist.CLIENT)
    public interface Entry {
        int width();

        int height();

        ResourceLocation name();
    }

    @OnlyIn(Dist.CLIENT)
    static record Holder<T extends Stitcher.Entry>(T entry, int width, int height) {
        public Holder(T p_250261_, int p_250127_) {
            this(p_250261_, Stitcher.smallestFittingMinTexel(p_250261_.width(), p_250127_), Stitcher.smallestFittingMinTexel(p_250261_.height(), p_250127_));
        }
    }

    @OnlyIn(Dist.CLIENT)
    public static class Region<T extends Stitcher.Entry> {
        private final int originX;
        private final int originY;
        private final int width;
        private final int height;
        @Nullable
        private List<Stitcher.Region<T>> subSlots;
        @Nullable
        private Stitcher.Holder<T> holder;

        public Region(int p_118216_, int p_118217_, int p_118218_, int p_118219_) {
            this.originX = p_118216_;
            this.originY = p_118217_;
            this.width = p_118218_;
            this.height = p_118219_;
        }

        public int getX() {
            return this.originX;
        }

        public int getY() {
            return this.originY;
        }

        public boolean add(Stitcher.Holder<T> p_118222_) {
            if (this.holder != null) {
                return false;
            } else {
                int i = p_118222_.width;
                int j = p_118222_.height;
                if (i <= this.width && j <= this.height) {
                    if (i == this.width && j == this.height) {
                        this.holder = p_118222_;
                        return true;
                    } else {
                        if (this.subSlots == null) {
                            this.subSlots = new ArrayList<>(1);
                            this.subSlots.add(new Stitcher.Region<>(this.originX, this.originY, i, j));
                            int k = this.width - i;
                            int l = this.height - j;
                            if (l > 0 && k > 0) {
                                int i1 = Math.max(this.height, k);
                                int j1 = Math.max(this.width, l);
                                if (i1 >= j1) {
                                    this.subSlots.add(new Stitcher.Region<>(this.originX, this.originY + j, i, l));
                                    this.subSlots.add(new Stitcher.Region<>(this.originX + i, this.originY, k, this.height));
                                } else {
                                    this.subSlots.add(new Stitcher.Region<>(this.originX + i, this.originY, k, j));
                                    this.subSlots.add(new Stitcher.Region<>(this.originX, this.originY + j, this.width, l));
                                }
                            } else if (k == 0) {
                                this.subSlots.add(new Stitcher.Region<>(this.originX, this.originY + j, i, l));
                            } else if (l == 0) {
                                this.subSlots.add(new Stitcher.Region<>(this.originX + i, this.originY, k, j));
                            }
                        }

                        for (Stitcher.Region<T> region : this.subSlots) {
                            if (region.add(p_118222_)) {
                                return true;
                            }
                        }

                        return false;
                    }
                } else {
                    return false;
                }
            }
        }

        public void walk(Stitcher.SpriteLoader<T> p_250195_) {
            if (this.holder != null) {
                p_250195_.load(this.holder.entry, this.getX(), this.getY());
            } else if (this.subSlots != null) {
                for (Stitcher.Region<T> region : this.subSlots) {
                    region.walk(p_250195_);
                }
            }
        }

        @Override
        public String toString() {
            return "Slot{originX="
                + this.originX
                + ", originY="
                + this.originY
                + ", width="
                + this.width
                + ", height="
                + this.height
                + ", texture="
                + this.holder
                + ", subSlots="
                + this.subSlots
                + "}";
        }
    }

    @OnlyIn(Dist.CLIENT)
    public interface SpriteLoader<T extends Stitcher.Entry> {
        void load(T p_249434_, int p_118230_, int p_118231_);
    }
}