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::Concise, 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 auto minPosition = Vector3( Vector3::ZERO );
82 auto maxPosition = Vector3( Vector3::ZERO );
85 for( unsigned int i=0; i<GetChildCount(); ++i )
87 auto childLayout = GetChildAt( i );
90 auto childOwner = childLayout->GetOwner();
93 MeasureChild( childLayout, widthMeasureSpec, heightMeasureSpec );
94 auto childWidth = childLayout->GetMeasuredWidth();
95 auto childHeight = childLayout->GetMeasuredHeight();
97 // Determine the width and height needed by the children using their given position and size.
98 // Children could overlap so find the left most and right most child.
99 auto childPosition = childOwner.GetProperty< Vector3 >( Actor::Property::POSITION );
100 minPosition.x = std::min( minPosition.x, childPosition.x );
101 maxPosition.x = std::max( maxPosition.x, childPosition.x + childWidth );
102 // Children could overlap so find the highest and lowest child.
103 minPosition.y = std::min( minPosition.y, childPosition.y );
104 maxPosition.y = std::max( maxPosition.y, childPosition.y + childHeight );
106 // Store current width and height needed to contain all children.
107 totalWidth = maxPosition.x - minPosition.x;
108 totalHeight = maxPosition.y - minPosition.y;
109 DALI_LOG_INFO( gLogFilter, Debug::Concise, "AbsoluteLayout::OnMeasure child width(%f) height(%f) \n", (float)totalWidth, (float)totalHeight );
111 if( childLayout->GetMeasuredWidthAndState().GetState() == MeasuredSize::State::MEASURED_SIZE_TOO_SMALL )
113 childState.widthState = MeasuredSize::State::MEASURED_SIZE_TOO_SMALL;
115 if( childLayout->GetMeasuredHeightAndState().GetState() == MeasuredSize::State::MEASURED_SIZE_TOO_SMALL )
117 childState.heightState = MeasuredSize::State::MEASURED_SIZE_TOO_SMALL;
122 MeasuredSize widthSizeAndState = ResolveSizeAndState( totalWidth, widthMeasureSpec, MeasuredSize::State::MEASURED_SIZE_OK);
123 MeasuredSize heightSizeAndState = ResolveSizeAndState( totalHeight, heightMeasureSpec, MeasuredSize::State::MEASURED_SIZE_OK);
124 totalWidth = widthSizeAndState.GetSize();
125 totalHeight = heightSizeAndState.GetSize();
127 // Ensure layout respects it's given minimum size
128 totalWidth = std::max( totalWidth, GetSuggestedMinimumWidth() );
129 totalHeight = std::max( totalHeight, GetSuggestedMinimumHeight() );
131 widthSizeAndState.SetState( childState.widthState );
132 heightSizeAndState.SetState( childState.heightState );
134 SetMeasuredDimensions( ResolveSizeAndState( totalWidth, widthMeasureSpec, childState.widthState ),
135 ResolveSizeAndState( totalHeight, heightMeasureSpec, childState.heightState ) );
139 void AbsoluteLayout::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
141 // Absolute layout positions it's children at their Actor positions.
142 // Children could overlap or spill outside the parent, as is the nature of absolute positions.
143 auto count = GetChildCount();
145 for( unsigned int i = 0; i < count; i++)
147 LayoutItemPtr childLayout = GetChildAt( i );
148 if( childLayout != nullptr )
150 auto childOwner = childLayout->GetOwner();
151 auto childWidth = childLayout->GetMeasuredWidth();
152 auto childHeight = childLayout->GetMeasuredHeight();
154 auto childPosition = childOwner.GetProperty< Vector3 >( Actor::Property::POSITION );
156 auto childTop = childPosition.y;
157 auto childLeft = childPosition.x;
159 childLayout->Layout( childLeft, childTop, childLeft + childWidth, childTop + childHeight );
164 } // namespace Internal
165 } // namespace Toolkit