#include "fsNGuiLayout.h"
#include <algorithm>
#include <cmath>
#include <vector>
#include <fsRenderer/src/components/fsISpriteComponent.h>
#include "fsCEntity.h"
namespace
{
struct Row
{
fsF32 y;
std::vector<fsINode*> nodes;
fsBool hidden;
};
}
void fsNGuiLayout::hiddenRowsCollapse(fsCEntity* const pPanel, const fsF32 pTolerance)<--- Parameter 'pPanel' can be declared as pointer to const
{
if (!pPanel)
{
return;
}
std::vector<Row> rows;
for (fsINode* const child : pPanel->childrenGet())
{
const fsF32 y = child->posGet().y;
auto row = std::find_if(rows.begin(), rows.end(), [&](const Row& pRow) { return std::fabs(pRow.y - y) <= pTolerance; });
if (row == rows.end())
{
rows.push_back({y, {}, true});
row = rows.end() - 1;
}
row->nodes.push_back(child);
row->hidden = row->hidden && !child->visibleGet();
}
std::sort(rows.begin(), rows.end(), [](const Row& pA, const Row& pB) { return pA.y < pB.y; });
// Top to bottom; each hidden row takes the gap down to the next row (or up to the last one).
for (std::size_t index = 0; index < rows.size(); ++index)
{
if (!rows[index].hidden || rows.size() < 2)
{
continue;
}
const fsF32 gap = index + 1 < rows.size() ? rows[index + 1].y - rows[index].y : rows[index].y - rows[index - 1].y;
for (std::size_t other = 0; other < rows.size(); ++other)
{
if (other == index)
{
continue;
}
const fsF32 shift = (other < index ? gap : -gap) * 0.5f;
rows[other].y += shift;
for (fsINode* const node : rows[other].nodes)
{
const fsVec2d pos = node->posGet();
node->posSet(fsVec2d(pos.x, pos.y + shift));
}
}
if (auto* const sprite = pPanel->spriteComponentGet())
{
sprite->heightSet(sprite->heightGet() - static_cast<fsS32>(gap));
}
}
}