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 ),
69 mInhibitLocalTransform( false ),
71 mDrawMode( DrawMode::NORMAL ),
72 mPositionInheritanceMode( DEFAULT_POSITION_INHERITANCE_MODE ),
73 mColorMode( DEFAULT_COLOR_MODE )
75 mUniformMapChanged[0] = 0u;
76 mUniformMapChanged[1] = 0u;
83 void Node::OnDestroy()
85 // Node attachments should be notified about the disconnection.
88 mAttachment->OnDestroy();
91 // Animators, Constraints etc. should be disconnected from the child's properties.
92 PropertyOwner::Destroy();
95 void Node::Attach( NodeAttachment& object )
97 DALI_ASSERT_DEBUG(!mAttachment);
99 object.SetParent(*this);
101 mAttachment = &object;
106 mAttachment->ConnectedToSceneGraph();
110 void Node::SetRoot(bool isRoot)
112 DALI_ASSERT_DEBUG(!isRoot || mParent == NULL); // Root nodes cannot have a parent
117 bool Node::ResolveVisibility( BufferIndex updateBufferIndex )
120 const Vector4& color = GetWorldColor( updateBufferIndex );
121 if( color.a > FULLY_TRANSPARENT ) // not fully transparent
123 const float MAX_NODE_SIZE = float(1u<<30);
124 const Vector3& size = GetSize( updateBufferIndex );
125 if( ( size.width > Math::MACHINE_EPSILON_1000 ) && // width is greater than a very small number
126 ( size.height > Math::MACHINE_EPSILON_1000 ) ) // height is greater than a very small number
128 if( ( size.width < MAX_NODE_SIZE ) && // width is smaller than the maximum allowed size
129 ( size.height < MAX_NODE_SIZE ) ) // height is smaller than the maximum allowed size
135 DALI_LOG_ERROR("Actor size should not be bigger than %f.\n", MAX_NODE_SIZE );
136 DALI_LOG_ACTOR_TREE( mParent );
143 void Node::AddUniformMapping( UniformPropertyMapping* map )
145 PropertyOwner::AddUniformMapping( map );
146 mRegenerateUniformMap = 2;
149 void Node::RemoveUniformMapping( const std::string& uniformName )
151 PropertyOwner::RemoveUniformMapping( uniformName );
152 mRegenerateUniformMap = 2;
155 void Node::PrepareRender( BufferIndex bufferIndex )
157 if(mRegenerateUniformMap != 0 )
159 if( mRegenerateUniformMap == 2 )
161 CollectedUniformMap& localMap = mCollectedUniformMap[ bufferIndex ];
164 for( unsigned int i=0, count=mUniformMaps.Count(); i<count; ++i )
166 localMap.PushBack( &mUniformMaps[i] );
169 else if( mRegenerateUniformMap == 1 )
171 CollectedUniformMap& localMap = mCollectedUniformMap[ bufferIndex ];
172 CollectedUniformMap& oldMap = mCollectedUniformMap[ 1-bufferIndex ];
174 localMap.Resize( oldMap.Count() );
176 unsigned int index=0;
177 for( CollectedUniformMap::Iterator iter = oldMap.Begin(), end = oldMap.End() ; iter != end ; ++iter, ++index )
179 localMap[index] = *iter;
182 --mRegenerateUniformMap;
183 mUniformMapChanged[bufferIndex] = 1u;
187 void Node::ConnectChild( Node* childNode )
189 DALI_ASSERT_ALWAYS( this != childNode );
190 DALI_ASSERT_ALWAYS( IsRoot() || NULL != mParent ); // Parent should be connected first
191 DALI_ASSERT_ALWAYS( !childNode->IsRoot() && NULL == childNode->GetParent() ); // Child should be disconnected
193 childNode->SetParent( *this );
195 // Everything should be reinherited when reconnected to scene-graph
196 childNode->SetAllDirtyFlags();
198 // Add the node to the end of the child list.
199 mChildren.PushBack( childNode );
201 // Inform property observers of new connection
202 childNode->ConnectToSceneGraph();
204 // Inform child node attachment that the node has been added to the stage
205 if( childNode->mAttachment )
207 childNode->mAttachment->ConnectedToSceneGraph();
211 void Node::DisconnectChild( BufferIndex updateBufferIndex, Node& childNode, std::set<Node*>& connectedNodes, std::set<Node*>& disconnectedNodes )
213 DALI_ASSERT_ALWAYS( this != &childNode );
214 DALI_ASSERT_ALWAYS( childNode.GetParent() == this );
216 // Find the childNode and remove it
219 const NodeIter endIter = mChildren.End();
220 for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
222 Node* current = *iter;
223 if ( current == &childNode )
226 mChildren.Erase( iter ); // order matters here
227 break; // iter is no longer valid
230 DALI_ASSERT_ALWAYS( NULL != found );
232 found->RecursiveDisconnectFromSceneGraph( updateBufferIndex, connectedNodes, disconnectedNodes );
235 void Node::RemoveRenderer( Renderer* renderer )
237 unsigned int rendererCount( mRenderer.Size() );
238 for( unsigned int i(0); i<rendererCount; ++i )
240 if( mRenderer[i] == renderer )
242 mRenderer.Erase( mRenderer.Begin()+i);
248 int Node::GetDirtyFlags() const
250 // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
251 int flags = mDirtyFlags;
252 const bool sizeFlag = mSize.IsClean();
254 if ( !(flags & TransformFlag) )
256 // Check whether the transform related properties have changed
258 !mPosition.IsClean() ||
259 !mOrientation.IsClean() ||
261 mParentOrigin.InputChanged() || // parent origin and anchor point rarely change
262 mAnchorPoint.InputChanged() )
264 flags |= TransformFlag;
268 // Check whether the visible property has changed
269 if ( !mVisible.IsClean() )
271 flags |= VisibleFlag;
274 // Check whether the color property has changed
275 if ( !mColor.IsClean() )
280 // Check whether the size property has changed
289 void Node::ResetDefaultProperties( BufferIndex updateBufferIndex )
291 // clear dirty flags in parent origin & anchor point
292 mParentOrigin.Clear();
293 mAnchorPoint.Clear();
294 // Reset default properties
295 mSize.ResetToBaseValue( updateBufferIndex );
296 mPosition.ResetToBaseValue( updateBufferIndex );
297 mOrientation.ResetToBaseValue( updateBufferIndex );
298 mScale.ResetToBaseValue( updateBufferIndex );
299 mVisible.ResetToBaseValue( updateBufferIndex );
300 mColor.ResetToBaseValue( updateBufferIndex );
302 mDirtyFlags = NothingFlag;
305 bool Node::IsFullyVisible( BufferIndex updateBufferIndex ) const
307 if( !IsVisible( updateBufferIndex ) )
312 Node* parent = mParent;
314 while( NULL != parent )
316 if( !parent->IsVisible( updateBufferIndex ) )
321 parent = parent->GetParent();
327 void Node::SetParent(Node& parentNode)
329 DALI_ASSERT_ALWAYS(this != &parentNode);
330 DALI_ASSERT_ALWAYS(!mIsRoot);
331 DALI_ASSERT_ALWAYS(mParent == NULL);
333 mParent = &parentNode;
334 mDepth = mParent->GetDepth() + 1u;
337 void Node::RecursiveDisconnectFromSceneGraph( BufferIndex updateBufferIndex, std::set<Node*>& connectedNodes, std::set<Node*>& disconnectedNodes )
339 DALI_ASSERT_ALWAYS(!mIsRoot);
340 DALI_ASSERT_ALWAYS(mParent != NULL);
342 const NodeIter endIter = mChildren.End();
343 for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
345 (*iter)->RecursiveDisconnectFromSceneGraph( updateBufferIndex, connectedNodes, disconnectedNodes );
348 // Animators, Constraints etc. should be disconnected from the child's properties.
349 PropertyOwner::DisconnectFromSceneGraph( updateBufferIndex );
351 // Remove back-pointer to parent
355 // Remove all child pointers
358 // Inform child node attachment that the node has been removed from the stage
361 mAttachment->DisconnectedFromSceneGraph();
364 // Move into disconnectedNodes
365 std::set<Node*>::size_type removed = connectedNodes.erase( this );
366 DALI_ASSERT_ALWAYS( removed );
367 disconnectedNodes.insert( this );
370 } // namespace SceneGraph
372 } // namespace Internal