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.
17 #include <dali/public-api/common/stage.h>
18 #include <dali/public-api/actors/layer.h>
19 #include <dali-toolkit/internal/layouting/layout-controller-impl.h>
20 #include <dali-toolkit/internal/layouting/layout-item-data-impl.h>
21 #include <dali-toolkit/devel-api/layouting/layout-group-impl.h>
22 #include <dali-toolkit/public-api/controls/control.h>
23 #include <dali-toolkit/public-api/controls/control-impl.h>
24 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
25 #include <dali-toolkit/internal/layouting/layout-controller-debug.h>
26 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
33 #if defined(DEBUG_ENABLED)
34 static Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_LAYOUT" );
46 LayoutController::LayoutController()
47 : mLayoutRequested( false ),
48 mFocusChangedFunctor( *this ),
53 LayoutController::~LayoutController()
57 void LayoutController::Initialize()
59 mAnimation = Animation::New( 0.0f );
61 Dali::Toolkit::KeyInputFocusManager manager = Dali::Toolkit::KeyInputFocusManager::Get();
62 manager.KeyInputFocusChangedSignal().Connect( mSlotDelegate.GetConnectionTracker(), mFocusChangedFunctor );
65 void LayoutController::FocusChangedFunctor::operator() ( Dali::Toolkit::Control gainingControl, Dali::Toolkit::Control lostControl )
67 Toolkit::LayoutItem layoutItem = Toolkit::DevelControl::GetLayout( gainingControl );
70 Toolkit::Internal::LayoutItem& layoutItemImpl = GetImplementation( layoutItem );
71 LayoutParent* layoutParent = layoutItemImpl.GetParent();
74 LayoutGroup* layoutGroup = static_cast< LayoutGroup* >( layoutParent );
75 layoutController.RequestLayout( dynamic_cast< Toolkit::Internal::LayoutItem& >( *layoutGroup ), Dali::Toolkit::LayoutTransitionData::ON_CHILD_FOCUS, gainingControl, lostControl );
80 void LayoutController::RequestLayout( LayoutItem& layoutItem, int layoutTransitionType, Actor gainedChild, Actor lostChild )
82 auto actor = Actor::DownCast( layoutItem.GetOwner() );
85 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LayoutController::RequestLayout owner[%s] layoutItem[%p] layoutTransitionType(%d)\n", actor.GetName().c_str(), &layoutItem, layoutTransitionType );
89 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LayoutController::RequestLayout layoutItem[%p] layoutAnimationType(%d)\n", &layoutItem, layoutTransitionType );
92 mLayoutRequested = true;
93 if( layoutTransitionType != -1 )
95 LayoutTransition layoutTransition = LayoutTransition( layoutItem, layoutTransitionType, gainedChild, lostChild );
96 if( std::find( mLayoutTransitions.begin(), mLayoutTransitions.end(), layoutTransition ) == mLayoutTransitions.end() && layoutItem.GetTransitionData( layoutTransitionType ).Get() )
98 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LayoutController::RequestLayout Add transition layoutTransitionType(%d)\n", layoutTransitionType );
99 mLayoutTransitions.push_back( layoutTransition );
103 // Go up the tree and mark all parents to relayout
104 LayoutParent* layoutParent = layoutItem.GetParent();
107 LayoutGroup& layoutGroup = static_cast< LayoutGroup& >( *layoutParent );
108 if( ! layoutGroup.IsLayoutRequested() )
110 layoutGroup.RequestLayout();
115 void LayoutController::Process()
117 // Perform the full process.
118 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LayoutController::Process\n" );
120 if( mLayoutRequested )
122 // If window size has changed, expect stage to have already been updated
123 Stage stage = Stage::GetCurrent();
124 auto stageWidth = stage.GetSize().width;
125 auto stageHeight = stage.GetSize().height;
127 MeasureSpec widthSpec( stageWidth, MeasureSpec::Mode::EXACTLY );
128 MeasureSpec heightSpec( stageHeight, MeasureSpec::Mode::EXACTLY );
130 LayoutTransition layoutTransition;
131 LayoutPositionDataArray layoutPositionDataArray;
132 LayoutAnimatorArray layoutAnimatorArray;
133 LayoutDataArray layoutDataArray;
134 LayoutDataArray childrenLayoutDataArray;
136 if ( mLayoutTransitions.size() )
138 layoutTransition = mLayoutTransitions.front();
139 mLayoutTransitions.pop_front();
140 mLayoutRequested = ( mLayoutTransitions.size() != 0 );
144 mLayoutRequested = false;
147 LayoutData layoutData( layoutTransition, layoutPositionDataArray, layoutAnimatorArray, layoutDataArray, childrenLayoutDataArray );
148 LayoutItem::Impl::sLayoutData = &layoutData;
150 if( layoutTransition.layoutTransitionType != -1 )
152 UpdateMeasureHierarchyForAnimation( layoutData );
155 // Test how to perform a measure on each control.
156 MeasureHierarchy( stage.GetRootLayer(), widthSpec, heightSpec );
158 LAYOUT_DEBUG_MEASURE_STATES( stage.GetRootLayer() );
160 if( layoutTransition.layoutTransitionType != -1 )
162 RestoreActorsSpecs();
165 layoutAnimatorArray.clear();
166 layoutDataArray.clear();
167 childrenLayoutDataArray.clear();
169 PerformLayout( stage.GetRootLayer(), 0, 0, stageWidth, stageHeight );
171 PerformLayoutPositioning( layoutPositionDataArray, false );
173 PerformLayoutAnimation( layoutTransition, layoutPositionDataArray, layoutDataArray, layoutAnimatorArray );
174 LayoutItem::Impl::sLayoutData = nullptr;
176 LAYOUT_DEBUG_AFTER_LAYOUT( stage.GetRootLayer() );
180 void LayoutController::MeasureHierarchy( Actor root, MeasureSpec widthSpec, MeasureSpec heightSpec )
182 // Does this actor have a layout?
183 // Yes - measure the layout. It will call this method again for each of it's children.
184 // No - recurse through actor children.
186 // If in a leaf actor with no layout, it's natural size is bubbled back up.
188 // What happens if nothing in the tree has a layout?
190 Toolkit::Control control = Toolkit::Control::DownCast( root );
193 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutController::Measuring control:%s\n", control.GetName().c_str() );
194 Internal::Control& controlImpl = GetImplementation( control );
196 Internal::Control::Impl& controlDataImpl = Internal::Control::Impl::Get( controlImpl );
197 LayoutItemPtr layout = controlDataImpl.GetLayout();
201 layout->Measure( widthSpec, heightSpec );
206 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutController::Measuring (%u) children\n", root.GetChildCount() );
207 // Depth first descent through actor children
208 for( unsigned int i = 0, count = root.GetChildCount(); i < count; ++i )
210 Actor child = root.GetChildAt( i );
211 MeasureHierarchy( child, widthSpec, heightSpec );
216 void LayoutController::UpdateMeasureHierarchyForAnimation( LayoutData& layoutData )
218 LayoutTransition& layoutTransition = layoutData.layoutTransition;
219 Actor transitionOwner = Actor::DownCast( layoutTransition.layoutItem.Get()->GetOwner() );
220 LayoutTransitionDataPtr layoutTransitionDataPtr = layoutTransition.layoutItem->GetTransitionData( layoutTransition.layoutTransitionType );
222 if( !layoutTransitionDataPtr->HasUpdateMeasuredSize() )
227 layoutData.updateMeasuredSize = true;
228 layoutTransitionDataPtr->CollectLayoutDataElements( transitionOwner, layoutData );
230 UpdateMeasureHierarchyForAnimation( transitionOwner, layoutData );
232 for( auto layoutDataElement : layoutData.layoutDataArray )
234 if( !layoutDataElement.updateMeasuredSize )
239 Actor actor = Actor::DownCast( layoutDataElement.handle );
240 LayoutDataAnimator animator = layoutData.layoutAnimatorArray[ layoutDataElement.animatorIndex ];
241 float width = actor.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
242 float height = actor.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION );
244 if( layoutDataElement.AdjustMeasuredSize( width, height, animator.animatorType ) )
246 mActorSizeSpecs.push_back( ActorSizeSpec( actor ) );
247 actor.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, static_cast<int>( width ) );
248 actor.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, static_cast<int>( height ) );
252 layoutData.updateMeasuredSize = false;
255 void LayoutController::UpdateMeasureHierarchyForAnimation( Actor root, LayoutData& layoutData )
257 Toolkit::Control control = Toolkit::Control::DownCast( root );
260 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutController::UpdateMeasureHierarchyForAnimation control:%s\n", control.GetName().c_str() );
261 Internal::Control& controlImpl = GetImplementation( control );
262 Internal::Control::Impl& controlDataImpl = Internal::Control::Impl::Get( controlImpl );
263 LayoutItemPtr layout = controlDataImpl.GetLayout();
267 auto layoutGroup = Toolkit::LayoutGroup::DownCast( layout.Get() );
270 unsigned int childCount = layoutGroup.GetChildCount();
271 for( unsigned int i=0; i<childCount; ++i )
273 auto childLayout = layoutGroup.GetChildAt( i );
276 auto childControl = Toolkit::Control::DownCast( childLayout.GetOwner() );
277 LayoutTransitionData::CollectChildrenLayoutDataElements( childControl, layoutData );
285 // Depth first descent through actor children
286 for( unsigned int i = 0, count = root.GetChildCount(); i < count; ++i )
288 UpdateMeasureHierarchyForAnimation( root.GetChildAt( i ), layoutData );
293 void LayoutController::RestoreActorsSpecs()
295 for( auto& actorSizeSpec : mActorSizeSpecs )
297 Actor actor = actorSizeSpec.actor;
298 actor.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, actorSizeSpec.widthSpec );
299 actor.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, actorSizeSpec.heightSpec );
303 void LayoutController::PerformLayout( Actor root, int left, int top, int right, int bottom )
305 Toolkit::Control control = Toolkit::Control::DownCast( root );
308 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutController::PerformLayout on control[%s]\n", control.GetName().c_str() );
309 Internal::Control& controlImpl = GetImplementation( control );
310 Internal::Control::Impl& controlDataImpl = Internal::Control::Impl::Get( controlImpl );
311 LayoutItemPtr layout = controlDataImpl.GetLayout();
315 layout->Layout( left, top, right, bottom );
320 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutController::PerformLayout (%u) children\n", root.GetChildCount() );
321 // Depth first descent through actor children
322 for( unsigned int i = 0, count = root.GetChildCount(); i < count; ++i )
324 Actor child = root.GetChildAt( i );
325 PerformLayout( child, left, top, right, bottom );
330 void LayoutController::PerformLayoutPositioning( LayoutPositionDataArray& layoutPositionDataArray, bool all ) const
332 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutController::PerformLayoutPositioning %d\n", (int)all );
334 for( auto layoutPositionData : layoutPositionDataArray )
336 Actor actor = Actor::DownCast( layoutPositionData.handle );
337 if( actor && ( !layoutPositionData.animated || all ) )
339 if ( !layoutPositionData.animated )
341 actor.SetPosition( layoutPositionData.left, layoutPositionData.top );
342 actor.SetSize( layoutPositionData.right - layoutPositionData.left, layoutPositionData.bottom - layoutPositionData.top );
346 actor.SetPosition( actor.GetCurrentPosition() );
347 actor.SetSize( actor.GetCurrentSize() );
353 void LayoutController::PerformLayoutAnimation( LayoutTransition& layoutTransition, LayoutPositionDataArray& layoutPositionDataArray, LayoutDataArray& layoutDataArray, LayoutAnimatorArray& layoutAnimatorArray )
355 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutController::PerformLayoutAnimation\n" );
356 Animation animation = Animation::New( 0 );
357 bool isAnimatorAdded = false;
359 if( layoutAnimatorArray.size() == 0 )
361 layoutAnimatorArray.push_back( LayoutDataAnimator() );
364 for( auto layoutDataElement : layoutDataArray )
366 if( layoutDataElement.animatorIndex >= 0 )
368 Actor actor = Actor::DownCast( layoutDataElement.handle );
371 LayoutDataAnimator animator = layoutAnimatorArray[ layoutDataElement.animatorIndex ];
372 TimePeriod timePeriod = TimePeriod( 0, animation.GetDuration() );
373 if( animator.timePeriod.durationSeconds >= 0 )
375 timePeriod = animator.timePeriod;
378 Property::Value value = layoutDataElement.targetValue;
379 // Capture calculated position and size values after layout if target values are not set.
380 // Other values are set to current actor ones.
381 if( value.GetType() == Property::NONE )
383 if( layoutDataElement.positionDataIndex < 0)
385 auto result = std::find_if( layoutPositionDataArray.begin(), layoutPositionDataArray.end(), [&actor](const LayoutPositionData& iter)
386 { return iter.handle == actor; } );
387 if( result == layoutPositionDataArray.end() )
391 layoutDataElement.positionDataIndex = std::distance( layoutPositionDataArray.begin(), result );
394 LayoutPositionData& positionData = layoutPositionDataArray[ layoutDataElement.positionDataIndex ];
395 // with updated measured size scale animation the measured size includes scale, so we need to fit in the centre of the measured rectangle
396 // the real size child so that the all scale related animations placed correctly
397 if( positionData.updateWithCurrentSize )
399 Vector3 size = actor.GetCurrentSize();
400 float dX = ( ( positionData.right - positionData.left ) - size.width ) / 2;
401 float dY = ( ( positionData.bottom - positionData.top ) - size.height ) / 2;
402 positionData.left += dX;
403 positionData.top += dY;
404 positionData.right -= dX;
405 positionData.bottom -= dY;
406 positionData.updateWithCurrentSize = false;
409 switch ( layoutDataElement.propertyIndex )
411 case Actor::Property::POSITION:
412 value = Vector3( positionData.left, positionData.top, 0.0f );
414 case Actor::Property::POSITION_X:
415 value = positionData.left;
417 case Actor::Property::POSITION_Y:
418 value = positionData.top;
420 case Actor::Property::SIZE:
421 value = Vector3( positionData.right - positionData.left, positionData.bottom - positionData.top, 0.0f );
423 case Actor::Property::SIZE_WIDTH:
424 value = positionData.right - positionData.left;
426 case Actor::Property::SIZE_HEIGHT:
427 value = positionData.bottom - positionData.top;
430 value = actor.GetProperty( layoutDataElement.propertyIndex );
434 // Failed to get target value, just move to the next one
435 if( value.GetType() == Property::NONE )
441 Property::Value initialValue = layoutDataElement.initialValue;
442 if( initialValue.GetType() != Property::NONE )
444 actor.SetProperty( layoutDataElement.propertyIndex, initialValue );
447 // Create an animator for the property
448 switch( animator.animatorType )
450 case Toolkit::LayoutTransitionData::Animator::ANIMATE_TO:
452 animation.AnimateTo( Property( actor, layoutDataElement.propertyIndex ), value, animator.alphaFunction, timePeriod );
455 case Toolkit::LayoutTransitionData::Animator::ANIMATE_BY:
457 animation.AnimateBy( Property( actor, layoutDataElement.propertyIndex ), value, animator.alphaFunction, timePeriod );
460 case Toolkit::LayoutTransitionData::Animator::ANIMATE_BETWEEN:
462 animation.AnimateBetween( Property( actor, layoutDataElement.propertyIndex ), animator.keyFrames, animator.alphaFunction, animator.interpolation );
465 case Toolkit::LayoutTransitionData::Animator::ANIMATE_PATH:
467 animation.Animate( actor, animator.path, animator.forward, animator.alphaFunction, timePeriod );
471 isAnimatorAdded = true;
476 if( isAnimatorAdded )
478 if( mAnimation.GetState() == Animation::PLAYING )
480 mAnimation.SetCurrentProgress( 1.0f );
482 mAnimation = animation;
483 mAnimationFinishedFunctors.push_back( AnimationFinishedFunctor( *this, layoutTransition, layoutPositionDataArray ) );
484 mAnimation.FinishedSignal().Connect( mSlotDelegate.GetConnectionTracker(), mAnimationFinishedFunctors.back() );
489 } // namespace Internal
490 } // namespace Toolkit