2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 <dali/internal/update/nodes/node.h>
21 #include <dali/internal/update/node-attachments/node-attachment.h>
22 #include <dali/internal/update/common/discard-queue.h>
23 #include <dali/internal/render/shaders/shader.h>
24 #include <dali/public-api/common/dali-common.h>
25 #include <dali/public-api/common/constants.h>
36 const PositionInheritanceMode Node::DEFAULT_POSITION_INHERITANCE_MODE( INHERIT_PARENT_POSITION );
37 const ColorMode Node::DEFAULT_COLOR_MODE( USE_OWN_MULTIPLY_PARENT_ALPHA );
45 : mParentOrigin( ParentOrigin::DEFAULT, mDirtyFlags ),
46 mAnchorPoint( AnchorPoint::DEFAULT, mDirtyFlags ),
47 mSize( Vector3::ZERO ),
48 mPosition( Vector3::ZERO ),
49 mRotation( Quaternion::IDENTITY ),
50 mScale( Vector3::ONE ),
52 mColor( Color::WHITE ),
53 mWorldPosition( Vector3::ZERO ),
54 mWorldRotation( Quaternion::IDENTITY ),
55 mWorldScale( Vector3::ONE ),
56 mWorldMatrix( Matrix::IDENTITY ),
57 mWorldColor( Color::WHITE ),
59 mInheritShader( true ),
60 mInheritRotation( true ),
61 mInheritScale( true ),
62 mTransmitGeometryScaling( false ),
63 mInhibitLocalTransform( false ),
65 mDrawMode( DrawMode::NORMAL ),
66 mPositionInheritanceMode( DEFAULT_POSITION_INHERITANCE_MODE ),
67 mColorMode( DEFAULT_COLOR_MODE ),
70 mAppliedShader( NULL ),
71 mInheritedShader( NULL ),
72 mDirtyFlags(AllFlags),
73 mGeometryScale( Vector3::ONE ),
74 mInitialVolume( Vector3::ONE ),
75 mExclusiveRenderTask( NULL )
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::DisconnectFromSceneGraph();
95 void Node::Attach( NodeAttachment& object )
97 DALI_ASSERT_DEBUG(!mAttachment);
99 object.SetParent(*this);
101 mAttachment = &object;
105 void Node::SetRoot(bool isRoot)
107 DALI_ASSERT_DEBUG(!isRoot || mParent == NULL); // Root nodes cannot have a parent
112 void Node::ConnectChild( Node* childNode )
114 DALI_ASSERT_ALWAYS( this != childNode );
115 DALI_ASSERT_ALWAYS( IsRoot() || NULL != mParent ); // Parent should be connected first
116 DALI_ASSERT_ALWAYS( !childNode->IsRoot() && NULL == childNode->GetParent() ); // Child should be disconnected
118 childNode->SetParent( *this );
120 // Everything should be reinherited when reconnected to scene-graph
121 childNode->SetAllDirtyFlags();
123 mChildren.PushBack( childNode );
126 void Node::DisconnectChild( BufferIndex updateBufferIndex, Node& childNode, std::set<Node*>& connectedNodes, std::set<Node*>& disconnectedNodes )
128 DALI_ASSERT_ALWAYS( this != &childNode );
129 DALI_ASSERT_ALWAYS( childNode.GetParent() == this );
131 // Find the childNode and remove it
134 const NodeIter endIter = mChildren.End();
135 for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
137 Node* current = *iter;
138 if ( current == &childNode )
141 mChildren.Erase( iter ); // order matters here
142 break; // iter is no longer valid
145 DALI_ASSERT_ALWAYS( NULL != found );
147 found->RecursiveDisconnectFromSceneGraph( updateBufferIndex, connectedNodes, disconnectedNodes );
150 void Node::ApplyShader( Shader* shader )
152 DALI_ASSERT_DEBUG( shader && "null shader passed to node" );
154 mAppliedShader = shader;
156 SetDirtyFlag(ShaderFlag);
159 void Node::RemoveShader()
161 mAppliedShader = NULL; // Wait until InheritShader to grab default shader
163 SetDirtyFlag(ShaderFlag);
166 Shader* Node::GetAppliedShader() const
168 return mAppliedShader;
171 void Node::SetInheritedShader(Shader* shader)
173 mInheritedShader = shader;
176 Shader* Node::GetInheritedShader() const
178 return mInheritedShader;
181 void Node::InheritShader(Shader* defaultShader)
183 DALI_ASSERT_DEBUG(mParent != NULL);
185 // If we have a custom shader for this node, then use it
186 if ( mAppliedShader != NULL )
188 mInheritedShader = mAppliedShader;
192 // Otherwise we either inherit a shader, or fall-back to the default
195 mInheritedShader = mParent->GetInheritedShader();
199 mInheritedShader = defaultShader;
202 DALI_ASSERT_DEBUG( mInheritedShader != NULL );
205 int Node::GetDirtyFlags() const
207 // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
208 int flags = mDirtyFlags;
209 const bool sizeFlag = mSize.IsClean();
211 if ( !(flags & TransformFlag) )
213 // Check whether the transform related properties have changed, ParentOrigin and AnchorPoint modify mDirtyFlags directly when being modified
215 !mPosition.IsClean() ||
216 !mRotation.IsClean() ||
219 flags |= TransformFlag;
223 // Check whether the visible property has changed
224 if ( !mVisible.IsClean() )
226 flags |= VisibleFlag;
229 // Check whether the color property has changed
230 if ( !mColor.IsClean() )
235 // Check whether the size property has changed
244 void Node::ResetDefaultProperties( BufferIndex updateBufferIndex )
246 // Reset default properties
247 mSize.ResetToBaseValue( updateBufferIndex );
248 mPosition.ResetToBaseValue( updateBufferIndex );
249 mRotation.ResetToBaseValue( updateBufferIndex );
250 mScale.ResetToBaseValue( updateBufferIndex );
251 mVisible.ResetToBaseValue( updateBufferIndex );
252 mColor.ResetToBaseValue( updateBufferIndex );
254 mDirtyFlags = NothingFlag;
257 bool Node::IsFullyVisible( BufferIndex updateBufferIndex ) const
259 if( !IsVisible( updateBufferIndex ) )
264 Node* parent = mParent;
266 while( NULL != parent )
268 if( !parent->IsVisible( updateBufferIndex ) )
273 parent = parent->GetParent();
279 void Node::SetParent(Node& parentNode)
281 DALI_ASSERT_ALWAYS(this != &parentNode);
282 DALI_ASSERT_ALWAYS(!mIsRoot);
283 DALI_ASSERT_ALWAYS(mParent == NULL);
285 mParent = &parentNode;
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();
303 mAppliedShader = NULL;
304 mInheritedShader = NULL;
306 // Remove back-pointer to parent
309 // Remove all child pointers
312 // Move into disconnectedNodes
313 std::set<Node*>::size_type removed = connectedNodes.erase( this );
314 DALI_ASSERT_ALWAYS( removed );
315 disconnectedNodes.insert( this );
318 } // namespace SceneGraph
320 } // namespace Internal