Removed unnecessary templating and initializing internal objects with exported constants
[platform/core/uifw/dali-core.git] / dali / internal / update / nodes / node.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/update/nodes/node.h>
20
21 // INTERNAL INCLUDES
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
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 namespace SceneGraph
34 {
35
36 const PositionInheritanceMode Node::DEFAULT_POSITION_INHERITANCE_MODE( INHERIT_PARENT_POSITION );
37 const ColorMode Node::DEFAULT_COLOR_MODE( USE_OWN_MULTIPLY_PARENT_ALPHA );
38
39 Node* Node::New()
40 {
41   return new Node();
42 }
43
44 Node::Node()
45 : mParentOrigin( ParentOrigin::DEFAULT ),
46   mAnchorPoint( AnchorPoint::DEFAULT ),
47   mSize(),     // zero initialized by default
48   mPosition(), // zero initialized by default
49   mRotation(), // initialized to identity by default
50   mScale( Vector3::ONE ),
51   mVisible( true ),
52   mColor( Color::WHITE ),
53   mWorldPosition(), // zero initialized by default
54   mWorldRotation(), // initialized to identity by default
55   mWorldScale( Vector3::ONE ),
56   mWorldMatrix(),
57   mWorldColor( Color::WHITE ),
58   mParent( NULL ),
59   mExclusiveRenderTask( NULL ),
60   mAttachment( NULL ),
61   mChildren(),
62   mGeometryScale( Vector3::ONE ),
63   mInitialVolume( Vector3::ONE ),
64   mDirtyFlags(AllFlags),
65   mIsRoot( false ),
66   mInheritRotation( true ),
67   mInheritScale( true ),
68   mTransmitGeometryScaling( false ),
69   mInhibitLocalTransform( false ),
70   mIsActive( true ),
71   mDrawMode( DrawMode::NORMAL ),
72   mPositionInheritanceMode( DEFAULT_POSITION_INHERITANCE_MODE ),
73   mColorMode( DEFAULT_COLOR_MODE )
74 {
75 }
76
77 Node::~Node()
78 {
79 }
80
81 void Node::OnDestroy()
82 {
83   // Node attachments should be notified about the disconnection.
84   if ( mAttachment )
85   {
86     mAttachment->OnDestroy();
87   }
88
89   // Animators, Constraints etc. should be disconnected from the child's properties.
90   PropertyOwner::Destroy();
91 }
92
93 void Node::Attach( NodeAttachment& object )
94 {
95   DALI_ASSERT_DEBUG(!mAttachment);
96
97   object.SetParent(*this);
98
99   mAttachment = &object;
100   SetAllDirtyFlags();
101 }
102
103 void Node::SetRoot(bool isRoot)
104 {
105   DALI_ASSERT_DEBUG(!isRoot || mParent == NULL); // Root nodes cannot have a parent
106
107   mIsRoot = isRoot;
108 }
109
110 void Node::ConnectChild( Node* childNode, int index )
111 {
112   DALI_ASSERT_ALWAYS( this != childNode );
113   DALI_ASSERT_ALWAYS( IsRoot() || NULL != mParent ); // Parent should be connected first
114   DALI_ASSERT_ALWAYS( !childNode->IsRoot() && NULL == childNode->GetParent() ); // Child should be disconnected
115
116   childNode->SetParent( *this );
117
118   // Everything should be reinherited when reconnected to scene-graph
119   childNode->SetAllDirtyFlags();
120
121   if (index == -1)
122   {
123     mChildren.PushBack( childNode );
124   }
125   else
126   {
127     mChildren.Insert(mChildren.Begin()+index, childNode);
128   }
129 }
130
131 void Node::DisconnectChild( BufferIndex updateBufferIndex, Node& childNode, std::set<Node*>& connectedNodes,  std::set<Node*>& disconnectedNodes )
132 {
133   DALI_ASSERT_ALWAYS( this != &childNode );
134   DALI_ASSERT_ALWAYS( childNode.GetParent() == this );
135
136   // Find the childNode and remove it
137   Node* found( NULL );
138
139   const NodeIter endIter = mChildren.End();
140   for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
141   {
142     Node* current = *iter;
143     if ( current == &childNode )
144     {
145       found = current;
146       mChildren.Erase( iter ); // order matters here
147       break; // iter is no longer valid
148     }
149   }
150   DALI_ASSERT_ALWAYS( NULL != found );
151
152   found->RecursiveDisconnectFromSceneGraph( updateBufferIndex, connectedNodes, disconnectedNodes );
153 }
154
155 int Node::GetDirtyFlags() const
156 {
157   // get initial dirty flags, they are reset ResetDefaultProperties, but setters may have made the node dirty already
158   int flags = mDirtyFlags;
159   const bool sizeFlag = mSize.IsClean();
160
161   if ( !(flags & TransformFlag) )
162   {
163     // Check whether the transform related properties have changed
164     if( !sizeFlag            ||
165         !mPosition.IsClean() ||
166         !mRotation.IsClean() ||
167         !mScale.IsClean()    ||
168         mParentOrigin.InputChanged() || // parent origin and anchor point rarely change
169         mAnchorPoint.InputChanged() )
170     {
171       flags |= TransformFlag;
172     }
173   }
174
175   // Check whether the visible property has changed
176   if ( !mVisible.IsClean() )
177   {
178     flags |= VisibleFlag;
179   }
180
181   // Check whether the color property has changed
182   if ( !mColor.IsClean() )
183   {
184     flags |= ColorFlag;
185   }
186
187   // Check whether the size property has changed
188   if ( !sizeFlag )
189   {
190     flags |= SizeFlag;
191    }
192
193   return flags;
194 }
195
196 void Node::ResetDefaultProperties( BufferIndex updateBufferIndex )
197 {
198   // clear dirty flags in parent origin & anchor point
199   mParentOrigin.Clear();
200   mAnchorPoint.Clear();
201   // Reset default properties
202   mSize.ResetToBaseValue( updateBufferIndex );
203   mPosition.ResetToBaseValue( updateBufferIndex );
204   mRotation.ResetToBaseValue( updateBufferIndex );
205   mScale.ResetToBaseValue( updateBufferIndex );
206   mVisible.ResetToBaseValue( updateBufferIndex );
207   mColor.ResetToBaseValue( updateBufferIndex );
208
209   mDirtyFlags = NothingFlag;
210 }
211
212 bool Node::IsFullyVisible( BufferIndex updateBufferIndex ) const
213 {
214   if( !IsVisible( updateBufferIndex ) )
215   {
216     return false;
217   }
218
219   Node* parent = mParent;
220
221   while( NULL != parent )
222   {
223     if( !parent->IsVisible( updateBufferIndex ) )
224     {
225       return false;
226     }
227
228     parent = parent->GetParent();
229   }
230
231   return true;
232 }
233
234 void Node::SetParent(Node& parentNode)
235 {
236   DALI_ASSERT_ALWAYS(this != &parentNode);
237   DALI_ASSERT_ALWAYS(!mIsRoot);
238   DALI_ASSERT_ALWAYS(mParent == NULL);
239
240   mParent = &parentNode;
241 }
242
243 void Node::RecursiveDisconnectFromSceneGraph( BufferIndex updateBufferIndex, std::set<Node*>& connectedNodes,  std::set<Node*>& disconnectedNodes )
244 {
245   DALI_ASSERT_ALWAYS(!mIsRoot);
246   DALI_ASSERT_ALWAYS(mParent != NULL);
247
248   const NodeIter endIter = mChildren.End();
249   for ( NodeIter iter = mChildren.Begin(); iter != endIter; ++iter )
250   {
251     (*iter)->RecursiveDisconnectFromSceneGraph( updateBufferIndex, connectedNodes, disconnectedNodes );
252   }
253
254   // Animators, Constraints etc. should be disconnected from the child's properties.
255   PropertyOwner::DisconnectFromSceneGraph( updateBufferIndex );
256
257   // Remove back-pointer to parent
258   mParent = NULL;
259
260   // Remove all child pointers
261   mChildren.Clear();
262
263   // Move into disconnectedNodes
264   std::set<Node*>::size_type removed = connectedNodes.erase( this );
265   DALI_ASSERT_ALWAYS( removed );
266   disconnectedNodes.insert( this );
267 }
268
269 } // namespace SceneGraph
270
271 } // namespace Internal
272
273 } // namespace Dali