From c68f78f3d16ca6da4479afaba7c179072a60e905 Mon Sep 17 00:00:00 2001 From: shadash Date: Fri, 15 Oct 2021 21:28:12 +0200 Subject: [PATCH 1/4] fix right align Signed-off-by: Sophia Hadash --- es-core/src/components/FlexboxComponent.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/es-core/src/components/FlexboxComponent.cpp b/es-core/src/components/FlexboxComponent.cpp index f9b81abd9..f448fbc16 100644 --- a/es-core/src/components/FlexboxComponent.cpp +++ b/es-core/src/components/FlexboxComponent.cpp @@ -166,10 +166,9 @@ void FlexboxComponent::computeLayout() image.second.setSize(image.second.getSize().x, maxItemSize.y); } - // TODO: Doesn't work correctly. // Apply overall container alignment. if (mAlignment == "right") - x += (mSize.x - size.x * grid.x) - mItemMargin.x; + x += (mSize.x - maxItemSize.x * grid.x - (grid.x - 1) * mItemMargin.x); // Store final item position. image.second.setPosition(x, y); @@ -193,5 +192,22 @@ void FlexboxComponent::computeLayout() } } + // Apply right-align + if (mAlignment == "right") { + unsigned int n = mItemsPerLine - (--i + 1) % std::max(1, static_cast(mItemsPerLine)); + i = 0; + unsigned int line = 1; + for (auto& image : mImages) { + if (!image.second.isVisible()) + continue; + if (line == mLines) + image.second.setPosition(image.second.getPosition().x + + (maxItemSize.x + mItemMargin.x) * n, + image.second.getPosition().y); + if ((i++ + 1) % std::max(1, static_cast(mItemsPerLine)) == 0) + line++; + } + } + mLayoutValid = true; } From 1c93ca2c0759fc2a5d067b9bf4b3a08d33d70cbf Mon Sep 17 00:00:00 2001 From: Sophia Hadash Date: Fri, 15 Oct 2021 21:33:34 +0200 Subject: [PATCH 2/4] fix right align Signed-off-by: Sophia Hadash --- es-core/src/components/FlexboxComponent.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/es-core/src/components/FlexboxComponent.cpp b/es-core/src/components/FlexboxComponent.cpp index f448fbc16..a31871102 100644 --- a/es-core/src/components/FlexboxComponent.cpp +++ b/es-core/src/components/FlexboxComponent.cpp @@ -201,9 +201,10 @@ void FlexboxComponent::computeLayout() if (!image.second.isVisible()) continue; if (line == mLines) - image.second.setPosition(image.second.getPosition().x + - (maxItemSize.x + mItemMargin.x) * n, - image.second.getPosition().y); + image.second.setPosition( + image.second.getPosition().x + + floorf((maxItemSize.x + mItemMargin.x) * static_cast(n)), + image.second.getPosition().y); if ((i++ + 1) % std::max(1, static_cast(mItemsPerLine)) == 0) line++; } From 8fd05fcd77a3e747431a7d5fd90f8f2d250d41fd Mon Sep 17 00:00:00 2001 From: shadash Date: Sun, 17 Oct 2021 01:11:01 +0200 Subject: [PATCH 3/4] bugfix, simplification Signed-off-by: Sophia Hadash --- es-core/src/components/FlexboxComponent.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/es-core/src/components/FlexboxComponent.cpp b/es-core/src/components/FlexboxComponent.cpp index a31871102..612a61bad 100644 --- a/es-core/src/components/FlexboxComponent.cpp +++ b/es-core/src/components/FlexboxComponent.cpp @@ -194,7 +194,8 @@ void FlexboxComponent::computeLayout() // Apply right-align if (mAlignment == "right") { - unsigned int n = mItemsPerLine - (--i + 1) % std::max(1, static_cast(mItemsPerLine)); + unsigned int m = i % std::max(1, static_cast(mItemsPerLine)); + unsigned int n = m > 0 ? mItemsPerLine - m : m; i = 0; unsigned int line = 1; for (auto& image : mImages) { From af1d1b310966e2cd342e989ae07e60b9acd32502 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Sun, 17 Oct 2021 18:45:21 +0200 Subject: [PATCH 4/4] Fixed some rounding issues and the right-alignment in FlexboxComponent. --- es-core/src/components/FlexboxComponent.cpp | 27 +++++++++------------ 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/es-core/src/components/FlexboxComponent.cpp b/es-core/src/components/FlexboxComponent.cpp index 612a61bad..4979de675 100644 --- a/es-core/src/components/FlexboxComponent.cpp +++ b/es-core/src/components/FlexboxComponent.cpp @@ -122,7 +122,7 @@ void FlexboxComponent::computeLayout() newSize = sizeMaxX.x * sizeMaxX.y >= sizeMaxY.x * sizeMaxY.y ? sizeMaxX : sizeMaxY; if (image.second.getSize() != newSize) - image.second.setResize(newSize.x, newSize.y); + image.second.setResize(std::round(newSize.x), std::round(newSize.y)); // In case maxItemSize needs to be updated. if (newSize.x != sizeChange.x) @@ -132,9 +132,9 @@ void FlexboxComponent::computeLayout() } if (maxItemSize.x != sizeChange.x) - maxItemSize.x = sizeChange.x; + maxItemSize.x = std::round(sizeChange.x); if (maxItemSize.y != sizeChange.y) - maxItemSize.y = sizeChange.y; + maxItemSize.y = std::round(sizeChange.y); // Pre-compute layout parameters. float anchorXStart{anchorX}; @@ -192,22 +192,19 @@ void FlexboxComponent::computeLayout() } } - // Apply right-align + // Apply right-align to the images on the last row, if needed. if (mAlignment == "right") { - unsigned int m = i % std::max(1, static_cast(mItemsPerLine)); - unsigned int n = m > 0 ? mItemsPerLine - m : m; - i = 0; - unsigned int line = 1; + std::vector imagesToAlign; for (auto& image : mImages) { if (!image.second.isVisible()) continue; - if (line == mLines) - image.second.setPosition( - image.second.getPosition().x + - floorf((maxItemSize.x + mItemMargin.x) * static_cast(n)), - image.second.getPosition().y); - if ((i++ + 1) % std::max(1, static_cast(mItemsPerLine)) == 0) - line++; + // Only include images on the last row. + if (image.second.getPosition().y == anchorY) + imagesToAlign.push_back(&image.second); + } + for (auto& moveImage : imagesToAlign) { + float offset = (maxItemSize.x + mItemMargin.x) * (grid.x - imagesToAlign.size()); + moveImage->setPosition(moveImage->getPosition().x + offset, moveImage->getPosition().y); } }