Data oriented transform manager
[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) 2016 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(size_t bufferIndex) = 0;
51
52   /**
53    * @copydoc Dali::SceneGraph::AnimatableProperty::Get()
54    */
55   virtual const T& Get(size_t 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(unsigned int 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, unsigned int 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, unsigned int 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(size_t bufferIndex)
167   {
168     return mTxManager->GetVector3PropertyValue( mId, mProperty );
169   }
170
171   const Vector3& Get(size_t 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( unsigned int 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, unsigned int component)
192   {
193     mTxManager->SetVector3PropertyComponentValue( mId, mProperty, value, component);
194   }
195
196   void BakeComponent(BufferIndex bufferIndex, float value, unsigned int 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, unsigned int component)
222   {
223     mTxManager->SetVector3PropertyComponentValue( mId, mProperty, value, component);
224   }
225
226   void BakeFloatComponent( float value, unsigned int 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(size_t bufferIndex)
258   {
259     return mTxManager->GetQuaternionPropertyValue( mId );
260   }
261
262   const Quaternion& Get(size_t bufferIndex) const
263   {
264     return mTxManager->GetQuaternionPropertyValue( mId );
265   }
266
267   const float& GetFloatComponent( unsigned int 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
288 /**
289  * A Vector3 property used as input.
290  */
291 class TransformManagerVector3Input : public PropertyInputImpl
292 {
293 public:
294
295   /**
296    * Create an TransformManagerVector3Input
297    */
298   TransformManagerVector3Input( TransformManagerProperty property, const Vector3& initialValue )
299   :mTxManager(0),
300    mId(INVALID_TRANSFORM_ID),
301    mProperty(property),
302    mValue(initialValue)
303   {}
304
305   /**
306    * Virtual destructor.
307    */
308   virtual ~TransformManagerVector3Input()
309   {
310   }
311
312   /**
313    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
314    */
315   virtual Dali::Property::Type GetType() const
316   {
317     return Dali::PropertyTypes::Get<Vector3>();
318   }
319
320   /**
321    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
322    */
323   virtual bool IsClean() const
324   {
325     return false;
326   }
327
328   /**
329    * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
330    */
331   virtual bool InputInitialized() const
332   {
333     return true;
334   }
335
336   /**
337    * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
338    * @note A constraint can only receive the inherited property from the previous frame.
339    */
340   virtual bool InputChanged() const
341   {
342     return true;
343   }
344
345   /**
346    * Helper function to get the transform components out of the world matrix.
347    * It stores the value in the mValue member variable
348    */
349   void ComputeTransformComponent() const
350   {
351     if( mTxManager )
352     {
353       const Matrix& worldMatrix = mTxManager->GetWorldMatrix(mId);
354
355       if( mProperty == TRANSFORM_PROPERTY_WORLD_POSITION )
356       {
357         mValue = worldMatrix.GetTranslation3();
358       }
359       else if( mProperty == TRANSFORM_PROPERTY_WORLD_SCALE )
360       {
361         Vector3 position;
362         Quaternion orientation;
363         worldMatrix.GetTransformComponents(position, orientation, mValue);
364       }
365     }
366   }
367
368   /**
369    * @copydoc Dali::PropertyInput::GetVector3()
370    */
371   virtual const Vector3& GetVector3( BufferIndex bufferIndex ) const
372   {
373     ComputeTransformComponent();
374     return mValue;
375   }
376
377   /**
378    * @copydoc Dali::PropertyInput::GetConstraintInputVector3()
379    */
380   virtual const Vector3& GetConstraintInputVector3( BufferIndex bufferIndex ) const
381   {
382     ComputeTransformComponent();
383     return mValue;
384   }
385
386   /**
387    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
388    */
389   Vector3& Get(size_t bufferIndex)
390   {
391     ComputeTransformComponent();
392     return mValue;
393   }
394
395   /**
396    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
397    */
398   const Vector3& Get(size_t bufferIndex) const
399   {
400     ComputeTransformComponent();
401     return mValue;
402   }
403
404   /**
405    * Retrieve the property value.
406    * @param[in] bufferIndex The buffer to read.
407    * @return The property value.
408    */
409   const Vector3& operator[](size_t bufferIndex) const
410   {
411     ComputeTransformComponent();
412     return mValue;
413   }
414
415   /**
416    * Initializes the property
417    * @param[in] transformManager Pointer to the transform manager
418    * @param[in] Id of the transformation the property is associated with
419    */
420   void Initialize( TransformManager* transformManager, TransformId id )
421   {
422     mTxManager = transformManager;
423     mId = id;
424   }
425
426   /**
427    * @copydoc Dali::Internal::PropertyInputImpl::IsTransformManagerProperty()
428    */
429   virtual bool IsTransformManagerProperty() const
430   {
431     return true;
432   }
433
434 private:
435
436   // Undefined
437   TransformManagerVector3Input(const TransformManagerVector3Input& property);
438
439   // Undefined
440   TransformManagerVector3Input& operator=(const TransformManagerVector3Input& rhs);
441
442 public:
443
444   TransformManager* mTxManager;
445   TransformId mId;
446   TransformManagerProperty mProperty;
447   mutable Vector3 mValue;
448 };
449
450 /**
451  * A Quaternion property used as input.
452  */
453 class TransformManagerQuaternionInput : public PropertyInputImpl
454 {
455 public:
456
457   /**
458    * Constructor
459    */
460   TransformManagerQuaternionInput()
461   :mTxManager(0),
462    mId(INVALID_TRANSFORM_ID),
463    mValue(1.0f,0.0f,0.0f,0.0f)
464   {
465   }
466
467   /**
468    * Virtual destructor.
469    */
470   virtual ~TransformManagerQuaternionInput()
471   {
472   }
473
474   /**
475    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
476    */
477   virtual Dali::Property::Type GetType() const
478   {
479     return Dali::PropertyTypes::Get<Quaternion>();
480   }
481
482   /**
483    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
484    */
485   virtual bool IsClean() const
486   {
487     return false;
488   }
489
490   /**
491    * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
492    */
493   virtual bool InputInitialized() const
494   {
495     return true;
496   }
497
498   /**
499    * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
500    * @note A constraint can only receive the inherited property from the previous frame.
501    */
502   virtual bool InputChanged() const
503   {
504     return true;
505   }
506
507   /**
508    * Helper function to get the orientation out of the world matrix.
509    * It stores the result in the mValue member variable
510    */
511   void ComputeTransformComponent() const
512   {
513     if( mTxManager )
514     {
515       const Matrix& worldMatrix = mTxManager->GetWorldMatrix(mId);
516       Vector3 position, scale;
517       worldMatrix.GetTransformComponents(position, mValue, scale);
518     }
519   }
520
521   /**
522    * @copydoc Dali::PropertyInput::GetQuaternion()
523    */
524   virtual const Quaternion& GetQuaternion( BufferIndex bufferIndex ) const
525   {
526     ComputeTransformComponent();
527     return mValue;
528   }
529
530   /**
531    * @copydoc Dali::PropertyInput::GetConstraintInputQuaternion()
532    */
533   virtual const Quaternion& GetConstraintInputQuaternion( BufferIndex bufferIndex ) const
534   {
535     ComputeTransformComponent();
536     return mValue;
537   }
538
539   /**
540    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
541    */
542   Quaternion& Get(size_t bufferIndex)
543   {
544     ComputeTransformComponent();
545     return mValue;
546   }
547
548   /**
549    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
550    */
551   const Quaternion& Get(size_t bufferIndex) const
552   {
553     ComputeTransformComponent();
554     return mValue;
555   }
556
557   /**
558    * Retrieve the property value.
559    * @param[in] bufferIndex The buffer to read.
560    * @return The property value.
561    */
562   const Quaternion& operator[](size_t bufferIndex) const
563   {
564     ComputeTransformComponent();
565     return mValue;
566   }
567
568   /**
569    * Initializes the property
570    * @param[in] transformManager Pointer to the transform manager
571    * @param[in] Id of the transformation the property is associated with
572    */
573   void Initialize( TransformManager* transformManager, TransformId id )
574   {
575     mTxManager = transformManager;
576     mId = id;
577   }
578
579   /**
580    * @copydoc Dali::Internal::PropertyInputImpl::IsTransformManagerProperty()
581    */
582   virtual bool IsTransformManagerProperty() const
583   {
584     return true;
585   }
586
587 private:
588
589   // Undefined
590   TransformManagerQuaternionInput(const TransformManagerQuaternionInput& property);
591
592   // Undefined
593   TransformManagerQuaternionInput& operator=(const TransformManagerQuaternionInput& rhs);
594
595 public:
596
597   TransformManager* mTxManager;
598   TransformId mId;
599   mutable Quaternion mValue;
600 };
601
602 /**
603  * A Matrix property used as input.
604  */
605 class TransformManagerMatrixInput : public PropertyInputImpl
606 {
607 public:
608
609   /**
610    * Constructor
611    */
612   TransformManagerMatrixInput()
613   :mTxManager(0),
614    mId(INVALID_TRANSFORM_ID)
615   {
616   }
617
618   /**
619    * Virtual destructor.
620    */
621   virtual ~TransformManagerMatrixInput()
622   {
623   }
624
625   /**
626    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
627    */
628   virtual Dali::Property::Type GetType() const
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   virtual bool InputInitialized() const
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   virtual bool InputChanged() const
654   {
655     return true;
656   }
657
658   /**
659    * @copydoc Dali::PropertyInput::GetMatrix()
660    */
661   virtual const Matrix& GetMatrix( BufferIndex bufferIndex ) const
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   virtual const Matrix& GetConstraintInputMatrix( BufferIndex bufferIndex ) const
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(size_t bufferIndex)
688   {
689     DALI_ASSERT_ALWAYS( mTxManager != 0 );
690     return mTxManager->GetWorldMatrix(mId);
691   }
692
693   /**
694    * @copydoc Dali::SceneGraph::PropertyInterface::Get()
695    */
696   const Matrix& Get(size_t 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[](size_t 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   virtual bool IsTransformManagerProperty() const
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_