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