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