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