2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
19 #include "relayout-controller-impl.h"
25 #include <dali/integration-api/debug.h>
29 #include "dali-toolkit/public-api/controls/control.h"
30 #include "dali-toolkit/public-api/controls/control-impl.h"
31 #include "dali-toolkit/public-api/controls/text-view/text-view.h"
39 typedef std::pair< Control, Vector2 > ControlSizePair;
40 typedef std::stack< ControlSizePair > ControlStack;
47 #if defined(DEBUG_ENABLED)
49 Integration::Log::Filter* gLogFilter( Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_RELAYOUT_CONTROLLER") );
52 * Prints out all the children of the given actor when debug is enabled.
54 * @param[in] actor The actor whose children to print.
55 * @param[in] level The number of " | " to put in front of the children.
57 void PrintChildren( Actor actor, int level )
59 std::ostringstream output;
61 for ( int t = 0; t < level; ++t )
66 output << actor.GetTypeName();
68 output << " - Pos: " << actor.GetCurrentPosition() << " Size: " << actor.GetCurrentSize() << ",";
70 output << " (" << actor.GetObjectPtr() << ")" << std::endl;
72 DALI_LOG_INFO( gLogFilter, Debug::Verbose, output.str().c_str() );
75 unsigned int numChildren = actor.GetChildCount();
76 for( unsigned int i=0; i<numChildren; ++i )
78 PrintChildren( actor.GetChildAt(i), level );
84 * Prints the entire hierarchy of the scene.
88 PrintChildren( Stage().GetCurrent().GetRootLayer(), 0 );
91 #define PRINT_HIERARCHY PrintHierarchy()
93 #else // defined(DEBUG_ENABLED)
95 #define PRINT_HIERARCHY
97 #endif // defined(DEBUG_ENABLED)
100 * Sets the target to source if the individual elements of source are NOT zero.
102 * @param[out] target The Vector2 elements to set if the source Vector2 elements are not 0.
103 * @param[in] source The Vector2 elements that are to be set to the target Vector2.
105 void SetIfNotZero( Vector2& target, const Vector2& source )
107 // Only set the width if it is non zero.
108 if ( !EqualsZero( source.width ) )
110 target.width = source.width;
113 // Only set the height if it is non zero.
114 if ( !EqualsZero( source.height ) )
116 target.height = source.height;
121 * Finds controls in the hierarchy of actor. It descends the tree if the actor has more Actors.
122 * If it is a control, it no longer descends the tree.
124 * @param[in] actor The actor in which controls should be found.
125 * @param[in] size The size that this actor and its children should be.
127 void FindControls( Actor actor, ControlStack& controls, Vector2 size )
129 Control control( Control::DownCast( actor ) );
132 // If the control size has been set by the application / control, then we should try and honour that.
133 Vector2 controlSetSize( control.GetImplementation().GetSizeSet() );
135 // Only set the width and height if they are non zero.
136 SetIfNotZero( size, controlSetSize );
138 controls.push( ControlSizePair( control, size ) );
142 unsigned int numChildren = actor.GetChildCount();
143 for( unsigned int i=numChildren; i>0; --i )
145 FindControls( actor.GetChildAt(i-1), controls, size );
151 * Pushes the controls in the container, to the stack.
153 * @param[in,out] controlStack The stack to push controls to.
154 * @param[in] container The container to push controls from.
156 void PushToStack( ControlStack& controlStack, const ActorSizeContainer& container )
158 for ( ActorSizeContainer::const_reverse_iterator iter = container.rbegin(), endIter = container.rend(); iter != endIter; ++iter )
160 FindControls( iter->first, controlStack, iter->second );
164 } // unnamed namespace
166 RelayoutControllerImpl::~RelayoutControllerImpl()
170 void RelayoutControllerImpl::Request()
172 //TODO use Relayout Request to set up logic to optimize relayout of the actors/controls in the scene
174 if( !mRelayoutConnection )
176 Stage stage = Stage::GetCurrent();
177 stage.EventProcessingFinishedSignal().Connect( this, &RelayoutControllerImpl::Relayout );
178 mRelayoutConnection = true;
182 void RelayoutControllerImpl::Relayout()
186 // 1. Finds all top-level controls from the root actor and allocate them the size of the stage
187 // These controls are paired with the stage size and added to the stack.
188 ControlStack controlStack;
189 FindControls( Stage().GetCurrent().GetRootLayer(), controlStack, Stage::GetCurrent().GetSize() );
191 // 2. Iterate through the stack until it's empty.
192 while ( !controlStack.empty() )
194 ControlSizePair pair ( controlStack.top() );
195 Control control ( pair.first );
196 Vector2 size ( pair.second );
199 DALI_LOG_INFO( gLogFilter, Debug::General, "Allocating %p (%.2f, %.2f)\n", control.GetObjectPtr(), size.width, size.height );
201 // 3. Negotiate the size with the current control. Pass it an empty container which the control
202 // has to fill with all the actors it has not done any size negotiation for.
203 ActorSizeContainer container;
204 control.GetImplementation().NegotiateSize( size, container );
206 // 4. Push the controls from the actors in the container to the stack.
207 PushToStack( controlStack, container );
210 //Disconnect so that we relayout only when requested to do so.
214 void RelayoutControllerImpl::Disconnect()
216 if( mRelayoutConnection )
218 Stage stage = Stage::GetCurrent();
219 stage.EventProcessingFinishedSignal().Disconnect( this, &RelayoutControllerImpl::Relayout );
220 mRelayoutConnection = false;
224 RelayoutControllerImpl::RelayoutControllerImpl()
225 : mRelayoutConnection( false )
229 } // namespace Internal
231 } // namespace Toolkit