Merge "DALi Version 1.4.46" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / layouting / flex-node.cpp
1 /*
2  * Copyright (c) 2019 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 "flex-node.h"
19
20 //EXTERNAL INCLUDES
21 #include <dali/integration-api/debug.h>
22 #include <dali/public-api/actors/actor.h>
23 #include <dali/public-api/object/weak-handle.h>
24
25 //INTERNAL INCLUDES
26 #include <dali-toolkit/third-party/yoga/Yoga.h>
27
28 #if defined(DEBUG_ENABLED)
29 static Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_FLEX" );
30 #endif
31
32 namespace Dali
33 {
34 namespace Toolkit
35 {
36 namespace Flex
37 {
38
39 namespace
40 {
41 // Common callback function that is registered when AddChild is called.
42 // Calls MeasureNode which in turns calls the actual callback passed in AddChild not the common callback.
43 YGSize MeasureChild(YGNodeRef child, float width, YGMeasureMode measureModeWidth, float height, YGMeasureMode measureModeHeight)
44 {
45   DALI_LOG_INFO( gLogFilter, Debug::General, "MeasureChild\n" );
46   // Get the Node from the YGNodeRef
47   Toolkit::Flex::Node* childNode =  static_cast<Toolkit::Flex::Node*>(YGNodeGetContext(child));
48
49   YGSize childSize = YGSize{.width = 1, .height = 1};  // Initialise variable.
50
51   DALI_ASSERT_DEBUG( childNode );
52
53   // Call measure function assigned to this Node
54   Toolkit::Flex::SizeTuple nodeSize = childNode->MeasureNode( width, measureModeWidth, height, measureModeHeight );
55   childSize.width = nodeSize.width;
56   childSize.height = nodeSize.height;
57   DALI_LOG_INFO( gLogFilter, Debug::General, "MeasureChild, childNode valid %f,%f\n", childSize.width, childSize.height );
58
59   return childSize;
60 }
61
62 } // unamed namespace
63
64 struct Node;
65
66 using NodePtr = std::unique_ptr<Node>;
67
68 using  FlexNodeVector = std::vector< NodePtr>;
69
70 struct Node::Impl
71 {
72   YGNodeRef mYogaNode;
73   MeasureCallback mMeasureCallback;
74   WeakHandle< Dali::Actor > mActor;
75   FlexNodeVector mChildNodes;
76 };
77
78 Node::Node() : mImpl( new Impl )
79 {
80   mImpl->mYogaNode = YGNodeNew();
81   YGNodeSetContext( mImpl->mYogaNode, this );
82   mImpl->mMeasureCallback = NULL;
83   DALI_LOG_INFO( gLogFilter, Debug::General, "Node()  Context [%p] set to mYogaNode[%p]\n", this, mImpl->mYogaNode );
84
85   // Set default style
86   YGNodeStyleSetFlexDirection( mImpl->mYogaNode, YGFlexDirectionColumn );
87   YGNodeStyleSetFlexWrap( mImpl->mYogaNode, YGWrapNoWrap );
88   YGNodeStyleSetJustifyContent( mImpl->mYogaNode, YGJustifyFlexStart );
89   YGNodeStyleSetAlignContent( mImpl->mYogaNode, YGAlignFlexStart );
90   YGNodeStyleSetAlignItems( mImpl->mYogaNode, YGAlignFlexStart );
91 }
92
93 Node::~Node()
94 {
95   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Destructor() >> \n");
96   if( mImpl->mYogaNode )
97   {
98     YGNodeFreeRecursive( mImpl->mYogaNode );
99     mImpl->mYogaNode = nullptr;
100   }
101   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Destructor() <<\n");
102 }
103
104 void Node::AddChild( Actor child, MeasureCallback measureFunction, int index )
105 {
106   if( child )
107   {
108     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "AddChild[%s] to node[%p] at index:%d\n", child.GetName().c_str(), mImpl->mYogaNode, index );
109
110     NodePtr childNode( new Node() );
111
112     // Store measure function passed in so can call it when the MeasureChild function is called.
113     childNode->mImpl->mMeasureCallback = measureFunction;
114
115     childNode->mImpl->mActor = child;
116     Vector2 minumumSize = child.GetMinimumSize();
117     Vector2 maximumSize = child.GetMaximumSize();
118     YGNodeStyleSetMaxWidth( childNode->mImpl->mYogaNode, maximumSize.width );
119     YGNodeStyleSetMaxHeight( childNode->mImpl->mYogaNode, maximumSize.height );
120     YGNodeStyleSetMinWidth( childNode->mImpl->mYogaNode, minumumSize.width );
121     YGNodeStyleSetMinHeight( childNode->mImpl->mYogaNode, minumumSize.height );
122
123     YGNodeSetMeasureFunc( childNode->mImpl->mYogaNode, &MeasureChild );
124
125     YGNodeInsertChild( mImpl->mYogaNode, childNode->mImpl->mYogaNode, index );
126
127     mImpl->mChildNodes.emplace_back( std::move(childNode) );
128   }
129 }
130
131 void Node::RemoveChild( Actor child )
132 {
133   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "RemoveChild child:[%s] from internal nodeCount[%d] childCount[%d]\n", child.GetName().c_str(), YGNodeGetChildCount( mImpl->mYogaNode ), mImpl->mChildNodes.size()  );
134
135   auto iterator = std::find_if( mImpl->mChildNodes.begin(),mImpl->mChildNodes.end(),
136                                 [&child]( NodePtr& childNode ){ return childNode->mImpl->mActor.GetHandle() == child;});
137
138   if( iterator != mImpl->mChildNodes.end() )
139   {
140       YGNodeRemoveChild( mImpl->mYogaNode, (*iterator)->mImpl->mYogaNode );
141       mImpl->mChildNodes.erase(iterator);
142   }
143
144   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "RemoveChild internal nodeCount[%d] childCount[%d]\n", YGNodeGetChildCount( mImpl->mYogaNode ), mImpl->mChildNodes.size()  );
145 }
146
147 SizeTuple Node::MeasureNode( float width, int widthMode, float height, int heightMode)
148 {
149   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MeasureNode\n" );
150
151   // Execute callback registered with AddChild
152   Toolkit::Flex::SizeTuple nodeSize{8,8}; // Default size set to 8,8 to aid bug detection.
153   if( mImpl->mMeasureCallback && mImpl->mActor.GetHandle() )
154   {
155     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MeasureNode MeasureCallback executing on %s\n", mImpl->mActor.GetHandle().GetName().c_str() );
156     nodeSize = mImpl->mMeasureCallback( mImpl->mActor.GetHandle(), width, widthMode, height, heightMode );
157   }
158   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MeasureNode nodeSize width:%f height:%f\n", nodeSize.width, nodeSize.height );
159   return nodeSize;
160 }
161
162 void Node::CalculateLayout(float availableWidth, float availableHeight, bool isRTL)
163 {
164   DALI_LOG_INFO( gLogFilter, Debug::General, "CalculateLayout availableSize(%f,%f)\n", availableWidth, availableHeight );
165   YGNodeCalculateLayout( mImpl->mYogaNode, availableWidth, availableHeight, isRTL ? YGDirectionRTL : YGDirectionLTR );
166 }
167
168 Dali::Vector4 Node::GetNodeFrame( int index ) const
169 {
170   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "GetNodeFrame[%d]\n", index );
171   YGNodeRef childNode = YGNodeGetChild( mImpl->mYogaNode, index );
172   Dali::Vector4 frame = Vector4::ZERO;
173   if(childNode)
174   {
175     frame.x = YGNodeLayoutGetLeft( childNode );
176     frame.y = YGNodeLayoutGetTop( childNode );
177     frame.z = frame.x + YGNodeLayoutGetWidth( childNode );
178     frame.w = frame.y + YGNodeLayoutGetHeight( childNode );
179     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "GetNodeFrame Node index[%d] child ptr[%p] GetYogaNodeFrame left:%f top:%f right:%f bottom:%f\n",
180                    index, childNode, frame.x , frame.y, frame.z, frame.w);
181   }
182   else
183   {
184     frame.x = YGNodeLayoutGetLeft( mImpl->mYogaNode );
185     frame.y = YGNodeLayoutGetTop( mImpl->mYogaNode );
186     frame.z = frame.x + YGNodeLayoutGetWidth( mImpl->mYogaNode );
187     frame.w = frame.y + YGNodeLayoutGetHeight( mImpl->mYogaNode );
188     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "GetNodeFrame Root index[%d] root ptr[%p] GetYogaNodeFrame left:%f top:%f right:%f bottom:%f\n",
189                    index, mImpl->mYogaNode, frame.x , frame.y, frame.z, frame.w);
190   }
191
192   return frame;
193 }
194 void Node::SetFlexDirection( Dali::Toolkit::Flex::FlexDirection flexDirection )
195 {
196   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Set flex direction[%d]\n", flexDirection );
197
198   YGNodeStyleSetFlexDirection( mImpl->mYogaNode, static_cast<YGFlexDirection>(flexDirection) );
199 }
200
201 Dali::Toolkit::Flex::FlexDirection Node::GetFlexDirection() const
202 {
203   return static_cast<Dali::Toolkit::Flex::FlexDirection>(YGNodeStyleGetFlexDirection( mImpl->mYogaNode ));
204 }
205
206 void Node::SetFlexJustification( Dali::Toolkit::Flex::Justification flexJustification )
207 {
208   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Set flex justification[%d]\n", flexJustification )
209
210   YGNodeStyleSetJustifyContent( mImpl->mYogaNode, static_cast<YGJustify>(flexJustification) );
211 }
212
213 Dali::Toolkit::Flex::Justification Node::GetFlexJustification() const
214 {
215   return static_cast<Dali::Toolkit::Flex::Justification>(YGNodeStyleGetJustifyContent( mImpl->mYogaNode ));
216 }
217
218 Dali::Toolkit::Flex::WrapType Node::GetFlexWrap() const
219 {
220   return static_cast<Dali::Toolkit::Flex::WrapType>(YGNodeStyleGetFlexWrap( mImpl->mYogaNode ));
221 }
222
223 void Node::SetFlexAlignment(Dali::Toolkit::Flex::Alignment flexAlignment )
224 {
225   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Set flex alignment[%d]\n", flexAlignment )
226
227   YGNodeStyleSetAlignContent( mImpl->mYogaNode , static_cast<YGAlign>(flexAlignment) );
228 }
229
230 Dali::Toolkit::Flex::Alignment Node::GetFlexAlignment() const
231 {
232   return static_cast<Dali::Toolkit::Flex::Alignment>(YGNodeStyleGetAlignContent( mImpl->mYogaNode ));
233 }
234
235 void Node::SetFlexItemsAlignment(Dali::Toolkit::Flex::Alignment flexAlignment )
236 {
237   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Set flex items alignment[%d] on mYogaNode[%p]\n", flexAlignment, mImpl->mYogaNode )
238
239   YGNodeStyleSetAlignItems( mImpl->mYogaNode, static_cast<YGAlign>(flexAlignment) );
240 }
241
242 Dali::Toolkit::Flex::Alignment Node::GetFlexItemsAlignment() const
243 {
244   return static_cast<Dali::Toolkit::Flex::Alignment>( YGNodeStyleGetAlignItems( mImpl->mYogaNode ));
245 }
246
247 float Node::GetFlexWidth() const
248 {
249   float flexWidth = YGNodeLayoutGetWidth( mImpl->mYogaNode );
250   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Get flex mYogaNode[%p] width[%f]\n", mImpl->mYogaNode, flexWidth)
251
252   return flexWidth;
253 }
254
255 float Node::GetFlexHeight() const
256 {
257   float flexHeight = YGNodeLayoutGetHeight( mImpl->mYogaNode );
258   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Get flex mYogaNode[%p] height[%f]\n", mImpl->mYogaNode, flexHeight)
259
260   return flexHeight;
261 }
262
263 void Node::SetMargin( Extents margin )
264 {
265   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Set flex margin\n")
266
267   YGNodeStyleSetMargin( mImpl->mYogaNode, YGEdgeLeft, margin.start );
268   YGNodeStyleSetMargin( mImpl->mYogaNode, YGEdgeTop, margin.top );
269   YGNodeStyleSetMargin( mImpl->mYogaNode, YGEdgeRight, margin.end );
270   YGNodeStyleSetMargin( mImpl->mYogaNode, YGEdgeBottom, margin.bottom );
271 }
272
273 void Node::SetPadding( Extents padding )
274 {
275   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Set padding\n")
276
277   YGNodeStyleSetPadding( mImpl->mYogaNode, YGEdgeLeft, padding.start );
278   YGNodeStyleSetPadding( mImpl->mYogaNode, YGEdgeTop, padding.top );
279   YGNodeStyleSetPadding( mImpl->mYogaNode, YGEdgeRight, padding.end );
280   YGNodeStyleSetPadding( mImpl->mYogaNode, YGEdgeBottom, padding.bottom );
281 }
282
283 void Node::SetFlexWrap( Dali::Toolkit::Flex::WrapType wrapType )
284 {
285   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Set flex wrap[%d] on mYogaNode[%p]\n", wrapType, mImpl->mYogaNode )
286
287   YGNodeStyleSetFlexWrap( mImpl->mYogaNode, static_cast<YGWrap>(wrapType) );
288 }
289
290 } // Flex
291 } // namespace Toolkit
292 } // namespace Dali