2 * Copyright (c) 2019 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.
18 #include "flex-node.h"
21 #include <dali/integration-api/debug.h>
22 #include <dali/public-api/actors/actor.h>
23 #include <dali/public-api/object/weak-handle.h>
26 #include <dali-toolkit/third-party/yoga/Yoga.h>
28 #if defined(DEBUG_ENABLED)
29 static Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_FLEX" );
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)
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));
49 YGSize childSize{ 1, 1 }; // Initialise variable.
51 DALI_ASSERT_DEBUG( childNode );
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 );
66 using NodePtr = std::unique_ptr<Node>;
68 using FlexNodeVector = std::vector< NodePtr>;
73 MeasureCallback mMeasureCallback;
74 WeakHandle< Dali::Actor > mActor;
75 FlexNodeVector mChildNodes;
78 Node::Node() : mImpl( new Impl )
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 );
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 );
95 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Destructor() >> \n");
96 if( mImpl->mYogaNode )
98 YGNodeFreeRecursive( mImpl->mYogaNode );
99 mImpl->mYogaNode = nullptr;
101 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Destructor() <<\n");
104 Node* Node::AddChild( Actor child, Extents margin, MeasureCallback measureFunction, int index )
108 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "AddChild[%s] to node[%p] at index:%d\n", child.GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str(), mImpl->mYogaNode, index );
110 NodePtr childNode( new Node() );
112 // Store measure function passed in so can call it when the MeasureChild function is called.
113 childNode->mImpl->mMeasureCallback = measureFunction;
115 childNode->mImpl->mActor = child;
116 Vector2 minumumSize = child.GetProperty< Vector2 >( Actor::Property::MINIMUM_SIZE );
117 Vector2 maximumSize = child.GetProperty< Vector2 >( Actor::Property::MAXIMUM_SIZE );
119 YGNodeStyleSetMaxWidth( childNode->mImpl->mYogaNode, maximumSize.width );
120 YGNodeStyleSetMaxHeight( childNode->mImpl->mYogaNode, maximumSize.height );
121 YGNodeStyleSetMinWidth( childNode->mImpl->mYogaNode, minumumSize.width );
122 YGNodeStyleSetMinHeight( childNode->mImpl->mYogaNode, minumumSize.height );
124 YGNodeStyleSetMargin( childNode->mImpl->mYogaNode, YGEdgeLeft, margin.start );
125 YGNodeStyleSetMargin( childNode->mImpl->mYogaNode, YGEdgeTop, margin.top );
126 YGNodeStyleSetMargin( childNode->mImpl->mYogaNode, YGEdgeRight, margin.end );
127 YGNodeStyleSetMargin( childNode->mImpl->mYogaNode, YGEdgeBottom, margin.bottom );
129 YGNodeSetMeasureFunc( childNode->mImpl->mYogaNode, &MeasureChild );
131 YGNodeInsertChild( mImpl->mYogaNode, childNode->mImpl->mYogaNode, index );
133 Node* result = childNode.get();
134 mImpl->mChildNodes.emplace_back( std::move(childNode) );
141 void Node::RemoveChild( Actor child )
143 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "RemoveChild child:[%s] from internal nodeCount[%d] childCount[%d]\n", child.GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str(), YGNodeGetChildCount( mImpl->mYogaNode ), mImpl->mChildNodes.size() );
145 auto iterator = std::find_if( mImpl->mChildNodes.begin(),mImpl->mChildNodes.end(),
146 [&child]( NodePtr& childNode ){ return childNode->mImpl->mActor.GetHandle() == child;});
148 if( iterator != mImpl->mChildNodes.end() )
150 YGNodeRemoveChild( mImpl->mYogaNode, (*iterator)->mImpl->mYogaNode );
151 mImpl->mChildNodes.erase(iterator);
154 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "RemoveChild internal nodeCount[%d] childCount[%d]\n", YGNodeGetChildCount( mImpl->mYogaNode ), mImpl->mChildNodes.size() );
157 SizeTuple Node::MeasureNode( float width, int widthMode, float height, int heightMode)
159 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MeasureNode\n" );
161 // Execute callback registered with AddChild
162 Toolkit::Flex::SizeTuple nodeSize{8,8}; // Default size set to 8,8 to aid bug detection.
163 if( mImpl->mMeasureCallback && mImpl->mActor.GetHandle() )
165 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MeasureNode MeasureCallback executing on %s\n", mImpl->mActor.GetHandle().GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str() );
166 nodeSize = mImpl->mMeasureCallback( mImpl->mActor.GetHandle(), width, widthMode, height, heightMode );
168 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MeasureNode nodeSize width:%f height:%f\n", nodeSize.width, nodeSize.height );
172 void Node::CalculateLayout(float availableWidth, float availableHeight, bool isRTL)
174 DALI_LOG_INFO( gLogFilter, Debug::General, "CalculateLayout availableSize(%f,%f)\n", availableWidth, availableHeight );
175 YGNodeCalculateLayout( mImpl->mYogaNode, availableWidth, availableHeight, isRTL ? YGDirectionRTL : YGDirectionLTR );
178 Dali::Vector4 Node::GetNodeFrame( int index ) const
180 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "GetNodeFrame[%d]\n", index );
181 YGNodeRef childNode = YGNodeGetChild( mImpl->mYogaNode, index );
182 Dali::Vector4 frame = Vector4::ZERO;
185 frame.x = YGNodeLayoutGetLeft( childNode );
186 frame.y = YGNodeLayoutGetTop( childNode );
187 frame.z = frame.x + YGNodeLayoutGetWidth( childNode );
188 frame.w = frame.y + YGNodeLayoutGetHeight( childNode );
189 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "GetNodeFrame Node index[%d] child ptr[%p] GetYogaNodeFrame left:%f top:%f right:%f bottom:%f\n",
190 index, childNode, frame.x , frame.y, frame.z, frame.w);
194 frame.x = YGNodeLayoutGetLeft( mImpl->mYogaNode );
195 frame.y = YGNodeLayoutGetTop( mImpl->mYogaNode );
196 frame.z = frame.x + YGNodeLayoutGetWidth( mImpl->mYogaNode );
197 frame.w = frame.y + YGNodeLayoutGetHeight( mImpl->mYogaNode );
198 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "GetNodeFrame Root index[%d] root ptr[%p] GetYogaNodeFrame left:%f top:%f right:%f bottom:%f\n",
199 index, mImpl->mYogaNode, frame.x , frame.y, frame.z, frame.w);
204 void Node::SetFlexDirection( Dali::Toolkit::Flex::FlexDirection flexDirection )
206 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Set flex direction[%d]\n", flexDirection );
208 YGNodeStyleSetFlexDirection( mImpl->mYogaNode, static_cast<YGFlexDirection>(flexDirection) );
211 Dali::Toolkit::Flex::FlexDirection Node::GetFlexDirection() const
213 return static_cast<Dali::Toolkit::Flex::FlexDirection>(YGNodeStyleGetFlexDirection( mImpl->mYogaNode ));
216 void Node::SetFlexJustification( Dali::Toolkit::Flex::Justification flexJustification )
218 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Set flex justification[%d]\n", flexJustification )
220 YGNodeStyleSetJustifyContent( mImpl->mYogaNode, static_cast<YGJustify>(flexJustification) );
223 Dali::Toolkit::Flex::Justification Node::GetFlexJustification() const
225 return static_cast<Dali::Toolkit::Flex::Justification>(YGNodeStyleGetJustifyContent( mImpl->mYogaNode ));
228 Dali::Toolkit::Flex::WrapType Node::GetFlexWrap() const
230 return static_cast<Dali::Toolkit::Flex::WrapType>(YGNodeStyleGetFlexWrap( mImpl->mYogaNode ));
233 void Node::SetFlexAlignment(Dali::Toolkit::Flex::Alignment flexAlignment )
235 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Set flex alignment[%d]\n", flexAlignment )
237 YGNodeStyleSetAlignContent( mImpl->mYogaNode , static_cast<YGAlign>(flexAlignment) );
240 Dali::Toolkit::Flex::Alignment Node::GetFlexAlignment() const
242 return static_cast<Dali::Toolkit::Flex::Alignment>(YGNodeStyleGetAlignContent( mImpl->mYogaNode ));
245 void Node::SetFlexItemsAlignment(Dali::Toolkit::Flex::Alignment flexAlignment )
247 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Set flex items alignment[%d] on mYogaNode[%p]\n", flexAlignment, mImpl->mYogaNode )
249 YGNodeStyleSetAlignItems( mImpl->mYogaNode, static_cast<YGAlign>(flexAlignment) );
252 Dali::Toolkit::Flex::Alignment Node::GetFlexItemsAlignment() const
254 return static_cast<Dali::Toolkit::Flex::Alignment>( YGNodeStyleGetAlignItems( mImpl->mYogaNode ));
257 void Node::SetFlexAlignmentSelf( Dali::Toolkit::Flex::Alignment flexAlignmentSelf )
259 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Set flex alignment self [%d] on mYogaNode[%p]\n", flexAlignmentSelf, mImpl->mYogaNode )
261 YGNodeStyleSetAlignSelf( mImpl->mYogaNode, static_cast<YGAlign>(flexAlignmentSelf) );
264 Dali::Toolkit::Flex::Alignment Node::GetFlexAlignmentSelf() const
266 return static_cast<Dali::Toolkit::Flex::Alignment>(YGNodeStyleGetAlignSelf( mImpl->mYogaNode ));
269 void Node::SetFlexPositionType( Dali::Toolkit::Flex::PositionType flexPositionType )
271 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Set flex position type [%d] on mYogaNode[%p]\n", flexPositionType, mImpl->mYogaNode )
273 YGNodeStyleSetPositionType( mImpl->mYogaNode, static_cast<YGPositionType>(flexPositionType) );
276 Dali::Toolkit::Flex::PositionType Node::GetFlexPositionType() const
278 return static_cast<Dali::Toolkit::Flex::PositionType>(YGNodeStyleGetPositionType( mImpl->mYogaNode ));
281 void Node::SetFlexAspectRatio( float flexAspectRatio )
283 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Set flex aspect ratio [%d] on mYogaNode[%p]\n", flexAspectRatio, mImpl->mYogaNode )
285 YGNodeStyleSetAspectRatio( mImpl->mYogaNode, static_cast<float>(flexAspectRatio) );
288 float Node::GetFlexAspectRatio() const
290 return static_cast<float>(YGNodeStyleGetAspectRatio( mImpl->mYogaNode ));
293 void Node::SetFlexBasis( float flexBasis )
295 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Set flex basis [%d] on mYogaNode[%p]\n", flexBasis, mImpl->mYogaNode )
297 YGNodeStyleSetFlexBasis( mImpl->mYogaNode, static_cast<float>(flexBasis) );
300 float Node::GetFlexBasis() const
302 return static_cast<float>(YGNodeStyleGetFlexBasis( mImpl->mYogaNode ).value);
305 void Node::SetFlexShrink( float flexShrink )
307 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Set flex shrink [%d] on mYogaNode[%p]\n", flexShrink, mImpl->mYogaNode )
309 YGNodeStyleSetFlexShrink( mImpl->mYogaNode, static_cast<float>(flexShrink) );
312 float Node::GetFlexShrink() const
314 return static_cast<float>(YGNodeStyleGetFlexShrink( mImpl->mYogaNode ));
317 void Node::SetFlexGrow( float flexGrow )
319 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Set flex grow [%d] on mYogaNode[%p]\n", flexGrow, mImpl->mYogaNode )
321 YGNodeStyleSetFlexGrow( mImpl->mYogaNode, static_cast<float>(flexGrow) );
324 float Node::GetFlexGrow() const
326 return static_cast<float>(YGNodeStyleGetFlexGrow( mImpl->mYogaNode ));
329 float Node::GetFlexWidth() const
331 float flexWidth = YGNodeLayoutGetWidth( mImpl->mYogaNode );
332 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Get flex mYogaNode[%p] width[%f]\n", mImpl->mYogaNode, flexWidth)
337 float Node::GetFlexHeight() const
339 float flexHeight = YGNodeLayoutGetHeight( mImpl->mYogaNode );
340 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Get flex mYogaNode[%p] height[%f]\n", mImpl->mYogaNode, flexHeight)
345 void Node::SetMargin( Extents margin )
347 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Set flex margin\n")
349 YGNodeStyleSetMargin( mImpl->mYogaNode, YGEdgeLeft, margin.start );
350 YGNodeStyleSetMargin( mImpl->mYogaNode, YGEdgeTop, margin.top );
351 YGNodeStyleSetMargin( mImpl->mYogaNode, YGEdgeRight, margin.end );
352 YGNodeStyleSetMargin( mImpl->mYogaNode, YGEdgeBottom, margin.bottom );
355 void Node::SetPadding( Extents padding )
357 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Set padding\n")
359 YGNodeStyleSetPadding( mImpl->mYogaNode, YGEdgeLeft, padding.start );
360 YGNodeStyleSetPadding( mImpl->mYogaNode, YGEdgeTop, padding.top );
361 YGNodeStyleSetPadding( mImpl->mYogaNode, YGEdgeRight, padding.end );
362 YGNodeStyleSetPadding( mImpl->mYogaNode, YGEdgeBottom, padding.bottom );
365 void Node::SetFlexWrap( Dali::Toolkit::Flex::WrapType wrapType )
367 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Set flex wrap[%d] on mYogaNode[%p]\n", wrapType, mImpl->mYogaNode )
369 YGNodeStyleSetFlexWrap( mImpl->mYogaNode, static_cast<YGWrap>(wrapType) );
373 } // namespace Toolkit