Widgets/BoxLayout: Center widgets on the other axis

This commit is contained in:
SirLynix 2024-01-14 22:04:02 +01:00
parent db83413536
commit c52ffa15c4
1 changed files with 8 additions and 3 deletions

View File

@ -33,6 +33,7 @@ namespace Nz
BaseWidget::Layout(); BaseWidget::Layout();
std::size_t axis = 0; std::size_t axis = 0;
std::size_t otherAxis = 1;
bool reversed = false; bool reversed = false;
switch (m_orientation) switch (m_orientation)
@ -40,12 +41,14 @@ namespace Nz
case BoxLayoutOrientation::LeftToRight: case BoxLayoutOrientation::LeftToRight:
case BoxLayoutOrientation::RightToLeft: case BoxLayoutOrientation::RightToLeft:
axis = 0; //< x axis = 0; //< x
otherAxis = 1; //< y
reversed = (m_orientation == BoxLayoutOrientation::RightToLeft); reversed = (m_orientation == BoxLayoutOrientation::RightToLeft);
break; break;
case BoxLayoutOrientation::BottomToTop: case BoxLayoutOrientation::BottomToTop:
case BoxLayoutOrientation::TopToBottom: case BoxLayoutOrientation::TopToBottom:
axis = 1; //< y axis = 1; //< y
otherAxis = 0; //< x
reversed = (m_orientation == BoxLayoutOrientation::TopToBottom); reversed = (m_orientation == BoxLayoutOrientation::TopToBottom);
break; break;
} }
@ -130,18 +133,20 @@ namespace Nz
float cursor = (reversed) ? layoutSize[axis] : 0.f; float cursor = (reversed) ? layoutSize[axis] : 0.f;
ForEachWidgetChild([&](BaseWidget* child) ForEachWidgetChild([&](BaseWidget* child)
{ {
Vector2f childSize = child->GetSize();
if (reversed) if (reversed)
cursor -= child->GetSize()[axis]; cursor -= childSize[axis];
Nz::Vector2f position(0.f, 0.f); Nz::Vector2f position(0.f, 0.f);
position[axis] = cursor ; position[axis] = cursor;
position[otherAxis] = layoutSize[otherAxis] / 2.f - childSize[otherAxis] / 2.f;
child->SetPosition(position); child->SetPosition(position);
if (reversed) if (reversed)
cursor -= spacing; cursor -= spacing;
else else
cursor += child->GetSize()[axis] + spacing; cursor += childSize[axis] + spacing;
}); });
} }