2022-04-04 21:10:03 +00:00
|
|
|
#include "flex.hpp"
|
|
|
|
|
|
|
|
namespace LuaUi
|
|
|
|
{
|
|
|
|
void LuaFlex::updateProperties()
|
|
|
|
{
|
|
|
|
mHorizontal = propertyValue("horizontal", false);
|
|
|
|
mAutoSized = propertyValue("autoSize", true);
|
|
|
|
mAlign = propertyValue("align", Alignment::Start);
|
|
|
|
mArrange = propertyValue("arrange", Alignment::Start);
|
|
|
|
WidgetExtension::updateProperties();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2022-04-05 15:26:55 +00:00
|
|
|
int alignSize(int container, int content, Alignment alignment)
|
2022-04-04 21:10:03 +00:00
|
|
|
{
|
2022-04-05 15:26:55 +00:00
|
|
|
int alignedPosition = 0;
|
2022-04-04 21:10:03 +00:00
|
|
|
{
|
|
|
|
switch (alignment)
|
|
|
|
{
|
|
|
|
case Alignment::Start:
|
2022-04-05 15:26:55 +00:00
|
|
|
alignedPosition = 0;
|
2022-04-04 21:10:03 +00:00
|
|
|
break;
|
|
|
|
case Alignment::Center:
|
2022-04-05 15:26:55 +00:00
|
|
|
alignedPosition = (container - content) / 2;
|
2022-04-04 21:10:03 +00:00
|
|
|
break;
|
|
|
|
case Alignment::End:
|
2022-04-05 15:26:55 +00:00
|
|
|
alignedPosition = container - content;
|
2022-04-04 21:10:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return alignedPosition;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaFlex::updateChildren()
|
|
|
|
{
|
|
|
|
float totalGrow = 0;
|
|
|
|
MyGUI::IntSize childrenSize;
|
|
|
|
for (auto* w: children())
|
|
|
|
{
|
|
|
|
w->clearForced();
|
2022-04-05 15:26:55 +00:00
|
|
|
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));
|
2022-04-04 21:10:03 +00:00
|
|
|
}
|
|
|
|
mChildrenSize = childrenSize;
|
|
|
|
|
|
|
|
MyGUI::IntSize flexSize = calculateSize();
|
2022-04-05 15:26:55 +00:00
|
|
|
int growSize = 0;
|
|
|
|
float growFactor = 0;
|
2022-04-04 21:10:03 +00:00
|
|
|
if (totalGrow > 0)
|
|
|
|
{
|
2022-04-05 15:26:55 +00:00
|
|
|
growSize = getPrimary(flexSize) - getPrimary(childrenSize);
|
|
|
|
growFactor = growSize / totalGrow;
|
2022-04-04 21:10:03 +00:00
|
|
|
}
|
2022-04-05 15:26:55 +00:00
|
|
|
setPrimary(flexSize, getPrimary(flexSize) - growSize);
|
2022-04-04 21:10:03 +00:00
|
|
|
|
|
|
|
MyGUI::IntPoint childPosition;
|
2022-04-05 15:26:55 +00:00
|
|
|
setPrimary(childPosition, alignSize(getPrimary(flexSize), getPrimary(childrenSize), mAlign));
|
|
|
|
setSecondary(childPosition, alignSize(getSecondary(flexSize), getSecondary(childrenSize), mArrange));
|
2022-04-04 21:10:03 +00:00
|
|
|
for (auto* w : children())
|
|
|
|
{
|
|
|
|
w->forcePosition(childPosition);
|
2022-04-05 15:26:55 +00:00
|
|
|
MyGUI::IntSize size = w->widget()->getSize();
|
|
|
|
float grow = std::max(0.0f, w->externalValue("grow", 0.0f));
|
|
|
|
setPrimary(size, getPrimary(size) + static_cast<int>(growFactor * grow));
|
|
|
|
w->forceSize(size);
|
|
|
|
setPrimary(childPosition, getPrimary(childPosition) + getPrimary(size));
|
2022-04-04 21:10:03 +00:00
|
|
|
}
|
|
|
|
WidgetExtension::updateProperties();
|
|
|
|
}
|
|
|
|
|
|
|
|
MyGUI::IntSize LuaFlex::calculateSize()
|
|
|
|
{
|
|
|
|
MyGUI::IntSize size = WidgetExtension::calculateSize();
|
|
|
|
if (mAutoSized) {
|
2022-04-05 15:26:55 +00:00
|
|
|
setPrimary(size, getPrimary(mChildrenSize));
|
|
|
|
setSecondary(size, std::max(getSecondary(size), getSecondary(mChildrenSize)));
|
2022-04-04 21:10:03 +00:00
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
}
|