[dali_1.0.20] Merge branch 'tizen'
[platform/core/uifw/dali-core.git] / dali / internal / update / common / inherited-property.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_INHERITED_PROPERTY_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_INHERITED_PROPERTY_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/internal/event/common/property-input-impl.h>
23 #include <dali/internal/update/common/double-buffered.h>
24 #include <dali/internal/update/common/property-base.h>
25 #include <dali/internal/update/common/scene-graph-buffers.h>
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/public-api/object/property.h>
28 #include <dali/public-api/object/property-input.h>
29 #include <dali/public-api/object/property-types.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace SceneGraph
38 {
39
40 /**
41  * An inherited Vector3 property.
42  */
43 class InheritedVector3 : public PropertyInputImpl
44 {
45 public:
46
47   /**
48    * Create an inherited Vector3.
49    */
50   InheritedVector3()
51   : mValue(),
52     mInheritedFlag( false ),
53     mReinheritedFlag( true )
54   {
55   }
56
57   /**
58    * Create an inherited Vector3.
59    * @param [in] initialValue The initial value of the property.
60    */
61   InheritedVector3( const Vector3& initialValue )
62   : mValue( initialValue ),
63     mInheritedFlag( false ),
64     mReinheritedFlag( true )
65   {
66   }
67   /**
68    * Virtual destructor.
69    */
70   virtual ~InheritedVector3()
71   {
72   }
73
74   /**
75    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
76    */
77   virtual Dali::Property::Type GetType() const
78   {
79     return Dali::PropertyTypes::Get<Vector3>();
80   }
81
82   /**
83    * Called once per Update (only) if the property did not need to be re-inherited.
84    * @param[in] updateBufferIndex The current update buffer index.
85    */
86   void CopyPrevious( BufferIndex updateBufferIndex )
87   {
88     if ( mReinheritedFlag )
89     {
90       mValue[updateBufferIndex] = mValue[updateBufferIndex ? 0 : 1];
91
92       mReinheritedFlag = false;
93     }
94   }
95
96   /**
97    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
98    */
99   virtual bool IsClean() const
100   {
101     return ( false == mReinheritedFlag );
102   }
103
104   /**
105    * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
106    */
107   virtual bool InputInitialized() const
108   {
109     // A constraint cannot use the property until it has been inherited (at least once).
110     return mInheritedFlag;
111   }
112
113   /**
114    * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
115    * @note A constraint can only receive the inherited property from the previous frame.
116    */
117   virtual bool InputChanged() const
118   {
119     return !IsClean();
120   }
121
122   /**
123    * @copydoc Dali::PropertyInput::GetVector3()
124    */
125   virtual const Vector3& GetVector3( BufferIndex bufferIndex ) const
126   {
127     return mValue[ bufferIndex ];
128   }
129
130   /**
131    * @copydoc Dali::PropertyInput::GetConstraintInputVector3()
132    */
133   virtual const Vector3& GetConstraintInputVector3( BufferIndex bufferIndex ) const
134   {
135     // For inherited properties, constraints work with the value from the previous frame.
136     // This is because constraints are applied to position etc, before world-position is calculated.
137     BufferIndex eventBufferIndex = bufferIndex ? 0u : 1u;
138
139     return mValue[ eventBufferIndex ];
140   }
141
142   /**
143    * Set the property value. This will only persist for the current frame; the property
144    * will be reset with the base value, at the beginning of the next frame.
145    * @param[in] bufferIndex The buffer to write.
146    * @param[in] value The new property value.
147    */
148   void Set(BufferIndex bufferIndex, const Vector3& value)
149   {
150     mValue[bufferIndex] = value;
151
152     // The value has been inherited for the first time
153     mInheritedFlag = true;
154
155     mReinheritedFlag = true;
156   }
157
158   /**
159    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
160    */
161   Vector3& Get(size_t bufferIndex)
162   {
163     return mValue[bufferIndex];
164   }
165
166   /**
167    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
168    */
169   const Vector3& Get(size_t bufferIndex) const
170   {
171     return mValue[bufferIndex];
172   }
173
174   /**
175    * Retrieve the property value.
176    * @param[in] bufferIndex The buffer to read.
177    * @return The property value.
178    */
179   const Vector3& operator[](size_t bufferIndex) const
180   {
181     return mValue[bufferIndex];
182   }
183
184 private:
185
186   // Undefined
187   InheritedVector3(const InheritedVector3& property);
188
189   // Undefined
190   InheritedVector3& operator=(const InheritedVector3& rhs);
191
192 private:
193
194   DoubleBuffered<Vector3> mValue; ///< The double-buffered property value
195
196   bool mInheritedFlag   :1; ///< Flag whether the value has ever been inherited
197   bool mReinheritedFlag :1; ///< Flag whether value was re-inherited in previous frame
198 };
199
200 /**
201  * An inherited Color property.
202  */
203 class InheritedColor : public PropertyInputImpl
204 {
205 public:
206
207   /**
208    * Create an inherited property.
209    * @param [in] initialValue The initial value of the property.
210    */
211   InheritedColor( const Vector4& initialValue )
212   : mValue( initialValue ),
213     mInheritedFlag( false ),
214     mReinheritedFlag( true )
215   {
216   }
217
218   /**
219    * Virtual destructor.
220    */
221   virtual ~InheritedColor()
222   {
223   }
224
225   /**
226    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
227    */
228   virtual Dali::Property::Type GetType() const
229   {
230     return Dali::PropertyTypes::Get<Vector4>();
231   }
232
233   /**
234    * Called once per Update (only) if the property did not need to be re-inherited.
235    * @param[in] updateBufferIndex The current update buffer index.
236    */
237   void CopyPrevious( BufferIndex updateBufferIndex )
238   {
239     if ( mReinheritedFlag )
240     {
241       mValue[updateBufferIndex] = mValue[updateBufferIndex ? 0 : 1];
242
243       mReinheritedFlag = false;
244     }
245   }
246
247   /**
248    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
249    */
250   virtual bool IsClean() const
251   {
252     return ( false == mReinheritedFlag );
253   }
254
255   /**
256    * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
257    */
258   virtual bool InputInitialized() const
259   {
260     // A constraint cannot use the property until it has been inherited (at least once).
261     return mInheritedFlag;
262   }
263
264   /**
265    * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
266    * @note A constraint can only receive the inherited property from the previous frame.
267    */
268   virtual bool InputChanged() const
269   {
270     return !IsClean();
271   }
272
273   /**
274    * @copydoc Dali::PropertyInput::GetVector4()
275    */
276   virtual const Vector4& GetVector4( BufferIndex bufferIndex ) const
277   {
278     return mValue[ bufferIndex ];
279   }
280
281   /**
282    * @copydoc Dali::PropertyInput::GetConstraintInputVector4()
283    */
284   virtual const Vector4& GetConstraintInputVector4( BufferIndex bufferIndex ) const
285   {
286     // For inherited properties, constraints work with the value from the previous frame.
287     // This is because constraints are applied to position etc, before world-position is calculated.
288     BufferIndex eventBufferIndex = bufferIndex ? 0u : 1u;
289
290     return mValue[ eventBufferIndex ];
291   }
292
293   /**
294    * Set the property value. This will only persist for the current frame; the property
295    * will be reset with the base value, at the beginning of the next frame.
296    * @param[in] bufferIndex The buffer to write.
297    * @param[in] value The new property value.
298    */
299   void Set(BufferIndex bufferIndex, const Vector4& value)
300   {
301     mValue[bufferIndex] = Clamp( value, 0.0f, 1.0f ); // color values are clamped between 0 and 1
302
303     // The value has been inherited for the first time
304     mInheritedFlag = true;
305     mReinheritedFlag = true;
306   }
307
308   /**
309    * Set the property value. This will only persist for the current frame; the property
310    * will be reset with the base value, at the beginning of the next frame.
311    * @param[in] bufferIndex The buffer to write.
312    * @param[in] r The new red value.
313    * @param[in] g The new green value.
314    * @param[in] b The new blue value.
315    * @param[in] a The new alpha value.
316    */
317   void Set(BufferIndex bufferIndex, float r, float g, float b, float a )
318   {
319     mValue[bufferIndex].r = Clamp( r, 0.0f, 1.0f ); // color values are clamped between 0 and 1
320     mValue[bufferIndex].g = Clamp( g, 0.0f, 1.0f ); // color values are clamped between 0 and 1
321     mValue[bufferIndex].b = Clamp( b, 0.0f, 1.0f ); // color values are clamped between 0 and 1
322     mValue[bufferIndex].a = Clamp( a, 0.0f, 1.0f ); // color values are clamped between 0 and 1
323
324     // The value has been inherited for the first time
325     mInheritedFlag = true;
326     mReinheritedFlag = true;
327   }
328
329   /**
330    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
331    */
332   Vector4& Get(size_t bufferIndex)
333   {
334     return mValue[bufferIndex];
335   }
336
337   /**
338    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
339    */
340   const Vector4& Get(size_t bufferIndex) const
341   {
342     return mValue[bufferIndex];
343   }
344
345   /**
346    * Retrieve the property value.
347    * @param[in] bufferIndex The buffer to read.
348    * @return The property value.
349    */
350   const Vector4& operator[](size_t bufferIndex) const
351   {
352     return mValue[bufferIndex];
353   }
354
355 private:
356
357   // Undefined
358   InheritedColor(const InheritedColor& property);
359   // Undefined
360   InheritedColor& operator=(const InheritedColor& rhs);
361
362 private:
363
364   DoubleBuffered<Vector4> mValue; ///< The double-buffered property value
365
366   bool mInheritedFlag   :1; ///< Flag whether the value has ever been inherited
367   bool mReinheritedFlag :1; ///< Flag whether value was re-inherited in previous frame
368
369 };
370
371 /**
372  * An inherited Quaternion property.
373  */
374 class InheritedQuaternion : public PropertyInputImpl
375 {
376 public:
377
378   /**
379    * Create an inherited property.
380    */
381   InheritedQuaternion()
382   : mValue(),
383     mInheritedFlag( false ),
384     mReinheritedFlag( true )
385   {
386   }
387
388   /**
389    * Virtual destructor.
390    */
391   virtual ~InheritedQuaternion()
392   {
393   }
394
395   /**
396    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
397    */
398   virtual Dali::Property::Type GetType() const
399   {
400     return Dali::PropertyTypes::Get<Quaternion>();
401   }
402
403   /**
404    * Called once per Update (only) if the property did not need to be re-inherited.
405    * @param[in] updateBufferIndex The current update buffer index.
406    */
407   void CopyPrevious( BufferIndex updateBufferIndex )
408   {
409     if ( mReinheritedFlag )
410     {
411       mValue[updateBufferIndex] = mValue[updateBufferIndex ? 0 : 1];
412
413       mReinheritedFlag = false;
414     }
415   }
416
417   /**
418    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
419    */
420   virtual bool IsClean() const
421   {
422     return ( false == mReinheritedFlag );
423   }
424
425   /**
426    * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
427    */
428   virtual bool InputInitialized() const
429   {
430     // A constraint cannot use the property until it has been inherited (at least once).
431     return mInheritedFlag;
432   }
433
434   /**
435    * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
436    * @note A constraint can only receive the inherited property from the previous frame.
437    */
438   virtual bool InputChanged() const
439   {
440     return !IsClean();
441   }
442
443   /**
444    * @copydoc Dali::PropertyInput::GetQuaternion()
445    */
446   virtual const Quaternion& GetQuaternion( BufferIndex bufferIndex ) const
447   {
448     return mValue[ bufferIndex ];
449   }
450
451   /**
452    * @copydoc Dali::PropertyInput::GetConstraintInputQuaternion()
453    */
454   virtual const Quaternion& GetConstraintInputQuaternion( BufferIndex bufferIndex ) const
455   {
456     // For inherited properties, constraints work with the value from the previous frame.
457     // This is because constraints are applied to position etc, before world-position is calculated.
458     BufferIndex eventBufferIndex = bufferIndex ? 0u : 1u;
459
460     return mValue[ eventBufferIndex ];
461   }
462
463   /**
464    * Set the property value. This will only persist for the current frame; the property
465    * will be reset with the base value, at the beginning of the next frame.
466    * @param[in] bufferIndex The buffer to write.
467    * @param[in] value The new property value.
468    */
469   void Set(BufferIndex bufferIndex, const Quaternion& value)
470   {
471     mValue[bufferIndex] = value;
472
473     // The value has been inherited for the first time
474     mInheritedFlag = true;
475
476     mReinheritedFlag = true;
477   }
478
479   /**
480    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
481    */
482   Quaternion& Get(size_t bufferIndex)
483   {
484     return mValue[bufferIndex];
485   }
486
487   /**
488    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
489    */
490   const Quaternion& Get(size_t bufferIndex) const
491   {
492     return mValue[bufferIndex];
493   }
494
495   /**
496    * Retrieve the property value.
497    * @param[in] bufferIndex The buffer to read.
498    * @return The property value.
499    */
500   const Quaternion& operator[](size_t bufferIndex) const
501   {
502     return mValue[bufferIndex];
503   }
504
505 private:
506
507   // Undefined
508   InheritedQuaternion(const InheritedQuaternion& property);
509
510   // Undefined
511   InheritedQuaternion& operator=(const InheritedQuaternion& rhs);
512
513 private:
514
515   DoubleBuffered<Quaternion> mValue; ///< The double-buffered property value
516
517   bool mInheritedFlag   :1;   ///< Flag whether the value has ever been inherited
518   bool mReinheritedFlag :1;   ///< Flag whether value was re-inherited in previous frame
519 };
520
521 /**
522  * An inherited Matrix property.
523  */
524 class InheritedMatrix : public PropertyInputImpl
525 {
526 public:
527
528   /**
529    * Create an inherited property.
530    * @param [in] initialValue The initial value of the property.
531    */
532   InheritedMatrix()
533   : mValue(),
534     mInheritedFlag( false ),
535     mReinheritedFlag( true )
536   {
537   }
538
539   /**
540    * Virtual destructor.
541    */
542   virtual ~InheritedMatrix()
543   {
544   }
545
546   /**
547    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
548    */
549   virtual Dali::Property::Type GetType() const
550   {
551     return Dali::PropertyTypes::Get<Matrix>();
552   }
553
554   /**
555    * Called once per Update (only) if the property did not need to be re-inherited.
556    * @param[in] updateBufferIndex The current update buffer index.
557    */
558   void CopyPrevious( BufferIndex updateBufferIndex )
559   {
560     if ( mReinheritedFlag )
561     {
562       mValue[updateBufferIndex] = mValue[updateBufferIndex ? 0 : 1];
563
564       mReinheritedFlag = false;
565     }
566   }
567
568   /**
569    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
570    */
571   virtual bool IsClean() const
572   {
573     return ( false == mReinheritedFlag );
574   }
575
576   /**
577    * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
578    */
579   virtual bool InputInitialized() const
580   {
581     // A constraint cannot use the property until it has been inherited (at least once).
582     return mInheritedFlag;
583   }
584
585   /**
586    * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
587    * @note A constraint can only receive the inherited property from the previous frame.
588    */
589   virtual bool InputChanged() const
590   {
591     return !IsClean();
592   }
593
594   /**
595    * @copydoc Dali::Internal::PropertyInputImpl::GetMatrix()
596    */
597   virtual const Matrix& GetMatrix( BufferIndex bufferIndex ) const
598   {
599     return mValue[ bufferIndex ];
600   }
601
602   /**
603    * @copydoc Dali::Internal::PropertyInputImpl::GetConstraintInputMatrix()
604    */
605   virtual const Matrix& GetConstraintInputMatrix( BufferIndex bufferIndex ) const
606   {
607     // For inherited properties, constraints work with the value from the previous frame.
608     // This is because constraints are applied to position etc, before world-position is calculated.
609     BufferIndex eventBufferIndex = bufferIndex ? 0u : 1u;
610
611     return mValue[ eventBufferIndex ];
612   }
613
614   /**
615    * @copydoc Dali::PropertyInput::GetQuaternion()
616    */
617   virtual const Quaternion& GetQuaternion() const
618   {
619     DALI_ASSERT_ALWAYS( false && "Property type mismatch" );
620     return DUMMY_QUATERNION_VALUE;
621   }
622
623   /**
624    * @copydoc Dali::PropertyInput::GetQuaternion()
625    */
626   virtual const Quaternion& GetQuaternion( BufferIndex bufferIndex ) const
627   {
628     DALI_ASSERT_ALWAYS( false && "Property type mismatch" );
629     return DUMMY_QUATERNION_VALUE;
630   }
631
632   /**
633    * Set the property value. This will only persist for the current frame; the property
634    * will be reset with the base value, at the beginning of the next frame.
635    * @param[in] bufferIndex The buffer to write.
636    * @param[in] value The new property value.
637    */
638   void Set(BufferIndex bufferIndex, const Matrix& value)
639   {
640     mValue[bufferIndex] = value;
641
642     // The value has been inherited for the first time
643     mInheritedFlag = true;
644
645     mReinheritedFlag = true;
646   }
647
648   /**
649    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
650    */
651   Matrix& Get(size_t bufferIndex)
652   {
653     return mValue[bufferIndex];
654   }
655
656   /**
657    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
658    */
659   const Matrix& Get(size_t bufferIndex) const
660   {
661     return mValue[bufferIndex];
662   }
663
664   /**
665    * Retrieve the property value.
666    * @param[in] bufferIndex The buffer to read.
667    * @return The property value.
668    */
669   const Matrix& operator[](size_t bufferIndex) const
670   {
671     return mValue[bufferIndex];
672   }
673
674   void SetDirty(size_t bufferIndex)
675   {
676     mReinheritedFlag = true;
677
678     // The value has been inherited for the first time
679     mInheritedFlag = true;
680   }
681
682 private:
683
684   // Undefined
685   InheritedMatrix(const InheritedMatrix& property);
686
687   // Undefined
688   InheritedMatrix& operator=(const InheritedMatrix& rhs);
689
690 private:
691
692   DoubleBuffered<Matrix> mValue; ///< The double-buffered property value
693
694   bool mInheritedFlag   :1;   ///< Flag whether the value has ever been inherited
695   bool mReinheritedFlag :1;   ///< Flag whether value was re-inherited in previous frame
696
697 };
698
699 } // namespace SceneGraph
700
701 } // namespace Internal
702
703 } // namespace Dali
704
705 #endif // __DALI_INTERNAL_SCENE_GRAPH_INHERITED_PROPERTY_H__