|
1 | 1 | package com.cleanroommc.modularui.widgets.layout; |
2 | 2 |
|
| 3 | +import com.cleanroommc.modularui.ModularUI; |
3 | 4 | import com.cleanroommc.modularui.api.GuiAxis; |
4 | 5 | import com.cleanroommc.modularui.api.layout.ILayoutWidget; |
5 | 6 | import com.cleanroommc.modularui.api.widget.IWidget; |
@@ -210,34 +211,41 @@ public boolean postLayoutWidgets() { |
210 | 211 | public static boolean layoutCrossAxisListLike(IWidget parent, GuiAxis axis, Alignment.CrossAxis caa, boolean reverseLayout) { |
211 | 212 | if (!parent.hasChildren()) return true; |
212 | 213 | GuiAxis other = axis.getOther(); |
213 | | - int width = parent.getArea().getSize(other); |
| 214 | + int availableSize = parent.getArea().getSize(other); |
214 | 215 | 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; |
217 | 218 | List<IWidget> childrenList = reverseLayout ? new ReversedList<>(parent.getChildren()) : parent.getChildren(); |
218 | 219 | for (IWidget widget : childrenList) { |
219 | 220 | // exclude children whose position of main axis is fixed |
220 | 221 | if (widget.resizer().hasPos(axis)) continue; |
221 | 222 | Box margin = widget.getArea().getMargin(); |
222 | 223 | // don't align auto positioned children in cross axis |
223 | 224 | 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 | + } |
230 | 242 | } |
231 | 243 | } |
232 | 244 | widget.getArea().setRelativePoint(other, crossAxisPos); |
233 | 245 | widget.getArea().setPoint(other, parent.getArea().getPoint(other) + crossAxisPos); |
234 | 246 | widget.resizer().setPosResized(other, true); |
235 | 247 | widget.resizer().setMarginPaddingApplied(other, true); |
236 | 248 | } |
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 | | - }*/ |
241 | 249 | } |
242 | 250 | return true; |
243 | 251 | } |
|
0 commit comments