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 // Test how to perform a measure on each control.
131 MeasureHierarchy( stage.GetRootLayer(), widthSpec, heightSpec );
133 LAYOUT_DEBUG_MEASURE_STATES( stage.GetRootLayer() );
135 LayoutTransition layoutTransition;
136 LayoutPositionDataArray layoutPositionDataArray;
137 LayoutDataArray layoutDataArray;
138 LayoutAnimatorArray layoutAnimatorArray;
139 layoutAnimatorArray.push_back( LayoutDataAnimator() );
140 PropertyAnimatorArray childrenPropertiesAnimators;
142 if ( mLayoutTransitions.size() )
144 layoutTransition = mLayoutTransitions.front();
145 mLayoutTransitions.pop_front();
146 mLayoutRequested = ( mLayoutTransitions.size() != 0 );
150 mLayoutRequested = false;
153 LayoutData layoutData( layoutTransition, layoutPositionDataArray, layoutDataArray, layoutAnimatorArray, childrenPropertiesAnimators );
154 LayoutItem::Impl::sLayoutData = &layoutData;
155 PerformLayout( stage.GetRootLayer(), 0, 0, stageWidth, stageHeight );
157 PerformLayoutPositioning( layoutPositionDataArray, false );
159 PerformLayoutAnimation( layoutTransition, layoutPositionDataArray, layoutDataArray, layoutAnimatorArray );
160 LayoutItem::Impl::sLayoutData = nullptr;
162 LAYOUT_DEBUG_AFTER_LAYOUT( stage.GetRootLayer() );
166 void LayoutController::MeasureHierarchy( Actor root, MeasureSpec widthSpec, MeasureSpec heightSpec )
168 // Does this actor have a layout?
169 // Yes - measure the layout. It will call this method again for each of it's children.
170 // No - recurse through actor children.
172 // If in a leaf actor with no layout, it's natural size is bubbled back up.
174 // What happens if nothing in the tree has a layout?
176 Toolkit::Control control = Toolkit::Control::DownCast( root );
179 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutController::Measuring control:%s\n", control.GetName().c_str() );
180 Internal::Control& controlImpl = GetImplementation( control );
182 Internal::Control::Impl& controlDataImpl = Internal::Control::Impl::Get( controlImpl );
183 LayoutItemPtr layout = controlDataImpl.GetLayout();
187 layout->Measure( widthSpec, heightSpec );
192 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutController::Measuring (%u) children\n", root.GetChildCount() );
193 // Depth first descent through actor children
194 for( unsigned int i = 0, count = root.GetChildCount(); i < count; ++i )
196 Actor child = root.GetChildAt( i );
197 MeasureHierarchy( child, widthSpec, heightSpec );
202 void LayoutController::PerformLayout( Actor root, int left, int top, int right, int bottom )
204 Toolkit::Control control = Toolkit::Control::DownCast( root );
207 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutController::PerformLayout on control[%s]\n", control.GetName().c_str() );
208 Internal::Control& controlImpl = GetImplementation( control );
209 Internal::Control::Impl& controlDataImpl = Internal::Control::Impl::Get( controlImpl );
210 LayoutItemPtr layout = controlDataImpl.GetLayout();
214 layout->Layout( left, top, right, bottom );
219 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutController::PerformLayout (%u) children\n", root.GetChildCount() );
220 // Depth first descent through actor children
221 for( unsigned int i = 0, count = root.GetChildCount(); i < count; ++i )
223 Actor child = root.GetChildAt( i );
224 PerformLayout( child, left, top, right, bottom );
229 void LayoutController::PerformLayoutPositioning( LayoutPositionDataArray& layoutPositionDataArray, bool all ) const
231 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutController::PerformLayoutPositioning %d\n", (int)all );
233 for( auto layoutPositionData : layoutPositionDataArray )
235 Actor actor = Actor::DownCast( layoutPositionData.handle );
236 if( actor && ( !layoutPositionData.animated || all ) )
238 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutController::PerformLayoutPositioning %s\n", actor.GetName().c_str() );
239 if ( !layoutPositionData.animated )
241 actor.SetPosition( layoutPositionData.left, layoutPositionData.top );
242 actor.SetSize( layoutPositionData.right - layoutPositionData.left, layoutPositionData.bottom - layoutPositionData.top );
246 actor.SetPosition( actor.GetCurrentPosition() );
247 actor.SetSize( actor.GetCurrentSize() );
253 void LayoutController::PerformLayoutAnimation( LayoutTransition& layoutTransition, LayoutPositionDataArray& layoutPositionDataArray, LayoutDataArray& layoutDataArray, LayoutAnimatorArray& layoutAnimatorArray )
255 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutController::PerformLayoutAnimation\n" );
256 Animation animation = Animation::New( 0 );
257 bool isAnimatorAdded = false;
259 for( auto layoutDataElement : layoutDataArray )
261 if ( layoutDataElement.animatorIndex >= 0 )
263 Actor actor = Actor::DownCast( layoutDataElement.handle );
266 LayoutDataAnimator animator = layoutAnimatorArray[ layoutDataElement.animatorIndex ];
267 TimePeriod timePeriod = TimePeriod( 0, animation.GetDuration() );
268 if (animator.timePeriod.durationSeconds >= 0)
270 timePeriod = animator.timePeriod;
273 Property::Value value = layoutDataElement.targetValue;
274 // Capture calculated position and size values after layout if target values are not set.
275 // Other values are set to current actor ones.
276 if( value.GetType() == Property::NONE )
278 if ( layoutDataElement.positionDataIndex < 0)
280 auto result = std::find_if( layoutPositionDataArray.begin(), layoutPositionDataArray.end(), [&actor](const LayoutPositionData& iter)
281 { return iter.handle == actor; } );
282 if( result == layoutPositionDataArray.end() )
286 layoutDataElement.positionDataIndex = std::distance(layoutPositionDataArray.begin(), result);
289 LayoutPositionData& positionData = layoutPositionDataArray[ layoutDataElement.positionDataIndex ];
291 switch ( layoutDataElement.propertyIndex )
293 case Actor::Property::POSITION:
294 value = Vector3( positionData.left, positionData.top, 0.0f );
296 case Actor::Property::POSITION_X:
297 value = positionData.left;
299 case Actor::Property::POSITION_Y:
300 value = positionData.top;
302 case Actor::Property::SIZE:
303 value = Vector3( positionData.right - positionData.left, positionData.bottom - positionData.top, 0.0f );
305 case Actor::Property::SIZE_WIDTH:
306 value = positionData.right - positionData.left;
308 case Actor::Property::SIZE_HEIGHT:
309 value = positionData.bottom - positionData.top;
312 value = actor.GetProperty( layoutDataElement.propertyIndex );
316 // Failed to get target value, just move the next one
317 if( value.GetType() == Property::NONE )
323 Property::Value initialValue = layoutDataElement.initialValue;
324 if( initialValue.GetType() != Property::NONE )
326 actor.SetProperty( layoutDataElement.propertyIndex, initialValue );
329 // Create an animator for the property
330 switch (animator.animatorType)
332 case LayoutDataAnimator::AnimatorType::ANIMATE_TO:
334 animation.AnimateTo( Property( actor, layoutDataElement.propertyIndex ), value, animator.alphaFunction, timePeriod );
337 case LayoutDataAnimator::AnimatorType::ANIMATE_BY:
339 animation.AnimateBy( Property( actor, layoutDataElement.propertyIndex ), value, animator.alphaFunction, timePeriod );
342 case LayoutDataAnimator::AnimatorType::ANIMATE_BETWEEN:
344 animation.AnimateBetween( Property( actor, layoutDataElement.propertyIndex ), animator.keyFrames, animator.alphaFunction, animator.interpolation );
347 case LayoutDataAnimator::AnimatorType::ANIMATE_PATH:
348 animation.Animate( actor, animator.path, animator.forward, animator.alphaFunction, timePeriod );
352 isAnimatorAdded = true;
356 if( isAnimatorAdded )
358 if( mAnimation.GetState() == Animation::PLAYING )
360 mAnimation.SetCurrentProgress( 1.0f );
362 mAnimation = animation;
363 mAnimationFinishedFunctors.push_back( AnimationFinishedFunctor( *this, layoutTransition, layoutPositionDataArray ) );
364 mAnimation.FinishedSignal().Connect( mSlotDelegate.GetConnectionTracker(), mAnimationFinishedFunctors.back() );
369 } // namespace Internal
370 } // namespace Toolkit