2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <dali-toolkit/internal/layouting/absolute-layout-impl.h>
21 #include <dali/integration-api/debug.h>
22 #include <dali/public-api/common/extents.h>
23 #include <dali/public-api/actors/actor.h>
24 #include <dali-toolkit/devel-api/layouting/layout-item.h>
25 #include <dali-toolkit/public-api/controls/control-impl.h>
26 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
30 #if defined(DEBUG_ENABLED)
31 static Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_LAYOUT" );
42 AbsoluteLayoutPtr AbsoluteLayout::New()
44 AbsoluteLayoutPtr layout( new AbsoluteLayout() );
48 AbsoluteLayout::AbsoluteLayout()
53 AbsoluteLayout::~AbsoluteLayout()
57 void AbsoluteLayout::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
59 #if defined(DEBUG_ENABLED)
60 auto actor = Actor::DownCast(GetOwner());
62 std::ostringstream oss;
63 oss << "AbsoluteLayout::OnMeasure ";
66 oss << "Actor Id:" << actor.GetId() << " Name:" << actor.GetName() << " ";
68 oss << "widthMeasureSpec:" << widthMeasureSpec << " heightMeasureSpec:" << heightMeasureSpec << std::endl;
69 DALI_LOG_INFO( gLogFilter, Debug::Concise, oss.str().c_str() );
72 LayoutLength totalHeight( 0 );
73 LayoutLength totalWidth( 0 );
77 MeasuredSize::State widthState;
78 MeasuredSize::State heightState;
79 } childState = { MeasuredSize::State::MEASURED_SIZE_OK, MeasuredSize::State::MEASURED_SIZE_OK };
81 LayoutLength minPositionX( 0 );
82 LayoutLength minPositionY( 0 );
83 LayoutLength maxPositionX( 0 );
84 LayoutLength maxPositionY( 0 );
87 for( unsigned int i=0; i<GetChildCount(); ++i )
89 auto childLayout = GetChildAt( i );
92 auto childOwner = childLayout->GetOwner();
95 MeasureChild( childLayout, widthMeasureSpec, heightMeasureSpec );
96 LayoutLength childWidth = childLayout->GetMeasuredWidth();
97 LayoutLength childHeight = childLayout->GetMeasuredHeight();
99 // Determine the width and height needed by the children using their given position and size.
100 // Children could overlap so find the left most and right most child.
101 auto childPosition = childOwner.GetProperty< Vector3 >( Actor::Property::POSITION );
102 LayoutLength childLeft = childPosition.x;
103 LayoutLength childTop = childPosition.y;
105 minPositionX = std::min( minPositionX, childLeft );
106 maxPositionX = std::max( maxPositionX, childLeft + childWidth );
107 // Children could overlap so find the highest and lowest child.
108 minPositionY = std::min( minPositionY, childTop );
109 maxPositionY = std::max( maxPositionY, childTop + childHeight );
111 // Store current width and height needed to contain all children.
112 totalWidth = maxPositionX - minPositionX;
113 totalHeight = maxPositionY - minPositionY;
114 DALI_LOG_INFO( gLogFilter, Debug::Concise, "AbsoluteLayout::OnMeasure child width(%f) height(%f) \n", totalWidth.AsFloat(), totalHeight.AsFloat() );
116 if( childLayout->GetMeasuredWidthAndState().GetState() == MeasuredSize::State::MEASURED_SIZE_TOO_SMALL )
118 childState.widthState = MeasuredSize::State::MEASURED_SIZE_TOO_SMALL;
120 if( childLayout->GetMeasuredHeightAndState().GetState() == MeasuredSize::State::MEASURED_SIZE_TOO_SMALL )
122 childState.heightState = MeasuredSize::State::MEASURED_SIZE_TOO_SMALL;
127 MeasuredSize widthSizeAndState = ResolveSizeAndState( totalWidth, widthMeasureSpec, MeasuredSize::State::MEASURED_SIZE_OK);
128 MeasuredSize heightSizeAndState = ResolveSizeAndState( totalHeight, heightMeasureSpec, MeasuredSize::State::MEASURED_SIZE_OK);
129 totalWidth = widthSizeAndState.GetSize();
130 totalHeight = heightSizeAndState.GetSize();
132 // Ensure layout respects it's given minimum size
133 totalWidth = std::max( totalWidth, GetSuggestedMinimumWidth() );
134 totalHeight = std::max( totalHeight, GetSuggestedMinimumHeight() );
136 widthSizeAndState.SetState( childState.widthState );
137 heightSizeAndState.SetState( childState.heightState );
139 SetMeasuredDimensions( ResolveSizeAndState( totalWidth, widthMeasureSpec, childState.widthState ),
140 ResolveSizeAndState( totalHeight, heightMeasureSpec, childState.heightState ) );
144 void AbsoluteLayout::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
146 // Absolute layout positions it's children at their Actor positions.
147 // Children could overlap or spill outside the parent, as is the nature of absolute positions.
148 auto count = GetChildCount();
149 DALI_LOG_INFO( gLogFilter, Debug::Concise, "AbsoluteLayout::OnLayout child count(%d)\n", count );
151 for( unsigned int i = 0; i < count; i++)
153 LayoutItemPtr childLayout = GetChildAt( i );
154 if( childLayout != nullptr )
156 auto childOwner = childLayout->GetOwner();
157 LayoutLength childWidth = childLayout->GetMeasuredWidth();
158 LayoutLength childHeight = childLayout->GetMeasuredHeight();
160 auto childPosition = childOwner.GetProperty< Vector3 >( Actor::Property::POSITION );
162 LayoutLength childTop = childPosition.y;
163 LayoutLength childLeft = childPosition.x;
165 DALI_LOG_INFO( gLogFilter, Debug::General, "AbsoluteLayout::OnLayout child[%s] position(%f,%f) child width[%d] height(%d)\n",
166 Toolkit::Control::DownCast( childOwner ).GetName().c_str(),
167 childPosition.x, childPosition.y,
168 childWidth.mValue, childHeight.mValue
171 childLayout->Layout( childLeft, childTop, childLeft + childWidth, childTop + childHeight );
176 } // namespace Internal
177 } // namespace Toolkit