[5.0] Workaround in AbsoluteLayout for animation bug in legacy application.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / layouting / absolute-layout-impl.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 //CLASS HEADER
18 #include <dali-toolkit/internal/layouting/absolute-layout-impl.h>
19
20 //INTERNAL HEADERS
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>
27
28 namespace
29 {
30 #if defined(DEBUG_ENABLED)
31 static Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_LAYOUT" );
32 #endif
33 }
34
35 namespace Dali
36 {
37 namespace Toolkit
38 {
39 namespace Internal
40 {
41
42 AbsoluteLayoutPtr AbsoluteLayout::New()
43 {
44   AbsoluteLayoutPtr layout( new AbsoluteLayout() );
45   return layout;
46 }
47
48 AbsoluteLayout::AbsoluteLayout()
49 : LayoutGroup()
50 {
51 }
52
53 AbsoluteLayout::~AbsoluteLayout()
54 {
55 }
56
57 void AbsoluteLayout::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
58 {
59 #if defined(DEBUG_ENABLED)
60   auto actor = Actor::DownCast(GetOwner());
61
62   std::ostringstream oss;
63   oss << "AbsoluteLayout::OnMeasure  ";
64   if( actor )
65   {
66     oss << "Actor Id:" << actor.GetId() << " Name:" << actor.GetName() << "  ";
67   }
68   oss << "widthMeasureSpec:" << widthMeasureSpec << " heightMeasureSpec:" << heightMeasureSpec << std::endl;
69   DALI_LOG_INFO( gLogFilter, Debug::Concise, oss.str().c_str() );
70 #endif
71
72   LayoutLength totalHeight( 0 );
73   LayoutLength totalWidth( 0 );
74
75   struct
76   {
77     MeasuredSize::State widthState;
78     MeasuredSize::State heightState;
79   } childState = { MeasuredSize::State::MEASURED_SIZE_OK, MeasuredSize::State::MEASURED_SIZE_OK };
80
81   LayoutLength minPositionX( 0 );
82   LayoutLength minPositionY( 0 );
83   LayoutLength maxPositionX( 0 );
84   LayoutLength maxPositionY( 0 );
85
86   // measure children
87   for( unsigned int i=0; i<GetChildCount(); ++i )
88   {
89     auto childLayout = GetChildAt( i );
90     if( childLayout )
91     {
92       auto childOwner = childLayout->GetOwner();
93
94       // Get size of child
95       MeasureChild( childLayout, widthMeasureSpec, heightMeasureSpec );
96       LayoutLength childWidth = childLayout->GetMeasuredWidth();
97       LayoutLength childHeight = childLayout->GetMeasuredHeight();
98
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       auto childSize = childOwner.GetProperty< Vector3 >( Actor::Property::SIZE );
103
104       // Check if there on going position or size animation and skip it to avoid legacy application regressions
105       if( childPosition != childOwner.GetCurrentProperty< Vector3 >( Actor::Property::POSITION ) ||
106           childSize != childOwner.GetCurrentProperty< Vector3 >( Actor::Property::SIZE ) )
107       {
108         break;
109       }
110
111       LayoutLength childLeft = childPosition.x;
112       LayoutLength childTop = childPosition.y;
113
114       minPositionX = std::min( minPositionX, childLeft );
115       maxPositionX = std::max( maxPositionX, childLeft + childWidth );
116       // Children could overlap so find the highest and lowest child.
117       minPositionY = std::min( minPositionY, childTop );
118       maxPositionY = std::max( maxPositionY, childTop + childHeight );
119
120       // Store current width and height needed to contain all children.
121       totalWidth = maxPositionX - minPositionX;
122       totalHeight = maxPositionY - minPositionY;
123       DALI_LOG_INFO( gLogFilter, Debug::Concise, "AbsoluteLayout::OnMeasure child width(%f) height(%f) \n", totalWidth.AsDecimal(), totalHeight.AsDecimal() );
124
125       if( childLayout->GetMeasuredWidthAndState().GetState() == MeasuredSize::State::MEASURED_SIZE_TOO_SMALL )
126       {
127         childState.widthState = MeasuredSize::State::MEASURED_SIZE_TOO_SMALL;
128       }
129       if( childLayout->GetMeasuredHeightAndState().GetState() == MeasuredSize::State::MEASURED_SIZE_TOO_SMALL )
130       {
131         childState.heightState = MeasuredSize::State::MEASURED_SIZE_TOO_SMALL;
132       }
133     }
134   }
135
136   MeasuredSize widthSizeAndState = ResolveSizeAndState( totalWidth, widthMeasureSpec, MeasuredSize::State::MEASURED_SIZE_OK);
137   MeasuredSize heightSizeAndState = ResolveSizeAndState( totalHeight, heightMeasureSpec, MeasuredSize::State::MEASURED_SIZE_OK);
138   totalWidth = widthSizeAndState.GetSize();
139   totalHeight = heightSizeAndState.GetSize();
140
141   // Ensure layout respects it's given minimum size
142   totalWidth = std::max( totalWidth, GetSuggestedMinimumWidth() );
143   totalHeight = std::max( totalHeight, GetSuggestedMinimumHeight() );
144
145   widthSizeAndState.SetState( childState.widthState );
146   heightSizeAndState.SetState( childState.heightState );
147
148   SetMeasuredDimensions( ResolveSizeAndState( totalWidth, widthMeasureSpec, childState.widthState ),
149                          ResolveSizeAndState( totalHeight, heightMeasureSpec, childState.heightState ) );
150
151 }
152
153 void AbsoluteLayout::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
154 {
155   // Absolute layout positions it's children at their Actor positions.
156   // Children could overlap or spill outside the parent, as is the nature of absolute positions.
157   auto count = GetChildCount();
158   DALI_LOG_INFO( gLogFilter, Debug::Concise, "AbsoluteLayout::OnLayout child count(%d)\n", count  );
159
160   for( unsigned int i = 0; i < count; i++)
161   {
162     LayoutItemPtr childLayout = GetChildAt( i );
163     if( childLayout != nullptr )
164     {
165       auto childOwner = childLayout->GetOwner();
166       LayoutLength childWidth = childLayout->GetMeasuredWidth();
167       LayoutLength childHeight = childLayout->GetMeasuredHeight();
168
169       auto childPosition = childOwner.GetProperty< Vector3 >( Actor::Property::POSITION );
170       auto childSize = childOwner.GetProperty< Vector3 >( Actor::Property::SIZE );
171
172       // Check if there on going position or size animation and skip it to avoid legacy application regressions
173       if( childPosition != childOwner.GetCurrentProperty< Vector3 >( Actor::Property::POSITION ) ||
174           childSize != childOwner.GetCurrentProperty< Vector3 >( Actor::Property::SIZE ) )
175       {
176         break;
177       }
178
179       LayoutLength childTop = childPosition.y;
180       LayoutLength childLeft = childPosition.x;
181
182       DALI_LOG_STREAM( gLogFilter, Debug::General,
183                        "AbsoluteLayout::OnLayout child[" << Toolkit::Control::DownCast( childOwner ).GetName().c_str() <<
184                        "] position(" << childPosition << ") child width[" << childWidth << "] height[" << childHeight << "]\n" );
185
186       childLayout->Layout( childLeft, childTop, childLeft + childWidth, childTop + childHeight );
187     }
188   }
189 }
190
191 } // namespace Internal
192 } // namespace Toolkit
193 } // namespace Dali