Skip to content

Commit 2d28489

Browse files
authored
add padding to axislayout and simpleaxislayout (#1999)
1 parent efb3def commit 2d28489

4 files changed

Lines changed: 185 additions & 41 deletions

File tree

loader/include/Geode/ui/Layout.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,36 @@ enum class AxisAlignment {
7070
Between,
7171
};
7272

73+
/**
74+
* Specifies the padding inside a layout
75+
*/
76+
struct Padding final {
77+
float left = 0.f;
78+
float top = 0.f;
79+
float right = 0.f;
80+
float bottom = 0.f;
81+
82+
constexpr Padding() = default;
83+
constexpr Padding(float left, float top, float right, float bottom)
84+
: left(left), top(top), right(right), bottom(bottom) {}
85+
86+
constexpr static Padding uniform(float padding) {
87+
return { padding, padding, padding, padding };
88+
}
89+
90+
constexpr static Padding symmetric(float horizontal, float vertical) {
91+
return { horizontal, vertical, horizontal, vertical };
92+
}
93+
94+
constexpr static Padding horizontal(float horizontal) {
95+
return { horizontal, 0.f, horizontal, 0.f };
96+
}
97+
98+
constexpr static Padding vertical(float vertical) {
99+
return { 0.f, vertical, 0.f, vertical };
100+
}
101+
};
102+
73103
constexpr float AXISLAYOUT_DEFAULT_MIN_SCALE = 0.65f;
74104
constexpr int AXISLAYOUT_DEFAULT_PRIORITY = 0;
75105

@@ -243,6 +273,7 @@ class GEODE_DLL AxisLayout : public Layout {
243273
std::optional<float> getAutoGrowAxis() const;
244274
float getDefaultMinScale() const;
245275
float getDefaultMaxScale() const;
276+
Padding getPadding() const;
246277

247278
AxisLayout* setAxis(Axis axis);
248279
/**
@@ -305,6 +336,11 @@ class GEODE_DLL AxisLayout : public Layout {
305336
*/
306337
AxisLayout* ignoreInvisibleChildren(bool ignore = true);
307338
bool isIgnoreInvisibleChildren() const;
339+
/**
340+
* Sets the padding inside the layout, which is the empty space between the
341+
* layout's border and its children. The default is set to 0 on all sides
342+
*/
343+
AxisLayout* setPadding(Padding const& padding);
308344
};
309345

310346
/**

loader/include/Geode/ui/SimpleAxisLayout.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ class GEODE_DLL SimpleAxisLayout : public Layout {
189189
*/
190190
SimpleAxisLayout* ignoreInvisibleChildren(bool ignore = true);
191191
bool isIgnoreInvisibleChildren() const;
192+
/**
193+
* Sets the padding inside the layout, which is the empty space between the
194+
* layout's border and its children. The default is set to 0 on all sides
195+
*/
196+
SimpleAxisLayout* setPadding(Padding const& padding);
192197

193198
Axis getAxis() const;
194199
AxisScaling getMainAxisScaling() const;
@@ -200,6 +205,7 @@ class GEODE_DLL SimpleAxisLayout : public Layout {
200205
float getGap() const;
201206
std::optional<float> getMinRelativeScale() const;
202207
std::optional<float> getMaxRelativeScale() const;
208+
Padding getPadding() const;
203209
};
204210

205211
class GEODE_DLL SimpleRowLayout final : public SimpleAxisLayout {

loader/src/cocos2d-ext/AxisLayout.cpp

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,31 @@ class AxisLayout::Impl : public BaseAxisLayoutImpl {
140140
bool m_growCrossAxis = false;
141141
std::optional<float> m_autoGrowAxisMinLength;
142142
std::pair<float, float> m_defaultScaleLimits = { AXISLAYOUT_DEFAULT_MIN_SCALE, 1 };
143+
Padding m_padding = { 0.f, 0.f, 0.f, 0.f };
143144

144145
Impl(Axis axis) : BaseAxisLayoutImpl(axis, 5.f) {}
145146

147+
float axisPadding() const {
148+
if (m_axis == Axis::Row) {
149+
return m_padding.left + m_padding.right;
150+
}
151+
return m_padding.top + m_padding.bottom;
152+
}
153+
154+
float crossPadding() const {
155+
if (m_axis == Axis::Row) {
156+
return m_padding.top + m_padding.bottom;
157+
}
158+
return m_padding.left + m_padding.right;
159+
}
160+
161+
AxisPosition availableForLayout(CCNode* on) const {
162+
auto available = nodeAxis(on, m_axis, 1.f / on->getScale());
163+
available.axisLength = available.axisLength - this->axisPadding();
164+
available.crossLength = available.crossLength - this->crossPadding();
165+
return available;
166+
}
167+
146168
struct Row : public CCObject {
147169
float nextOverflowScaleDownFactor;
148170
float nextOverflowSquishFactor;
@@ -316,7 +338,7 @@ class AxisLayout::Impl : public BaseAxisLayoutImpl {
316338
float crossLength;
317339
auto res = CCArray::create();
318340

319-
auto available = nodeAxis(on, m_axis, 1.f / on->getScale());
341+
auto available = this->availableForLayout(on);
320342

321343
auto fit = [&](CCArray* nodes) {
322344
nextAxisScalableLength = 0.f;
@@ -504,7 +526,7 @@ class AxisLayout::Impl : public BaseAxisLayoutImpl {
504526
return;
505527
}
506528

507-
auto available = nodeAxis(on, m_axis, 1.f / on->getScale());
529+
auto available = this->availableForLayout(on);
508530
if (available.axisLength <= 0.f) {
509531
return;
510532
}
@@ -562,14 +584,14 @@ class AxisLayout::Impl : public BaseAxisLayoutImpl {
562584
available.crossLength = totalRowCrossLength;
563585
if (m_axis == Axis::Row) {
564586
on->setContentSize({
565-
available.axisLength,
566-
totalRowCrossLength,
587+
available.axisLength + m_padding.left + m_padding.right,
588+
totalRowCrossLength + m_padding.top + m_padding.bottom,
567589
});
568590
}
569591
else {
570592
on->setContentSize({
571-
totalRowCrossLength,
572-
available.axisLength,
593+
totalRowCrossLength + m_padding.left + m_padding.right,
594+
available.axisLength + m_padding.top + m_padding.bottom,
573595
});
574596
}
575597
}
@@ -718,10 +740,16 @@ class AxisLayout::Impl : public BaseAxisLayoutImpl {
718740
} break;
719741
}
720742
if (m_axis == Axis::Row) {
721-
node->setPosition(axisPos, rowCrossPos + crossOffset);
743+
node->setPosition(
744+
m_padding.left + axisPos,
745+
m_padding.bottom + rowCrossPos + crossOffset
746+
);
722747
}
723748
else {
724-
node->setPosition(rowCrossPos + crossOffset, axisPos);
749+
node->setPosition(
750+
m_padding.left + rowCrossPos + crossOffset,
751+
m_padding.bottom + axisPos
752+
);
725753
}
726754
prev = opts;
727755
ix++;
@@ -793,14 +821,15 @@ void AxisLayout::apply(CCNode* on) {
793821
}
794822

795823
if (m_impl->m_autoGrowAxisMinLength.has_value()) {
796-
if (totalLength < m_impl->m_autoGrowAxisMinLength.value()) {
797-
totalLength = m_impl->m_autoGrowAxisMinLength.value();
824+
auto totalOuterLength = totalLength + m_impl->axisPadding();
825+
if (totalOuterLength < m_impl->m_autoGrowAxisMinLength.value()) {
826+
totalOuterLength = m_impl->m_autoGrowAxisMinLength.value();
798827
}
799828
if (m_impl->m_axis == Axis::Row) {
800-
on->setContentSize({ totalLength, on->getContentSize().height });
829+
on->setContentSize({ totalOuterLength, on->getContentSize().height });
801830
}
802831
else {
803-
on->setContentSize({ on->getContentSize().width, totalLength });
832+
on->setContentSize({ on->getContentSize().width, totalOuterLength });
804833
}
805834
}
806835

@@ -815,25 +844,34 @@ void AxisLayout::apply(CCNode* on) {
815844
CCSize AxisLayout::getSizeHint(CCNode* on) const {
816845
// Ideal is single row / column with no scaling
817846
auto nodes = m_impl->getNodesToPosition(on);
818-
float length = 0.f;
819-
float cross = 0.f;
847+
float innerLength = 0.f;
848+
float innerCross = 0.f;
820849
for (auto& node : CCArrayExt<CCNode*>(nodes)) {
821850
auto axis = nodeAxis(node, m_impl->m_axis, 1.f);
822-
length += axis.axisLength;
823-
if (axis.crossLength > cross) {
824-
axis.crossLength = cross;
851+
innerLength += axis.axisLength;
852+
if (axis.crossLength > innerCross) {
853+
axis.crossLength = innerCross;
825854
}
826855
}
856+
857+
auto available = nodeAxis(on, m_impl->m_axis, 1.f);
858+
auto axisPadding = m_impl->axisPadding();
859+
auto crossPadding = m_impl->crossPadding();
860+
auto availableInnerAxis = available.axisLength - axisPadding;
861+
827862
if (auto l = m_impl->m_autoGrowAxisMinLength) {
828-
length = std::max(length, *l);
863+
innerLength = std::max(innerLength, *l - axisPadding);
829864
}
830865
// No overflow
831866
else {
832-
length = std::min(length, nodeAxis(on, m_impl->m_axis, 1.f).axisLength);
867+
innerLength = std::min(innerLength, availableInnerAxis);
833868
}
834869
if (!m_impl->m_allowCrossAxisOverflow) {
835-
cross = nodeAxis(on, m_impl->m_axis, 1.f).crossLength;
870+
innerCross = available.crossLength - crossPadding;
836871
}
872+
873+
auto length = innerLength + axisPadding;
874+
auto cross = innerCross + crossPadding;
837875
if (m_impl->m_axis == Axis::Row) {
838876
return { length, cross };
839877
}
@@ -882,6 +920,10 @@ float AxisLayout::getDefaultMaxScale() const {
882920
return m_impl->m_defaultScaleLimits.second;
883921
}
884922

923+
Padding AxisLayout::getPadding() const {
924+
return m_impl->m_padding;
925+
}
926+
885927
AxisLayout* AxisLayout::setAxis(Axis axis) {
886928
m_impl->m_axis = axis;
887929
return this;
@@ -939,6 +981,11 @@ bool AxisLayout::isIgnoreInvisibleChildren() const {
939981
return m_impl->m_ignoreInvisibleChildren;
940982
}
941983

984+
AxisLayout* AxisLayout::setPadding(Padding const& padding) {
985+
m_impl->m_padding = padding;
986+
return this;
987+
}
988+
942989
AxisLayout::AxisLayout(Axis axis) : m_impl(std::make_unique<Impl>(axis)) {}
943990
AxisLayout::~AxisLayout() {}
944991

0 commit comments

Comments
 (0)