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