| 1 | package net.minecraft.client.gui.navigation;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.util.Mth;
|
| 6 | import org.joml.Matrix3x2fc;
|
| 7 | import org.joml.Vector2f;
|
| 8 | import org.jspecify.annotations.Nullable;
|
| 9 |
|
| 10 | @Environment(EnvType.CLIENT)
|
| 11 | public record ScreenRectangle(ScreenPosition position, int width, int height) {
|
| 12 | private static final ScreenRectangle EMPTY = new ScreenRectangle(0, 0, 0, 0);
|
| 13 |
|
| 14 | public ScreenRectangle(final int x, final int y, final int width, final int height) {
|
| 15 | this(new ScreenPosition(x, y), width, height);
|
| 16 | }
|
| 17 |
|
| 18 | public static ScreenRectangle empty() {
|
| 19 | return EMPTY;
|
| 20 | }
|
| 21 |
|
| 22 | public static ScreenRectangle of(
|
| 23 | final ScreenAxis primaryAxis, final int primaryIndex, final int secondaryIndex, final int primaryLength, final int secondaryLength
|
| 24 | ) {
|
| 25 | return switch (primaryAxis) {
|
| 26 | case HORIZONTAL -> new ScreenRectangle(primaryIndex, secondaryIndex, primaryLength, secondaryLength);
|
| 27 | case VERTICAL -> new ScreenRectangle(secondaryIndex, primaryIndex, secondaryLength, primaryLength);
|
| 28 | };
|
| 29 | }
|
| 30 |
|
| 31 | public ScreenRectangle step(final ScreenDirection direction) {
|
| 32 | return new ScreenRectangle(this.position.step(direction), this.width, this.height);
|
| 33 | }
|
| 34 |
|
| 35 | public int getLength(final ScreenAxis axis) {
|
| 36 | return switch (axis) {
|
| 37 | case HORIZONTAL -> this.width;
|
| 38 | case VERTICAL -> this.height;
|
| 39 | };
|
| 40 | }
|
| 41 |
|
| 42 | public int getBoundInDirection(final ScreenDirection direction) {
|
| 43 | ScreenAxis axis = direction.getAxis();
|
| 44 | return direction.isPositive() ? this.position.getCoordinate(axis) + this.getLength(axis) - 1 : this.position.getCoordinate(axis);
|
| 45 | }
|
| 46 |
|
| 47 | public ScreenRectangle getBorder(final ScreenDirection direction) {
|
| 48 | int startFirst = this.getBoundInDirection(direction);
|
| 49 | ScreenAxis orthogonalAxis = direction.getAxis().orthogonal();
|
| 50 | int startSecond = this.getBoundInDirection(orthogonalAxis.getNegative());
|
| 51 | int length = this.getLength(orthogonalAxis);
|
| 52 | return of(direction.getAxis(), startFirst, startSecond, 1, length).step(direction);
|
| 53 | }
|
| 54 |
|
| 55 | public boolean overlaps(final ScreenRectangle other) {
|
| 56 | return this.overlapsInAxis(other, ScreenAxis.HORIZONTAL) && this.overlapsInAxis(other, ScreenAxis.VERTICAL);
|
| 57 | }
|
| 58 |
|
| 59 | public boolean overlapsInAxis(final ScreenRectangle other, final ScreenAxis axis) {
|
| 60 | int thisLower = this.getBoundInDirection(axis.getNegative());
|
| 61 | int otherLower = other.getBoundInDirection(axis.getNegative());
|
| 62 | int thisHigher = this.getBoundInDirection(axis.getPositive());
|
| 63 | int otherHigher = other.getBoundInDirection(axis.getPositive());
|
| 64 | return Math.max(thisLower, otherLower) <= Math.min(thisHigher, otherHigher);
|
| 65 | }
|
| 66 |
|
| 67 | public int getCenterInAxis(final ScreenAxis axis) {
|
| 68 | return (this.getBoundInDirection(axis.getPositive()) + this.getBoundInDirection(axis.getNegative())) / 2;
|
| 69 | }
|
| 70 |
|
| 71 | public @Nullable ScreenRectangle intersection(final ScreenRectangle other) {
|
| 72 | int left = Math.max(this.left(), other.left());
|
| 73 | int top = Math.max(this.top(), other.top());
|
| 74 | int right = Math.min(this.right(), other.right());
|
| 75 | int bottom = Math.min(this.bottom(), other.bottom());
|
| 76 | return left < right && top < bottom ? new ScreenRectangle(left, top, right - left, bottom - top) : null;
|
| 77 | }
|
| 78 |
|
| 79 | public boolean intersects(final ScreenRectangle other) {
|
| 80 | return this.left() < other.right() && this.right() > other.left() && this.top() < other.bottom() && this.bottom() > other.top();
|
| 81 | }
|
| 82 |
|
| 83 | public boolean encompasses(final ScreenRectangle other) {
|
| 84 | return other.left() >= this.left() && other.top() >= this.top() && other.right() <= this.right() && other.bottom() <= this.bottom();
|
| 85 | }
|
| 86 |
|
| 87 | public int top() {
|
| 88 | return this.position.y();
|
| 89 | }
|
| 90 |
|
| 91 | public int bottom() {
|
| 92 | return this.position.y() + this.height;
|
| 93 | }
|
| 94 |
|
| 95 | public int left() {
|
| 96 | return this.position.x();
|
| 97 | }
|
| 98 |
|
| 99 | public int right() {
|
| 100 | return this.position.x() + this.width;
|
| 101 | }
|
| 102 |
|
| 103 | public boolean containsPoint(final int x, final int y) {
|
| 104 | return x >= this.left() && x < this.right() && y >= this.top() && y < this.bottom();
|
| 105 | }
|
| 106 |
|
| 107 | public ScreenRectangle transformAxisAligned(final Matrix3x2fc matrix) {
|
| 108 | Vector2f topLeft = matrix.transformPosition(this.left(), this.top(), new Vector2f());
|
| 109 | Vector2f bottomRight = matrix.transformPosition(this.right(), this.bottom(), new Vector2f());
|
| 110 | return new ScreenRectangle(Mth.floor(topLeft.x), Mth.floor(topLeft.y), Mth.floor(bottomRight.x - topLeft.x), Mth.floor(bottomRight.y - topLeft.y));
|
| 111 | }
|
| 112 |
|
| 113 | public ScreenRectangle transformMaxBounds(final Matrix3x2fc matrix) {
|
| 114 | Vector2f topLeft = matrix.transformPosition(this.left(), this.top(), new Vector2f());
|
| 115 | Vector2f topRight = matrix.transformPosition(this.right(), this.top(), new Vector2f());
|
| 116 | Vector2f bottomLeft = matrix.transformPosition(this.left(), this.bottom(), new Vector2f());
|
| 117 | Vector2f bottomRight = matrix.transformPosition(this.right(), this.bottom(), new Vector2f());
|
| 118 | float minX = Math.min(Math.min(topLeft.x(), bottomLeft.x()), Math.min(topRight.x(), bottomRight.x()));
|
| 119 | float maxX = Math.max(Math.max(topLeft.x(), bottomLeft.x()), Math.max(topRight.x(), bottomRight.x()));
|
| 120 | float minY = Math.min(Math.min(topLeft.y(), bottomLeft.y()), Math.min(topRight.y(), bottomRight.y()));
|
| 121 | float maxY = Math.max(Math.max(topLeft.y(), bottomLeft.y()), Math.max(topRight.y(), bottomRight.y()));
|
| 122 | return new ScreenRectangle(Mth.floor(minX), Mth.floor(minY), Mth.ceil(maxX - minX), Mth.ceil(maxY - minY));
|
| 123 | }
|
| 124 | }
|
| 125 |
|