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