[dali_2.3.26] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / update / animation / property-accessor.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_PROPERTY_ACCESSOR_H
2 #define DALI_INTERNAL_SCENE_GRAPH_PROPERTY_ACCESSOR_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 // INTERNAL INCLUDES
22 #include <dali/internal/update/common/animatable-property.h>
23 #include <dali/internal/update/manager/transform-manager-property.h>
24 #include <dali/public-api/common/dali-common.h>
25
26 namespace Dali
27 {
28 namespace Internal
29 {
30 /**
31  * A wrapper class for getting/setting a property.
32  * Animators use this instead of accessing properties directly.
33  */
34 template<typename PropertyType>
35 class PropertyAccessor
36 {
37 public:
38   /**
39    * Create a property component.
40    * @param [in] property The property to access.
41    */
42   PropertyAccessor(SceneGraph::PropertyBase* property)
43   : mProperty(static_cast<SceneGraph::AnimatableProperty<PropertyType>*>(property)) // we know the type
44   {
45   }
46
47   /**
48    * Non-virtual destructor; PropertyAccessor is not suitable as a base class.
49    */
50   ~PropertyAccessor() = default;
51
52   /**
53    * Query whether the accessor is set.
54    * @return True if set.
55    */
56   bool IsSet() const
57   {
58     return mProperty != nullptr;
59   }
60
61   /**
62    * Reset the property accessor
63    * @post Calling any other PropertyAccessor is invalid.
64    */
65   void Reset()
66   {
67     mProperty = nullptr;
68   }
69
70   /**
71    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
72    */
73   bool IsClean() const
74   {
75     return mProperty->IsClean();
76   }
77
78   /**
79    * Read access to the property.
80    * @param [in] bufferIndex The current update buffer index.
81    */
82   const PropertyType& Get(BufferIndex bufferIndex) const
83   {
84     DALI_ASSERT_DEBUG(nullptr != mProperty && "PropertyAccessor::Get() mProperty was nullptr");
85     const SceneGraph::AnimatableProperty<PropertyType>* property = mProperty;
86     return property->Get(bufferIndex);
87   }
88
89   /**
90    * @copydoc AnimatableProperty<float>::Set()
91    */
92   void Set(BufferIndex bufferIndex, const PropertyType& value) const
93   {
94     DALI_ASSERT_DEBUG(nullptr != mProperty && "PropertyAccessor::Set() mProperty was nullptr");
95     mProperty->Set(bufferIndex, value);
96   }
97
98   /**
99    * @copydoc AnimatableProperty<float>::Bake()
100    */
101   void Bake(BufferIndex bufferIndex, const PropertyType& value) const
102   {
103     DALI_ASSERT_DEBUG(nullptr != mProperty && "PropertyAccessor::Bake() mProperty was nullptr");
104     mProperty->Bake(bufferIndex, value);
105   }
106
107 private:
108   // Undefined
109   PropertyAccessor()                                 = delete;
110   PropertyAccessor(const PropertyAccessor& property) = delete;
111   PropertyAccessor& operator=(const PropertyAccessor& rhs) = delete;
112
113 private:
114   SceneGraph::AnimatableProperty<PropertyType>* mProperty; ///< The real property
115 };
116
117 /**
118  * A wrapper class for getting/setting a transform manager property
119  * Animators use this instead of accessing properties directly.
120  */
121 template<typename T>
122 class TransformManagerPropertyAccessor
123 {
124 public:
125   /**
126    * Create a property component.
127    * @param [in] property The property to access.
128    */
129   TransformManagerPropertyAccessor(SceneGraph::PropertyBase* property)
130   : mProperty(static_cast<SceneGraph::TransformManagerPropertyHandler<T>*>(property)) // we know the type
131   {
132   }
133
134   /**
135    * Non-virtual destructor; PropertyAccessor is not suitable as a base class.
136    */
137   ~TransformManagerPropertyAccessor() = default;
138
139   /**
140    * Query whether the accessor is set.
141    * @return True if set.
142    */
143   bool IsSet() const
144   {
145     return mProperty != nullptr;
146   }
147
148   /**
149    * Reset the property accessor
150    * @post Calling any other PropertyAccessor is invalid.
151    */
152   void Reset()
153   {
154     mProperty = nullptr;
155   }
156
157   /**
158    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
159    */
160   bool IsClean() const
161   {
162     return mProperty->IsClean();
163   }
164
165   /**
166    * Read access to the property.
167    * @param [in] bufferIndex The current update buffer index.
168    * @return The value of the property
169    */
170   const T& Get(BufferIndex bufferIndex) const
171   {
172     DALI_ASSERT_DEBUG(nullptr != mProperty && "PropertyAccessor::Get() mProperty was nullptr");
173     const SceneGraph::TransformManagerPropertyHandler<T>* property = mProperty;
174     return property->Get(bufferIndex);
175   }
176
177   /**
178    * @copydoc AnimatableProperty<float>::Set()
179    */
180   void Set(BufferIndex bufferIndex, const T& value) const
181   {
182     DALI_ASSERT_DEBUG(nullptr != mProperty && "PropertyAccessor::Set() mProperty was nullptr");
183     mProperty->Set(bufferIndex, value);
184   }
185
186   /**
187    * @copydoc AnimatableProperty<float>::Bake()
188    */
189   void Bake(BufferIndex bufferIndex, const T& value) const
190   {
191     DALI_ASSERT_DEBUG(nullptr != mProperty && "PropertyAccessor::Bake() mProperty was nullptr");
192     mProperty->Bake(bufferIndex, value);
193   }
194
195 private:
196   // Undefined
197   TransformManagerPropertyAccessor()                                                 = delete;
198   TransformManagerPropertyAccessor(const TransformManagerPropertyAccessor& property) = delete;
199   TransformManagerPropertyAccessor& operator=(const TransformManagerPropertyAccessor& rhs) = delete;
200
201 private:
202   SceneGraph::TransformManagerPropertyHandler<T>* mProperty; ///< The real property
203 };
204
205 /**
206  * A wrapper class for getting/setting a transform manager property component
207  * Animators use this instead of accessing properties directly.
208  */
209 template<typename T, uint32_t COMPONENT>
210 class TransformManagerPropertyComponentAccessor
211 {
212 public:
213   /**
214    * Create a property component.
215    * @param [in] property The property to access.
216    */
217   TransformManagerPropertyComponentAccessor(SceneGraph::PropertyBase* property)
218   : mProperty(static_cast<SceneGraph::TransformManagerPropertyHandler<T>*>(property)) // we know the type
219   {
220   }
221
222   /**
223    * Non-virtual destructor; PropertyAccessor is not suitable as a base class.
224    */
225   ~TransformManagerPropertyComponentAccessor() = default;
226
227   /**
228    * Query whether the accessor is set.
229    * @return True if set.
230    */
231   bool IsSet() const
232   {
233     return mProperty != nullptr;
234   }
235
236   /**
237    * Reset the property accessor
238    * @post Calling any other PropertyAccessor is invalid.
239    */
240   void Reset()
241   {
242     mProperty = nullptr;
243   }
244
245   /**
246    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
247    */
248   bool IsClean() const
249   {
250     return mProperty->IsClean();
251   }
252
253   /**
254    * Read access to the property.
255    * @param [in] bufferIndex The current update buffer index.
256    * @return The value of the component of the property
257    */
258   float Get(BufferIndex bufferIndex) const
259   {
260     DALI_ASSERT_DEBUG(nullptr != mProperty && "PropertyAccessor::Get() mProperty was nullptr");
261     const SceneGraph::TransformManagerPropertyHandler<T>* property = mProperty;
262     return property->GetFloatComponent(COMPONENT);
263   }
264
265   /**
266    * @copydoc AnimatableProperty<float>::Set()
267    */
268   void Set(BufferIndex bufferIndex, float value) const
269   {
270     DALI_ASSERT_DEBUG(nullptr != mProperty && "PropertyAccessor::Set() mProperty was nullptr");
271     mProperty->SetFloatComponent(value, COMPONENT);
272   }
273
274   /**
275    * @copydoc AnimatableProperty<float>::Bake()
276    */
277   void Bake(BufferIndex bufferIndex, float value) const
278   {
279     DALI_ASSERT_DEBUG(nullptr != mProperty && "PropertyAccessor::Bake() mProperty was nullptr");
280     mProperty->BakeFloatComponent(value, COMPONENT);
281   }
282
283 private:
284   // Undefined
285   TransformManagerPropertyComponentAccessor()                                                          = delete;
286   TransformManagerPropertyComponentAccessor(const TransformManagerPropertyComponentAccessor& property) = delete;
287   TransformManagerPropertyComponentAccessor& operator=(const TransformManagerPropertyComponentAccessor& rhs) = delete;
288
289 private:
290   SceneGraph::TransformManagerPropertyHandler<T>* mProperty; ///< The real property
291 };
292
293 } // namespace Internal
294
295 } // namespace Dali
296
297 #endif // DALI_INTERNAL_SCENE_GRAPH_PROPERTY_ACCESSOR_H