Revert "License conversion from Flora to Apache 2.0"
[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 Flora License, Version 1.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://floralicense.org/license/
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 // INTERNAL INCLUDES
21 #include <dali/internal/event/common/property-input-impl.h>
22 #include <dali/internal/update/common/double-buffered.h>
23 #include <dali/internal/update/common/property-base.h>
24 #include <dali/internal/update/common/scene-graph-buffers.h>
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/object/property.h>
27 #include <dali/public-api/object/property-input.h>
28 #include <dali/public-api/object/property-types.h>
29
30 namespace Dali
31 {
32
33 namespace Internal
34 {
35
36 namespace SceneGraph
37 {
38
39 template <class T>
40 class InheritedProperty;
41
42 /**
43  * An inherited Vector3 property.
44  */
45 template <>
46 class InheritedProperty<Vector3> : public PropertyInputImpl
47 {
48 public:
49
50   /**
51    * Create an inherited property.
52    * @param [in] initialValue The initial value of the property.
53    */
54   InheritedProperty( const Vector3& initialValue )
55   : mValue( initialValue ),
56     mInheritedFlag( false ),
57     mReinheritedFlag( true )
58   {
59   }
60
61   /**
62    * Virtual destructor.
63    */
64   virtual ~InheritedProperty()
65   {
66   }
67
68   /**
69    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
70    */
71   virtual Dali::Property::Type GetType() const
72   {
73     return Dali::PropertyTypes::Get<Vector3>();
74   }
75
76   /**
77    * Called once per Update (only) if the property did not need to be re-inherited.
78    * @param[in] updateBufferIndex The current update buffer index.
79    */
80   void CopyPrevious( BufferIndex updateBufferIndex )
81   {
82     if ( mReinheritedFlag )
83     {
84       mValue[updateBufferIndex] = mValue[updateBufferIndex ? 0 : 1];
85
86       mReinheritedFlag = false;
87     }
88   }
89
90   /**
91    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
92    */
93   virtual bool IsClean() const
94   {
95     return ( false == mReinheritedFlag );
96   }
97
98   /**
99    * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
100    */
101   virtual bool InputInitialized() const
102   {
103     // A constraint cannot use the property until it has been inherited (at least once).
104     return mInheritedFlag;
105   }
106
107   /**
108    * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
109    * @note A constraint can only receive the inherited property from the previous frame.
110    */
111   virtual bool InputChanged() const
112   {
113     return !IsClean();
114   }
115
116   /**
117    * @copydoc Dali::PropertyInput::GetVector3()
118    */
119   virtual const Vector3& GetVector3( BufferIndex bufferIndex ) const
120   {
121     return mValue[ bufferIndex ];
122   }
123
124   /**
125    * @copydoc Dali::PropertyInput::GetConstraintInputVector3()
126    */
127   virtual const Vector3& GetConstraintInputVector3( BufferIndex bufferIndex ) const
128   {
129     // For inherited properties, constraints work with the value from the previous frame.
130     // This is because constraints are applied to position etc, before world-position is calculated.
131     BufferIndex eventBufferIndex = bufferIndex ? 0u : 1u;
132
133     return mValue[ eventBufferIndex ];
134   }
135
136   /**
137    * Set the property value. This will only persist for the current frame; the property
138    * will be reset with the base value, at the beginning of the next frame.
139    * @param[in] bufferIndex The buffer to write.
140    * @param[in] value The new property value.
141    */
142   void Set(BufferIndex bufferIndex, const Vector3& value)
143   {
144     mValue[bufferIndex] = value;
145
146     // The value has been inherited for the first time
147     mInheritedFlag = true;
148
149     mReinheritedFlag = true;
150   }
151
152   /**
153    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
154    */
155   Vector3& Get(size_t bufferIndex)
156   {
157     return mValue[bufferIndex];
158   }
159
160   /**
161    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
162    */
163   const Vector3& Get(size_t bufferIndex) const
164   {
165     return mValue[bufferIndex];
166   }
167
168   /**
169    * Retrieve the property value.
170    * @param[in] bufferIndex The buffer to read.
171    * @return The property value.
172    */
173   const Vector3& operator[](size_t bufferIndex) const
174   {
175     return mValue[bufferIndex];
176   }
177
178 private:
179
180   // Undefined
181   InheritedProperty(const InheritedProperty& property);
182
183   // Undefined
184   InheritedProperty& operator=(const InheritedProperty& rhs);
185
186 private:
187
188   DoubleBuffered<Vector3> mValue; ///< The double-buffered property value
189
190   bool mInheritedFlag   :1; ///< Flag whether the value has ever been inherited
191   bool mReinheritedFlag :1; ///< Flag whether value was re-inherited in previous frame
192 };
193
194 /**
195  * An inherited Color property.
196  */
197 class InheritedColor : public PropertyInputImpl
198 {
199 public:
200
201   /**
202    * Create an inherited property.
203    * @param [in] initialValue The initial value of the property.
204    */
205   InheritedColor( const Vector4& initialValue )
206   : mValue( initialValue ),
207     mInheritedFlag( false ),
208     mReinheritedFlag( true )
209   {
210   }
211
212   /**
213    * Virtual destructor.
214    */
215   virtual ~InheritedColor()
216   {
217   }
218
219   /**
220    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
221    */
222   virtual Dali::Property::Type GetType() const
223   {
224     return Dali::PropertyTypes::Get<Vector4>();
225   }
226
227   /**
228    * Called once per Update (only) if the property did not need to be re-inherited.
229    * @param[in] updateBufferIndex The current update buffer index.
230    */
231   void CopyPrevious( BufferIndex updateBufferIndex )
232   {
233     if ( mReinheritedFlag )
234     {
235       mValue[updateBufferIndex] = mValue[updateBufferIndex ? 0 : 1];
236
237       mReinheritedFlag = false;
238     }
239   }
240
241   /**
242    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
243    */
244   virtual bool IsClean() const
245   {
246     return ( false == mReinheritedFlag );
247   }
248
249   /**
250    * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
251    */
252   virtual bool InputInitialized() const
253   {
254     // A constraint cannot use the property until it has been inherited (at least once).
255     return mInheritedFlag;
256   }
257
258   /**
259    * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
260    * @note A constraint can only receive the inherited property from the previous frame.
261    */
262   virtual bool InputChanged() const
263   {
264     return !IsClean();
265   }
266
267   /**
268    * @copydoc Dali::PropertyInput::GetVector4()
269    */
270   virtual const Vector4& GetVector4( BufferIndex bufferIndex ) const
271   {
272     return mValue[ bufferIndex ];
273   }
274
275   /**
276    * @copydoc Dali::PropertyInput::GetConstraintInputVector4()
277    */
278   virtual const Vector4& GetConstraintInputVector4( BufferIndex bufferIndex ) const
279   {
280     // For inherited properties, constraints work with the value from the previous frame.
281     // This is because constraints are applied to position etc, before world-position is calculated.
282     BufferIndex eventBufferIndex = bufferIndex ? 0u : 1u;
283
284     return mValue[ eventBufferIndex ];
285   }
286
287   /**
288    * Set the property value. This will only persist for the current frame; the property
289    * will be reset with the base value, at the beginning of the next frame.
290    * @param[in] bufferIndex The buffer to write.
291    * @param[in] value The new property value.
292    */
293   void Set(BufferIndex bufferIndex, const Vector4& value)
294   {
295     mValue[bufferIndex] = Clamp( value, 0.0f, 1.0f ); // color values are clamped between 0 and 1
296
297     // The value has been inherited for the first time
298     mInheritedFlag = true;
299     mReinheritedFlag = true;
300   }
301
302   /**
303    * Set the property value. This will only persist for the current frame; the property
304    * will be reset with the base value, at the beginning of the next frame.
305    * @param[in] bufferIndex The buffer to write.
306    * @param[in] r The new red value.
307    * @param[in] g The new green value.
308    * @param[in] b The new blue value.
309    * @param[in] a The new alpha value.
310    */
311   void Set(BufferIndex bufferIndex, float r, float g, float b, float a )
312   {
313     mValue[bufferIndex].r = Clamp( r, 0.0f, 1.0f ); // color values are clamped between 0 and 1
314     mValue[bufferIndex].g = Clamp( g, 0.0f, 1.0f ); // color values are clamped between 0 and 1
315     mValue[bufferIndex].b = Clamp( b, 0.0f, 1.0f ); // color values are clamped between 0 and 1
316     mValue[bufferIndex].a = Clamp( a, 0.0f, 1.0f ); // color values are clamped between 0 and 1
317
318     // The value has been inherited for the first time
319     mInheritedFlag = true;
320     mReinheritedFlag = true;
321   }
322
323   /**
324    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
325    */
326   Vector4& Get(size_t bufferIndex)
327   {
328     return mValue[bufferIndex];
329   }
330
331   /**
332    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
333    */
334   const Vector4& Get(size_t bufferIndex) const
335   {
336     return mValue[bufferIndex];
337   }
338
339   /**
340    * Retrieve the property value.
341    * @param[in] bufferIndex The buffer to read.
342    * @return The property value.
343    */
344   const Vector4& operator[](size_t bufferIndex) const
345   {
346     return mValue[bufferIndex];
347   }
348
349 private:
350
351   // Undefined
352   InheritedColor(const InheritedColor& property);
353   // Undefined
354   InheritedColor& operator=(const InheritedColor& rhs);
355
356 private:
357
358   DoubleBuffered<Vector4> mValue; ///< The double-buffered property value
359
360   bool mInheritedFlag   :1; ///< Flag whether the value has ever been inherited
361   bool mReinheritedFlag :1; ///< Flag whether value was re-inherited in previous frame
362
363 };
364
365 /**
366  * An inherited Quaternion property.
367  */
368 template <>
369 class InheritedProperty<Quaternion> : public PropertyInputImpl
370 {
371 public:
372
373   /**
374    * Create an inherited property.
375    * @param [in] initialValue The initial value of the property.
376    */
377   InheritedProperty( const Quaternion& initialValue )
378   : mValue( initialValue ),
379     mInheritedFlag( false ),
380     mReinheritedFlag( true )
381   {
382   }
383
384   /**
385    * Virtual destructor.
386    */
387   virtual ~InheritedProperty()
388   {
389   }
390
391   /**
392    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
393    */
394   virtual Dali::Property::Type GetType() const
395   {
396     return Dali::PropertyTypes::Get<Quaternion>();
397   }
398
399   /**
400    * Called once per Update (only) if the property did not need to be re-inherited.
401    * @param[in] updateBufferIndex The current update buffer index.
402    */
403   void CopyPrevious( BufferIndex updateBufferIndex )
404   {
405     if ( mReinheritedFlag )
406     {
407       mValue[updateBufferIndex] = mValue[updateBufferIndex ? 0 : 1];
408
409       mReinheritedFlag = false;
410     }
411   }
412
413   /**
414    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
415    */
416   virtual bool IsClean() const
417   {
418     return ( false == mReinheritedFlag );
419   }
420
421   /**
422    * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
423    */
424   virtual bool InputInitialized() const
425   {
426     // A constraint cannot use the property until it has been inherited (at least once).
427     return mInheritedFlag;
428   }
429
430   /**
431    * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
432    * @note A constraint can only receive the inherited property from the previous frame.
433    */
434   virtual bool InputChanged() const
435   {
436     return !IsClean();
437   }
438
439   /**
440    * @copydoc Dali::PropertyInput::GetQuaternion()
441    */
442   virtual const Quaternion& GetQuaternion( BufferIndex bufferIndex ) const
443   {
444     return mValue[ bufferIndex ];
445   }
446
447   /**
448    * @copydoc Dali::PropertyInput::GetConstraintInputQuaternion()
449    */
450   virtual const Quaternion& GetConstraintInputQuaternion( BufferIndex bufferIndex ) const
451   {
452     // For inherited properties, constraints work with the value from the previous frame.
453     // This is because constraints are applied to position etc, before world-position is calculated.
454     BufferIndex eventBufferIndex = bufferIndex ? 0u : 1u;
455
456     return mValue[ eventBufferIndex ];
457   }
458
459   /**
460    * Set the property value. This will only persist for the current frame; the property
461    * will be reset with the base value, at the beginning of the next frame.
462    * @param[in] bufferIndex The buffer to write.
463    * @param[in] value The new property value.
464    */
465   void Set(BufferIndex bufferIndex, const Quaternion& value)
466   {
467     mValue[bufferIndex] = value;
468
469     // The value has been inherited for the first time
470     mInheritedFlag = true;
471
472     mReinheritedFlag = true;
473   }
474
475   /**
476    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
477    */
478   Quaternion& Get(size_t bufferIndex)
479   {
480     return mValue[bufferIndex];
481   }
482
483   /**
484    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
485    */
486   const Quaternion& Get(size_t bufferIndex) const
487   {
488     return mValue[bufferIndex];
489   }
490
491   /**
492    * Retrieve the property value.
493    * @param[in] bufferIndex The buffer to read.
494    * @return The property value.
495    */
496   const Quaternion& operator[](size_t bufferIndex) const
497   {
498     return mValue[bufferIndex];
499   }
500
501 private:
502
503   // Undefined
504   InheritedProperty();
505
506   // Undefined
507   InheritedProperty(const InheritedProperty& property);
508
509   // Undefined
510   InheritedProperty& operator=(const InheritedProperty& rhs);
511
512 private:
513
514   DoubleBuffered<Quaternion> mValue; ///< The double-buffered property value
515
516   bool mInheritedFlag   :1;   ///< Flag whether the value has ever been inherited
517   bool mReinheritedFlag :1;   ///< Flag whether value was re-inherited in previous frame
518 };
519
520 /**
521  * An inherited Matrix property.
522  */
523 template <>
524 class InheritedProperty<Matrix> : public PropertyInputImpl
525 {
526 public:
527
528   /**
529    * Create an inherited property.
530    * @param [in] initialValue The initial value of the property.
531    */
532   InheritedProperty( const Matrix& initialValue )
533   : mValue( initialValue ),
534     mInheritedFlag( false ),
535     mReinheritedFlag( true )
536   {
537   }
538
539   /**
540    * Virtual destructor.
541    */
542   virtual ~InheritedProperty()
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   InheritedProperty();
686
687   // Undefined
688   InheritedProperty(const InheritedProperty& property);
689
690   // Undefined
691   InheritedProperty& operator=(const InheritedProperty& rhs);
692
693 private:
694
695   DoubleBuffered<Matrix> mValue; ///< The double-buffered property value
696
697   bool mInheritedFlag   :1;   ///< Flag whether the value has ever been inherited
698   bool mReinheritedFlag :1;   ///< Flag whether value was re-inherited in previous frame
699
700 };
701
702 } // namespace SceneGraph
703
704 } // namespace Internal
705
706 } // namespace Dali
707
708 #endif // __DALI_INTERNAL_SCENE_GRAPH_INHERITED_PROPERTY_H__