DALi Version 1.0.33
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / relayout-controller-impl.cpp
1 /*
2  * Copyright (c) 2014 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
18 // CLASS HEADER
19 #include "relayout-controller-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/actors/layer.h>
23 #include <dali/public-api/common/stage.h>
24 #include <dali/integration-api/debug.h>
25
26 #if defined(DEBUG_ENABLED)
27 #include <sstream>
28 #endif // defined(DEBUG_ENABLED)
29
30 // INTERNAL INCLUDES
31 #include <dali-toolkit/public-api/controls/text-view/text-view.h>
32
33 namespace Dali
34 {
35
36 namespace Toolkit
37 {
38
39 namespace Internal
40 {
41
42 namespace
43 {
44 #if defined(DEBUG_ENABLED)
45
46 Integration::Log::Filter* gLogFilter( Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_RELAYOUT_CONTROLLER") );
47
48 /**
49  * Prints out all the children of the given actor when debug is enabled.
50  *
51  * @param[in]  actor  The actor whose children to print.
52  * @param[in]  level  The number of " | " to put in front of the children.
53  */
54 void PrintChildren( Actor actor, int level )
55 {
56   std::ostringstream output;
57
58   for ( int t = 0; t < level; ++t )
59   {
60     output << " | ";
61   }
62
63   output << actor.GetTypeName();
64
65   output << " - Pos: " << actor.GetCurrentPosition() << " Size: " << actor.GetCurrentSize() << ",";
66
67   output << " (" << actor.GetObjectPtr() << ")" << std::endl;
68
69   DALI_LOG_INFO( gLogFilter, Debug::Verbose, output.str().c_str() );
70
71   ++level;
72   unsigned int numChildren = actor.GetChildCount();
73   for( unsigned int i=0; i<numChildren; ++i )
74   {
75     PrintChildren( actor.GetChildAt(i), level );
76   }
77   --level;
78 }
79
80 /**
81  * Prints the entire hierarchy of the scene.
82  */
83 void PrintHierarchy()
84 {
85   if ( gLogFilter->IsEnabledFor( Debug::Verbose ) )
86   {
87     PrintChildren( Stage().GetCurrent().GetRootLayer(), 0 );
88   }
89 }
90
91 #define PRINT_HIERARCHY PrintHierarchy()
92
93 #else // defined(DEBUG_ENABLED)
94
95 #define PRINT_HIERARCHY
96
97 #endif // defined(DEBUG_ENABLED)
98
99 /**
100  * Sets the target to source if the individual elements of source are NOT zero.
101  *
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.
104  */
105 void SetIfNotZero( Vector2& target, const Vector2& source )
106 {
107   // Only set the width if it is non zero.
108   if ( !EqualsZero( source.width ) )
109   {
110     target.width  = source.width;
111   }
112
113   // Only set the height if it is non zero.
114   if ( !EqualsZero( source.height ) )
115   {
116     target.height = source.height;
117   }
118 }
119
120 /**
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.
123  *
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.
126  */
127 void FindControls( Actor actor, ControlStack& controls, Vector2 size )
128 {
129   Toolkit::Control control( Toolkit::Control::DownCast( actor ) );
130   if( control )
131   {
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() );
134
135     // Only set the width and height if they are non zero.
136     SetIfNotZero( size, controlSetSize );
137
138     controls.push_back( ControlSizePair( control, size ) );
139   }
140   else
141   {
142     unsigned int numChildren = actor.GetChildCount();
143     for( unsigned int i=numChildren; i>0; --i )
144     {
145       FindControls( actor.GetChildAt(i-1), controls, size );
146     }
147   }
148 }
149
150 /**
151  * Pushes the controls in the container, to the stack.
152  *
153  * @param[in,out]  controlStack  The stack to push controls to.
154  * @param[in]      container     The container to push controls from.
155  */
156 void PushToStack( ControlStack& controlStack, const ActorSizeContainer& container )
157 {
158   for ( ActorSizeContainer::const_reverse_iterator iter = container.rbegin(), endIter = container.rend(); iter != endIter; ++iter )
159   {
160     FindControls( iter->first, controlStack, iter->second );
161   }
162 }
163
164 } // unnamed namespace
165
166 RelayoutControllerImpl::RelayoutControllerImpl( bool& relayoutFlag )
167 : mRelayoutFlag( relayoutFlag ),
168   mRelayoutConnection( false )
169 {
170   // make space for 32 controls to avoid having to copy construct a lot in the beginning
171   mControlStack.reserve( 32 );
172   mSizecontainer.reserve( 32 );
173 }
174
175 RelayoutControllerImpl::~RelayoutControllerImpl()
176 {
177 }
178
179 void RelayoutControllerImpl::Request()
180 {
181   //TODO use Relayout Request to set up logic to optimize relayout of the actors/controls in the scene
182
183   if( !mRelayoutConnection )
184   {
185     Stage stage = Stage::GetCurrent();
186     stage.EventProcessingFinishedSignal().Connect( this, &RelayoutControllerImpl::Relayout );
187     mRelayoutConnection = true;
188   }
189 }
190
191 void RelayoutControllerImpl::Relayout()
192 {
193   // only do something when requested
194   if( mRelayoutFlag )
195   {
196     // clear the flag as we're now doing the relayout
197     mRelayoutFlag = false;
198     PRINT_HIERARCHY;
199
200     mControlStack.clear(); // we do not release memory, just empty the container
201
202     // 1. Finds all top-level controls from the root actor and allocate them the size of the stage
203     //    These controls are paired with the stage size and added to the stack.
204     FindControls( Stage().GetCurrent().GetRootLayer(), mControlStack, Stage::GetCurrent().GetSize() );
205
206     // 2. Iterate through the stack until it's empty.
207     while ( !mControlStack.empty() )
208     {
209       ControlSizePair pair ( mControlStack.back() );
210       Toolkit::Control control ( pair.first );
211       Vector2 size ( pair.second );
212       mControlStack.pop_back();
213
214       DALI_LOG_INFO( gLogFilter, Debug::General, "Allocating %p (%.2f, %.2f)\n", control.GetObjectPtr(), size.width, size.height );
215
216       mSizecontainer.clear();
217       // 3. Negotiate the size with the current control. Pass it an empty container which the control
218       //    has to fill with all the actors it has not done any size negotiation for.
219       control.GetImplementation().NegotiateSize( size, mSizecontainer );
220
221       // 4. Push the controls from the actors in the container to the stack.
222       PushToStack( mControlStack, mSizecontainer );
223     }
224   }
225   // should not disconnect the signal as that causes some control size negotiations to not work correctly
226   // this algorithm needs more optimization as well
227 }
228
229 } // namespace Internal
230
231 } // namespace Toolkit
232
233 } // namespace Dali