From cf94a1d99d7fd115956fdba2784a92c3f9f98125 Mon Sep 17 00:00:00 2001 From: Arkadiusz Lach Date: Tue, 5 Nov 2024 19:15:49 +0100 Subject: [PATCH] Percent size code cleanup --- src/framework/ui/uianchorlayout.cpp | 13 +------------ src/framework/ui/uiflexbox.cpp | 13 +------------ src/framework/ui/uihorizontallayout.cpp | 13 +------------ src/framework/ui/uiverticallayout.cpp | 13 +------------ src/framework/ui/uiwidget.cpp | 17 +++++++++++++++++ src/framework/ui/uiwidget.h | 1 + 6 files changed, 22 insertions(+), 48 deletions(-) diff --git a/src/framework/ui/uianchorlayout.cpp b/src/framework/ui/uianchorlayout.cpp index f0be68fe..0ea41a69 100644 --- a/src/framework/ui/uianchorlayout.cpp +++ b/src/framework/ui/uianchorlayout.cpp @@ -174,19 +174,8 @@ bool UIAnchorLayout::updateWidget(const UIWidgetPtr& widget, const UIAnchorGroup first = widget; if (widget->isSizePercantage()) { - auto percentSize = widget->getPercentSize(); - int width = widget->getWidth(); - int height = widget->getHeight(); Rect paddingRect = parentWidget->getPaddingRect(); - if (percentSize.width() > 0) { - width = static_cast(paddingRect.width() * (percentSize.width() / 100.0)); - } - if (percentSize.height() > 0) { - height = static_cast(paddingRect.height() * (percentSize.height() / 100.0)); - } - - widget->setWidth(width); - widget->setHeight(height); + widget->updatePercentSize(paddingRect.size()); } Rect newRect = widget->getRect(); diff --git a/src/framework/ui/uiflexbox.cpp b/src/framework/ui/uiflexbox.cpp index 4c9ce8da..c7468baa 100644 --- a/src/framework/ui/uiflexbox.cpp +++ b/src/framework/ui/uiflexbox.cpp @@ -53,18 +53,7 @@ bool UIFlexBox::internalUpdate() continue; if (widget->isSizePercantage()) { - auto percentSize = widget->getPercentSize(); - int width = widget->getWidth(); - int height = widget->getHeight(); - if (percentSize.width() > 0) { - width = static_cast(clippingRect.width() * (percentSize.width() / 100.0)); - } - if (percentSize.height() > 0) { - height = static_cast(clippingRect.height() * (percentSize.height() / 100.0)); - } - - widget->setWidth(width); - widget->setHeight(height); + widget->updatePercentSize(clippingRect.size()); } Size childSize = widget->getSize(); diff --git a/src/framework/ui/uihorizontallayout.cpp b/src/framework/ui/uihorizontallayout.cpp index 33e697ab..09055fb2 100644 --- a/src/framework/ui/uihorizontallayout.cpp +++ b/src/framework/ui/uihorizontallayout.cpp @@ -56,18 +56,7 @@ bool UIHorizontalLayout::internalUpdate() continue; if (widget->isSizePercantage()) { - auto percentSize = widget->getPercentSize(); - int width = widget->getWidth(); - int height = widget->getHeight(); - if (percentSize.width() > 0) { - width = static_cast(paddingRect.width() * (percentSize.width() / 100.0)); - } - if (percentSize.height() > 0) { - height = static_cast(paddingRect.height() * (percentSize.height() / 100.0)); - } - - widget->setWidth(width); - widget->setHeight(height); + widget->updatePercentSize(paddingRect.size()); } Size size = widget->getSize(); diff --git a/src/framework/ui/uiverticallayout.cpp b/src/framework/ui/uiverticallayout.cpp index af76d58b..5311500a 100644 --- a/src/framework/ui/uiverticallayout.cpp +++ b/src/framework/ui/uiverticallayout.cpp @@ -57,18 +57,7 @@ bool UIVerticalLayout::internalUpdate() continue; if (widget->isSizePercantage()) { - auto percentSize = widget->getPercentSize(); - int width = widget->getWidth(); - int height = widget->getHeight(); - if (percentSize.width() > 0) { - width = static_cast(paddingRect.width() * (percentSize.width() / 100.0)); - } - if (percentSize.height() > 0) { - height = static_cast(paddingRect.height() * (percentSize.height() / 100.0)); - } - - widget->setWidth(width); - widget->setHeight(height); + widget->updatePercentSize(paddingRect.size()); } Size size = widget->getSize(); diff --git a/src/framework/ui/uiwidget.cpp b/src/framework/ui/uiwidget.cpp index bf72f382..adb8ff63 100644 --- a/src/framework/ui/uiwidget.cpp +++ b/src/framework/ui/uiwidget.cpp @@ -2120,3 +2120,20 @@ void UIWidget::setHeight(int height, bool percentage) resize(getWidth(), height); } + +void UIWidget::updatePercentSize(const Size& size) +{ + auto percentSize = getPercentSize(); + int width = getWidth(); + int height = getHeight(); + + if (percentSize.width() > 0) { + width = static_cast(size.width() * (percentSize.width() / 100.0)); + } + if (percentSize.height() > 0) { + height = static_cast(size.height() * (percentSize.height() / 100.0)); + } + + setWidth(width); + setHeight(height); +} diff --git a/src/framework/ui/uiwidget.h b/src/framework/ui/uiwidget.h index 1261b145..a6cf03d3 100644 --- a/src/framework/ui/uiwidget.h +++ b/src/framework/ui/uiwidget.h @@ -396,6 +396,7 @@ class UIWidget : public LuaObject void setRotation(float degrees) { m_rotation = degrees; } void setChangeCursorImage(bool enable) { m_changeCursorImage = enable; } void setCursor(const std::string& cursor); + void updatePercentSize(const Size& size); int getX() { return m_rect.x(); } int getY() { return m_rect.y(); }