Skip to content

Commit f35155e

Browse files
committed
fix scrollbar overlapping with content & fix cross axis padding
1 parent 97065b3 commit f35155e

3 files changed

Lines changed: 26 additions & 19 deletions

File tree

src/main/java/com/cleanroommc/modularui/test/TestGuis.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ public TestGuis() {
9393
super(ModularUI.ID);
9494
}
9595

96+
/**
97+
* This method finds all 'build___UI' methods in this class via reflection and adds a button that opens that UI to a list widget.
98+
* This makes it very convenient to add and test test-screens without having to swap out the screen that the diamond item opens.
99+
*/
96100
@Override
97101
public @NotNull ModularPanel buildUI(ModularGuiContext context) {
98102
// collect all test from all build methods in this class via reflection
@@ -124,6 +128,7 @@ public TestGuis() {
124128
try {
125129
ModularPanel panel = (ModularPanel) m.invoke(null);
126130
if (TestGuis.withCode) {
131+
// WIP: this is meant to put an image of the code next to ui for showcase purpose
127132
panel.child(UITexture.builder()
128133
.location("gui/code/" + codeTextureName)
129134
.build()

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ public String getText(GuiAxis axis) {
2727
}
2828
}
2929

30-
public static final byte UNUSED = -2;
31-
public static final byte DEFAULT = -1;
32-
public static final byte START = 0;
33-
public static final byte END = 1;
34-
public static final byte SIZE = 2;
35-
3630
private boolean autoAnchor = true;
3731
private float value = 0f;
3832
private DoubleSupplier valueSupplier = null;

src/main/java/com/cleanroommc/modularui/widgets/layout/Flow.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cleanroommc.modularui.widgets.layout;
22

3+
import com.cleanroommc.modularui.ModularUI;
34
import com.cleanroommc.modularui.api.GuiAxis;
45
import com.cleanroommc.modularui.api.layout.ILayoutWidget;
56
import com.cleanroommc.modularui.api.widget.IWidget;
@@ -210,34 +211,41 @@ public boolean postLayoutWidgets() {
210211
public static boolean layoutCrossAxisListLike(IWidget parent, GuiAxis axis, Alignment.CrossAxis caa, boolean reverseLayout) {
211212
if (!parent.hasChildren()) return true;
212213
GuiAxis other = axis.getOther();
213-
int width = parent.getArea().getSize(other);
214+
int availableSize = parent.getArea().getSize(other);
214215
Box padding = parent.getArea().getPadding();
215-
boolean hasWidth = parent.resizer().isSizeCalculated(other);
216-
if (!hasWidth && caa != Alignment.CrossAxis.START) return false;
216+
boolean hasSize = parent.resizer().isSizeCalculated(other);
217+
if (!hasSize && caa != Alignment.CrossAxis.START) return false;
217218
List<IWidget> childrenList = reverseLayout ? new ReversedList<>(parent.getChildren()) : parent.getChildren();
218219
for (IWidget widget : childrenList) {
219220
// exclude children whose position of main axis is fixed
220221
if (widget.resizer().hasPos(axis)) continue;
221222
Box margin = widget.getArea().getMargin();
222223
// don't align auto positioned children in cross axis
223224
if (!widget.resizer().hasPos(other) && widget.resizer().isSizeCalculated(other)) {
224-
int crossAxisPos = margin.getStart(other) + padding.getStart(other);
225-
if (hasWidth) {
226-
if (caa == Alignment.CrossAxis.CENTER) {
227-
crossAxisPos = (int) (width / 2f - widget.getArea().getSize(other) / 2f);
228-
} else if (caa == Alignment.CrossAxis.END) {
229-
crossAxisPos = width - widget.getArea().getSize(other) - margin.getEnd(other) - padding.getStart(other);
225+
int start = margin.getStart(other) + padding.getStart(other);
226+
int crossAxisPos = 0;
227+
if (caa == Alignment.CrossAxis.START) {
228+
crossAxisPos = start;
229+
} else {
230+
int end = margin.getEnd(other) + padding.getEnd(other);
231+
int s = widget.getArea().getSize(other);
232+
if (caa == Alignment.CrossAxis.END) {
233+
crossAxisPos = availableSize - s - end;
234+
} else if (caa == Alignment.CrossAxis.CENTER) {
235+
crossAxisPos = (int) (availableSize / 2f - widget.getArea().getSize(other) / 2f);
236+
if (availableSize < s + start + end) {
237+
ModularUI.LOGGER.warn("Widget {} is larger with padding on axis {} than parent {}. Padding can't be applied correctly!", widget, other, parent);
238+
} else {
239+
if (crossAxisPos < start) crossAxisPos = start;
240+
else if (crossAxisPos > availableSize - end - s) crossAxisPos = availableSize - end - s;
241+
}
230242
}
231243
}
232244
widget.getArea().setRelativePoint(other, crossAxisPos);
233245
widget.getArea().setPoint(other, parent.getArea().getPoint(other) + crossAxisPos);
234246
widget.resizer().setPosResized(other, true);
235247
widget.resizer().setMarginPaddingApplied(other, true);
236248
}
237-
/*if (parent.isValid()) {
238-
// we changed rel pos, but we need to calculate the new absolute pos and other stuff
239-
widget.resizer().applyPos();
240-
}*/
241249
}
242250
return true;
243251
}

0 commit comments

Comments
 (0)