Skip to content

Commit 26be1c4

Browse files
committed
chore: introduced FlexRect
Makes it easier to deal with rectangles that should be based on the size of enclosing rectangles
1 parent 0b69e07 commit 26be1c4

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.codejive.context.terminal;
2+
3+
public class FlexRect {
4+
private final int left, top, width, height;
5+
6+
public FlexRect(int left, int top, int width, int height) {
7+
this.left = left;
8+
this.top = top;
9+
this.width = width;
10+
this.height = height;
11+
}
12+
13+
public Rect actualRect(Size availableSize) {
14+
Rect availableRect = new Rect(0, 0, availableSize.width(), availableSize.height());
15+
int w = width >= 0 ? width : availableSize.width() - left + width + 1;
16+
int h = height >= 0 ? height : availableSize.height() - top + height + 1;
17+
return new Rect(left, top, w, h).limited(availableRect);
18+
}
19+
}

src/main/java/org/codejive/context/terminal/Rect.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ public Rect grow(int leftAmount, int topAmount, int rightAmount, int bottomAmoun
5151
Math.max(height() + topAmount + bottomAmount, 0));
5252
}
5353

54+
public Rect limited(Rect availableRect) {
55+
int l = Math.max(left, availableRect.left());
56+
int t = Math.max(top, availableRect.top());
57+
int r = Math.min(right(), availableRect.right());
58+
int b = Math.min(bottom(), availableRect.bottom());
59+
return new Rect(l, t, r - l + 1, b - t + 1);
60+
}
61+
5462
@Override
5563
public String toString() {
5664
return "Rect{"

0 commit comments

Comments
 (0)