Merge branch 'tizen' of platform/core/uifw/dali-core into devel/new_mesh
[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/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
40 namespace Dali
41 {
42
43 namespace Internal
44 {
45
46 // value types used by messages
47 template <> struct ParameterType< ColorMode > : public BasicType< ColorMode > {};
48 template <> struct ParameterType< PositionInheritanceMode > : public BasicType< PositionInheritanceMode > {};
49 template <> struct ParameterType< SizeMode > : public BasicType< SizeMode > {};
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    * Retreive the object attached to this node.
178    * @return The attachment.
179    */
180   NodeAttachment& GetAttachment() const
181   {
182     return *mAttachment;
183   }
184
185   // Containment methods
186
187   /**
188    * Query whether a node is the root node. Root nodes cannot have a parent node.
189    * A node becomes a root node, when it is installed by UpdateManager.
190    * @return True if the node is a root node.
191    */
192   bool IsRoot() const
193   {
194     return mIsRoot;
195   }
196
197   /**
198    * Set whether a node is the root node. Root nodes cannot have a parent node.
199    * This method should only be called by UpdateManager.
200    * @pre When isRoot is true, the node must not have a parent.
201    * @param[in] isRoot Whether the node is now a root node.
202    */
203   void SetRoot(bool isRoot);
204
205   /**
206    * Retrieve the parent of a Node.
207    * @return The parent node, or NULL if the Node has not been added to the scene-graph.
208    */
209   Node* GetParent()
210   {
211     return mParent;
212   }
213
214   /**
215    * Retrieve the parent of a Node.
216    * @return The parent node, or NULL if the Node has not been added to the scene-graph.
217    */
218   const Node* GetParent() const
219   {
220     return mParent;
221   }
222
223   /**
224    * Connect a node to the scene-graph.
225    * @pre A node cannot be added to itself.
226    * @pre The parent node is connected to the scene-graph.
227    * @pre The childNode does not already have a parent.
228    * @pre The childNode is not a root node.
229    * @param[in] childNode The child to add.
230    * @param[in] index to insert at, if not supplied or -1 it will be appended
231    *
232    */
233   void ConnectChild( Node* childNode, int index = -1);
234
235   /**
236    * Disconnect a child (& its children) from the scene-graph.
237    * @pre childNode is a child of this Node.
238    * @param[in] updateBufferIndex The current update buffer index.
239    * @param[in] childNode The node to disconnect.
240    * @param[in] connectedNodes Disconnected Node attachments should be removed from here.
241    * @param[in] disconnectedNodes Disconnected Node attachments should be added here.
242    */
243   void DisconnectChild( BufferIndex updateBufferIndex, Node& childNode, std::set<Node*>& connectedNodes,  std::set<Node*>& disconnectedNodes );
244
245   /**
246    * Retrieve the children a Node.
247    * @return The container of children.
248    */
249   const NodeContainer& GetChildren() const
250   {
251     return mChildren;
252   }
253
254   /**
255    * Retrieve the children a Node.
256    * @return The container of children.
257    */
258   NodeContainer& GetChildren()
259   {
260     return mChildren;
261   }
262
263   // Update methods
264
265   /**
266    * Flag that one of the node values has changed in the current frame.
267    * @param[in] flag The flag to set.
268    */
269   void SetDirtyFlag(NodePropertyFlags flag)
270   {
271     mDirtyFlags |= flag;
272   }
273
274   /**
275    * Flag that all of the node values are dirty.
276    */
277   void SetAllDirtyFlags()
278   {
279     mDirtyFlags = AllFlags;
280   }
281
282   /**
283    * Query whether a node is dirty.
284    * @return The dirty flags
285    */
286   int GetDirtyFlags() const;
287
288   /**
289    * Query whether a node is clean.
290    * @return True if the node is clean.
291    */
292   bool IsClean() const
293   {
294     return ( NothingFlag == GetDirtyFlags() );
295   }
296
297   /**
298    * Retrieve the parent-origin of the node.
299    * @return The parent-origin.
300    */
301   const Vector3& GetParentOrigin() const
302   {
303     return mParentOrigin.mValue;
304   }
305
306   /**
307    * Sets both the local & base parent-origins of the node.
308    * @param[in] origin The new local & base parent-origins.
309    */
310   void SetParentOrigin(const Vector3& origin)
311   {
312     mParentOrigin.mValue = origin;
313     mParentOrigin.OnSet();
314   }
315
316   /**
317    * Retrieve the anchor-point of the node.
318    * @return The anchor-point.
319    */
320   const Vector3& GetAnchorPoint() const
321   {
322     return mAnchorPoint.mValue;
323   }
324
325   /**
326    * Sets both the local & base anchor-points of the node.
327    * @param[in] anchor The new local & base anchor-points.
328    */
329   void SetAnchorPoint(const Vector3& anchor)
330   {
331     mAnchorPoint.mValue = anchor;
332     mAnchorPoint.OnSet();
333   }
334
335   /**
336    * Retrieve the local position of the node, relative to its parent.
337    * @param[in] bufferIndex The buffer to read from.
338    * @return The local position.
339    */
340   const Vector3& GetPosition(BufferIndex bufferIndex) const
341   {
342     return mPosition[bufferIndex];
343   }
344
345   /**
346    * Sets both the local & base positions of the node.
347    * @param[in] updateBufferIndex The current update buffer index.
348    * @param[in] position The new local & base position.
349    */
350   void BakePosition(BufferIndex updateBufferIndex, const Vector3& position)
351   {
352     mPosition.Bake( updateBufferIndex, position );
353   }
354
355   /**
356    * Sets the world of the node derived from the position of all its parents.
357    * @param[in] updateBufferIndex The current update buffer index.
358    * @param[in] position The world position.
359    */
360   void SetWorldPosition( BufferIndex updateBufferIndex, const Vector3& position )
361   {
362     mWorldPosition.Set( updateBufferIndex, position );
363   }
364
365   /**
366    * Sets the position of the node derived from the position of all its parents.
367    * This method should only be called when the parent's world position is up-to-date.
368    * With a non-central anchor-point, the local orientation and scale affects the world position.
369    * Therefore the world orientation & scale must be updated before the world position.
370    * @pre The node has a parent.
371    * @param[in] updateBufferIndex The current update buffer index.
372    */
373   void InheritWorldPosition(BufferIndex updateBufferIndex)
374   {
375     DALI_ASSERT_DEBUG(mParent != NULL);
376
377     switch( mPositionInheritanceMode )
378     {
379       case INHERIT_PARENT_POSITION  : ///@see Dali::PositionInheritanceMode for how these modes are expected to work
380       {
381         Vector3 finalPosition(-0.5f, -0.5f, -0.5f);
382
383         finalPosition += mParentOrigin.mValue;
384         finalPosition *= mParent->GetSize(updateBufferIndex);
385         finalPosition += mPosition[updateBufferIndex];
386         finalPosition *= mParent->GetWorldScale(updateBufferIndex);
387         const Quaternion& parentWorldOrientation = mParent->GetWorldOrientation(updateBufferIndex);
388         if(!parentWorldOrientation.IsIdentity())
389         {
390           finalPosition *= parentWorldOrientation;
391         }
392
393         // check if a node needs to be offsetted locally (only applies when AnchorPoint is not central)
394         // dont use operator== as that does a slower comparison (and involves function calls)
395         Vector3 localOffset(0.5f, 0.5f, 0.5f);    // AnchorPoint::CENTER
396         localOffset -= mAnchorPoint.mValue;
397
398         if( ( fabsf( localOffset.x ) >= Math::MACHINE_EPSILON_0 ) ||
399             ( fabsf( localOffset.y ) >= Math::MACHINE_EPSILON_0 ) ||
400             ( fabsf( localOffset.z ) >= Math::MACHINE_EPSILON_0 ) )
401         {
402           localOffset *= mSize[updateBufferIndex];
403
404           Vector3 scale = mWorldScale[updateBufferIndex];
405
406           // 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    * Retrieve the local scale of the node, relative to its parent.
595    * @param[in] bufferIndex The buffer to read from.
596    * @return The local scale.
597    */
598   const Vector3& GetScale(BufferIndex bufferIndex) const
599   {
600     return mScale[bufferIndex];
601   }
602
603   /**
604    * Sets the scale of the node derived from the scale of all its parents and a pre-scale
605    * @param[in] updateBufferIndex The current update buffer index.
606    * @param[in] scale The world scale.
607    */
608   void SetWorldScale(BufferIndex updateBufferIndex, const Vector3& scale)
609   {
610     mWorldScale.Set( updateBufferIndex, scale );
611   }
612
613   /**
614    * Sets the scale of the node derived from the scale of all its parents and a pre-scale.
615    * This method should only be called when the parents world scale is up-to-date.
616    * @pre The node has a parent.
617    * @param[in] updateBufferIndex The current update buffer index.
618    */
619   void InheritWorldScale(BufferIndex updateBufferIndex)
620   {
621     DALI_ASSERT_DEBUG(mParent != NULL);
622
623     mWorldScale.Set( updateBufferIndex, mParent->GetWorldScale(updateBufferIndex) * mScale[updateBufferIndex] );
624   }
625
626   /**
627    * Copies the previous inherited scale, if this changed in the previous frame.
628    * This method should be called instead of InheritWorldScale i.e. if the inherited scale
629    * does not need to be recalculated in the current frame.
630    * @param[in] updateBufferIndex The current update buffer index.
631    */
632   void CopyPreviousWorldScale( BufferIndex updateBufferIndex )
633   {
634     mWorldScale.CopyPrevious( updateBufferIndex );
635   }
636
637   /**
638    * Retrieve the scale of the node derived from the scale of all its parents.
639    * @param[in] bufferIndex The buffer to read from.
640    * @return The world scale.
641    */
642   const Vector3& GetWorldScale( BufferIndex bufferIndex ) const
643   {
644     return mWorldScale[bufferIndex];
645   }
646
647   /**
648    * Set whether the Node inherits scale.
649    * @param inherit True if the Node inherits scale.
650    */
651   void SetInheritScale( bool inherit )
652   {
653     if( inherit != mInheritScale )
654     {
655       mInheritScale = inherit;
656
657       SetDirtyFlag( TransformFlag );
658     }
659   }
660
661   /**
662    * Query whether the Node inherits scale.
663    * @return if scale is inherited
664    */
665   bool IsScaleInherited() const
666   {
667     return mInheritScale;
668   }
669
670   /**
671    * Copies the previously used size, if this changed in the previous frame.
672    * @param[in] updateBufferIndex The current update buffer index.
673    */
674   void CopyPreviousSize( BufferIndex updateBufferIndex )
675   {
676     SetSize( updateBufferIndex, GetSize( 1u - updateBufferIndex ) );
677   }
678
679   /**
680    * Retrieve the visibility of the node.
681    * @param[in] bufferIndex The buffer to read from.
682    * @return True if the node is visible.
683    */
684   bool IsVisible(BufferIndex bufferIndex) const
685   {
686     return mVisible[bufferIndex];
687   }
688
689   /**
690    * Retrieves whether a node is fully visible.
691    * A node is fully visible if is visible and all its ancestors are visible.
692    * @param[in] updateBufferIndex The current update buffer index.
693    * @return True if the node is fully visible.
694    */
695   bool IsFullyVisible( BufferIndex updateBufferIndex ) const;
696
697   /**
698    * Retrieve the opacity of the node.
699    * @param[in] bufferIndex The buffer to read from.
700    * @return The opacity.
701    */
702   float GetOpacity(BufferIndex bufferIndex) const
703   {
704     return mColor[bufferIndex].a;
705   }
706
707   /**
708    * Retrieve the color of the node.
709    * @param[in] bufferIndex The buffer to read from.
710    * @return The color.
711    */
712   const Vector4& GetColor(BufferIndex bufferIndex) const
713   {
714     return mColor[bufferIndex];
715   }
716
717   /**
718    * Sets the color of the node derived from the color of all its parents.
719    * @param[in] color The world color.
720    * @param[in] updateBufferIndex The current update buffer index.
721    */
722   void SetWorldColor(const Vector4& color, BufferIndex updateBufferIndex)
723   {
724     mWorldColor.Set( updateBufferIndex, color );
725   }
726
727   /**
728    * Sets the color of the node derived from the color of all its parents.
729    * This method should only be called when the parents world color is up-to-date.
730    * @pre The node has a parent.
731    * @param[in] updateBufferIndex The current update buffer index.
732    */
733   void InheritWorldColor( BufferIndex updateBufferIndex )
734   {
735     DALI_ASSERT_DEBUG(mParent != NULL);
736
737     // default first
738     if( mColorMode == USE_OWN_MULTIPLY_PARENT_ALPHA )
739     {
740       const Vector4& ownColor = mColor[updateBufferIndex];
741       mWorldColor.Set( updateBufferIndex, ownColor.r, ownColor.g, ownColor.b, ownColor.a * mParent->GetWorldColor(updateBufferIndex).a );
742     }
743     else if( mColorMode == USE_OWN_MULTIPLY_PARENT_COLOR )
744     {
745       mWorldColor.Set( updateBufferIndex, mParent->GetWorldColor(updateBufferIndex) * mColor[updateBufferIndex] );
746     }
747     else if( mColorMode == USE_PARENT_COLOR )
748     {
749       mWorldColor.Set( updateBufferIndex, mParent->GetWorldColor(updateBufferIndex) );
750     }
751     else // USE_OWN_COLOR
752     {
753       mWorldColor.Set( updateBufferIndex, mColor[updateBufferIndex] );
754     }
755   }
756
757   /**
758    * Copies the previous inherited scale, if this changed in the previous frame.
759    * This method should be called instead of InheritWorldScale i.e. if the inherited scale
760    * does not need to be recalculated in the current frame.
761    * @param[in] updateBufferIndex The current update buffer index.
762    */
763   void CopyPreviousWorldColor( BufferIndex updateBufferIndex )
764   {
765     mWorldColor.CopyPrevious( updateBufferIndex );
766   }
767
768   /**
769    * Retrieve the color of the node, possibly derived from the color
770    * of all its parents, depending on the value of mColorMode.
771    * @param[in] bufferIndex The buffer to read from.
772    * @return The world color.
773    */
774   const Vector4& GetWorldColor(BufferIndex bufferIndex) const
775   {
776     return mWorldColor[bufferIndex];
777   }
778
779   /**
780    * Set the color mode. This specifies whether the Node uses its own color,
781    * or inherits its parent color.
782    * @param[in] colorMode The new color mode.
783    */
784   void SetColorMode(ColorMode colorMode)
785   {
786     mColorMode = colorMode;
787
788     SetDirtyFlag(ColorFlag);
789   }
790
791   /**
792    * Retrieve the color mode.
793    * @return The color mode.
794    */
795   ColorMode GetColorMode() const
796   {
797     return mColorMode;
798   }
799
800   /**
801    * Sets the size of the node.
802    * @param[in] bufferIndex The buffer to write to.
803    * @param[in] size The size to write.
804    */
805   void SetSize( BufferIndex bufferIndex, const Vector3& size )
806   {
807     mSize[bufferIndex] = size;
808   }
809
810   /**
811    * Retrieve the size of the node.
812    * @param[in] bufferIndex The buffer to read from.
813    * @return The size.
814    */
815   const Vector3& GetSize(BufferIndex bufferIndex) const
816   {
817     return mSize[bufferIndex];
818   }
819
820   /**
821    * Set the world-matrix of a node, with scale + rotation + translation.
822    * Scale and rotation are centered at the origin.
823    * Translation is applied independently of the scale or rotatation axis.
824    * @param[in] updateBufferIndex The current update buffer index.
825    * @param[in] scale The scale.
826    * @param[in] rotation The rotation.
827    * @param[in] translation The translation.
828    */
829   void SetWorldMatrix( BufferIndex updateBufferIndex, const Vector3& scale, const Quaternion& rotation, const Vector3& translation )
830   {
831     mWorldMatrix.Get( updateBufferIndex ).SetTransformComponents( scale, rotation, translation );
832     mWorldMatrix.SetDirty( updateBufferIndex );
833   }
834
835   /**
836    * Retrieve the cached world-matrix of a node.
837    * @param[in] bufferIndex The buffer to read from.
838    * @return The world-matrix.
839    */
840   const Matrix& GetWorldMatrix( BufferIndex bufferIndex ) const
841   {
842     return mWorldMatrix[ bufferIndex ];
843   }
844
845   /**
846    * Copy previous frames world matrix
847    * @param[in] updateBufferIndex The current update buffer index.
848    */
849   void CopyPreviousWorldMatrix( BufferIndex updateBufferIndex )
850   {
851     mWorldMatrix.CopyPrevious( updateBufferIndex );
852   }
853
854   /**
855    * Mark the node as exclusive to a single RenderTask.
856    * @param[in] renderTask The render-task, or NULL if the Node is not exclusive to a single RenderTask.
857    */
858   void SetExclusiveRenderTask( RenderTask* renderTask )
859   {
860     mExclusiveRenderTask = renderTask;
861   }
862
863   /**
864    * Query whether the node is exclusive to a single RenderTask.
865    * @return The render-task, or NULL if the Node is not exclusive to a single RenderTask.
866    */
867   RenderTask* GetExclusiveRenderTask() const
868   {
869     return mExclusiveRenderTask;
870   }
871
872   /**
873    * Set how the Node and its children should be drawn; see Dali::Actor::SetDrawMode() for more details.
874    * @param[in] drawMode The new draw-mode to use.
875    */
876   void SetDrawMode( const DrawMode::Type& drawMode )
877   {
878     mDrawMode = drawMode;
879   }
880
881   /**
882    * Returns whether node is an overlay or not.
883    * @return True if node is an overlay, false otherwise.
884    */
885   DrawMode::Type GetDrawMode() const
886   {
887     return mDrawMode;
888   }
889
890   /**
891    * Equality operator, checks for identity, not values.
892    *
893    */
894   bool operator==( const Node* rhs ) const
895   {
896     if ( this == rhs )
897     {
898       return true;
899     }
900     return false;
901   }
902
903   /**
904    * Set the inhibit local transform flag.@n
905    * Setting this flag will stop the node's local transform (position, scale and orientation)
906    * being applied on top of its parents transformation.
907    * @param[in] flag When true, local transformation is inhibited when calculating the world matrix.
908    */
909   void SetInhibitLocalTransform( bool flag )
910   {
911     SetDirtyFlag( TransformFlag );
912     mInhibitLocalTransform = flag;
913   }
914
915   /**
916    * Get the inhibit local transform flag.@n
917    * See @ref SetInhibitLocalTransform
918    * @result A flag, when true, local transformation is inhibited when calculating the world matrix.
919    */
920   bool GetInhibitLocalTransform() const
921   {
922     return mInhibitLocalTransform;
923   }
924
925 protected:
926
927   /**
928    * Set the parent of a Node.
929    * @param[in] parentNode the new parent.
930    */
931   void SetParent(Node& parentNode);
932
933   /**
934    * Protected constructor; See also Node::New()
935    */
936   Node();
937
938 private: // from NodeDataProvider
939
940   /**
941    * @copydoc NodeDataProvider::GetModelMatrix
942    */
943   virtual const Matrix& GetModelMatrix( unsigned int bufferId )
944   {
945     return GetWorldMatrix( bufferId );
946   }
947
948   /**
949    * @copydoc NodeDataProvider::GetRenderColor
950    */
951   virtual const Vector4& GetRenderColor( unsigned int bufferId )
952   {
953     return GetWorldColor( bufferId );
954   }
955
956   virtual const Vector3& GetRenderSize( unsigned int bufferId )
957   {
958     return GetSize( bufferId );
959   }
960
961
962 private:
963
964   // Undefined
965   Node(const Node&);
966
967   // Undefined
968   Node& operator=(const Node& rhs);
969
970   /**
971    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
972    */
973   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
974
975   /**
976    * Recursive helper to disconnect a Node and its children.
977    * Disconnected Nodes have no parent or children.
978    * @param[in] updateBufferIndex The current update buffer index.
979    * @param[in] connectedNodes Disconnected Node attachments should be removed from here.
980    * @param[in] disconnectedNodes Disconnected Node attachments should be added here.
981    */
982   void RecursiveDisconnectFromSceneGraph( BufferIndex updateBufferIndex, std::set<Node*>& connectedNodes, std::set<Node*>& disconnectedNodes );
983
984 public: // Default properties
985
986   PropertyVector3                mParentOrigin;  ///< Local transform; the position is relative to this. Sets the TransformFlag dirty when changed
987   PropertyVector3                mAnchorPoint;   ///< Local transform; local center of rotation. Sets the TransformFlag dirty when changed
988
989   AnimatableProperty<Vector3>    mSize;          ///< Size is provided for layouting
990   AnimatableProperty<Vector3>    mPosition;      ///< Local transform; distance between parent-origin & anchor-point
991   AnimatableProperty<Quaternion> mOrientation;   ///< Local transform; rotation relative to parent node
992   AnimatableProperty<Vector3>    mScale;         ///< Local transform; scale relative to parent node
993   AnimatableProperty<bool>       mVisible;       ///< Visibility can be inherited from the Node hierachy
994   AnimatableProperty<Vector4>    mColor;         ///< Color can be inherited from the Node hierarchy
995
996   // Inherited properties; read-only from public API
997
998   InheritedVector3    mWorldPosition;     ///< Full inherited position
999   InheritedQuaternion mWorldOrientation;  ///< Full inherited orientation
1000   InheritedVector3    mWorldScale;        ///< Full inherited scale
1001   InheritedMatrix     mWorldMatrix;       ///< Full inherited world matrix
1002   InheritedColor      mWorldColor;        ///< Full inherited color
1003
1004 protected:
1005
1006   Node*               mParent;                       ///< Pointer to parent node (a child is owned by its parent)
1007   RenderTask*         mExclusiveRenderTask;          ///< Nodes can be marked as exclusive to a single RenderTask
1008
1009   NodeAttachmentOwner mAttachment;                   ///< Optional owned attachment
1010   NodeContainer       mChildren;                     ///< Container of children; not owned
1011
1012
1013   // flags, compressed to bitfield
1014   int  mDirtyFlags:10;                               ///< A composite set of flags for each of the Node properties
1015
1016   bool mIsRoot:1;                                    ///< True if the node cannot have a parent
1017   bool mInheritOrientation:1;                        ///< Whether the parent's orientation should be inherited.
1018   bool mInheritScale:1;                              ///< Whether the parent's scale should be inherited.
1019   bool mInhibitLocalTransform:1;                     ///< whether local transform should be applied.
1020   bool mIsActive:1;                                  ///< When a Node is marked "active" it has been disconnected, and its properties have not been modified
1021
1022   DrawMode::Type          mDrawMode:2;               ///< How the Node and its children should be drawn
1023   PositionInheritanceMode mPositionInheritanceMode:2;///< Determines how position is inherited, 2 bits is enough
1024   ColorMode               mColorMode:2;              ///< Determines whether mWorldColor is inherited, 2 bits is enough
1025
1026   // Changes scope, should be at end of class
1027   DALI_LOG_OBJECT_STRING_DECLARATION;
1028 };
1029
1030 // Messages for Node
1031
1032 inline void SetInheritOrientationMessage( EventThreadServices& eventThreadServices, const Node& node, bool inherit )
1033 {
1034   typedef MessageValue1< Node, bool > LocalType;
1035
1036   // Reserve some memory inside the message queue
1037   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
1038
1039   // Construct message in the message queue memory; note that delete should not be called on the return value
1040   new (slot) LocalType( &node, &Node::SetInheritOrientation, inherit );
1041 }
1042
1043
1044
1045 inline void SetParentOriginMessage( EventThreadServices& eventThreadServices, const Node& node, const Vector3& origin )
1046 {
1047   typedef MessageValue1< Node, Vector3 > LocalType;
1048
1049   // Reserve some memory inside the message queue
1050   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
1051
1052   // Construct message in the message queue memory; note that delete should not be called on the return value
1053   new (slot) LocalType( &node, &Node::SetParentOrigin, origin );
1054 }
1055
1056 inline void SetAnchorPointMessage( EventThreadServices& eventThreadServices, const Node& node, const Vector3& anchor )
1057 {
1058   typedef MessageValue1< Node, Vector3 > LocalType;
1059
1060   // Reserve some memory inside the message queue
1061   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
1062
1063   // Construct message in the message queue memory; note that delete should not be called on the return value
1064   new (slot) LocalType( &node, &Node::SetAnchorPoint, anchor );
1065 }
1066
1067 inline void SetPositionInheritanceModeMessage( EventThreadServices& eventThreadServices, const Node& node, PositionInheritanceMode mode )
1068 {
1069   typedef MessageValue1< Node, PositionInheritanceMode > LocalType;
1070
1071   // Reserve some memory inside the message queue
1072   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
1073
1074   // Construct message in the message queue memory; note that delete should not be called on the return value
1075   new (slot) LocalType( &node, &Node::SetPositionInheritanceMode, mode );
1076 }
1077
1078 inline void SetInheritScaleMessage( EventThreadServices& eventThreadServices, const Node& node, bool inherit )
1079 {
1080   typedef MessageValue1< Node, bool > LocalType;
1081
1082   // Reserve some memory inside the message queue
1083   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
1084
1085   // Construct message in the message queue memory; note that delete should not be called on the return value
1086   new (slot) LocalType( &node, &Node::SetInheritScale, inherit );
1087 }
1088
1089 inline void SetColorModeMessage( EventThreadServices& eventThreadServices, const Node& node, ColorMode colorMode )
1090 {
1091   typedef MessageValue1< Node, ColorMode > LocalType;
1092
1093   // Reserve some memory inside the message queue
1094   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
1095
1096   // Construct message in the message queue memory; note that delete should not be called on the return value
1097   new (slot) LocalType( &node, &Node::SetColorMode, colorMode );
1098 }
1099
1100 inline void SetDrawModeMessage( EventThreadServices& eventThreadServices, const Node& node, DrawMode::Type drawMode )
1101 {
1102   typedef MessageValue1< Node, DrawMode::Type > LocalType;
1103
1104   // Reserve some memory inside the message queue
1105   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
1106
1107   // Construct message in the message queue memory; note that delete should not be called on the return value
1108   new (slot) LocalType( &node, &Node::SetDrawMode, drawMode );
1109 }
1110
1111 } // namespace SceneGraph
1112
1113 } // namespace Internal
1114
1115 } // namespace Dali
1116
1117 #endif // __DALI_INTERNAL_SCENE_GRAPH_NODE_H_