| 1 | package net.minecraft.client.renderer.texture;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import org.jspecify.annotations.Nullable;
|
| 6 |
|
| 7 | @Environment(EnvType.CLIENT)
|
| 8 | public class DynamicAtlasTree implements DynamicAtlasTreeSlot {
|
| 9 | private final int x;
|
| 10 | private final int y;
|
| 11 | private final int width;
|
| 12 | private final int height;
|
| 13 | private @Nullable DynamicAtlasTree left;
|
| 14 | private @Nullable DynamicAtlasTree right;
|
| 15 | private boolean occupied;
|
| 16 |
|
| 17 | public DynamicAtlasTree(final int x, final int y, final int width, final int height) {
|
| 18 | this.x = x;
|
| 19 | this.y = y;
|
| 20 | this.width = width;
|
| 21 | this.height = height;
|
| 22 | }
|
| 23 |
|
| 24 | public @Nullable DynamicAtlasTreeSlot insert(final int slotWidth, final int slotHeight, final int spacing) {
|
| 25 | return this.insertInner(slotWidth, slotHeight, spacing);
|
| 26 | }
|
| 27 |
|
| 28 | private @Nullable DynamicAtlasTree insertInner(final int slotWidth, final int slotHeight, final int spacing) {
|
| 29 | if (this.left != null && this.right != null) {
|
| 30 | DynamicAtlasTree newNode = this.left.insertInner(slotWidth, slotHeight, spacing);
|
| 31 | if (newNode == null) {
|
| 32 | newNode = this.right.insertInner(slotWidth, slotHeight, spacing);
|
| 33 | }
|
| 34 |
|
| 35 | return newNode;
|
| 36 | } else {
|
| 37 | if (this.occupied) {
|
| 38 | return null;
|
| 39 | }
|
| 40 |
|
| 41 | if (slotWidth > this.width || slotHeight > this.height) {
|
| 42 | return null;
|
| 43 | }
|
| 44 |
|
| 45 | if (slotWidth == this.width && slotHeight == this.height) {
|
| 46 | this.occupied = true;
|
| 47 | return this;
|
| 48 | }
|
| 49 |
|
| 50 | int deltaWidth = this.width - slotWidth;
|
| 51 | int deltaHeight = this.height - slotHeight;
|
| 52 | if (deltaWidth > deltaHeight) {
|
| 53 | this.left = new DynamicAtlasTree(this.x, this.y, slotWidth, this.height);
|
| 54 | this.right = new DynamicAtlasTree(this.x + slotWidth + spacing, this.y, this.width - slotWidth - spacing, this.height);
|
| 55 | } else {
|
| 56 | this.left = new DynamicAtlasTree(this.x, this.y, this.width, slotHeight);
|
| 57 | this.right = new DynamicAtlasTree(this.x, this.y + slotHeight + spacing, this.width, this.height - slotHeight - spacing);
|
| 58 | }
|
| 59 |
|
| 60 | return this.left.insertInner(slotWidth, slotHeight, spacing);
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | public int x() {
|
| 66 | return this.x;
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public int y() {
|
| 71 | return this.y;
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public int width() {
|
| 76 | return this.width;
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | public int height() {
|
| 81 | return this.height;
|
| 82 | }
|
| 83 | }
|
| 84 |
|