41e08488def239afc3ec176f053788f7b50c29ab
[platform/core/uifw/dali-core.git] / dali / internal / event / common / property-metadata.cpp
1 /*
2  * Copyright (c) 2017 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
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 namespace
35 {
36
37 /// Helper to adjust the property value by an amount specified in another property-value
38 template < typename PropertyType >
39 inline void AdjustProperty( Property::Value& currentPropertyValue, const Property::Value& relativePropertyValue )
40 {
41   PropertyType currentValue;
42   PropertyType relativeValue;
43   if( currentPropertyValue.Get( currentValue ) && relativePropertyValue.Get( relativeValue ) )
44   {
45     currentPropertyValue = currentValue + relativeValue;
46   }
47 }
48
49 } // unnamed namespace
50
51 void PropertyMetadata::SetPropertyValue( const Property::Value& propertyValue )
52 {
53   switch ( GetType() )
54   {
55     case Property::NONE:
56     case Property::RECTANGLE:
57     case Property::STRING:
58     case Property::ARRAY:
59     case Property::MAP:
60     case Property::BOOLEAN:
61     case Property::INTEGER:
62     case Property::FLOAT:
63     case Property::ROTATION:
64     case Property::MATRIX:
65     case Property::MATRIX3:
66     {
67       value = propertyValue;
68       break;
69     }
70
71     case Property::VECTOR2:
72     {
73       Vector2 vector2Value;
74       value.Get( vector2Value );
75
76       if( componentIndex == 0 )
77       {
78         vector2Value.x = propertyValue.Get< float >();
79       }
80       else if( componentIndex == 1 )
81       {
82         vector2Value.y = propertyValue.Get< float >();
83       }
84       else
85       {
86         propertyValue.Get( vector2Value );
87       }
88
89       value = vector2Value;
90       break;
91     }
92
93     case Property::VECTOR3:
94     {
95       Vector3 vector3Value;
96       value.Get( vector3Value );
97
98       if( componentIndex == 0 )
99       {
100         vector3Value.x = propertyValue.Get< float >();
101       }
102       else if( componentIndex == 1 )
103       {
104         vector3Value.y = propertyValue.Get< float >();
105       }
106       else if( componentIndex == 2 )
107       {
108         vector3Value.z = propertyValue.Get< float >();
109       }
110       else
111       {
112         propertyValue.Get( vector3Value );
113       }
114
115       value = vector3Value;
116       break;
117     }
118
119     case Property::VECTOR4:
120     {
121       Vector4 vector4Value;
122       value.Get( vector4Value );
123
124       if( componentIndex == 0 )
125       {
126         vector4Value.x = propertyValue.Get< float >();
127       }
128       else if( componentIndex == 1 )
129       {
130         vector4Value.y = propertyValue.Get< float >();
131       }
132       else if( componentIndex == 2 )
133       {
134         vector4Value.z = propertyValue.Get< float >();
135       }
136       else if( componentIndex == 3 )
137       {
138         vector4Value.w = propertyValue.Get< float >();
139       }
140       else
141       {
142         propertyValue.Get( vector4Value );
143       }
144
145       value = vector4Value;
146       break;
147     }
148   }
149 }
150
151 Property::Value PropertyMetadata::GetPropertyValue() const
152 {
153   Property::Value propertyValue;
154
155   if( !IsAnimatable() )
156   {
157     propertyValue = value;
158   }
159   else
160   {
161     switch ( GetType() )
162     {
163       case Property::NONE:
164       case Property::RECTANGLE:
165       case Property::STRING:
166       case Property::ARRAY:
167       case Property::MAP:
168       case Property::BOOLEAN:
169       case Property::INTEGER:
170       case Property::FLOAT:
171       case Property::MATRIX:
172       case Property::MATRIX3:
173       case Property::ROTATION:
174       {
175         propertyValue = value;
176         break;
177       }
178
179       case Property::VECTOR2:
180       {
181         Vector2 vector2Value;
182         value.Get( vector2Value );
183
184         if( componentIndex == 0 )
185         {
186           propertyValue = vector2Value.x;
187         }
188         else if( componentIndex == 1 )
189         {
190           propertyValue = vector2Value.y;
191         }
192         else
193         {
194           propertyValue = vector2Value;
195         }
196         break;
197       }
198
199       case Property::VECTOR3:
200       {
201         Vector3 vector3Value;
202         value.Get( vector3Value );
203
204         if( componentIndex == 0 )
205         {
206           propertyValue = vector3Value.x;
207         }
208         else if( componentIndex == 1 )
209         {
210           propertyValue = vector3Value.y;
211         }
212         else if( componentIndex == 2 )
213         {
214           propertyValue = vector3Value.z;
215         }
216         else
217         {
218           propertyValue = vector3Value;
219         }
220         break;
221       }
222
223       case Property::VECTOR4:
224       {
225         Vector4 vector4Value;
226         value.Get( vector4Value );
227
228         if( componentIndex == 0 )
229         {
230           propertyValue = vector4Value.x;
231         }
232         else if( componentIndex == 1 )
233         {
234           propertyValue = vector4Value.y;
235         }
236         else if( componentIndex == 2 )
237         {
238           propertyValue = vector4Value.z;
239         }
240         else if( componentIndex == 3 )
241         {
242           propertyValue = vector4Value.w;
243         }
244         else
245         {
246           propertyValue = vector4Value;
247         }
248         break;
249       }
250     }
251   }
252
253   return propertyValue;
254 }
255
256 void PropertyMetadata::AdjustPropertyValueBy( const Property::Value& relativePropertyValue )
257 {
258   switch ( GetType() )
259   {
260     case Property::NONE:
261     case Property::RECTANGLE:
262     case Property::STRING:
263     case Property::ARRAY:
264     case Property::MAP:
265     case Property::MATRIX:
266     case Property::MATRIX3:
267     {
268       // Not animated
269       break;
270     }
271
272     case Property::BOOLEAN:
273     {
274       bool currentValue = false;
275       bool relativeValue = false;
276       if( value.Get( currentValue ) && relativePropertyValue.Get( relativeValue ) )
277       {
278         value = currentValue || relativeValue;
279       }
280       break;
281     }
282
283     case Property::INTEGER:
284     {
285       AdjustProperty< int >( value, relativePropertyValue );
286       break;
287     }
288
289     case Property::FLOAT:
290     {
291       AdjustProperty< float >( value, relativePropertyValue );
292       break;
293     }
294
295     case Property::ROTATION:
296     {
297       Quaternion currentValue;
298       Quaternion relativeValue;
299       if( value.Get( currentValue ) && relativePropertyValue.Get( relativeValue ) )
300       {
301         value = currentValue * relativeValue;
302       }
303       break;
304     }
305
306     case Property::VECTOR2:
307     {
308       if( componentIndex == Property::INVALID_COMPONENT_INDEX )
309       {
310         AdjustProperty< Vector2 >( value, relativePropertyValue );
311       }
312       else
313       {
314         Vector2 vector2Value;
315         value.Get( vector2Value );
316
317         if( componentIndex == 0 )
318         {
319           vector2Value.x += relativePropertyValue.Get< float >();
320         }
321         else if( componentIndex == 1 )
322         {
323           vector2Value.y += relativePropertyValue.Get< float >();
324         }
325
326         value = vector2Value;
327       }
328
329       break;
330     }
331
332     case Property::VECTOR3:
333     {
334       if( componentIndex == Property::INVALID_COMPONENT_INDEX )
335       {
336         AdjustProperty< Vector3 >( value, relativePropertyValue );
337       }
338       else
339       {
340         Vector3 vector3Value;
341         value.Get( vector3Value );
342
343         if( componentIndex == 0 )
344         {
345           vector3Value.x += relativePropertyValue.Get< float >();
346         }
347         else if( componentIndex == 1 )
348         {
349           vector3Value.y += relativePropertyValue.Get< float >();
350         }
351         else if( componentIndex == 2 )
352         {
353           vector3Value.z += relativePropertyValue.Get< float >();
354         }
355
356         value = vector3Value;
357       }
358       break;
359     }
360
361     case Property::VECTOR4:
362     {
363       if( componentIndex == Property::INVALID_COMPONENT_INDEX )
364       {
365         AdjustProperty< Vector4 >( value, relativePropertyValue );
366       }
367       else
368       {
369         Vector4 vector4Value;
370         value.Get( vector4Value );
371
372         if( componentIndex == 0 )
373         {
374           vector4Value.x += relativePropertyValue.Get< float >();
375         }
376         else if( componentIndex == 1 )
377         {
378           vector4Value.y += relativePropertyValue.Get< float >();
379         }
380         else if( componentIndex == 2 )
381         {
382           vector4Value.z += relativePropertyValue.Get< float >();
383         }
384         else if( componentIndex == 3 )
385         {
386           vector4Value.w += relativePropertyValue.Get< float >();
387         }
388
389         value = vector4Value;
390       }
391       break;
392     }
393   }
394 }
395
396 } // namespace Internal
397
398 } // namespace Dali