Refactor horizontal/vertical cases in Flex

pull/3226/head
uramer 3 years ago
parent 788745e004
commit 3c74a20335

@ -13,23 +13,20 @@ namespace LuaUi
namespace 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) switch (alignment)
{ {
case Alignment::Start: case Alignment::Start:
alignedPosition = MyGUI::IntPoint(0, 0); alignedPosition = 0;
break; break;
case Alignment::Center: case Alignment::Center:
alignSize -= content; alignedPosition = (container - content) / 2;
alignedPosition = { alignSize.width / 2, alignSize.height / 2 };
break; break;
case Alignment::End: case Alignment::End:
alignSize -= content; alignedPosition = container - content;
alignedPosition = { alignSize.width, alignSize.height };
break; break;
} }
} }
@ -44,50 +41,34 @@ namespace LuaUi
for (auto* w: children()) for (auto* w: children())
{ {
w->clearForced(); w->clearForced();
childrenSize += w->calculateSize(); MyGUI::IntSize size = w->calculateSize();
totalGrow += w->externalValue("grow", 0.0f); 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; mChildrenSize = childrenSize;
MyGUI::IntSize flexSize = calculateSize(); MyGUI::IntSize flexSize = calculateSize();
MyGUI::IntSize growSize; int growSize = 0;
MyGUI::FloatSize growFactor; float growFactor = 0;
if (totalGrow > 0) if (totalGrow > 0)
{ {
growSize = flexSize - childrenSize; growSize = getPrimary(flexSize) - getPrimary(childrenSize);
growFactor = { growSize.width / totalGrow, growSize.height / totalGrow }; growFactor = growSize / totalGrow;
} }
if (mHorizontal) setPrimary(flexSize, getPrimary(flexSize) - growSize);
flexSize.width -= growSize.width;
else
flexSize.height-= growSize.height;
MyGUI::IntPoint alignedPosition = alignSize(flexSize, childrenSize, mAlign);
MyGUI::IntPoint arrangedPosition = alignSize(flexSize, childrenSize, mArrange);
MyGUI::IntPoint childPosition; MyGUI::IntPoint childPosition;
if (mHorizontal) setPrimary(childPosition, alignSize(getPrimary(flexSize), getPrimary(childrenSize), mAlign));
childPosition = { alignedPosition.left, arrangedPosition.top }; setSecondary(childPosition, alignSize(getSecondary(flexSize), getSecondary(childrenSize), mArrange));
else
childPosition = { arrangedPosition.left, alignedPosition.top };
for (auto* w : children()) for (auto* w : children())
{ {
w->forcePosition(childPosition); w->forcePosition(childPosition);
float grow = w->externalValue("grow", 0); MyGUI::IntSize size = w->widget()->getSize();
MyGUI::IntSize growth(growFactor.width * grow, growFactor.height * grow); float grow = std::max(0.0f, w->externalValue("grow", 0.0f));
if (mHorizontal) setPrimary(size, getPrimary(size) + static_cast<int>(growFactor * grow));
{ w->forceSize(size);
int width = w->widget()->getWidth(); setPrimary(childPosition, getPrimary(childPosition) + getPrimary(size));
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;
}
} }
WidgetExtension::updateProperties(); WidgetExtension::updateProperties();
} }
@ -96,10 +77,8 @@ namespace LuaUi
{ {
MyGUI::IntSize size = WidgetExtension::calculateSize(); MyGUI::IntSize size = WidgetExtension::calculateSize();
if (mAutoSized) { if (mAutoSized) {
if (mHorizontal) setPrimary(size, getPrimary(mChildrenSize));
size.width = mChildrenSize.width; setSecondary(size, std::max(getSecondary(size), getSecondary(mChildrenSize)));
else
size.height = mChildrenSize.height;
} }
return size; return size;
} }

@ -14,6 +14,10 @@ namespace LuaUi
MyGUI::IntSize calculateSize() override; MyGUI::IntSize calculateSize() override;
void updateProperties() override; void updateProperties() override;
void updateChildren() override; void updateChildren() override;
MyGUI::IntSize childScalingSize() override
{
return MyGUI::IntSize();
}
private: private:
bool mHorizontal; bool mHorizontal;
@ -21,6 +25,66 @@ namespace LuaUi
MyGUI::IntSize mChildrenSize; MyGUI::IntSize mChildrenSize;
Alignment mAlign; Alignment mAlign;
Alignment mArrange; Alignment mArrange;
template<typename T>
inline T getPrimary(const MyGUI::types::TPoint<T>& point)
{
return mHorizontal ? point.left : point.top;
}
template<typename T>
inline T getSecondary(const MyGUI::types::TPoint<T>& point)
{
return mHorizontal ? point.top : point.left;
}
template<typename T>
inline void setPrimary(MyGUI::types::TPoint<T>& point, T value)
{
if (mHorizontal)
point.left = value;
else
point.top = value;
}
template<typename T>
inline void setSecondary(MyGUI::types::TPoint<T>& point, T value)
{
if (mHorizontal)
point.top = value;
else
point.left = value;
}
template<typename T>
inline T getPrimary(const MyGUI::types::TSize<T>& point)
{
return mHorizontal ? point.width : point.height;
}
template<typename T>
inline T getSecondary(const MyGUI::types::TSize<T>& point)
{
return mHorizontal ? point.height : point.width;
}
template<typename T>
inline void setPrimary(MyGUI::types::TSize<T>& point, T value)
{
if (mHorizontal)
point.width = value;
else
point.height = value;
}
template<typename T>
inline void setSecondary(MyGUI::types::TSize<T>& point, T value)
{
if (mHorizontal)
point.height = value;
else
point.width = value;
}
}; };
} }

Loading…
Cancel
Save