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) 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(0),
39    mId( INVALID_TRANSFORM_ID )
40   {}
41
42   /**
43    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
44    */
45   virtual Dali::Property::Type GetType() const = 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   virtual const Vector3& GetVector3( BufferIndex bufferIndex ) const{ 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   virtual void ResetToBaseValue(BufferIndex updateBufferIndex){}
124
125   /**
126    * @copydoc Dali::AnimatableProperty::IsClean()
127    */
128   virtual bool IsClean() const{ 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   virtual bool IsTransformManagerProperty() const
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
162   {
163     return Dali::PropertyTypes::Get<Vector3>();
164   }
165
166   Vector3& Get( BufferIndex bufferIndex )
167   {
168     return mTxManager->GetVector3PropertyValue( mId, mProperty );
169   }
170
171   const Vector3& Get( BufferIndex bufferIndex ) const
172   {
173     return mTxManager->GetVector3PropertyValue( mId, mProperty );
174   }
175
176   virtual const Vector3& GetVector3( BufferIndex bufferIndex ) const
177   {
178     return Get(bufferIndex);
179   }
180
181   const float& GetFloatComponent( uint32_t component )
182   {
183     return mTxManager->GetVector3PropertyComponentValue( mId, mProperty, component );
184   }
185
186   void Set(BufferIndex bufferIndex, const Vector3& value)
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)
202   {
203     mTxManager->BakeVector3PropertyValue(mId, mProperty, value );
204   }
205
206   void BakeX(BufferIndex bufferIndex, float value)
207   {
208     mTxManager->BakeXVector3PropertyValue(mId, mProperty, value );
209   }
210
211   void BakeY(BufferIndex bufferIndex, float value)
212   {
213     mTxManager->BakeYVector3PropertyValue(mId, mProperty, value );
214   }
215
216   void BakeZ(BufferIndex bufferIndex, float value)
217   {
218     mTxManager->BakeZVector3PropertyValue(mId, mProperty, value );
219   }
220
221   void SetFloatComponent( float value, uint32_t component)
222   {
223     mTxManager->SetVector3PropertyComponentValue( mId, mProperty, value, component);
224   }
225
226   void BakeFloatComponent( float value, uint32_t component )
227   {
228     mTxManager->BakeVector3PropertyComponentValue( mId, mProperty, value, component);
229   }
230
231   void BakeRelative(BufferIndex bufferIndex, const Vector3& value)
232   {
233     mTxManager->BakeRelativeVector3PropertyValue(mId, mProperty, value );
234   }
235
236   void BakeRelativeMultiply(BufferIndex bufferIndex, const Vector3& value)
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   virtual Dali::Property::Type GetType() const
253   {
254     return Dali::PropertyTypes::Get<Quaternion>();
255   }
256
257   Quaternion& Get( BufferIndex bufferIndex )
258   {
259     return mTxManager->GetQuaternionPropertyValue( mId );
260   }
261
262   const Quaternion& Get( BufferIndex bufferIndex ) const
263   {
264     return mTxManager->GetQuaternionPropertyValue( mId );
265   }
266
267   const float& GetFloatComponent( uint32_t component)
268   {
269     return mTxManager->GetQuaternionPropertyValue( mId ).mVector[component];
270   }
271
272   void Set(BufferIndex bufferIndex, const Quaternion& value)
273   {
274     return mTxManager->SetQuaternionPropertyValue( mId, value );
275   }
276
277   void Bake(BufferIndex bufferIndex, const Quaternion& value)
278   {
279     return mTxManager->BakeQuaternionPropertyValue( mId, value );
280   }
281
282   void BakeRelative(BufferIndex bufferIndex, const Quaternion& value)
283   {
284     return mTxManager->BakeRelativeQuaternionPropertyValue( mId, value );
285   }
286
287   virtual const Quaternion& GetQuaternion( BufferIndex bufferIndex ) const
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(0),
306    mId(INVALID_TRANSFORM_ID),
307    mProperty(property),
308    mValue(initialValue)
309   {}
310
311   /**
312    * Virtual destructor.
313    */
314   virtual ~TransformManagerVector3Input()
315   {
316   }
317
318   /**
319    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
320    */
321   virtual Dali::Property::Type GetType() const
322   {
323     return Dali::PropertyTypes::Get<Vector3>();
324   }
325
326   /**
327    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
328    */
329   virtual bool IsClean() const
330   {
331     return false;
332   }
333
334   /**
335    * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
336    */
337   virtual bool InputInitialized() const
338   {
339     return true;
340   }
341
342   /**
343    * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
344    * @note A constraint can only receive the inherited property from the previous frame.
345    */
346   virtual bool InputChanged() const
347   {
348     return true;
349   }
350
351   /**
352    * Helper function to get the transform components out of the world matrix.
353    * It stores the value in the mValue member variable
354    */
355   void ComputeTransformComponent() const
356   {
357     if( mTxManager )
358     {
359       const Matrix& worldMatrix = mTxManager->GetWorldMatrix(mId);
360
361       if( mProperty == TRANSFORM_PROPERTY_WORLD_POSITION )
362       {
363         mValue = worldMatrix.GetTranslation3();
364       }
365       else if( mProperty == TRANSFORM_PROPERTY_WORLD_SCALE )
366       {
367         Vector3 position;
368         Quaternion orientation;
369         worldMatrix.GetTransformComponents(position, orientation, mValue);
370       }
371     }
372   }
373
374   /**
375    * @copydoc Dali::PropertyInput::GetVector3()
376    */
377   virtual const Vector3& GetVector3( BufferIndex bufferIndex ) const
378   {
379     ComputeTransformComponent();
380     return mValue;
381   }
382
383   /**
384    * @copydoc Dali::PropertyInput::GetConstraintInputVector3()
385    */
386   virtual const Vector3& GetConstraintInputVector3( BufferIndex bufferIndex ) const
387   {
388     ComputeTransformComponent();
389     return mValue;
390   }
391
392   /**
393    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
394    */
395   Vector3& Get( BufferIndex bufferIndex )
396   {
397     ComputeTransformComponent();
398     return mValue;
399   }
400
401   /**
402    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
403    */
404   const Vector3& Get( BufferIndex bufferIndex ) const
405   {
406     ComputeTransformComponent();
407     return mValue;
408   }
409
410   /**
411    * Retrieve the property value.
412    * @param[in] bufferIndex The buffer to read.
413    * @return The property value.
414    */
415   const Vector3& operator[]( BufferIndex bufferIndex ) const
416   {
417     ComputeTransformComponent();
418     return mValue;
419   }
420
421   /**
422    * Initializes the property
423    * @param[in] transformManager Pointer to the transform manager
424    * @param[in] Id of the transformation the property is associated with
425    */
426   void Initialize( TransformManager* transformManager, TransformId id )
427   {
428     mTxManager = transformManager;
429     mId = id;
430   }
431
432   /**
433    * @copydoc Dali::Internal::PropertyInputImpl::IsTransformManagerProperty()
434    */
435   virtual bool IsTransformManagerProperty() const
436   {
437     return true;
438   }
439
440 private:
441
442   // Undefined
443   TransformManagerVector3Input(const TransformManagerVector3Input& property);
444
445   // Undefined
446   TransformManagerVector3Input& operator=(const TransformManagerVector3Input& rhs);
447
448 public:
449
450   TransformManager* mTxManager;
451   TransformId mId;
452   TransformManagerProperty mProperty;
453   mutable Vector3 mValue;
454 };
455
456 /**
457  * A Quaternion property used as input.
458  */
459 class TransformManagerQuaternionInput : public PropertyInputImpl
460 {
461 public:
462
463   /**
464    * Constructor
465    */
466   TransformManagerQuaternionInput()
467   :mTxManager(0),
468    mId(INVALID_TRANSFORM_ID),
469    mValue(1.0f,0.0f,0.0f,0.0f)
470   {
471   }
472
473   /**
474    * Virtual destructor.
475    */
476   virtual ~TransformManagerQuaternionInput()
477   {
478   }
479
480   /**
481    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
482    */
483   virtual Dali::Property::Type GetType() const
484   {
485     return Dali::PropertyTypes::Get<Quaternion>();
486   }
487
488   /**
489    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
490    */
491   virtual bool IsClean() const
492   {
493     return false;
494   }
495
496   /**
497    * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
498    */
499   virtual bool InputInitialized() const
500   {
501     return true;
502   }
503
504   /**
505    * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
506    * @note A constraint can only receive the inherited property from the previous frame.
507    */
508   virtual bool InputChanged() const
509   {
510     return true;
511   }
512
513   /**
514    * Helper function to get the orientation out of the world matrix.
515    * It stores the result in the mValue member variable
516    */
517   void ComputeTransformComponent() const
518   {
519     if( mTxManager )
520     {
521       const Matrix& worldMatrix = mTxManager->GetWorldMatrix(mId);
522       Vector3 position, scale;
523       worldMatrix.GetTransformComponents(position, mValue, scale);
524     }
525   }
526
527   /**
528    * @copydoc Dali::PropertyInput::GetQuaternion()
529    */
530   virtual const Quaternion& GetQuaternion( BufferIndex bufferIndex ) const
531   {
532     ComputeTransformComponent();
533     return mValue;
534   }
535
536   /**
537    * @copydoc Dali::PropertyInput::GetConstraintInputQuaternion()
538    */
539   virtual const Quaternion& GetConstraintInputQuaternion( BufferIndex bufferIndex ) const
540   {
541     ComputeTransformComponent();
542     return mValue;
543   }
544
545   /**
546    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
547    */
548   Quaternion& Get( BufferIndex bufferIndex )
549   {
550     ComputeTransformComponent();
551     return mValue;
552   }
553
554   /**
555    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
556    */
557   const Quaternion& Get( BufferIndex bufferIndex ) const
558   {
559     ComputeTransformComponent();
560     return mValue;
561   }
562
563   /**
564    * Retrieve the property value.
565    * @param[in] bufferIndex The buffer to read.
566    * @return The property value.
567    */
568   const Quaternion& operator[]( BufferIndex bufferIndex) const
569   {
570     ComputeTransformComponent();
571     return mValue;
572   }
573
574   /**
575    * Initializes the property
576    * @param[in] transformManager Pointer to the transform manager
577    * @param[in] Id of the transformation the property is associated with
578    */
579   void Initialize( TransformManager* transformManager, TransformId id )
580   {
581     mTxManager = transformManager;
582     mId = id;
583   }
584
585   /**
586    * @copydoc Dali::Internal::PropertyInputImpl::IsTransformManagerProperty()
587    */
588   virtual bool IsTransformManagerProperty() const
589   {
590     return true;
591   }
592
593 private:
594
595   // Undefined
596   TransformManagerQuaternionInput(const TransformManagerQuaternionInput& property);
597
598   // Undefined
599   TransformManagerQuaternionInput& operator=(const TransformManagerQuaternionInput& rhs);
600
601 public:
602
603   TransformManager* mTxManager;
604   TransformId mId;
605   mutable Quaternion mValue;
606 };
607
608 /**
609  * A Matrix property used as input.
610  */
611 class TransformManagerMatrixInput : public PropertyInputImpl
612 {
613 public:
614
615   /**
616    * Constructor
617    */
618   TransformManagerMatrixInput()
619   :mTxManager(0),
620    mId(INVALID_TRANSFORM_ID)
621   {
622   }
623
624   /**
625    * Virtual destructor.
626    */
627   virtual ~TransformManagerMatrixInput()
628   {
629   }
630
631   /**
632    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
633    */
634   virtual Dali::Property::Type GetType() const
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   virtual bool InputInitialized() const
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   virtual bool InputChanged() const
660   {
661     return true;
662   }
663
664   /**
665    * @copydoc Dali::PropertyInput::GetMatrix()
666    */
667   virtual const Matrix& GetMatrix( BufferIndex bufferIndex ) const
668   {
669     if( mTxManager )
670     {
671       return mTxManager->GetWorldMatrix(mId);
672     }
673
674     return Matrix::IDENTITY;
675   }
676
677   /**
678    * @copydoc Dali::PropertyInput::GetConstraintInputMatrix()
679    */
680   virtual const Matrix& GetConstraintInputMatrix( BufferIndex bufferIndex ) const
681   {
682     if( mTxManager )
683     {
684       return mTxManager->GetWorldMatrix(mId);
685     }
686
687     return Matrix::IDENTITY;
688   }
689
690   /**
691    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
692    */
693   Matrix& Get( BufferIndex bufferIndex )
694   {
695     DALI_ASSERT_ALWAYS( mTxManager != 0 );
696     return mTxManager->GetWorldMatrix(mId);
697   }
698
699   /**
700    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
701    */
702   const Matrix& Get( BufferIndex bufferIndex ) const
703   {
704     return GetMatrix( bufferIndex );
705   }
706
707   /**
708    * Retrieve the property value.
709    * @param[in] bufferIndex The buffer to read.
710    * @return The property value.
711    */
712   const Matrix& operator[]( BufferIndex bufferIndex ) const
713   {
714     return GetMatrix( bufferIndex );
715   }
716
717   void Initialize( TransformManager* transformManager, TransformId id )
718   {
719     mTxManager = transformManager;
720     mId = id;
721   }
722
723   /**
724    * @copydoc Dali::Internal::PropertyInputImpl::IsTransformManagerProperty()
725    */
726   virtual bool IsTransformManagerProperty() const
727   {
728     return true;
729   }
730
731 private:
732
733   // Undefined
734   TransformManagerMatrixInput(const TransformManagerMatrixInput& property);
735
736   // Undefined
737   TransformManagerMatrixInput& operator=(const TransformManagerMatrixInput& rhs);
738
739 public:
740
741   TransformManager* mTxManager;
742   TransformId mId;
743 };
744
745
746 } //namespace SceneGraph
747 } //namespace Internal
748 } //namespace Dali
749
750 #endif // TRANSFORM_MANAGER_PROPERTY_H_