From 3c74a20335109e16600144679327b5aa3e9db029 Mon Sep 17 00:00:00 2001 From: uramer Date: Tue, 5 Apr 2022 17:26:55 +0200 Subject: [PATCH] Refactor horizontal/vertical cases in Flex --- components/lua_ui/flex.cpp | 67 +++++++++++++------------------------- components/lua_ui/flex.hpp | 64 ++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 44 deletions(-) diff --git a/components/lua_ui/flex.cpp b/components/lua_ui/flex.cpp index bbbf70cb98..eb37a05fbf 100644 --- a/components/lua_ui/flex.cpp +++ b/components/lua_ui/flex.cpp @@ -13,23 +13,20 @@ namespace LuaUi namespace { - MyGUI::IntPoint alignSize(const MyGUI::IntSize& container, const MyGUI::IntSize& content, Alignment alignment) + int alignSize(int container, int content, Alignment alignment) { - MyGUI::IntPoint alignedPosition; + int alignedPosition = 0; { - MyGUI::IntSize alignSize = container; switch (alignment) { case Alignment::Start: - alignedPosition = MyGUI::IntPoint(0, 0); + alignedPosition = 0; break; case Alignment::Center: - alignSize -= content; - alignedPosition = { alignSize.width / 2, alignSize.height / 2 }; + alignedPosition = (container - content) / 2; break; case Alignment::End: - alignSize -= content; - alignedPosition = { alignSize.width, alignSize.height }; + alignedPosition = container - content; break; } } @@ -44,50 +41,34 @@ namespace LuaUi for (auto* w: children()) { w->clearForced(); - childrenSize += w->calculateSize(); - totalGrow += w->externalValue("grow", 0.0f); + MyGUI::IntSize size = w->calculateSize(); + setPrimary(childrenSize, getPrimary(childrenSize) + getPrimary(size)); + setSecondary(childrenSize, std::max(getSecondary(childrenSize), getSecondary(size))); + totalGrow += std::max(0.0f, w->externalValue("grow", 0.0f)); } mChildrenSize = childrenSize; MyGUI::IntSize flexSize = calculateSize(); - MyGUI::IntSize growSize; - MyGUI::FloatSize growFactor; + int growSize = 0; + float growFactor = 0; if (totalGrow > 0) { - growSize = flexSize - childrenSize; - growFactor = { growSize.width / totalGrow, growSize.height / totalGrow }; + growSize = getPrimary(flexSize) - getPrimary(childrenSize); + growFactor = growSize / totalGrow; } - if (mHorizontal) - flexSize.width -= growSize.width; - else - flexSize.height-= growSize.height; + setPrimary(flexSize, getPrimary(flexSize) - growSize); - MyGUI::IntPoint alignedPosition = alignSize(flexSize, childrenSize, mAlign); - MyGUI::IntPoint arrangedPosition = alignSize(flexSize, childrenSize, mArrange); MyGUI::IntPoint childPosition; - if (mHorizontal) - childPosition = { alignedPosition.left, arrangedPosition.top }; - else - childPosition = { arrangedPosition.left, alignedPosition.top }; + setPrimary(childPosition, alignSize(getPrimary(flexSize), getPrimary(childrenSize), mAlign)); + setSecondary(childPosition, alignSize(getSecondary(flexSize), getSecondary(childrenSize), mArrange)); for (auto* w : children()) { w->forcePosition(childPosition); - float grow = w->externalValue("grow", 0); - MyGUI::IntSize growth(growFactor.width * grow, growFactor.height * grow); - if (mHorizontal) - { - int width = w->widget()->getWidth(); - width += growth.width; - w->forceSize({width, w->widget()->getHeight()}); - childPosition.left += width; - } - else - { - int height = w->widget()->getHeight(); - height += growth.height; - w->forceSize({ w->widget()->getWidth(), height }); - childPosition.top += height; - } + MyGUI::IntSize size = w->widget()->getSize(); + float grow = std::max(0.0f, w->externalValue("grow", 0.0f)); + setPrimary(size, getPrimary(size) + static_cast(growFactor * grow)); + w->forceSize(size); + setPrimary(childPosition, getPrimary(childPosition) + getPrimary(size)); } WidgetExtension::updateProperties(); } @@ -96,10 +77,8 @@ namespace LuaUi { MyGUI::IntSize size = WidgetExtension::calculateSize(); if (mAutoSized) { - if (mHorizontal) - size.width = mChildrenSize.width; - else - size.height = mChildrenSize.height; + setPrimary(size, getPrimary(mChildrenSize)); + setSecondary(size, std::max(getSecondary(size), getSecondary(mChildrenSize))); } return size; } diff --git a/components/lua_ui/flex.hpp b/components/lua_ui/flex.hpp index 6cff6f0695..0ac8963f73 100644 --- a/components/lua_ui/flex.hpp +++ b/components/lua_ui/flex.hpp @@ -14,6 +14,10 @@ namespace LuaUi MyGUI::IntSize calculateSize() override; void updateProperties() override; void updateChildren() override; + MyGUI::IntSize childScalingSize() override + { + return MyGUI::IntSize(); + } private: bool mHorizontal; @@ -21,6 +25,66 @@ namespace LuaUi MyGUI::IntSize mChildrenSize; Alignment mAlign; Alignment mArrange; + + template + inline T getPrimary(const MyGUI::types::TPoint& point) + { + return mHorizontal ? point.left : point.top; + } + + template + inline T getSecondary(const MyGUI::types::TPoint& point) + { + return mHorizontal ? point.top : point.left; + } + + template + inline void setPrimary(MyGUI::types::TPoint& point, T value) + { + if (mHorizontal) + point.left = value; + else + point.top = value; + } + + template + inline void setSecondary(MyGUI::types::TPoint& point, T value) + { + if (mHorizontal) + point.top = value; + else + point.left = value; + } + + template + inline T getPrimary(const MyGUI::types::TSize& point) + { + return mHorizontal ? point.width : point.height; + } + + template + inline T getSecondary(const MyGUI::types::TSize& point) + { + return mHorizontal ? point.height : point.width; + } + + template + inline void setPrimary(MyGUI::types::TSize& point, T value) + { + if (mHorizontal) + point.width = value; + else + point.height = value; + } + + template + inline void setSecondary(MyGUI::types::TSize& point, T value) + { + if (mHorizontal) + point.height = value; + else + point.width = value; + } }; }