Changed SceneHolder's RenderTarget initialization
[platform/core/uifw/dali-core.git] / dali / internal / event / common / property-metadata.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/common/property-metadata.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/common/extents.h>
23 #include <dali/public-api/math/matrix.h>
24 #include <dali/public-api/math/matrix3.h>
25 #include <dali/public-api/math/quaternion.h>
26 #include <dali/public-api/math/rect.h>
27 #include <dali/public-api/math/vector2.h>
28 #include <dali/public-api/math/vector3.h>
29 #include <dali/public-api/math/vector4.h>
30 #include <dali/public-api/object/property-array.h>
31 #include <dali/public-api/object/property-map.h>
32 #include <dali/public-api/object/property.h>
33
34 namespace Dali
35 {
36 namespace Internal
37 {
38 namespace
39 {
40 /// Helper to adjust the property value by an amount specified in another property-value
41 template<typename PropertyType>
42 inline void AdjustProperty(Property::Value& currentPropertyValue, const Property::Value& relativePropertyValue)
43 {
44   PropertyType currentValue;
45   PropertyType relativeValue;
46   if(currentPropertyValue.Get(currentValue) && relativePropertyValue.Get(relativeValue))
47   {
48     currentPropertyValue = currentValue + relativeValue;
49   }
50 }
51
52 } // unnamed namespace
53
54 void PropertyMetadata::SetPropertyValue(const Property::Value& propertyValue)
55 {
56   switch(GetType())
57   {
58     case Property::NONE:
59     {
60       // NOOP
61       break;
62     }
63
64     case Property::RECTANGLE:
65     {
66       Rect<int32_t> convertedValue;
67       if(propertyValue.Get(convertedValue))
68       {
69         value = convertedValue;
70       }
71       break;
72     }
73
74     case Property::STRING:
75     {
76       std::string convertedValue;
77       if(propertyValue.Get(convertedValue))
78       {
79         value = convertedValue;
80       }
81       break;
82     }
83
84     case Property::ARRAY:
85     {
86       const Property::Array* array = propertyValue.GetArray();
87       if(array)
88       {
89         value = *array;
90       }
91       break;
92     }
93
94     case Property::MAP:
95     {
96       const Property::Map* map = propertyValue.GetMap();
97       if(map)
98       {
99         value = *map;
100       }
101       break;
102     }
103
104     case Property::EXTENTS:
105     {
106       Extents convertedValue;
107       if(propertyValue.Get(convertedValue))
108       {
109         value = convertedValue;
110       }
111       break;
112     }
113
114     case Property::BOOLEAN:
115     {
116       bool convertedValue;
117       if(propertyValue.Get(convertedValue))
118       {
119         value = convertedValue;
120       }
121       break;
122     }
123
124     case Property::INTEGER:
125     {
126       int32_t convertedValue;
127       if(propertyValue.Get(convertedValue))
128       {
129         value = convertedValue;
130       }
131       break;
132     }
133
134     case Property::FLOAT:
135     {
136       float convertedValue;
137       if(propertyValue.Get(convertedValue))
138       {
139         value = convertedValue;
140       }
141       break;
142     }
143
144     case Property::ROTATION:
145     {
146       Quaternion convertedValue;
147       if(propertyValue.Get(convertedValue))
148       {
149         value = convertedValue;
150       }
151       break;
152     }
153
154     case Property::MATRIX:
155     {
156       Matrix convertedValue;
157       if(propertyValue.Get(convertedValue))
158       {
159         value = convertedValue;
160       }
161       break;
162     }
163
164     case Property::MATRIX3:
165     {
166       Matrix3 convertedValue;
167       if(propertyValue.Get(convertedValue))
168       {
169         value = convertedValue;
170       }
171       break;
172     }
173
174     case Property::VECTOR2:
175     {
176       Vector2 vector2Value;
177       value.Get(vector2Value);
178
179       if(componentIndex == 0)
180       {
181         vector2Value.x = propertyValue.Get<float>();
182       }
183       else if(componentIndex == 1)
184       {
185         vector2Value.y = propertyValue.Get<float>();
186       }
187       else
188       {
189         propertyValue.Get(vector2Value);
190       }
191
192       value = vector2Value;
193       break;
194     }
195
196     case Property::VECTOR3:
197     {
198       Vector3 vector3Value;
199       value.Get(vector3Value);
200
201       if(componentIndex == 0)
202       {
203         vector3Value.x = propertyValue.Get<float>();
204       }
205       else if(componentIndex == 1)
206       {
207         vector3Value.y = propertyValue.Get<float>();
208       }
209       else if(componentIndex == 2)
210       {
211         vector3Value.z = propertyValue.Get<float>();
212       }
213       else
214       {
215         propertyValue.Get(vector3Value);
216       }
217
218       value = vector3Value;
219       break;
220     }
221
222     case Property::VECTOR4:
223     {
224       Vector4 vector4Value;
225       value.Get(vector4Value);
226
227       if(componentIndex == 0)
228       {
229         vector4Value.x = propertyValue.Get<float>();
230       }
231       else if(componentIndex == 1)
232       {
233         vector4Value.y = propertyValue.Get<float>();
234       }
235       else if(componentIndex == 2)
236       {
237         vector4Value.z = propertyValue.Get<float>();
238       }
239       else if(componentIndex == 3)
240       {
241         vector4Value.w = propertyValue.Get<float>();
242       }
243       else
244       {
245         propertyValue.Get(vector4Value);
246       }
247
248       value = vector4Value;
249       break;
250     }
251   }
252 }
253
254 Property::Value PropertyMetadata::GetPropertyValue() const
255 {
256   Property::Value propertyValue;
257
258   if(!IsAnimatable())
259   {
260     propertyValue = value;
261   }
262   else
263   {
264     switch(GetType())
265     {
266       case Property::NONE:
267       case Property::RECTANGLE:
268       case Property::STRING:
269       case Property::ARRAY:
270       case Property::MAP:
271       case Property::EXTENTS:
272       case Property::BOOLEAN:
273       case Property::INTEGER:
274       case Property::FLOAT:
275       case Property::MATRIX:
276       case Property::MATRIX3:
277       case Property::ROTATION:
278       {
279         propertyValue = value;
280         break;
281       }
282
283       case Property::VECTOR2:
284       {
285         Vector2 vector2Value;
286         value.Get(vector2Value);
287
288         if(componentIndex == 0)
289         {
290           propertyValue = vector2Value.x;
291         }
292         else if(componentIndex == 1)
293         {
294           propertyValue = vector2Value.y;
295         }
296         else
297         {
298           propertyValue = vector2Value;
299         }
300         break;
301       }
302
303       case Property::VECTOR3:
304       {
305         Vector3 vector3Value;
306         value.Get(vector3Value);
307
308         if(componentIndex == 0)
309         {
310           propertyValue = vector3Value.x;
311         }
312         else if(componentIndex == 1)
313         {
314           propertyValue = vector3Value.y;
315         }
316         else if(componentIndex == 2)
317         {
318           propertyValue = vector3Value.z;
319         }
320         else
321         {
322           propertyValue = vector3Value;
323         }
324         break;
325       }
326
327       case Property::VECTOR4:
328       {
329         Vector4 vector4Value;
330         value.Get(vector4Value);
331
332         if(componentIndex == 0)
333         {
334           propertyValue = vector4Value.x;
335         }
336         else if(componentIndex == 1)
337         {
338           propertyValue = vector4Value.y;
339         }
340         else if(componentIndex == 2)
341         {
342           propertyValue = vector4Value.z;
343         }
344         else if(componentIndex == 3)
345         {
346           propertyValue = vector4Value.w;
347         }
348         else
349         {
350           propertyValue = vector4Value;
351         }
352         break;
353       }
354     }
355   }
356
357   return propertyValue;
358 }
359
360 void PropertyMetadata::AdjustPropertyValueBy(const Property::Value& relativePropertyValue)
361 {
362   switch(GetType())
363   {
364     case Property::NONE:
365     case Property::RECTANGLE:
366     case Property::STRING:
367     case Property::ARRAY:
368     case Property::MAP:
369     case Property::EXTENTS:
370     case Property::MATRIX:
371     case Property::MATRIX3:
372     {
373       // Not animated
374       break;
375     }
376
377     case Property::BOOLEAN:
378     {
379       bool currentValue  = false;
380       bool relativeValue = false;
381       if(value.Get(currentValue) && relativePropertyValue.Get(relativeValue))
382       {
383         value = currentValue || relativeValue;
384       }
385       break;
386     }
387
388     case Property::INTEGER:
389     {
390       AdjustProperty<int>(value, relativePropertyValue);
391       break;
392     }
393
394     case Property::FLOAT:
395     {
396       AdjustProperty<float>(value, relativePropertyValue);
397       break;
398     }
399
400     case Property::ROTATION:
401     {
402       Quaternion currentValue;
403       Quaternion relativeValue;
404       if(value.Get(currentValue) && relativePropertyValue.Get(relativeValue))
405       {
406         value = currentValue * relativeValue;
407       }
408       break;
409     }
410
411     case Property::VECTOR2:
412     {
413       if(componentIndex == Property::INVALID_COMPONENT_INDEX)
414       {
415         AdjustProperty<Vector2>(value, relativePropertyValue);
416       }
417       else
418       {
419         Vector2 vector2Value;
420         value.Get(vector2Value);
421
422         if(componentIndex == 0)
423         {
424           vector2Value.x += relativePropertyValue.Get<float>();
425         }
426         else if(componentIndex == 1)
427         {
428           vector2Value.y += relativePropertyValue.Get<float>();
429         }
430
431         value = vector2Value;
432       }
433
434       break;
435     }
436
437     case Property::VECTOR3:
438     {
439       if(componentIndex == Property::INVALID_COMPONENT_INDEX)
440       {
441         AdjustProperty<Vector3>(value, relativePropertyValue);
442       }
443       else
444       {
445         Vector3 vector3Value;
446         value.Get(vector3Value);
447
448         if(componentIndex == 0)
449         {
450           vector3Value.x += relativePropertyValue.Get<float>();
451         }
452         else if(componentIndex == 1)
453         {
454           vector3Value.y += relativePropertyValue.Get<float>();
455         }
456         else if(componentIndex == 2)
457         {
458           vector3Value.z += relativePropertyValue.Get<float>();
459         }
460
461         value = vector3Value;
462       }
463       break;
464     }
465
466     case Property::VECTOR4:
467     {
468       if(componentIndex == Property::INVALID_COMPONENT_INDEX)
469       {
470         AdjustProperty<Vector4>(value, relativePropertyValue);
471       }
472       else
473       {
474         Vector4 vector4Value;
475         value.Get(vector4Value);
476
477         if(componentIndex == 0)
478         {
479           vector4Value.x += relativePropertyValue.Get<float>();
480         }
481         else if(componentIndex == 1)
482         {
483           vector4Value.y += relativePropertyValue.Get<float>();
484         }
485         else if(componentIndex == 2)
486         {
487           vector4Value.z += relativePropertyValue.Get<float>();
488         }
489         else if(componentIndex == 3)
490         {
491           vector4Value.w += relativePropertyValue.Get<float>();
492         }
493
494         value = vector4Value;
495       }
496       break;
497     }
498   }
499 }
500
501 } // namespace Internal
502
503 } // namespace Dali