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