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 ),
46 mAnchorPoint( AnchorPoint::DEFAULT ),
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 mAppliedShader( NULL ),
60 mInheritedShader( NULL ),
61 mExclusiveRenderTask( NULL ),
64 mGeometryScale( Vector3::ONE ),
65 mInitialVolume( Vector3::ONE ),
66 mDirtyFlags(AllFlags),
68 mInheritShader( true ),
69 mInheritRotation( true ),
70 mInheritScale( true ),
71 mTransmitGeometryScaling( false ),
72 mInhibitLocalTransform( false ),
74 mDrawMode( DrawMode::NORMAL ),
75 mPositionInheritanceMode( DEFAULT_POSITION_INHERITANCE_MODE ),
76 mColorMode( DEFAULT_COLOR_MODE )
84 void Node::OnDestroy()
86 // Node attachments should be notified about the disconnection.
89 mAttachment->OnDestroy();
92 // Animators, Constraints etc. should be disconnected from the child's properties.
93 PropertyOwner::DisconnectFromSceneGraph();
96 void Node::Attach( NodeAttachment& object )
98 DALI_ASSERT_DEBUG(!mAttachment);
100 object.SetParent(*this);
102 mAttachment = &object;
106 void Node::SetRoot(bool isRoot)
108 DALI_ASSERT_DEBUG(!isRoot || mParent == NULL); // Root nodes cannot have a parent
113 void Node::ConnectChild( Node* childNode )
115 DALI_ASSERT_ALWAYS( this != childNode );
116 DALI_ASSERT_ALWAYS( IsRoot() || NULL != mParent ); // Parent should be connected first
117 DALI_ASSERT_ALWAYS( !childNode->IsRoot() && NULL == childNode->GetParent() ); // Child should be disconnected
119 childNode->SetParent( *this );
121 // Everything should be reinherited when reconnected to scene-graph
122 childNode->SetAllDirtyFlags();
124 mChildren.PushBack( childNode );
127 void Node::DisconnectChild( BufferIndex updateBufferIndex, Node& childNode, std::set<Node*>& connectedNodes, std::set<Node*>& disconnectedNodes )
129 DALI_ASSERT_ALWAYS( this != &childNode );
130 DALI_ASSERT_ALWAYS( childNode.GetParent() == this );
132 // Find the childNode and remove it
135 const NodeIter endIter = mChildren.End();
136 for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
138 Node* current = *iter;
139 if ( current == &childNode )
142 mChildren.Erase( iter ); // order matters here
143 break; // iter is no longer valid
146 DALI_ASSERT_ALWAYS( NULL != found );
148 found->RecursiveDisconnectFromSceneGraph( updateBufferIndex, connectedNodes, disconnectedNodes );
151 void Node::ApplyShader( Shader* shader )
153 DALI_ASSERT_DEBUG( shader && "null shader passed to node" );
155 mAppliedShader = shader;
157 SetDirtyFlag(ShaderFlag);
160 void Node::RemoveShader()
162 mAppliedShader = NULL; // Wait until InheritShader to grab default shader
164 SetDirtyFlag(ShaderFlag);
167 Shader* Node::GetAppliedShader() const
169 return mAppliedShader;
172 void Node::SetInheritedShader(Shader* shader)
174 mInheritedShader = shader;
177 Shader* Node::GetInheritedShader() const
179 return mInheritedShader;
182 void Node::InheritShader(Shader* defaultShader)
184 DALI_ASSERT_DEBUG(mParent != NULL);
186 // If we have a custom shader for this node, then use it
187 if ( mAppliedShader != NULL )
189 mInheritedShader = mAppliedShader;
193 // Otherwise we either inherit a shader, or fall-back to the default
196 mInheritedShader = mParent->GetInheritedShader();
200 mInheritedShader = defaultShader;
203 DALI_ASSERT_DEBUG( mInheritedShader != NULL );
206 int Node::GetDirtyFlags() const
208 // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
209 int flags = mDirtyFlags;
210 const bool sizeFlag = mSize.IsClean();
212 if ( !(flags & TransformFlag) )
214 // Check whether the transform related properties have changed
216 !mPosition.IsClean() ||
217 !mRotation.IsClean() ||
219 mParentOrigin.InputChanged() || // parent origin and anchor point rarely change
220 mAnchorPoint.InputChanged() )
222 flags |= TransformFlag;
226 // Check whether the visible property has changed
227 if ( !mVisible.IsClean() )
229 flags |= VisibleFlag;
232 // Check whether the color property has changed
233 if ( !mColor.IsClean() )
238 // Check whether the size property has changed
247 void Node::ResetDefaultProperties( BufferIndex updateBufferIndex )
249 // clear dirty flags in parent origin & anchor point
250 mParentOrigin.Clear();
251 mAnchorPoint.Clear();
252 // Reset default properties
253 mSize.ResetToBaseValue( updateBufferIndex );
254 mPosition.ResetToBaseValue( updateBufferIndex );
255 mRotation.ResetToBaseValue( updateBufferIndex );
256 mScale.ResetToBaseValue( updateBufferIndex );
257 mVisible.ResetToBaseValue( updateBufferIndex );
258 mColor.ResetToBaseValue( updateBufferIndex );
260 mDirtyFlags = NothingFlag;
263 bool Node::IsFullyVisible( BufferIndex updateBufferIndex ) const
265 if( !IsVisible( updateBufferIndex ) )
270 Node* parent = mParent;
272 while( NULL != parent )
274 if( !parent->IsVisible( updateBufferIndex ) )
279 parent = parent->GetParent();
285 void Node::SetParent(Node& parentNode)
287 DALI_ASSERT_ALWAYS(this != &parentNode);
288 DALI_ASSERT_ALWAYS(!mIsRoot);
289 DALI_ASSERT_ALWAYS(mParent == NULL);
291 mParent = &parentNode;
294 void Node::RecursiveDisconnectFromSceneGraph( BufferIndex updateBufferIndex, std::set<Node*>& connectedNodes, std::set<Node*>& disconnectedNodes )
296 DALI_ASSERT_ALWAYS(!mIsRoot);
297 DALI_ASSERT_ALWAYS(mParent != NULL);
299 const NodeIter endIter = mChildren.End();
300 for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
302 (*iter)->RecursiveDisconnectFromSceneGraph( updateBufferIndex, connectedNodes, disconnectedNodes );
305 // Animators, Constraints etc. should be disconnected from the child's properties.
306 PropertyOwner::DisconnectFromSceneGraph();
309 mAppliedShader = NULL;
310 mInheritedShader = NULL;
312 // Remove back-pointer to parent
315 // Remove all child pointers
318 // Move into disconnectedNodes
319 std::set<Node*>::size_type removed = connectedNodes.erase( this );
320 DALI_ASSERT_ALWAYS( removed );
321 disconnectedNodes.insert( this );
324 } // namespace SceneGraph
326 } // namespace Internal