mirror of
https://github.com/geode-sdk/geode.git
synced 2024-11-14 19:15:05 -05:00
add AxisAlignment::Between, for equal space between elements
inspired by flexbox's space-between
This commit is contained in:
parent
97d2e44611
commit
adc4469f77
2 changed files with 42 additions and 11 deletions
|
@ -77,6 +77,9 @@ enum class AxisAlignment {
|
||||||
// Each item gets the same portion from the layout (disregards gap)
|
// Each item gets the same portion from the layout (disregards gap)
|
||||||
// |.o..o..o.|
|
// |.o..o..o.|
|
||||||
Even,
|
Even,
|
||||||
|
// Space between each item is the same (disregards gap)
|
||||||
|
// |o...o...o|
|
||||||
|
Between,
|
||||||
};
|
};
|
||||||
|
|
||||||
constexpr float AXISLAYOUT_DEFAULT_MIN_SCALE = 0.65f;
|
constexpr float AXISLAYOUT_DEFAULT_MIN_SCALE = 0.65f;
|
||||||
|
|
|
@ -592,6 +592,7 @@ public:
|
||||||
rowCrossPos = totalRowCrossLength - rowsEndsLength * 1.5f * scale * (1.f - columnSquish);
|
rowCrossPos = totalRowCrossLength - rowsEndsLength * 1.5f * scale * (1.f - columnSquish);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
|
case AxisAlignment::Between:
|
||||||
case AxisAlignment::Even: {
|
case AxisAlignment::Even: {
|
||||||
totalRowCrossLength = available.crossLength;
|
totalRowCrossLength = available.crossLength;
|
||||||
rowCrossPos = totalRowCrossLength - rowsEndsLength * 1.5f * scale * (1.f - columnSquish);
|
rowCrossPos = totalRowCrossLength - rowsEndsLength * 1.5f * scale * (1.f - columnSquish);
|
||||||
|
@ -609,6 +610,14 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
float rowEvenSpace = available.crossLength / rows->count();
|
float rowEvenSpace = available.crossLength / rows->count();
|
||||||
|
|
||||||
|
float rowCrossLengthTotal = ranges::reduce<float>(
|
||||||
|
CCArrayExt<Row*>(rows),
|
||||||
|
[](float& acc, Row* row) {
|
||||||
|
acc += row->crossLength;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
float rowCrossBetweenSpace = std::max(0.f, (available.crossLength - rowCrossLengthTotal) / std::max(rows->count() - 1, 1u));
|
||||||
|
|
||||||
for (auto row : CCArrayExt<Row*>(rows)) {
|
for (auto row : CCArrayExt<Row*>(rows)) {
|
||||||
row->accountSpacers(m_axis, available.axisLength, available.crossLength);
|
row->accountSpacers(m_axis, available.axisLength, available.crossLength);
|
||||||
|
@ -616,16 +625,18 @@ public:
|
||||||
if (m_crossAlignment == AxisAlignment::Even) {
|
if (m_crossAlignment == AxisAlignment::Even) {
|
||||||
rowCrossPos -= rowEvenSpace / 2 + row->crossLength / 2;
|
rowCrossPos -= rowEvenSpace / 2 + row->crossLength / 2;
|
||||||
}
|
}
|
||||||
|
else if (m_crossAlignment == AxisAlignment::Between) {
|
||||||
|
rowCrossPos -= row->crossLength * columnSquish;
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
rowCrossPos -= row->crossLength * columnSquish;
|
rowCrossPos -= row->crossLength * columnSquish;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// starting axis pos
|
||||||
float rowAxisPos;
|
float rowAxisPos;
|
||||||
switch (m_axisAlignment) {
|
switch (m_axisAlignment) {
|
||||||
case AxisAlignment::Start: {
|
case AxisAlignment::Start:
|
||||||
rowAxisPos = 0.f;
|
case AxisAlignment::Between:
|
||||||
} break;
|
|
||||||
|
|
||||||
case AxisAlignment::Even: {
|
case AxisAlignment::Even: {
|
||||||
rowAxisPos = 0.f;
|
rowAxisPos = 0.f;
|
||||||
} break;
|
} break;
|
||||||
|
@ -639,10 +650,7 @@ public:
|
||||||
} break;
|
} break;
|
||||||
}
|
}
|
||||||
|
|
||||||
float evenSpace = available.axisLength / row->nodes->count();
|
float rowLengthTotal = 0.f;
|
||||||
|
|
||||||
size_t ix = 0;
|
|
||||||
AxisLayoutOptions const* prev = nullptr;
|
|
||||||
for (auto& node : CCArrayExt<CCNode*>(row->nodes)) {
|
for (auto& node : CCArrayExt<CCNode*>(row->nodes)) {
|
||||||
auto opts = axisOpts(node);
|
auto opts = axisOpts(node);
|
||||||
// rescale node if overflowing
|
// rescale node if overflowing
|
||||||
|
@ -655,7 +663,17 @@ public:
|
||||||
}
|
}
|
||||||
node->setScale(nodeScale);
|
node->setScale(nodeScale);
|
||||||
}
|
}
|
||||||
if (!ix) {
|
auto pos = nodeAxis(node, m_axis, row->squish);
|
||||||
|
rowLengthTotal += pos.axisLength;
|
||||||
|
}
|
||||||
|
float evenSpace = available.axisLength / row->nodes->count();
|
||||||
|
float rowBetweenSpace = std::max(0.f, (available.axisLength - rowLengthTotal) / std::max(row->nodes->count() - 1, 1u));
|
||||||
|
|
||||||
|
size_t ix = 0;
|
||||||
|
AxisLayoutOptions const* prev = nullptr;
|
||||||
|
for (auto& node : CCArrayExt<CCNode*>(row->nodes)) {
|
||||||
|
auto opts = axisOpts(node);
|
||||||
|
if (ix == 0) {
|
||||||
rowAxisPos += row->axisEndsLength * row->scale / 2 * (1.f - row->squish);
|
rowAxisPos += row->axisEndsLength * row->scale / 2 * (1.f - row->squish);
|
||||||
}
|
}
|
||||||
auto pos = nodeAxis(node, m_axis, row->squish);
|
auto pos = nodeAxis(node, m_axis, row->squish);
|
||||||
|
@ -665,8 +683,12 @@ public:
|
||||||
rowAxisPos += evenSpace -
|
rowAxisPos += evenSpace -
|
||||||
row->axisEndsLength * row->scale * (1.f - row->squish) * 1.f / nodes->count();
|
row->axisEndsLength * row->scale * (1.f - row->squish) * 1.f / nodes->count();
|
||||||
}
|
}
|
||||||
|
else if (m_axisAlignment == AxisAlignment::Between) {
|
||||||
|
axisPos = rowAxisPos + pos.axisLength * pos.axisAnchor;
|
||||||
|
rowAxisPos += pos.axisLength + rowBetweenSpace;
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
if (ix) {
|
if (ix != 0) {
|
||||||
if (row->prio == minMaxPrios.first) {
|
if (row->prio == minMaxPrios.first) {
|
||||||
rowAxisPos += this->nextGap(prev, opts) * row->scale * row->squish;
|
rowAxisPos += this->nextGap(prev, opts) * row->scale * row->squish;
|
||||||
}
|
}
|
||||||
|
@ -684,7 +706,9 @@ public:
|
||||||
crossOffset = pos.crossLength * pos.crossAnchor;
|
crossOffset = pos.crossLength * pos.crossAnchor;
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case AxisAlignment::Center: case AxisAlignment::Even: {
|
case AxisAlignment::Center:
|
||||||
|
case AxisAlignment::Between:
|
||||||
|
case AxisAlignment::Even: {
|
||||||
crossOffset = row->crossLength / 2 - pos.crossLength * (.5f - pos.crossAnchor);
|
crossOffset = row->crossLength / 2 - pos.crossLength * (.5f - pos.crossAnchor);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
|
@ -706,6 +730,10 @@ public:
|
||||||
rowCrossPos -= rowEvenSpace / 2 - row->crossLength / 2 -
|
rowCrossPos -= rowEvenSpace / 2 - row->crossLength / 2 -
|
||||||
rowsEndsLength * 1.5f * row->scale * (1.f - columnSquish) * 1.f / rows->count();
|
rowsEndsLength * 1.5f * row->scale * (1.f - columnSquish) * 1.f / rows->count();
|
||||||
}
|
}
|
||||||
|
else if (m_crossAlignment == AxisAlignment::Between) {
|
||||||
|
rowCrossPos -= rowCrossBetweenSpace -
|
||||||
|
rowsEndsLength * 1.5f * row->scale * (1.f - columnSquish) * 1.f / rows->count();
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
rowCrossPos -= m_gap * columnSquish -
|
rowCrossPos -= m_gap * columnSquish -
|
||||||
rowsEndsLength * 1.5f * row->scale * (1.f - columnSquish) * 1.f / rows->count();
|
rowsEndsLength * 1.5f * row->scale * (1.f - columnSquish) * 1.f / rows->count();
|
||||||
|
|
Loading…
Reference in a new issue