1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#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));
		}
	}
}