Skip to content

Commit a4a74e3

Browse files
committed
fix: createFromMinAndSize clamps and validates result
On TeraMath classes `Rect2i` and `Rect2f` had the semantics that creating them from min and size would validate that the size is positive along both axis. The JOML utility class was missing this feature. This also adds overflow prevention.
1 parent b228db2 commit a4a74e3

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

nui/src/main/java/org/terasology/nui/util/RectUtility.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,30 @@
66
import org.joml.Vector2i;
77

88
public final class RectUtility {
9+
910
private RectUtility() {
1011
}
1112

1213
public static Rectanglei createFromMinAndSize(int minX, int minY, int width, int height) {
13-
return new Rectanglei(minX, minY, minX + width, minY + height);
14+
final int maxX = NUIMathUtil.addClampAtMax(minX, width);
15+
final int maxY = NUIMathUtil.addClampAtMax(minY, height);
16+
final Rectanglei rect = new Rectanglei(minX, minY, maxX, maxY);
17+
return rect.isValid() ? rect : new Rectanglei();
1418
}
1519

1620
public static Rectanglei createFromMinAndSize(Vector2i min, Vector2i size) {
17-
return new Rectanglei(min, min.add(size, new Vector2i()));
21+
return createFromMinAndSize(min.x, min.y, size.x, size.y);
1822
}
1923

2024
public static Rectanglef createFromMinAndSize(float minX, float minY, float width, float height) {
21-
return new Rectanglef(minX, minY, minX + width, minY + height);
25+
final float maxX = NUIMathUtil.addClampAtMax(minX, width);
26+
final float maxY = NUIMathUtil.addClampAtMax(minY, height);
27+
final Rectanglef rect = new Rectanglef(minX, minY, maxX, maxY);
28+
return rect.isValid() ? rect : new Rectanglef();
2229
}
2330

2431
public static Rectanglef createFromMinAndSize(Vector2f min, Vector2f size) {
25-
return new Rectanglef(min, min.add(size, new Vector2f()));
32+
return createFromMinAndSize(min.x, min.y, size.x, size.y);
2633
}
2734

2835
public static Rectanglef createFromCenterAndSize(Vector2f center, Vector2f size) {

0 commit comments

Comments
 (0)