Purge underscored header file barriers
[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) 2019 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 #include <dali/public-api/math/math-utils.h> // Clamp
31
32 namespace Dali
33 {
34
35 namespace Internal
36 {
37
38 namespace SceneGraph
39 {
40
41 /**
42  * An inherited Vector3 property.
43  */
44 class InheritedVector3 : public PropertyInputImpl
45 {
46 public:
47
48   /**
49    * Create an inherited Vector3.
50    */
51   InheritedVector3()
52   : mValue(),
53     mInheritedFlag( false ),
54     mReinheritedFlag( true )
55   {
56   }
57
58   /**
59    * Create an inherited Vector3.
60    * @param [in] initialValue The initial value of the property.
61    */
62   InheritedVector3( const Vector3& initialValue )
63   : mValue( initialValue ),
64     mInheritedFlag( false ),
65     mReinheritedFlag( true )
66   {
67   }
68   /**
69    * Virtual destructor.
70    */
71   virtual ~InheritedVector3()
72   {
73   }
74
75   /**
76    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
77    */
78   virtual Dali::Property::Type GetType() const
79   {
80     return Dali::PropertyTypes::Get<Vector3>();
81   }
82
83   /**
84    * Called once per Update (only) if the property did not need to be re-inherited.
85    * @param[in] updateBufferIndex The current update buffer index.
86    */
87   void CopyPrevious( BufferIndex updateBufferIndex )
88   {
89     if ( mReinheritedFlag )
90     {
91       mValue[updateBufferIndex] = mValue[updateBufferIndex ? 0 : 1];
92
93       mReinheritedFlag = false;
94     }
95   }
96
97   /**
98    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
99    */
100   virtual bool IsClean() const
101   {
102     return ( false == mReinheritedFlag );
103   }
104
105   /**
106    * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
107    */
108   virtual bool InputInitialized() const
109   {
110     // A constraint cannot use the property until it has been inherited (at least once).
111     return mInheritedFlag;
112   }
113
114   /**
115    * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
116    * @note A constraint can only receive the inherited property from the previous frame.
117    */
118   virtual bool InputChanged() const
119   {
120     return !IsClean();
121   }
122
123   /**
124    * @copydoc Dali::PropertyInput::GetVector3()
125    */
126   virtual const Vector3& GetVector3( BufferIndex bufferIndex ) const
127   {
128     return mValue[ bufferIndex ];
129   }
130
131   /**
132    * @copydoc Dali::PropertyInput::GetConstraintInputVector3()
133    */
134   virtual const Vector3& GetConstraintInputVector3( BufferIndex bufferIndex ) const
135   {
136     // For inherited properties, constraints work with the value from the previous frame.
137     // This is because constraints are applied to position etc, before world-position is calculated.
138     BufferIndex eventBufferIndex = bufferIndex ? 0u : 1u;
139
140     return mValue[ eventBufferIndex ];
141   }
142
143   /**
144    * Set the property value. This will only persist for the current frame; the property
145    * will be reset with the base value, at the beginning of the next frame.
146    * @param[in] bufferIndex The buffer to write.
147    * @param[in] value The new property value.
148    */
149   void Set(BufferIndex bufferIndex, const Vector3& value)
150   {
151     mValue[bufferIndex] = value;
152
153     // The value has been inherited for the first time
154     mInheritedFlag = true;
155
156     mReinheritedFlag = true;
157   }
158
159   /**
160    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
161    */
162   Vector3& Get( BufferIndex bufferIndex )
163   {
164     return mValue[bufferIndex];
165   }
166
167   /**
168    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
169    */
170   const Vector3& Get( BufferIndex bufferIndex ) const
171   {
172     return mValue[bufferIndex];
173   }
174
175   /**
176    * Retrieve the property value.
177    * @param[in] bufferIndex The buffer to read.
178    * @return The property value.
179    */
180   const Vector3& operator[]( BufferIndex bufferIndex ) const
181   {
182     return mValue[bufferIndex];
183   }
184
185 private:
186
187   // Undefined
188   InheritedVector3(const InheritedVector3& property);
189
190   // Undefined
191   InheritedVector3& operator=(const InheritedVector3& rhs);
192
193 private:
194
195   DoubleBuffered<Vector3> mValue; ///< The double-buffered property value
196
197   bool mInheritedFlag   :1; ///< Flag whether the value has ever been inherited
198   bool mReinheritedFlag :1; ///< Flag whether value was re-inherited in previous frame
199 };
200
201 /**
202  * An inherited Color property.
203  */
204 class InheritedColor : public PropertyInputImpl
205 {
206 public:
207
208   /**
209    * Create an inherited property.
210    * @param [in] initialValue The initial value of the property.
211    */
212   InheritedColor( const Vector4& initialValue )
213   : mValue( initialValue ),
214     mInheritedFlag( false ),
215     mReinheritedFlag( true )
216   {
217   }
218
219   /**
220    * Virtual destructor.
221    */
222   virtual ~InheritedColor()
223   {
224   }
225
226   /**
227    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
228    */
229   virtual Dali::Property::Type GetType() const
230   {
231     return Dali::PropertyTypes::Get<Vector4>();
232   }
233
234   /**
235    * Called once per Update (only) if the property did not need to be re-inherited.
236    * @param[in] updateBufferIndex The current update buffer index.
237    */
238   void CopyPrevious( BufferIndex updateBufferIndex )
239   {
240     if ( mReinheritedFlag )
241     {
242       mValue[updateBufferIndex] = mValue[updateBufferIndex ? 0 : 1];
243
244       mReinheritedFlag = false;
245     }
246   }
247
248   /**
249    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
250    */
251   virtual bool IsClean() const
252   {
253     return ( false == mReinheritedFlag );
254   }
255
256   /**
257    * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
258    */
259   virtual bool InputInitialized() const
260   {
261     // A constraint cannot use the property until it has been inherited (at least once).
262     return mInheritedFlag;
263   }
264
265   /**
266    * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
267    * @note A constraint can only receive the inherited property from the previous frame.
268    */
269   virtual bool InputChanged() const
270   {
271     return !IsClean();
272   }
273
274   /**
275    * @copydoc Dali::PropertyInput::GetVector4()
276    */
277   virtual const Vector4& GetVector4( BufferIndex bufferIndex ) const
278   {
279     return mValue[ bufferIndex ];
280   }
281
282   /**
283    * @copydoc Dali::PropertyInput::GetConstraintInputVector4()
284    */
285   virtual const Vector4& GetConstraintInputVector4( BufferIndex bufferIndex ) const
286   {
287     // For inherited properties, constraints work with the value from the previous frame.
288     // This is because constraints are applied to position etc, before world-position is calculated.
289     BufferIndex eventBufferIndex = bufferIndex ? 0u : 1u;
290
291     return mValue[ eventBufferIndex ];
292   }
293
294   /**
295    * Set the property value. This will only persist for the current frame; the property
296    * will be reset with the base value, at the beginning of the next frame.
297    * @param[in] bufferIndex The buffer to write.
298    * @param[in] value The new property value.
299    */
300   void Set(BufferIndex bufferIndex, const Vector4& value)
301   {
302     mValue[bufferIndex] = Clamp( value, 0.0f, 1.0f ); // color values are clamped between 0 and 1
303
304     // The value has been inherited for the first time
305     mInheritedFlag = true;
306     mReinheritedFlag = true;
307   }
308
309   /**
310    * Set the property value. This will only persist for the current frame; the property
311    * will be reset with the base value, at the beginning of the next frame.
312    * @param[in] bufferIndex The buffer to write.
313    * @param[in] r The new red value.
314    * @param[in] g The new green value.
315    * @param[in] b The new blue value.
316    * @param[in] a The new alpha value.
317    */
318   void Set(BufferIndex bufferIndex, float r, float g, float b, float a )
319   {
320     mValue[bufferIndex].r = Clamp( r, 0.0f, 1.0f ); // color values are clamped between 0 and 1
321     mValue[bufferIndex].g = Clamp( g, 0.0f, 1.0f ); // color values are clamped between 0 and 1
322     mValue[bufferIndex].b = Clamp( b, 0.0f, 1.0f ); // color values are clamped between 0 and 1
323     mValue[bufferIndex].a = Clamp( a, 0.0f, 1.0f ); // color values are clamped between 0 and 1
324
325     // The value has been inherited for the first time
326     mInheritedFlag = true;
327     mReinheritedFlag = true;
328   }
329
330   /**
331    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
332    */
333   Vector4& Get( BufferIndex bufferIndex )
334   {
335     return mValue[bufferIndex];
336   }
337
338   /**
339    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
340    */
341   const Vector4& Get( BufferIndex bufferIndex ) const
342   {
343     return mValue[bufferIndex];
344   }
345
346   /**
347    * Retrieve the property value.
348    * @param[in] bufferIndex The buffer to read.
349    * @return The property value.
350    */
351   const Vector4& operator[]( BufferIndex bufferIndex ) const
352   {
353     return mValue[bufferIndex];
354   }
355
356 private:
357
358   // Undefined
359   InheritedColor(const InheritedColor& property);
360   // Undefined
361   InheritedColor& operator=(const InheritedColor& rhs);
362
363 private:
364
365   DoubleBuffered<Vector4> mValue; ///< The double-buffered property value
366
367   bool mInheritedFlag   :1; ///< Flag whether the value has ever been inherited
368   bool mReinheritedFlag :1; ///< Flag whether value was re-inherited in previous frame
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( BufferIndex bufferIndex )
483   {
484     return mValue[bufferIndex];
485   }
486
487   /**
488    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
489    */
490   const Quaternion& Get( BufferIndex 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[]( BufferIndex 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    */
531   InheritedMatrix()
532   : mValue(),
533     mInheritedFlag( false ),
534     mReinheritedFlag( true )
535   {
536   }
537
538   /**
539    * Virtual destructor.
540    */
541   virtual ~InheritedMatrix()
542   {
543   }
544
545   /**
546    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
547    */
548   virtual Dali::Property::Type GetType() const
549   {
550     return Dali::PropertyTypes::Get<Matrix>();
551   }
552
553   /**
554    * Called once per Update (only) if the property did not need to be re-inherited.
555    * @param[in] updateBufferIndex The current update buffer index.
556    */
557   void CopyPrevious( BufferIndex updateBufferIndex )
558   {
559     if ( mReinheritedFlag )
560     {
561       mValue[updateBufferIndex] = mValue[updateBufferIndex ? 0 : 1];
562
563       mReinheritedFlag = false;
564     }
565   }
566
567   /**
568    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
569    */
570   virtual bool IsClean() const
571   {
572     return ( false == mReinheritedFlag );
573   }
574
575   /**
576    * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
577    */
578   virtual bool InputInitialized() const
579   {
580     // A constraint cannot use the property until it has been inherited (at least once).
581     return mInheritedFlag;
582   }
583
584   /**
585    * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
586    * @note A constraint can only receive the inherited property from the previous frame.
587    */
588   virtual bool InputChanged() const
589   {
590     return !IsClean();
591   }
592
593   /**
594    * @copydoc Dali::Internal::PropertyInputImpl::GetMatrix()
595    */
596   virtual const Matrix& GetMatrix( BufferIndex bufferIndex ) const
597   {
598     return mValue[ bufferIndex ];
599   }
600
601   /**
602    * @copydoc Dali::Internal::PropertyInputImpl::GetConstraintInputMatrix()
603    */
604   virtual const Matrix& GetConstraintInputMatrix( BufferIndex bufferIndex ) const
605   {
606     // For inherited properties, constraints work with the value from the previous frame.
607     // This is because constraints are applied to position etc, before world-position is calculated.
608     BufferIndex eventBufferIndex = bufferIndex ? 0u : 1u;
609
610     return mValue[ eventBufferIndex ];
611   }
612
613   /**
614    * Set the property value. This will only persist for the current frame; the property
615    * will be reset with the base value, at the beginning of the next frame.
616    * @param[in] bufferIndex The buffer to write.
617    * @param[in] value The new property value.
618    */
619   void Set(BufferIndex bufferIndex, const Matrix& value)
620   {
621     mValue[bufferIndex] = value;
622
623     // The value has been inherited for the first time
624     mInheritedFlag = true;
625
626     mReinheritedFlag = true;
627   }
628
629   /**
630    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
631    */
632   Matrix& Get( BufferIndex bufferIndex )
633   {
634     return mValue[bufferIndex];
635   }
636
637   /**
638    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
639    */
640   const Matrix& Get( BufferIndex bufferIndex ) const
641   {
642     return mValue[bufferIndex];
643   }
644
645   /**
646    * Retrieve the property value.
647    * @param[in] bufferIndex The buffer to read.
648    * @return The property value.
649    */
650   const Matrix& operator[]( BufferIndex bufferIndex ) const
651   {
652     return mValue[bufferIndex];
653   }
654
655   void SetDirty( BufferIndex bufferIndex )
656   {
657     mReinheritedFlag = true;
658
659     // The value has been inherited for the first time
660     mInheritedFlag = true;
661   }
662
663 private:
664
665   // Undefined
666   InheritedMatrix(const InheritedMatrix& property);
667
668   // Undefined
669   InheritedMatrix& operator=(const InheritedMatrix& rhs);
670
671 private:
672
673   DoubleBuffered<Matrix> mValue; ///< The double-buffered property value
674
675   bool mInheritedFlag   :1;   ///< Flag whether the value has ever been inherited
676   bool mReinheritedFlag :1;   ///< Flag whether value was re-inherited in previous frame
677
678 };
679
680 } // namespace SceneGraph
681
682 } // namespace Internal
683
684 } // namespace Dali
685
686 #endif // DALI_INTERNAL_SCENE_GRAPH_INHERITED_PROPERTY_H