@@ -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) {
815844CCSize 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+
885927AxisLayout* 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+
942989AxisLayout::AxisLayout (Axis axis) : m_impl(std::make_unique<Impl>(axis)) {}
943990AxisLayout::~AxisLayout () {}
944991
0 commit comments