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