removal of shader inheritance, renderable actors now have shaders and there is a...
[platform/core/uifw/dali-core.git] / dali / internal / update / nodes / node.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_NODE_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_NODE_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/actors/actor-enumerations.h>
23 #include <dali/public-api/actors/draw-mode.h>
24 #include <dali/public-api/math/quaternion.h>
25 #include <dali/public-api/math/math-utils.h>
26 #include <dali/public-api/math/vector3.h>
27 #include <dali/internal/common/message.h>
28 #include <dali/internal/common/event-to-update.h>
29 #include <dali/internal/update/common/animatable-property.h>
30 #include <dali/internal/update/common/double-buffered.h>
31 #include <dali/internal/update/common/property-owner.h>
32 #include <dali/internal/update/common/property-vector3.h>
33 #include <dali/internal/update/common/scene-graph-buffers.h>
34 #include <dali/internal/update/common/inherited-property.h>
35 #include <dali/integration-api/debug.h>
36 #include <dali/internal/update/nodes/node-declarations.h>
37 #include <dali/internal/update/node-attachments/node-attachment-declarations.h>
38 #include <dali/internal/render/renderers/render-data-provider.h>
39
40 namespace Dali
41 {
42
43 namespace Internal
44 {
45
46 // value types used by messages
47 template <> struct ParameterType< ColorMode > : public BasicType< ColorMode > {};
48 template <> struct ParameterType< PositionInheritanceMode > : public BasicType< PositionInheritanceMode > {};
49
50 namespace SceneGraph
51 {
52
53 class DiscardQueue;
54 class Layer;
55 class NodeAttachment;
56 class RenderTask;
57 class UpdateManager;
58
59 /**
60  * Flag whether property has changed, during the Update phase.
61  */
62 enum NodePropertyFlags
63 {
64   NothingFlag          = 0x000,
65   TransformFlag        = 0x001,
66   VisibleFlag          = 0x002,
67   ColorFlag            = 0x004,
68   SizeFlag             = 0x008,
69   OverlayFlag          = 0x010,
70   SortModifierFlag     = 0x020,
71   ChildDeletedFlag     = 0x040
72 };
73
74 static const int AllFlags = ( ChildDeletedFlag << 1 ) - 1; // all the flags
75
76 /**
77  * Size is not inherited.
78  * VisibleFlag is inherited so that attachments can be synchronized with nodes after they become visible
79  */
80 static const int InheritedDirtyFlags = TransformFlag | VisibleFlag | ColorFlag | OverlayFlag;
81
82 // Flags which require the scene renderable lists to be updated
83 static const int RenderableUpdateFlags = TransformFlag | SortModifierFlag | ChildDeletedFlag;
84
85 /**
86  * Node is the base class for all nodes in the Scene Graph.
87  * Each node provides a transformation which applies to the node and its children.
88  * Node data is double-buffered. This allows an update thread to modify node data, without interferring
89  * with another thread reading the values from the previous update traversal.
90  */
91 class Node : public PropertyOwner, public RenderDataProvider
92 {
93 public:
94
95   // Defaults
96   static const PositionInheritanceMode DEFAULT_POSITION_INHERITANCE_MODE;
97   static const ColorMode DEFAULT_COLOR_MODE;
98
99   // Creation methods
100
101   /**
102    * Construct a new Node.
103    */
104   static Node* New();
105
106   /**
107    * Virtual destructor
108    */
109   virtual ~Node();
110
111   /**
112    * When a Node is marked "active" it has been disconnected, but its properties have been modified.
113    * @note An inactive Node will be skipped during the UpdateManager ResetProperties stage.
114    * @param[in] isActive True if the Node is active.
115    */
116   void SetActive( bool isActive )
117   {
118     mIsActive = isActive;
119   }
120
121   /**
122    * Query whether the Node is active.
123    * @return True if the Node is active.
124    */
125   bool IsActive() const
126   {
127     return mIsActive;
128   }
129
130   /**
131    * Called during UpdateManager::DestroyNode shortly before Node is destroyed.
132    */
133   void OnDestroy();
134
135   // Layer interface
136
137   /**
138    * Query whether the node is a layer.
139    * @return True if the node is a layer.
140    */
141   bool IsLayer()
142   {
143     return (GetLayer() != NULL);
144   }
145
146   /**
147    * Convert a node to a layer.
148    * @return A pointer to the layer, or NULL.
149    */
150   virtual Layer* GetLayer()
151   {
152     return NULL;
153   }
154
155   // Attachments
156
157   /**
158    * Attach an object to this Node; This should only be done by UpdateManager::AttachToNode.
159    * @pre The Node does not already have an attachment.
160    * @param[in] attachment The object to attach.
161    */
162   void Attach( NodeAttachment& attachment );
163
164   /**
165    * Query the node if it has an attachment.
166    * @return True if it has an attachment.
167    */
168   bool HasAttachment() const
169   {
170     return mAttachment;
171   }
172
173   /**
174    * Retreive the object attached to this node.
175    * @return The attachment.
176    */
177   NodeAttachment& GetAttachment() const
178   {
179     return *mAttachment;
180   }
181
182   // Containment methods
183
184   /**
185    * Query whether a node is the root node. Root nodes cannot have a parent node.
186    * A node becomes a root node, when it is installed by UpdateManager.
187    * @return True if the node is a root node.
188    */
189   bool IsRoot() const
190   {
191     return mIsRoot;
192   }
193
194   /**
195    * Set whether a node is the root node. Root nodes cannot have a parent node.
196    * This method should only be called by UpdateManager.
197    * @pre When isRoot is true, the node must not have a parent.
198    * @param[in] isRoot Whether the node is now a root node.
199    */
200   void SetRoot(bool isRoot);
201
202   /**
203    * Retrieve the parent of a Node.
204    * @return The parent node, or NULL if the Node has not been added to the scene-graph.
205    */
206   Node* GetParent()
207   {
208     return mParent;
209   }
210
211   /**
212    * Retrieve the parent of a Node.
213    * @return The parent node, or NULL if the Node has not been added to the scene-graph.
214    */
215   const Node* GetParent() const
216   {
217     return mParent;
218   }
219
220   /**
221    * Connect a node to the scene-graph.
222    * @pre A node cannot be added to itself.
223    * @pre The parent node is connected to the scene-graph.
224    * @pre The childNode does not already have a parent.
225    * @pre The childNode is not a root node.
226    * @param[in] childNode The child to add.
227    */
228   void ConnectChild( Node* childNode );
229
230   /**
231    * Disconnect a child (& its children) from the scene-graph.
232    * @pre childNode is a child of this Node.
233    * @param[in] updateBufferIndex The current update buffer index.
234    * @param[in] childNode The node to disconnect.
235    * @param[in] connectedNodes Disconnected Node attachments should be removed from here.
236    * @param[in] disconnectedNodes Disconnected Node attachments should be added here.
237    */
238   void DisconnectChild( BufferIndex updateBufferIndex, Node& childNode, std::set<Node*>& connectedNodes,  std::set<Node*>& disconnectedNodes );
239
240   /**
241    * Retrieve the children a Node.
242    * @return The container of children.
243    */
244   const NodeContainer& GetChildren() const
245   {
246     return mChildren;
247   }
248
249   /**
250    * Retrieve the children a Node.
251    * @return The container of children.
252    */
253   NodeContainer& GetChildren()
254   {
255     return mChildren;
256   }
257
258   // Update methods
259
260   /**
261    * Flag that one of the node values has changed in the current frame.
262    * @param[in] flag The flag to set.
263    */
264   void SetDirtyFlag(NodePropertyFlags flag)
265   {
266     mDirtyFlags |= flag;
267   }
268
269   /**
270    * Flag that all of the node values are dirty.
271    */
272   void SetAllDirtyFlags()
273   {
274     mDirtyFlags = AllFlags;
275   }
276
277   /**
278    * Query whether a node is dirty.
279    * @return The dirty flags
280    */
281   int GetDirtyFlags() const;
282
283   /**
284    * Query whether a node is clean.
285    * @return True if the node is clean.
286    */
287   bool IsClean() const
288   {
289     return ( NothingFlag == GetDirtyFlags() );
290   }
291
292   /**
293    * Retrieve the parent-origin of the node.
294    * @return The parent-origin.
295    */
296   const Vector3& GetParentOrigin() const
297   {
298     return mParentOrigin.mValue;
299   }
300
301   /**
302    * Sets both the local & base parent-origins of the node.
303    * @param[in] origin The new local & base parent-origins.
304    */
305   void SetParentOrigin(const Vector3& origin)
306   {
307     mParentOrigin.mValue = origin;
308     mParentOrigin.OnSet();
309   }
310
311   /**
312    * Retrieve the anchor-point of the node.
313    * @return The anchor-point.
314    */
315   const Vector3& GetAnchorPoint() const
316   {
317     return mAnchorPoint.mValue;
318   }
319
320   /**
321    * Sets both the local & base anchor-points of the node.
322    * @param[in] anchor The new local & base anchor-points.
323    */
324   void SetAnchorPoint(const Vector3& anchor)
325   {
326     mAnchorPoint.mValue = anchor;
327     mAnchorPoint.OnSet();
328   }
329
330   /**
331    * Retrieve the local position of the node, relative to its parent.
332    * @param[in] bufferIndex The buffer to read from.
333    * @return The local position.
334    */
335   const Vector3& GetPosition(BufferIndex bufferIndex) const
336   {
337     return mPosition[bufferIndex];
338   }
339
340   /**
341    * Sets both the local & base positions of the node.
342    * @param[in] updateBufferIndex The current update buffer index.
343    * @param[in] position The new local & base position.
344    */
345   void BakePosition(BufferIndex updateBufferIndex, const Vector3& position)
346   {
347     mPosition.Bake( updateBufferIndex, position );
348   }
349
350   /**
351    * Sets the world of the node derived from the position of all its parents.
352    * @param[in] updateBufferIndex The current update buffer index.
353    * @param[in] position The world position.
354    */
355   void SetWorldPosition( BufferIndex updateBufferIndex, const Vector3& position )
356   {
357     mWorldPosition.Set( updateBufferIndex, position );
358   }
359
360   /**
361    * Sets the position of the node derived from the position of all its parents.
362    * This method should only be called when the parent's world position is up-to-date.
363    * With a non-central anchor-point, the local rotation and scale affects the world position.
364    * Therefore the world rotation & scale must be updated before the world position.
365    * @pre The node has a parent.
366    * @param[in] updateBufferIndex The current update buffer index.
367    */
368   void InheritWorldPosition(BufferIndex updateBufferIndex)
369   {
370     DALI_ASSERT_DEBUG(mParent != NULL);
371
372     switch( mPositionInheritanceMode )
373     {
374       case INHERIT_PARENT_POSITION  : ///@see Dali::PositionInheritanceMode for how these modes are expected to work
375       {
376         Vector3 finalPosition(-0.5f, -0.5f, -0.5f);
377
378         finalPosition += mParentOrigin.mValue;
379         finalPosition *= mParent->GetSize(updateBufferIndex);
380         finalPosition += mPosition[updateBufferIndex];
381         finalPosition *= mParent->GetWorldScale(updateBufferIndex);
382         const Quaternion& parentWorldRotation = mParent->GetWorldRotation(updateBufferIndex);
383         if(!parentWorldRotation.IsIdentity())
384         {
385           finalPosition *= parentWorldRotation;
386         }
387
388         // check if a node needs to be offsetted locally (only applies when AnchorPoint is not central)
389         // dont use operator== as that does a slower comparison (and involves function calls)
390         Vector3 localOffset(0.5f, 0.5f, 0.5f);    // AnchorPoint::CENTER
391         localOffset -= mAnchorPoint.mValue;
392
393         if( ( fabsf( localOffset.x ) >= Math::MACHINE_EPSILON_0 ) ||
394             ( fabsf( localOffset.y ) >= Math::MACHINE_EPSILON_0 ) ||
395             ( fabsf( localOffset.z ) >= Math::MACHINE_EPSILON_0 ) )
396         {
397           localOffset *= mSize[updateBufferIndex];
398
399           Vector3 scale = mWorldScale[updateBufferIndex];
400           if(GetTransmitGeometryScaling())
401           {
402             // Remove geometry scaling to get back to actor scale
403             scale /= mGeometryScale;
404           }
405           // Also pick up sign of local scale
406           if (mScale[updateBufferIndex].x < 0.0f)
407           {
408             scale.x = -scale.x;
409           }
410           if (mScale[updateBufferIndex].y < 0.0f)
411           {
412             scale.y = -scale.y;
413           }
414           if (mScale[updateBufferIndex].z < 0.0f)
415           {
416             scale.z = -scale.z;
417           }
418
419           // If the anchor-point is not central, then position is affected by the local rotation & scale
420           localOffset *= scale;
421           const Quaternion& localWorldRotation = mWorldRotation[updateBufferIndex];
422           if(!localWorldRotation.IsIdentity())
423           {
424             localOffset *= localWorldRotation;
425           }
426           finalPosition += localOffset;
427         }
428
429         finalPosition += mParent->GetWorldPosition(updateBufferIndex);
430         mWorldPosition.Set( updateBufferIndex, finalPosition );
431         break;
432       }
433       case USE_PARENT_POSITION_PLUS_LOCAL_POSITION :
434       {
435         // copy parents position plus local transform
436         mWorldPosition.Set( updateBufferIndex, mParent->GetWorldPosition(updateBufferIndex) + mPosition[updateBufferIndex] );
437         break;
438       }
439       case USE_PARENT_POSITION :
440       {
441         // copy parents position
442         mWorldPosition.Set( updateBufferIndex, mParent->GetWorldPosition(updateBufferIndex) );
443         break;
444       }
445       case DONT_INHERIT_POSITION :
446       {
447         // use local position as world position
448         mWorldPosition.Set( updateBufferIndex, mPosition[updateBufferIndex] );
449         break;
450       }
451     }
452   }
453
454   /**
455    * Copies the previous inherited position, if this changed in the previous frame.
456    * This method should be called instead of InheritWorldPosition i.e. if the inherited position
457    * does not need to be recalculated in the current frame.
458    * @param[in] updateBufferIndex The current update buffer index.
459    */
460   void CopyPreviousWorldPosition( BufferIndex updateBufferIndex )
461   {
462     mWorldPosition.CopyPrevious( updateBufferIndex );
463   }
464
465   /**
466    * Retrieve the position of the node derived from the position of all its parents.
467    * @return The world position.
468    */
469   const Vector3& GetWorldPosition( BufferIndex bufferIndex ) const
470   {
471     return mWorldPosition[bufferIndex];
472   }
473
474   /**
475    * Set the position inheritance mode.
476    * @see Dali::Actor::PositionInheritanceMode
477    * @param[in] mode The new position inheritance mode.
478    */
479   void SetPositionInheritanceMode( PositionInheritanceMode mode )
480   {
481     mPositionInheritanceMode = mode;
482
483     SetDirtyFlag(TransformFlag);
484   }
485
486   /**
487    * @return The position inheritance mode.
488    */
489   PositionInheritanceMode GetPositionInheritanceMode() const
490   {
491     return mPositionInheritanceMode;
492   }
493
494   /**
495    * Retrieve the local rotation of the node, relative to its parent.
496    * @param[in] bufferIndex The buffer to read from.
497    * @return The local rotation.
498    */
499   const Quaternion& GetRotation(BufferIndex bufferIndex) const
500   {
501     return mRotation[bufferIndex];
502   }
503
504   /**
505    * Sets both the local & base rotations of the node.
506    * @param[in] updateBufferIndex The current update buffer index.
507    * @param[in] rotation The new local & base rotation.
508    */
509   void BakeRotation(BufferIndex updateBufferIndex, const Quaternion& rotation)
510   {
511     mRotation.Bake( updateBufferIndex, rotation );
512   }
513
514   /**
515    * Sets the rotation of the node derived from the rotation of all its parents.
516    * @param[in] updateBufferIndex The current update buffer index.
517    * @param[in] rotation The world rotation.
518    */
519   void SetWorldRotation( BufferIndex updateBufferIndex, const Quaternion& rotation )
520   {
521     mWorldRotation.Set( updateBufferIndex, rotation );
522   }
523
524   /**
525    * Sets the rotation of the node derived from the rotation of all its parents.
526    * This method should only be called when the parents world rotation is up-to-date.
527    * @pre The node has a parent.
528    * @param[in] updateBufferIndex The current update buffer index.
529    */
530   void InheritWorldRotation( BufferIndex updateBufferIndex )
531   {
532     DALI_ASSERT_DEBUG(mParent != NULL);
533
534     const Quaternion& localRotation = mRotation[updateBufferIndex];
535
536     if(localRotation.IsIdentity())
537     {
538       mWorldRotation.Set( updateBufferIndex, mParent->GetWorldRotation(updateBufferIndex) );
539     }
540     else
541     {
542       Quaternion finalRotation( mParent->GetWorldRotation(updateBufferIndex) );
543       finalRotation *= localRotation;
544       mWorldRotation.Set( updateBufferIndex, finalRotation );
545     }
546   }
547
548   /**
549    * Copies the previous inherited rotation, if this changed in the previous frame.
550    * This method should be called instead of InheritWorldRotation i.e. if the inherited rotation
551    * does not need to be recalculated in the current frame.
552    * @param[in] updateBufferIndex The current update buffer index.
553    */
554   void CopyPreviousWorldRotation( BufferIndex updateBufferIndex )
555   {
556     mWorldRotation.CopyPrevious( updateBufferIndex );
557   }
558
559   /**
560    * Retrieve the rotation of the node derived from the rotation of all its parents.
561    * @param[in] bufferIndex The buffer to read from.
562    * @return The world rotation.
563    */
564   const Quaternion& GetWorldRotation( BufferIndex bufferIndex ) const
565   {
566     return mWorldRotation[bufferIndex];
567   }
568
569   /**
570    * Set whether the Node inherits rotation.
571    * @param[in] inherit True if the parent rotation is inherited.
572    */
573   void SetInheritRotation(bool inherit)
574   {
575     if (inherit != mInheritRotation)
576     {
577       mInheritRotation = inherit;
578
579       SetDirtyFlag(TransformFlag);
580     }
581   }
582
583   /**
584    * Query whether the node inherits rotation from its parent.
585    * @return True if the parent rotation is inherited.
586    */
587   bool IsRotationInherited() const
588   {
589     return mInheritRotation;
590   }
591
592   /**
593    * Set the initial volume of the node. Used for calculating geometry scaling
594    * as the node size is changed  when transmitGeometryScaling is set to true.
595    *
596    * This property is not animatable.
597    *
598    * @param[in] volume The initial volume of this nodes meshes & children
599    */
600   void SetInitialVolume( const Vector3& volume)
601   {
602     mInitialVolume = volume;
603     SetDirtyFlag(SizeFlag);
604   }
605
606   /**
607    * Get the initial volume.  Used for calculating geometry scaling
608    * when TransmitGeometryScaling is true (i.e., the scaling is baked
609    * into the node tranform)
610    *
611    * @return The initial volume of this node and children.
612    */
613   Vector3 GetInitialVolume()
614   {
615     return mInitialVolume;
616   }
617
618   /**
619    * Sets whether the geometry scaling should be applied to the node
620    * (In which case, set the initial scale using SetInitialVolume()).
621    *
622    * If it is applied to the node, then the attachments are not scaled,
623    * as the scaling is then already baked into the node transform.
624    *
625    * @param[in] transmitGeometryScaling true if scaling is to be applied
626    * to the node.
627    */
628   void SetTransmitGeometryScaling(bool transmitGeometryScaling)
629   {
630     mTransmitGeometryScaling = transmitGeometryScaling;
631     SetDirtyFlag(SizeFlag);
632   }
633
634   /**
635    * Find out whether the node allows geometry scaling to be transmitted to its children.
636    * @return true if transmitted.
637    */
638   bool GetTransmitGeometryScaling() const
639   {
640     return mTransmitGeometryScaling;
641   }
642
643   /**
644    * Retrieve the local scale of the node, relative to its parent.
645    * @param[in] bufferIndex The buffer to read from.
646    * @return The local scale.
647    */
648   const Vector3& GetScale(BufferIndex bufferIndex) const
649   {
650     return mScale[bufferIndex];
651   }
652
653   /**
654    * Sets the scale of the node derived from the scale of all its parents and a pre-scale
655    * @param[in] updateBufferIndex The current update buffer index.
656    * @param[in] scale The world scale.
657    */
658   void SetWorldScale(BufferIndex updateBufferIndex, const Vector3& scale)
659   {
660     mWorldScale.Set( updateBufferIndex, mGeometryScale * scale );
661   }
662
663   /**
664    * Sets the scale of the node derived from the scale of all its parents and a pre-scale.
665    * This method should only be called when the parents world scale is up-to-date.
666    * @pre The node has a parent.
667    * @param[in] updateBufferIndex The current update buffer index.
668    */
669   void InheritWorldScale(BufferIndex updateBufferIndex)
670   {
671     DALI_ASSERT_DEBUG(mParent != NULL);
672
673     mWorldScale.Set( updateBufferIndex, mParent->GetWorldScale(updateBufferIndex) * mGeometryScale * mScale[updateBufferIndex] );
674   }
675
676   /**
677    * Copies the previous inherited scale, if this changed in the previous frame.
678    * This method should be called instead of InheritWorldScale i.e. if the inherited scale
679    * does not need to be recalculated in the current frame.
680    * @param[in] updateBufferIndex The current update buffer index.
681    */
682   void CopyPreviousWorldScale( BufferIndex updateBufferIndex )
683   {
684     mWorldScale.CopyPrevious( updateBufferIndex );
685   }
686
687   /**
688    * Retrieve the scale of the node derived from the scale of all its parents.
689    * @param[in] bufferIndex The buffer to read from.
690    * @return The world scale.
691    */
692   const Vector3& GetWorldScale( BufferIndex bufferIndex ) const
693   {
694     return mWorldScale[bufferIndex];
695   }
696
697   /**
698    * Set whether the Node inherits scale.
699    * @param inherit True if the Node inherits scale.
700    */
701   void SetInheritScale( bool inherit )
702   {
703     if( inherit != mInheritScale )
704     {
705       mInheritScale = inherit;
706
707       SetDirtyFlag( TransformFlag );
708     }
709   }
710
711   /**
712    * Query whether the Node inherits scale.
713    * @return if scale is inherited
714    */
715   bool IsScaleInherited() const
716   {
717     return mInheritScale;
718   }
719
720   /**
721    * Sets a geometry scale, calculated when TransmitGeometryScaling is true.
722    * Must only be used from render thread.
723    * @param[in] geometryScale The geometry scale value
724    */
725   void SetGeometryScale(Vector3 geometryScale)
726   {
727     mGeometryScale = geometryScale;
728
729     SetDirtyFlag( TransformFlag );
730   }
731
732   /**
733    * Retrieve the geometry scale, calculated when TransmitGeometryScaling is true.
734    * @return The geometry scale value.
735    */
736   const Vector3& GetGeometryScale() const
737   {
738     return mGeometryScale;
739   }
740
741   /**
742    * Retrieve the visibility of the node.
743    * @param[in] bufferIndex The buffer to read from.
744    * @return True if the node is visible.
745    */
746   bool IsVisible(BufferIndex bufferIndex) const
747   {
748     return mVisible[bufferIndex];
749   }
750
751   /**
752    * Retrieves whether a node is fully visible.
753    * A node is fully visible if is visible and all its ancestors are visible.
754    * @param[in] updateBufferIndex The current update buffer index.
755    * @return True if the node is fully visible.
756    */
757   bool IsFullyVisible( BufferIndex updateBufferIndex ) const;
758
759   /**
760    * Retrieve the opacity of the node.
761    * @param[in] bufferIndex The buffer to read from.
762    * @return The opacity.
763    */
764   float GetOpacity(BufferIndex bufferIndex) const
765   {
766     return mColor[bufferIndex].a;
767   }
768
769   /**
770    * Retrieve the color of the node.
771    * @param[in] bufferIndex The buffer to read from.
772    * @return The color.
773    */
774   const Vector4& GetColor(BufferIndex bufferIndex) const
775   {
776     return mColor[bufferIndex];
777   }
778
779   /**
780    * Sets the color of the node derived from the color of all its parents.
781    * @param[in] color The world color.
782    * @param[in] updateBufferIndex The current update buffer index.
783    */
784   void SetWorldColor(const Vector4& color, BufferIndex updateBufferIndex)
785   {
786     mWorldColor.Set( updateBufferIndex, color );
787   }
788
789   /**
790    * Sets the color of the node derived from the color of all its parents.
791    * This method should only be called when the parents world color is up-to-date.
792    * @pre The node has a parent.
793    * @param[in] updateBufferIndex The current update buffer index.
794    */
795   void InheritWorldColor( BufferIndex updateBufferIndex )
796   {
797     DALI_ASSERT_DEBUG(mParent != NULL);
798
799     // default first
800     if( mColorMode == USE_OWN_MULTIPLY_PARENT_ALPHA )
801     {
802       const Vector4& ownColor = mColor[updateBufferIndex];
803       mWorldColor.Set( updateBufferIndex, ownColor.r, ownColor.g, ownColor.b, ownColor.a * mParent->GetWorldColor(updateBufferIndex).a );
804     }
805     else if( mColorMode == USE_OWN_MULTIPLY_PARENT_COLOR )
806     {
807       mWorldColor.Set( updateBufferIndex, mParent->GetWorldColor(updateBufferIndex) * mColor[updateBufferIndex] );
808     }
809     else if( mColorMode == USE_PARENT_COLOR )
810     {
811       mWorldColor.Set( updateBufferIndex, mParent->GetWorldColor(updateBufferIndex) );
812     }
813     else // USE_OWN_COLOR
814     {
815       mWorldColor.Set( updateBufferIndex, mColor[updateBufferIndex] );
816     }
817   }
818
819   /**
820    * Copies the previous inherited scale, if this changed in the previous frame.
821    * This method should be called instead of InheritWorldScale i.e. if the inherited scale
822    * does not need to be recalculated in the current frame.
823    * @param[in] updateBufferIndex The current update buffer index.
824    */
825   void CopyPreviousWorldColor( BufferIndex updateBufferIndex )
826   {
827     mWorldColor.CopyPrevious( updateBufferIndex );
828   }
829
830   /**
831    * Retrieve the color of the node, possibly derived from the color
832    * of all its parents, depending on the value of mColorMode.
833    * @param[in] bufferIndex The buffer to read from.
834    * @return The world color.
835    */
836   const Vector4& GetWorldColor(BufferIndex bufferIndex) const
837   {
838     return mWorldColor[bufferIndex];
839   }
840
841   /**
842    * Set the color mode. This specifies whether the Node uses its own color,
843    * or inherits its parent color.
844    * @param[in] colorMode The new color mode.
845    */
846   void SetColorMode(ColorMode colorMode)
847   {
848     mColorMode = colorMode;
849
850     SetDirtyFlag(ColorFlag);
851   }
852
853   /**
854    * Retrieve the color mode.
855    * @return The color mode.
856    */
857   ColorMode GetColorMode() const
858   {
859     return mColorMode;
860   }
861
862   /**
863    * Retrieve the size of the node.
864    * @param[in] bufferIndex The buffer to read from.
865    * @return The size.
866    */
867   const Vector3& GetSize(BufferIndex bufferIndex) const
868   {
869     return mSize[bufferIndex];
870   }
871
872   /**
873    * Set the world-matrix of a node, with scale + rotation + translation.
874    * Scale and rotation are centered at the origin.
875    * Translation is applied independently of the scale or rotatation axis.
876    * @param[in] updateBufferIndex The current update buffer index.
877    * @param[in] scale The scale.
878    * @param[in] rotation The rotation.
879    * @param[in] translation The translation.
880    */
881   void SetWorldMatrix( BufferIndex updateBufferIndex, const Vector3& scale, const Quaternion& rotation, const Vector3& translation )
882   {
883     mWorldMatrix.Get( updateBufferIndex ).SetTransformComponents( scale, rotation, translation );
884     mWorldMatrix.SetDirty( updateBufferIndex );
885   }
886
887   /**
888    * Retrieve the cached world-matrix of a node.
889    * @param[in] bufferIndex The buffer to read from.
890    * @return The world-matrix.
891    */
892   const Matrix& GetWorldMatrix( BufferIndex bufferIndex ) const
893   {
894     return mWorldMatrix[ bufferIndex ];
895   }
896
897   /**
898    * Copy previous frames world matrix
899    * @param[in] updateBufferIndex The current update buffer index.
900    */
901   void CopyPreviousWorldMatrix( BufferIndex updateBufferIndex )
902   {
903     mWorldMatrix.CopyPrevious( updateBufferIndex );
904   }
905
906   /**
907    * Mark the node as exclusive to a single RenderTask.
908    * @param[in] renderTask The render-task, or NULL if the Node is not exclusive to a single RenderTask.
909    */
910   void SetExclusiveRenderTask( RenderTask* renderTask )
911   {
912     mExclusiveRenderTask = renderTask;
913   }
914
915   /**
916    * Query whether the node is exclusive to a single RenderTask.
917    * @return The render-task, or NULL if the Node is not exclusive to a single RenderTask.
918    */
919   RenderTask* GetExclusiveRenderTask() const
920   {
921     return mExclusiveRenderTask;
922   }
923
924   /**
925    * Set how the Node and its children should be drawn; see Dali::Actor::SetDrawMode() for more details.
926    * @param[in] drawMode The new draw-mode to use.
927    */
928   void SetDrawMode( const DrawMode::Type& drawMode )
929   {
930     mDrawMode = drawMode;
931   }
932
933   /**
934    * Returns whether node is an overlay or not.
935    * @return True if node is an overlay, false otherwise.
936    */
937   DrawMode::Type GetDrawMode() const
938   {
939     return mDrawMode;
940   }
941
942   /**
943    * Equality operator, checks for identity, not values.
944    *
945    */
946   bool operator==( const Node* rhs ) const
947   {
948     if ( this == rhs )
949     {
950       return true;
951     }
952     return false;
953   }
954
955   /**
956    * Set the inhibit local transform flag.@n
957    * Setting this flag will stop the node's local transform (position, scale and orientation)
958    * being applied on top of its parents transformation.
959    * @param[in] flag When true, local transformation is inhibited when calculating the world matrix.
960    */
961   void SetInhibitLocalTransform( bool flag )
962   {
963     SetDirtyFlag( TransformFlag );
964     mInhibitLocalTransform = flag;
965   }
966
967   /**
968    * Get the inhibit local transform flag.@n
969    * See @ref SetInhibitLocalTransform
970    * @result A flag, when true, local transformation is inhibited when calculating the world matrix.
971    */
972   bool GetInhibitLocalTransform() const
973   {
974     return mInhibitLocalTransform;
975   }
976
977 protected:
978
979   /**
980    * Set the parent of a Node.
981    * @param[in] parentNode the new parent.
982    */
983   void SetParent(Node& parentNode);
984
985   /**
986    * Protected constructor; See also Node::New()
987    */
988   Node();
989
990 private: // from RenderDataProvider
991
992   /**
993    * @copydoc RenderDataProvider::GetModelMatrix
994    */
995   virtual const Matrix& GetModelMatrix( unsigned int bufferId )
996   {
997     return GetWorldMatrix( bufferId );
998   }
999
1000   /**
1001    * @copydoc RenderDataProvider::GetRenderColor
1002    */
1003   virtual const Vector4& GetRenderColor( unsigned int bufferId )
1004   {
1005     return GetWorldColor( bufferId );
1006   }
1007
1008 private:
1009
1010   // Undefined
1011   Node(const Node&);
1012
1013   // Undefined
1014   Node& operator=(const Node& rhs);
1015
1016   /**
1017    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
1018    */
1019   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
1020
1021   /**
1022    * Recursive helper to disconnect a Node and its children.
1023    * Disconnected Nodes have no parent or children.
1024    * @param[in] updateBufferIndex The current update buffer index.
1025    * @param[in] connectedNodes Disconnected Node attachments should be removed from here.
1026    * @param[in] disconnectedNodes Disconnected Node attachments should be added here.
1027    */
1028   void RecursiveDisconnectFromSceneGraph( BufferIndex updateBufferIndex, std::set<Node*>& connectedNodes, std::set<Node*>& disconnectedNodes );
1029
1030 public: // Default properties
1031
1032   PropertyVector3                mParentOrigin;  ///< Local transform; the position is relative to this. Sets the TransformFlag dirty when changed
1033   PropertyVector3                mAnchorPoint;   ///< Local transform; local center of rotation. Sets the TransformFlag dirty when changed
1034
1035   AnimatableProperty<Vector3>    mSize;          ///< Size is provided for layouting
1036   AnimatableProperty<Vector3>    mPosition;      ///< Local transform; distance between parent-origin & anchor-point
1037   AnimatableProperty<Quaternion> mRotation;      ///< Local transform; rotation relative to parent node
1038   AnimatableProperty<Vector3>    mScale;         ///< Local transform; scale relative to parent node
1039   AnimatableProperty<bool>       mVisible;       ///< Visibility can be inherited from the Node hierachy
1040   AnimatableProperty<Vector4>    mColor;         ///< Color can be inherited from the Node hierarchy
1041
1042   // Inherited properties; read-only from public API
1043
1044   InheritedProperty<Vector3>    mWorldPosition; ///< Full inherited position
1045   InheritedProperty<Quaternion> mWorldRotation; ///< Full inherited rotation
1046   InheritedProperty<Vector3>    mWorldScale;    ///< Full inherited scale
1047   InheritedProperty<Matrix>     mWorldMatrix;   ///< Full inherited world matrix
1048   InheritedColor                mWorldColor;    ///< Full inherited color
1049
1050 protected:
1051
1052   Node*               mParent;                       ///< Pointer to parent node (a child is owned by its parent)
1053   RenderTask*         mExclusiveRenderTask;          ///< Nodes can be marked as exclusive to a single RenderTask
1054
1055   NodeAttachmentOwner mAttachment;                   ///< Optional owned attachment
1056   NodeContainer       mChildren;                     ///< Container of children; not owned
1057
1058   Vector3             mGeometryScale;                ///< Applied before calculating world transform.
1059   Vector3             mInitialVolume;                ///< Initial volume... TODO - need a better name
1060
1061   // flags, compressed to bitfield
1062   int  mDirtyFlags:10;                               ///< A composite set of flags for each of the Node properties
1063
1064   bool mIsRoot:1;                                    ///< True if the node cannot have a parent
1065   bool mInheritRotation:1;                           ///< Whether the parent's rotation should be inherited.
1066   bool mInheritScale:1;                              ///< Whether the parent's scale should be inherited.
1067   bool mTransmitGeometryScaling:1;                   ///< Whether geometry scaling should be applied to world transform.
1068   bool mInhibitLocalTransform:1;                     ///< whether local transform should be applied.
1069   bool mIsActive:1;                                  ///< When a Node is marked "active" it has been disconnected, and its properties have not been modified
1070
1071   DrawMode::Type          mDrawMode:2;               ///< How the Node and its children should be drawn
1072   PositionInheritanceMode mPositionInheritanceMode:2;///< Determines how position is inherited, 2 bits is enough
1073   ColorMode               mColorMode:2;              ///< Determines whether mWorldColor is inherited, 2 bits is enough
1074
1075   // Changes scope, should be at end of class
1076   DALI_LOG_OBJECT_STRING_DECLARATION;
1077 };
1078
1079 // Messages for Node
1080
1081 inline void SetInheritRotationMessage( EventToUpdate& eventToUpdate, const Node& node, bool inherit )
1082 {
1083   typedef MessageValue1< Node, bool > LocalType;
1084
1085   // Reserve some memory inside the message queue
1086   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
1087
1088   // Construct message in the message queue memory; note that delete should not be called on the return value
1089   new (slot) LocalType( &node, &Node::SetInheritRotation, inherit );
1090 }
1091
1092 inline void SetInitialVolumeMessage( EventToUpdate& eventToUpdate, const Node& node, const Vector3& initialVolume )
1093 {
1094   typedef MessageValue1< Node, Vector3 > LocalType;
1095
1096   // Reserve some memory inside the message queue
1097   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
1098
1099   // Construct message in the message queue memory; note that delete should not be called on the return value
1100   new (slot) LocalType( &node, &Node::SetInitialVolume, initialVolume );
1101 }
1102
1103 inline void SetTransmitGeometryScalingMessage( EventToUpdate& eventToUpdate, const Node& node, bool transmitGeometryScaling )
1104 {
1105   typedef MessageValue1< Node, bool > LocalType;
1106
1107   // Reserve some memory inside the message queue
1108   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
1109
1110   // Construct message in the message queue memory; note that delete should not be called on the return value
1111   new (slot) LocalType( &node, &Node::SetTransmitGeometryScaling, transmitGeometryScaling );
1112 }
1113
1114 inline void SetParentOriginMessage( EventToUpdate& eventToUpdate, const Node& node, const Vector3& origin )
1115 {
1116   typedef MessageValue1< Node, Vector3 > LocalType;
1117
1118   // Reserve some memory inside the message queue
1119   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
1120
1121   // Construct message in the message queue memory; note that delete should not be called on the return value
1122   new (slot) LocalType( &node, &Node::SetParentOrigin, origin );
1123 }
1124
1125 inline void SetAnchorPointMessage( EventToUpdate& eventToUpdate, const Node& node, const Vector3& anchor )
1126 {
1127   typedef MessageValue1< Node, Vector3 > LocalType;
1128
1129   // Reserve some memory inside the message queue
1130   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
1131
1132   // Construct message in the message queue memory; note that delete should not be called on the return value
1133   new (slot) LocalType( &node, &Node::SetAnchorPoint, anchor );
1134 }
1135
1136 inline void SetPositionInheritanceModeMessage( EventToUpdate& eventToUpdate, const Node& node, PositionInheritanceMode mode )
1137 {
1138   typedef MessageValue1< Node, PositionInheritanceMode > LocalType;
1139
1140   // Reserve some memory inside the message queue
1141   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
1142
1143   // Construct message in the message queue memory; note that delete should not be called on the return value
1144   new (slot) LocalType( &node, &Node::SetPositionInheritanceMode, mode );
1145 }
1146
1147 inline void SetInheritScaleMessage( EventToUpdate& eventToUpdate, const Node& node, bool inherit )
1148 {
1149   typedef MessageValue1< Node, bool > LocalType;
1150
1151   // Reserve some memory inside the message queue
1152   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
1153
1154   // Construct message in the message queue memory; note that delete should not be called on the return value
1155   new (slot) LocalType( &node, &Node::SetInheritScale, inherit );
1156 }
1157
1158 inline void SetColorModeMessage( EventToUpdate& eventToUpdate, const Node& node, ColorMode colorMode )
1159 {
1160   typedef MessageValue1< Node, ColorMode > LocalType;
1161
1162   // Reserve some memory inside the message queue
1163   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
1164
1165   // Construct message in the message queue memory; note that delete should not be called on the return value
1166   new (slot) LocalType( &node, &Node::SetColorMode, colorMode );
1167 }
1168
1169 inline void SetDrawModeMessage( EventToUpdate& eventToUpdate, const Node& node, DrawMode::Type drawMode )
1170 {
1171   typedef MessageValue1< Node, DrawMode::Type > LocalType;
1172
1173   // Reserve some memory inside the message queue
1174   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
1175
1176   // Construct message in the message queue memory; note that delete should not be called on the return value
1177   new (slot) LocalType( &node, &Node::SetDrawMode, drawMode );
1178 }
1179
1180 } // namespace SceneGraph
1181
1182 } // namespace Internal
1183
1184 } // namespace Dali
1185
1186 #endif // __DALI_INTERNAL_SCENE_GRAPH_NODE_H_