2 * Copyright (c) 2014 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.
19 #include <dali/internal/update/nodes/node.h>
22 #include <dali/internal/update/node-attachments/node-attachment.h>
23 #include <dali/internal/update/common/discard-queue.h>
24 #include <dali/public-api/common/dali-common.h>
25 #include <dali/public-api/common/constants.h>
26 #include <dali/internal/common/internal-constants.h>
37 const PositionInheritanceMode Node::DEFAULT_POSITION_INHERITANCE_MODE( INHERIT_PARENT_POSITION );
38 const ColorMode Node::DEFAULT_COLOR_MODE( USE_OWN_MULTIPLY_PARENT_ALPHA );
46 : mParentOrigin( ParentOrigin::DEFAULT ),
47 mAnchorPoint( AnchorPoint::DEFAULT ),
48 mSize(), // zero initialized by default
49 mPosition(), // zero initialized by default
50 mOrientation(), // initialized to identity by default
51 mScale( Vector3::ONE ),
53 mColor( Color::WHITE ),
54 mWorldPosition(), // zero initialized by default
55 mWorldOrientation(), // initialized to identity by default
56 mWorldScale( Vector3::ONE ),
58 mWorldColor( Color::WHITE ),
60 mExclusiveRenderTask( NULL ),
63 mRegenerateUniformMap( 0 ),
65 mDirtyFlags(AllFlags),
67 mInheritOrientation( true ),
68 mInheritScale( true ),
70 mDrawMode( DrawMode::NORMAL ),
71 mPositionInheritanceMode( DEFAULT_POSITION_INHERITANCE_MODE ),
72 mColorMode( DEFAULT_COLOR_MODE )
74 mUniformMapChanged[0] = 0u;
75 mUniformMapChanged[1] = 0u;
82 void Node::OnDestroy()
84 // Node attachments should be notified about the disconnection.
87 mAttachment->OnDestroy();
90 // Animators, Constraints etc. should be disconnected from the child's properties.
91 PropertyOwner::Destroy();
94 void Node::Attach( NodeAttachment& object )
96 DALI_ASSERT_DEBUG(!mAttachment);
98 object.SetParent(*this);
100 mAttachment = &object;
105 mAttachment->ConnectedToSceneGraph();
109 void Node::SetRoot(bool isRoot)
111 DALI_ASSERT_DEBUG(!isRoot || mParent == NULL); // Root nodes cannot have a parent
116 bool Node::ResolveVisibility( BufferIndex updateBufferIndex )
119 const Vector4& color = GetWorldColor( updateBufferIndex );
120 if( color.a > FULLY_TRANSPARENT ) // not fully transparent
122 const float MAX_NODE_SIZE = float(1u<<30);
123 const Vector3& size = GetSize( updateBufferIndex );
124 if( ( size.width > Math::MACHINE_EPSILON_1000 ) && // width is greater than a very small number
125 ( size.height > Math::MACHINE_EPSILON_1000 ) ) // height is greater than a very small number
127 if( ( size.width < MAX_NODE_SIZE ) && // width is smaller than the maximum allowed size
128 ( size.height < MAX_NODE_SIZE ) ) // height is smaller than the maximum allowed size
134 DALI_LOG_ERROR("Actor size should not be bigger than %f.\n", MAX_NODE_SIZE );
135 DALI_LOG_ACTOR_TREE( mParent );
142 void Node::AddUniformMapping( UniformPropertyMapping* map )
144 PropertyOwner::AddUniformMapping( map );
145 mRegenerateUniformMap = 2;
148 void Node::RemoveUniformMapping( const std::string& uniformName )
150 PropertyOwner::RemoveUniformMapping( uniformName );
151 mRegenerateUniformMap = 2;
154 void Node::PrepareRender( BufferIndex bufferIndex )
156 if(mRegenerateUniformMap != 0 )
158 if( mRegenerateUniformMap == 2 )
160 CollectedUniformMap& localMap = mCollectedUniformMap[ bufferIndex ];
163 for( unsigned int i=0, count=mUniformMaps.Count(); i<count; ++i )
165 localMap.PushBack( &mUniformMaps[i] );
168 else if( mRegenerateUniformMap == 1 )
170 CollectedUniformMap& localMap = mCollectedUniformMap[ bufferIndex ];
171 CollectedUniformMap& oldMap = mCollectedUniformMap[ 1-bufferIndex ];
173 localMap.Resize( oldMap.Count() );
175 unsigned int index=0;
176 for( CollectedUniformMap::Iterator iter = oldMap.Begin(), end = oldMap.End() ; iter != end ; ++iter, ++index )
178 localMap[index] = *iter;
181 --mRegenerateUniformMap;
182 mUniformMapChanged[bufferIndex] = 1u;
186 void Node::ConnectChild( Node* childNode )
188 DALI_ASSERT_ALWAYS( this != childNode );
189 DALI_ASSERT_ALWAYS( IsRoot() || NULL != mParent ); // Parent should be connected first
190 DALI_ASSERT_ALWAYS( !childNode->IsRoot() && NULL == childNode->GetParent() ); // Child should be disconnected
192 childNode->SetParent( *this );
194 // Everything should be reinherited when reconnected to scene-graph
195 childNode->SetAllDirtyFlags();
197 // Add the node to the end of the child list.
198 mChildren.PushBack( childNode );
200 // Inform property observers of new connection
201 childNode->ConnectToSceneGraph();
203 // Inform child node attachment that the node has been added to the stage
204 if( childNode->mAttachment )
206 childNode->mAttachment->ConnectedToSceneGraph();
210 void Node::DisconnectChild( BufferIndex updateBufferIndex, Node& childNode, std::set<Node*>& connectedNodes, std::set<Node*>& disconnectedNodes )
212 DALI_ASSERT_ALWAYS( this != &childNode );
213 DALI_ASSERT_ALWAYS( childNode.GetParent() == this );
215 // Find the childNode and remove it
218 const NodeIter endIter = mChildren.End();
219 for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
221 Node* current = *iter;
222 if ( current == &childNode )
225 mChildren.Erase( iter ); // order matters here
226 break; // iter is no longer valid
229 DALI_ASSERT_ALWAYS( NULL != found );
231 found->RecursiveDisconnectFromSceneGraph( updateBufferIndex, connectedNodes, disconnectedNodes );
234 void Node::RemoveRenderer( Renderer* renderer )
236 unsigned int rendererCount( mRenderer.Size() );
237 for( unsigned int i(0); i<rendererCount; ++i )
239 if( mRenderer[i] == renderer )
241 mRenderer.Erase( mRenderer.Begin()+i);
247 int Node::GetDirtyFlags() const
249 // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
250 int flags = mDirtyFlags;
251 const bool sizeFlag = mSize.IsClean();
253 if ( !(flags & TransformFlag) )
255 // Check whether the transform related properties have changed
257 !mPosition.IsClean() ||
258 !mOrientation.IsClean() ||
260 mParentOrigin.InputChanged() || // parent origin and anchor point rarely change
261 mAnchorPoint.InputChanged() )
263 flags |= TransformFlag;
267 // Check whether the visible property has changed
268 if ( !mVisible.IsClean() )
270 flags |= VisibleFlag;
273 // Check whether the color property has changed
274 if ( !mColor.IsClean() )
279 // Check whether the size property has changed
288 void Node::ResetDefaultProperties( BufferIndex updateBufferIndex )
290 // clear dirty flags in parent origin & anchor point
291 mParentOrigin.Clear();
292 mAnchorPoint.Clear();
293 // Reset default properties
294 mSize.ResetToBaseValue( updateBufferIndex );
295 mPosition.ResetToBaseValue( updateBufferIndex );
296 mOrientation.ResetToBaseValue( updateBufferIndex );
297 mScale.ResetToBaseValue( updateBufferIndex );
298 mVisible.ResetToBaseValue( updateBufferIndex );
299 mColor.ResetToBaseValue( updateBufferIndex );
301 mDirtyFlags = NothingFlag;
304 void Node::SetParent(Node& parentNode)
306 DALI_ASSERT_ALWAYS(this != &parentNode);
307 DALI_ASSERT_ALWAYS(!mIsRoot);
308 DALI_ASSERT_ALWAYS(mParent == NULL);
310 mParent = &parentNode;
311 mDepth = mParent->GetDepth() + 1u;
314 void Node::RecursiveDisconnectFromSceneGraph( BufferIndex updateBufferIndex, std::set<Node*>& connectedNodes, std::set<Node*>& disconnectedNodes )
316 DALI_ASSERT_ALWAYS(!mIsRoot);
317 DALI_ASSERT_ALWAYS(mParent != NULL);
319 const NodeIter endIter = mChildren.End();
320 for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
322 (*iter)->RecursiveDisconnectFromSceneGraph( updateBufferIndex, connectedNodes, disconnectedNodes );
325 // Animators, Constraints etc. should be disconnected from the child's properties.
326 PropertyOwner::DisconnectFromSceneGraph( updateBufferIndex );
328 // Remove back-pointer to parent
332 // Remove all child pointers
335 // Inform child node attachment that the node has been removed from the stage
338 mAttachment->DisconnectedFromSceneGraph();
341 // Move into disconnectedNodes
342 std::set<Node*>::size_type removed = connectedNodes.erase( this );
343 DALI_ASSERT_ALWAYS( removed );
344 disconnectedNodes.insert( this );
347 } // namespace SceneGraph
349 } // namespace Internal