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