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 void Node::AddUniformMapping( UniformPropertyMapping* map )
118 PropertyOwner::AddUniformMapping( map );
119 mRegenerateUniformMap = 2;
122 void Node::RemoveUniformMapping( const std::string& uniformName )
124 PropertyOwner::RemoveUniformMapping( uniformName );
125 mRegenerateUniformMap = 2;
128 void Node::PrepareRender( BufferIndex bufferIndex )
130 if(mRegenerateUniformMap != 0 )
132 if( mRegenerateUniformMap == 2 )
134 CollectedUniformMap& localMap = mCollectedUniformMap[ bufferIndex ];
137 for( unsigned int i=0, count=mUniformMaps.Count(); i<count; ++i )
139 localMap.PushBack( &mUniformMaps[i] );
142 else if( mRegenerateUniformMap == 1 )
144 CollectedUniformMap& localMap = mCollectedUniformMap[ bufferIndex ];
145 CollectedUniformMap& oldMap = mCollectedUniformMap[ 1-bufferIndex ];
147 localMap.Resize( oldMap.Count() );
149 unsigned int index=0;
150 for( CollectedUniformMap::Iterator iter = oldMap.Begin(), end = oldMap.End() ; iter != end ; ++iter, ++index )
152 localMap[index] = *iter;
155 --mRegenerateUniformMap;
156 mUniformMapChanged[bufferIndex] = 1u;
160 void Node::ConnectChild( Node* childNode )
162 DALI_ASSERT_ALWAYS( this != childNode );
163 DALI_ASSERT_ALWAYS( IsRoot() || NULL != mParent ); // Parent should be connected first
164 DALI_ASSERT_ALWAYS( !childNode->IsRoot() && NULL == childNode->GetParent() ); // Child should be disconnected
166 childNode->SetParent( *this );
168 // Everything should be reinherited when reconnected to scene-graph
169 childNode->SetAllDirtyFlags();
171 // Add the node to the end of the child list.
172 mChildren.PushBack( childNode );
174 // Inform property observers of new connection
175 childNode->ConnectToSceneGraph();
177 // Inform child node attachment that the node has been added to the stage
178 if( childNode->mAttachment )
180 childNode->mAttachment->ConnectedToSceneGraph();
184 void Node::DisconnectChild( BufferIndex updateBufferIndex, Node& childNode, std::set<Node*>& connectedNodes, std::set<Node*>& disconnectedNodes )
186 DALI_ASSERT_ALWAYS( this != &childNode );
187 DALI_ASSERT_ALWAYS( childNode.GetParent() == this );
189 // Find the childNode and remove it
192 const NodeIter endIter = mChildren.End();
193 for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
195 Node* current = *iter;
196 if ( current == &childNode )
199 mChildren.Erase( iter ); // order matters here
200 break; // iter is no longer valid
203 DALI_ASSERT_ALWAYS( NULL != found );
205 found->RecursiveDisconnectFromSceneGraph( updateBufferIndex, connectedNodes, disconnectedNodes );
208 void Node::RemoveRenderer( Renderer* renderer )
210 unsigned int rendererCount( mRenderer.Size() );
211 for( unsigned int i(0); i<rendererCount; ++i )
213 if( mRenderer[i] == renderer )
215 mRenderer.Erase( mRenderer.Begin()+i);
221 int Node::GetDirtyFlags() const
223 // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
224 int flags = mDirtyFlags;
225 const bool sizeFlag = mSize.IsClean();
227 if ( !(flags & TransformFlag) )
229 // Check whether the transform related properties have changed
231 !mPosition.IsClean() ||
232 !mOrientation.IsClean() ||
234 mParentOrigin.InputChanged() || // parent origin and anchor point rarely change
235 mAnchorPoint.InputChanged() )
237 flags |= TransformFlag;
241 // Check whether the visible property has changed
242 if ( !mVisible.IsClean() )
244 flags |= VisibleFlag;
247 // Check whether the color property has changed
248 if ( !mColor.IsClean() )
253 // Check whether the size property has changed
262 void Node::ResetDefaultProperties( BufferIndex updateBufferIndex )
264 // clear dirty flags in parent origin & anchor point
265 mParentOrigin.Clear();
266 mAnchorPoint.Clear();
267 // Reset default properties
268 mSize.ResetToBaseValue( updateBufferIndex );
269 mPosition.ResetToBaseValue( updateBufferIndex );
270 mOrientation.ResetToBaseValue( updateBufferIndex );
271 mScale.ResetToBaseValue( updateBufferIndex );
272 mVisible.ResetToBaseValue( updateBufferIndex );
273 mColor.ResetToBaseValue( updateBufferIndex );
275 mDirtyFlags = NothingFlag;
278 void Node::SetParent(Node& parentNode)
280 DALI_ASSERT_ALWAYS(this != &parentNode);
281 DALI_ASSERT_ALWAYS(!mIsRoot);
282 DALI_ASSERT_ALWAYS(mParent == NULL);
284 mParent = &parentNode;
285 mDepth = mParent->GetDepth() + 1u;
288 void Node::RecursiveDisconnectFromSceneGraph( BufferIndex updateBufferIndex, std::set<Node*>& connectedNodes, std::set<Node*>& disconnectedNodes )
290 DALI_ASSERT_ALWAYS(!mIsRoot);
291 DALI_ASSERT_ALWAYS(mParent != NULL);
293 const NodeIter endIter = mChildren.End();
294 for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
296 (*iter)->RecursiveDisconnectFromSceneGraph( updateBufferIndex, connectedNodes, disconnectedNodes );
299 // Animators, Constraints etc. should be disconnected from the child's properties.
300 PropertyOwner::DisconnectFromSceneGraph( updateBufferIndex );
302 // Remove back-pointer to parent
306 // Remove all child pointers
309 // Inform child node attachment that the node has been removed from the stage
312 mAttachment->DisconnectedFromSceneGraph();
315 // Move into disconnectedNodes
316 std::set<Node*>::size_type removed = connectedNodes.erase( this );
317 DALI_ASSERT_ALWAYS( removed );
318 disconnectedNodes.insert( this );
321 } // namespace SceneGraph
323 } // namespace Internal