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