Skip to content

Commit cd38592

Browse files
committed
fix coverSize when widget has no children
1 parent 89b8237 commit cd38592

File tree

2 files changed

+112
-86
lines changed

2 files changed

+112
-86
lines changed

src/main/java/com/cleanroommc/modularui/widget/sizer/DimensionSizer.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,23 @@ public int postApply(Area area, Area relativeTo, int p0, int p1) {
250250
return moveAmount;
251251
}
252252

253+
public void coverChildrenForEmpty(Area area, Area relativeTo) {
254+
int s = 0;
255+
area.setSize(this.axis, s);
256+
this.sizeCalculated = true;
257+
if (!isPosCalculated()) {
258+
int p;
259+
if (this.start != null) {
260+
p = calcPoint(this.start, s, relativeTo.getSize(this.axis), true);
261+
} else if (this.end != null) {
262+
p = calcPoint(this.end, s, relativeTo.getSize(this.axis), true) - s;
263+
} else {
264+
p = area.getRelativePoint(this.axis);
265+
}
266+
area.setRelativePoint(this.axis, p);
267+
}
268+
}
269+
253270
public void applyMarginAndPaddingToPos(Area area, Area relativeTo) {
254271
// apply self margin and parent padding if not done yet
255272
if (isMarginPaddingApplied()) return;

src/main/java/com/cleanroommc/modularui/widget/sizer/Flex.java

Lines changed: 95 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -350,113 +350,122 @@ public boolean resize(IGuiElement guiElement) {
350350
@Override
351351
public boolean postResize(IGuiElement guiElement) {
352352
if (!this.x.dependsOnChildren() && !this.y.dependsOnChildren()) return isFullyCalculated();
353+
if (!(this.parent instanceof IWidget widget) || !widget.hasChildren()) {
354+
coverChildrenForEmpty();
355+
return isFullyCalculated();
356+
}
353357
if (this.parent instanceof ILayoutWidget) {
354358
// layout widgets handle widget layout's themselves, so we only need to fit the right and bottom border
355-
coverChildrenForLayout();
359+
coverChildrenForLayout(widget);
356360
return isFullyCalculated();
357361
}
358362
// non layout widgets can have their children in any position
359363
// we try to wrap all edges as close as possible to all widgets
360364
// this means for each edge there is at least one widget that touches it (plus padding and margin)
361365

362366
// children are now calculated and now this area can be calculated if it requires childrens area
363-
List<IWidget> children = ((IWidget) this.parent).getChildren();
364-
if (!children.isEmpty()) {
365-
int moveChildrenX = 0, moveChildrenY = 0;
366-
367-
Box padding = this.parent.getArea().getPadding();
368-
// first calculate the area the children span
369-
int x0 = Integer.MAX_VALUE, x1 = Integer.MIN_VALUE, y0 = Integer.MAX_VALUE, y1 = Integer.MIN_VALUE;
370-
int w = 0, h = 0;
371-
for (IWidget child : children) {
372-
Box margin = child.getArea().getMargin();
373-
IResizeable resizeable = child.resizer();
374-
Area area = child.getArea();
375-
if (this.x.dependsOnChildren() && resizeable.isWidthCalculated()) {
376-
// minimum width this widget requests
377-
w = Math.max(w, area.requestedWidth() + padding.horizontal());
378-
if (resizeable.isXCalculated()) {
379-
// if pos is calculated use that
380-
x0 = Math.min(x0, area.rx - padding.left - margin.left);
381-
x1 = Math.max(x1, area.rx + area.width + padding.right + margin.right);
382-
}
383-
}
384-
if (this.y.dependsOnChildren() && resizeable.isHeightCalculated()) {
385-
h = Math.max(h, area.requestedHeight() + padding.vertical());
386-
if (resizeable.isYCalculated()) {
387-
y0 = Math.min(y0, area.ry - padding.top - margin.top);
388-
y1 = Math.max(y1, area.ry + area.height + padding.bottom + margin.bottom);
389-
}
367+
List<IWidget> children = widget.getChildren();
368+
int moveChildrenX = 0, moveChildrenY = 0;
369+
370+
Box padding = this.parent.getArea().getPadding();
371+
// first calculate the area the children span
372+
int x0 = Integer.MAX_VALUE, x1 = Integer.MIN_VALUE, y0 = Integer.MAX_VALUE, y1 = Integer.MIN_VALUE;
373+
int w = 0, h = 0;
374+
for (IWidget child : children) {
375+
Box margin = child.getArea().getMargin();
376+
IResizeable resizeable = child.resizer();
377+
Area area = child.getArea();
378+
if (this.x.dependsOnChildren() && resizeable.isWidthCalculated()) {
379+
// minimum width this widget requests
380+
w = Math.max(w, area.requestedWidth() + padding.horizontal());
381+
if (resizeable.isXCalculated()) {
382+
// if pos is calculated use that
383+
x0 = Math.min(x0, area.rx - padding.left - margin.left);
384+
x1 = Math.max(x1, area.rx + area.width + padding.right + margin.right);
390385
}
391386
}
392-
if (x1 == Integer.MIN_VALUE) x1 = 0;
393-
if (y1 == Integer.MIN_VALUE) y1 = 0;
394-
if (x0 == Integer.MAX_VALUE) x0 = 0;
395-
if (y0 == Integer.MAX_VALUE) y0 = 0;
396-
if (w > x1 - x0)
397-
x1 = x0 + w; // we found at least one widget which was wider than what was calculated by start and end pos
398-
if (h > y1 - y0) y1 = y0 + h;
399-
400-
// now calculate new x, y, width and height based on the childrens area
401-
Area relativeTo = getRelativeTo().getArea();
402-
if (this.x.dependsOnChildren()) {
403-
// apply the size to this widget
404-
// the return value is the amount of pixels we need to move the children
405-
moveChildrenX = this.x.postApply(this.parent.getArea(), relativeTo, x0, x1);
406-
}
407-
if (this.y.dependsOnChildren()) {
408-
moveChildrenY = this.y.postApply(this.parent.getArea(), relativeTo, y0, y1);
409-
}
410-
// since the edges might have been moved closer to the widgets, the widgets should move back into it's original (absolute) position
411-
if (moveChildrenX != 0 || moveChildrenY != 0) {
412-
for (IWidget widget : children) {
413-
Area area = widget.getArea();
414-
IResizeable resizeable = widget.resizer();
415-
if (resizeable.isXCalculated()) area.rx += moveChildrenX;
416-
if (resizeable.isYCalculated()) area.ry += moveChildrenY;
387+
if (this.y.dependsOnChildren() && resizeable.isHeightCalculated()) {
388+
h = Math.max(h, area.requestedHeight() + padding.vertical());
389+
if (resizeable.isYCalculated()) {
390+
y0 = Math.min(y0, area.ry - padding.top - margin.top);
391+
y1 = Math.max(y1, area.ry + area.height + padding.bottom + margin.bottom);
417392
}
418393
}
419394
}
395+
if (x1 == Integer.MIN_VALUE) x1 = 0;
396+
if (y1 == Integer.MIN_VALUE) y1 = 0;
397+
if (x0 == Integer.MAX_VALUE) x0 = 0;
398+
if (y0 == Integer.MAX_VALUE) y0 = 0;
399+
if (w > x1 - x0)
400+
x1 = x0 + w; // we found at least one widget which was wider than what was calculated by start and end pos
401+
if (h > y1 - y0) y1 = y0 + h;
402+
403+
// now calculate new x, y, width and height based on the childrens area
404+
Area relativeTo = getRelativeTo().getArea();
405+
if (this.x.dependsOnChildren()) {
406+
// apply the size to this widget
407+
// the return value is the amount of pixels we need to move the children
408+
moveChildrenX = this.x.postApply(this.parent.getArea(), relativeTo, x0, x1);
409+
}
410+
if (this.y.dependsOnChildren()) {
411+
moveChildrenY = this.y.postApply(this.parent.getArea(), relativeTo, y0, y1);
412+
}
413+
// since the edges might have been moved closer to the widgets, the widgets should move back into it's original (absolute) position
414+
if (moveChildrenX != 0 || moveChildrenY != 0) {
415+
for (IWidget child : children) {
416+
Area area = child.getArea();
417+
IResizeable resizeable = child.resizer();
418+
if (resizeable.isXCalculated()) area.rx += moveChildrenX;
419+
if (resizeable.isYCalculated()) area.ry += moveChildrenY;
420+
}
421+
}
420422
return isFullyCalculated();
421423
}
422424

423-
private void coverChildrenForLayout() {
424-
List<IWidget> children = ((IWidget) this.parent).getChildren();
425-
if (!children.isEmpty()) {
426-
Box padding = this.parent.getArea().getPadding();
427-
// first calculate the area the children span
428-
int x1 = Integer.MIN_VALUE, y1 = Integer.MIN_VALUE;
429-
int w = 0, h = 0;
430-
for (IWidget child : children) {
431-
Box margin = child.getArea().getMargin();
432-
IResizeable resizeable = child.resizer();
433-
Area area = child.getArea();
434-
if (this.x.dependsOnChildren() && resizeable.isWidthCalculated()) {
435-
w = Math.max(w, area.requestedWidth() + padding.horizontal());
436-
if (resizeable.isXCalculated()) {
437-
x1 = Math.max(x1, area.rx + area.width + padding.right + margin.right);
438-
}
425+
private void coverChildrenForLayout(IWidget widget) {
426+
List<IWidget> children = widget.getChildren();
427+
Box padding = this.parent.getArea().getPadding();
428+
// first calculate the area the children span
429+
int x1 = Integer.MIN_VALUE, y1 = Integer.MIN_VALUE;
430+
int w = 0, h = 0;
431+
for (IWidget child : children) {
432+
Box margin = child.getArea().getMargin();
433+
IResizeable resizeable = child.resizer();
434+
Area area = child.getArea();
435+
if (this.x.dependsOnChildren() && resizeable.isWidthCalculated()) {
436+
w = Math.max(w, area.requestedWidth() + padding.horizontal());
437+
if (resizeable.isXCalculated()) {
438+
x1 = Math.max(x1, area.rx + area.width + padding.right + margin.right);
439439
}
440-
if (this.y.dependsOnChildren() && resizeable.isHeightCalculated()) {
441-
h = Math.max(h, area.requestedHeight() + padding.vertical());
442-
if (resizeable.isXCalculated()) {
443-
y1 = Math.max(y1, area.ry + area.height + padding.bottom + margin.bottom);
444-
}
445-
}
446-
}
447-
if (x1 == Integer.MIN_VALUE) x1 = 0;
448-
if (y1 == Integer.MIN_VALUE) y1 = 0;
449-
if (w > x1) x1 = w;
450-
if (h > y1) y1 = h;
451-
452-
Area relativeTo = getRelativeTo().getArea();
453-
if (this.x.dependsOnChildren()) {
454-
this.x.postApply(getArea(), relativeTo, 0, x1);
455440
}
456-
if (this.y.dependsOnChildren()) {
457-
this.y.postApply(getArea(), relativeTo, 0, y1);
441+
if (this.y.dependsOnChildren() && resizeable.isHeightCalculated()) {
442+
h = Math.max(h, area.requestedHeight() + padding.vertical());
443+
if (resizeable.isXCalculated()) {
444+
y1 = Math.max(y1, area.ry + area.height + padding.bottom + margin.bottom);
445+
}
458446
}
459447
}
448+
if (x1 == Integer.MIN_VALUE) x1 = 0;
449+
if (y1 == Integer.MIN_VALUE) y1 = 0;
450+
if (w > x1) x1 = w;
451+
if (h > y1) y1 = h;
452+
453+
Area relativeTo = getRelativeTo().getArea();
454+
if (this.x.dependsOnChildren()) {
455+
this.x.postApply(getArea(), relativeTo, 0, x1);
456+
}
457+
if (this.y.dependsOnChildren()) {
458+
this.y.postApply(getArea(), relativeTo, 0, y1);
459+
}
460+
}
461+
462+
private void coverChildrenForEmpty() {
463+
if (this.x.dependsOnChildren()) {
464+
this.x.coverChildrenForEmpty(this.parent.getArea(), getRelativeTo().getArea());
465+
}
466+
if (this.y.dependsOnChildren()) {
467+
this.y.coverChildrenForEmpty(this.parent.getArea(), getRelativeTo().getArea());
468+
}
460469
}
461470

462471
@Override

0 commit comments

Comments
 (0)