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