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/internal/render/shaders/shader.h>
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/common/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( Vector3::ZERO ),
49 mPosition( Vector3::ZERO ),
50 mRotation( Quaternion::IDENTITY ),
51 mScale( Vector3::ONE ),
53 mColor( Color::WHITE ),
54 mWorldPosition( Vector3::ZERO ),
55 mWorldRotation( Quaternion::IDENTITY ),
56 mWorldScale( Vector3::ONE ),
57 mWorldMatrix( Matrix::IDENTITY ),
58 mWorldColor( Color::WHITE ),
60 mAppliedShader( NULL ),
61 mInheritedShader( NULL ),
62 mExclusiveRenderTask( NULL ),
65 mGeometryScale( Vector3::ONE ),
66 mInitialVolume( Vector3::ONE ),
67 mDirtyFlags(AllFlags),
69 mInheritShader( true ),
70 mInheritRotation( true ),
71 mInheritScale( true ),
72 mTransmitGeometryScaling( false ),
73 mInhibitLocalTransform( false ),
75 mDrawMode( DrawMode::NORMAL ),
76 mPositionInheritanceMode( DEFAULT_POSITION_INHERITANCE_MODE ),
77 mColorMode( DEFAULT_COLOR_MODE )
85 void Node::OnDestroy()
87 // Node attachments should be notified about the disconnection.
90 mAttachment->OnDestroy();
93 // Animators, Constraints etc. should be disconnected from the child's properties.
94 PropertyOwner::DisconnectFromSceneGraph();
97 void Node::Attach( NodeAttachment& object )
99 DALI_ASSERT_DEBUG(!mAttachment);
101 object.SetParent(*this);
103 mAttachment = &object;
107 void Node::SetRoot(bool isRoot)
109 DALI_ASSERT_DEBUG(!isRoot || mParent == NULL); // Root nodes cannot have a parent
114 void Node::ConnectChild( Node* childNode )
116 DALI_ASSERT_ALWAYS( this != childNode );
117 DALI_ASSERT_ALWAYS( IsRoot() || NULL != mParent ); // Parent should be connected first
118 DALI_ASSERT_ALWAYS( !childNode->IsRoot() && NULL == childNode->GetParent() ); // Child should be disconnected
120 childNode->SetParent( *this );
122 // Everything should be reinherited when reconnected to scene-graph
123 childNode->SetAllDirtyFlags();
125 mChildren.PushBack( childNode );
128 void Node::DisconnectChild( BufferIndex updateBufferIndex, Node& childNode, std::set<Node*>& connectedNodes, std::set<Node*>& disconnectedNodes )
130 DALI_ASSERT_ALWAYS( this != &childNode );
131 DALI_ASSERT_ALWAYS( childNode.GetParent() == this );
133 // Find the childNode and remove it
136 const NodeIter endIter = mChildren.End();
137 for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
139 Node* current = *iter;
140 if ( current == &childNode )
143 mChildren.Erase( iter ); // order matters here
144 break; // iter is no longer valid
147 DALI_ASSERT_ALWAYS( NULL != found );
149 found->RecursiveDisconnectFromSceneGraph( updateBufferIndex, connectedNodes, disconnectedNodes );
152 void Node::ApplyShader( Shader* shader )
154 DALI_ASSERT_DEBUG( shader && "null shader passed to node" );
156 mAppliedShader = shader;
158 SetDirtyFlag(ShaderFlag);
161 void Node::RemoveShader()
163 mAppliedShader = NULL; // Wait until InheritShader to grab default shader
165 SetDirtyFlag(ShaderFlag);
168 Shader* Node::GetAppliedShader() const
170 return mAppliedShader;
173 void Node::SetInheritedShader(Shader* shader)
175 mInheritedShader = shader;
178 Shader* Node::GetInheritedShader() const
180 return mInheritedShader;
183 void Node::InheritShader(Shader* defaultShader)
185 DALI_ASSERT_DEBUG(mParent != NULL);
187 // If we have a custom shader for this node, then use it
188 if ( mAppliedShader != NULL )
190 mInheritedShader = mAppliedShader;
194 // Otherwise we either inherit a shader, or fall-back to the default
197 mInheritedShader = mParent->GetInheritedShader();
201 mInheritedShader = defaultShader;
204 DALI_ASSERT_DEBUG( mInheritedShader != NULL );
207 int Node::GetDirtyFlags() const
209 // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
210 int flags = mDirtyFlags;
211 const bool sizeFlag = mSize.IsClean();
213 if ( !(flags & TransformFlag) )
215 // Check whether the transform related properties have changed
217 !mPosition.IsClean() ||
218 !mRotation.IsClean() ||
220 mParentOrigin.InputChanged() || // parent origin and anchor point rarely change
221 mAnchorPoint.InputChanged() )
223 flags |= TransformFlag;
227 // Check whether the visible property has changed
228 if ( !mVisible.IsClean() )
230 flags |= VisibleFlag;
233 // Check whether the color property has changed
234 if ( !mColor.IsClean() )
239 // Check whether the size property has changed
248 void Node::ResetDefaultProperties( BufferIndex updateBufferIndex )
250 // clear dirty flags in parent origin & anchor point
251 mParentOrigin.Clear();
252 mAnchorPoint.Clear();
253 // Reset default properties
254 mSize.ResetToBaseValue( updateBufferIndex );
255 mPosition.ResetToBaseValue( updateBufferIndex );
256 mRotation.ResetToBaseValue( updateBufferIndex );
257 mScale.ResetToBaseValue( updateBufferIndex );
258 mVisible.ResetToBaseValue( updateBufferIndex );
259 mColor.ResetToBaseValue( updateBufferIndex );
261 mDirtyFlags = NothingFlag;
264 bool Node::IsFullyVisible( BufferIndex updateBufferIndex ) const
266 if( !IsVisible( updateBufferIndex ) )
271 Node* parent = mParent;
273 while( NULL != parent )
275 if( !parent->IsVisible( updateBufferIndex ) )
280 parent = parent->GetParent();
286 void Node::SetParent(Node& parentNode)
288 DALI_ASSERT_ALWAYS(this != &parentNode);
289 DALI_ASSERT_ALWAYS(!mIsRoot);
290 DALI_ASSERT_ALWAYS(mParent == NULL);
292 mParent = &parentNode;
295 void Node::RecursiveDisconnectFromSceneGraph( BufferIndex updateBufferIndex, std::set<Node*>& connectedNodes, std::set<Node*>& disconnectedNodes )
297 DALI_ASSERT_ALWAYS(!mIsRoot);
298 DALI_ASSERT_ALWAYS(mParent != NULL);
300 const NodeIter endIter = mChildren.End();
301 for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
303 (*iter)->RecursiveDisconnectFromSceneGraph( updateBufferIndex, connectedNodes, disconnectedNodes );
306 // Animators, Constraints etc. should be disconnected from the child's properties.
307 PropertyOwner::DisconnectFromSceneGraph();
310 mAppliedShader = NULL;
311 mInheritedShader = NULL;
313 // Remove back-pointer to parent
316 // Remove all child pointers
319 // Move into disconnectedNodes
320 std::set<Node*>::size_type removed = connectedNodes.erase( this );
321 DALI_ASSERT_ALWAYS( removed );
322 disconnectedNodes.insert( this );
325 } // namespace SceneGraph
327 } // namespace Internal