Merge "Added Scaling and cropping properties to the image visual mask" into devel...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / builder / builder-get-is.inl.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL__
2 #define __DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL__
3
4 /*
5  * Copyright (c) 2016 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-toolkit/internal/builder/builder-declarations.h>
23
24 inline OptionalChild IsChild(const TreeNode* node, const std::string& childName)
25 {
26   if( node )
27   {
28     const TreeNode* c = node->GetChild(childName);
29     if( NULL != c )
30     {
31       return OptionalChild( *c );
32     }
33     else
34     {
35       return OptionalChild();
36     }
37   }
38   else
39   {
40     return OptionalChild();
41   }
42 }
43
44 inline OptionalChild IsChildIgnoreCase(const TreeNode* node, const std::string& childName)
45 {
46   if( node )
47   {
48     const TreeNode* c = node->GetChildIgnoreCase(childName);
49     if( NULL != c )
50     {
51       return OptionalChild( *c );
52     }
53     else
54     {
55       return OptionalChild();
56     }
57   }
58   else
59   {
60     return OptionalChild();
61   }
62 }
63
64 inline OptionalChild IsChild(const TreeNode& node, const std::string& childName)
65 {
66   return IsChild(&node, childName);
67 }
68
69 inline OptionalChild IsChildIgnoreCase(const TreeNode& node, const std::string& childName)
70 {
71   return IsChildIgnoreCase(&node, childName);
72 }
73
74 inline OptionalString IsString(const OptionalChild& node)
75 {
76   if( node && (*node).GetType() == TreeNode::STRING )
77   {
78     return OptionalString((*node).GetString());
79   }
80   else
81   {
82     return OptionalString();
83   }
84 }
85
86 inline OptionalFloat IsFloat(const OptionalChild& node)
87 {
88   OptionalFloat ret;
89
90   if( node )
91   {
92     if( (*node).GetType() == TreeNode::FLOAT )
93     {
94       ret = (*node).GetFloat();
95     }
96     else if( (*node).GetType() == TreeNode::INTEGER )
97     {
98       // JSON has number not float/int but JsonParser discriminates.
99       // Here we don't care so we allow coercion
100       ret = static_cast<float>( (*node).GetInteger() );
101     }
102   }
103
104   return ret;
105 }
106
107 inline OptionalInteger IsInteger(const OptionalChild &node)
108 {
109   OptionalInteger ret;
110
111   if( node )
112   {
113     if( (*node).GetType() == TreeNode::INTEGER )
114     {
115       ret = (*node).GetInteger();
116     }
117     else if( (*node).GetType() == TreeNode::FLOAT )
118     {
119       ret = static_cast<int>(  (*node).GetFloat() );
120     }
121   }
122
123   return ret;
124 }
125
126 inline OptionalBoolean IsBoolean(const OptionalChild& node)
127 {
128   if( node && (*node).GetType() == TreeNode::BOOLEAN )
129   {
130     return OptionalBoolean(1 == (*node).GetInteger());
131   }
132   else
133   {
134     return OptionalBoolean();
135   }
136 }
137
138
139 // copy N Numbers
140 template <typename T>
141 inline bool CopyNumbers(TreeNode::ConstIterator iter, int N, T& vector)
142 {
143   for(int i = 0; i < N; ++i)
144   {
145     if( (*iter).second.GetType() == TreeNode::FLOAT)
146     {
147       vector[i] = (*iter).second.GetFloat();
148     }
149     else if( (*iter).second.GetType() == TreeNode::INTEGER )
150     {
151       vector[i] = static_cast<float>((*iter).second.GetInteger());
152     }
153     else
154     {
155       return false;
156     }
157     iter++;
158   }
159
160   return true;
161 }
162
163 inline OptionalVector4 IsVector4(const OptionalChild& node)
164 {
165   OptionalVector4 ret;
166
167   if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 4 )
168   {
169     Dali::Vector4 v;
170     if( CopyNumbers((*node).CBegin(), 4, v) )
171     {
172       ret = OptionalVector4(v);
173     }
174   }
175
176   return ret;
177 }
178
179 inline OptionalVector3 IsVector3(const OptionalChild& node)
180 {
181   OptionalVector3 ret;
182
183   if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 3 )
184   {
185     Dali::Vector3 v;
186     if( CopyNumbers((*node).CBegin(), 3, v) )
187     {
188       ret = OptionalVector3(v);
189     }
190   }
191
192   return ret;
193 }
194
195 inline OptionalVector2 IsVector2(const OptionalChild& node)
196 {
197   OptionalVector2 ret;
198
199   if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 2 )
200   {
201     Dali::Vector2 v;
202     if( CopyNumbers((*node).CBegin(), 2, v) )
203     {
204       ret = OptionalVector2(v);
205     }
206   }
207
208   return ret;
209 }
210
211 inline OptionalMatrix IsMatrix(const OptionalChild &node)
212 {
213   OptionalMatrix ret;
214
215   if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 16 )
216   {
217     float v[16];
218     if( CopyNumbers((*node).CBegin(), 16, v) )
219     {
220       ret = OptionalMatrix(Dali::Matrix(v));
221     }
222   }
223
224   return ret;
225 }
226
227 inline OptionalMatrix3 IsMatrix3(const OptionalChild& node)
228 {
229   OptionalMatrix3 ret;
230
231   if( node && (TreeNode::ARRAY == (*node).GetType()) && (*node).Size() >= 9 )
232   {
233     float v[9];
234     if( CopyNumbers((*node).CBegin(), 9, v) )
235     {
236       ret = OptionalMatrix3(Dali::Matrix3(v[0], v[1], v[2],
237                                           v[3], v[4], v[5],
238                                           v[6], v[7], v[8] ));
239     }
240   }
241
242   return ret;
243 }
244
245 inline OptionalRect IsRect(const OptionalChild& node)
246 {
247   OptionalRect ret;
248   if(node && (*node).Size())
249   {
250     if((*node).Size() >= 4)
251     {
252       TreeNode::ConstIterator iter((*node).CBegin());
253       int v[4];
254       if( CopyNumbers((*node).CBegin(), 4, v) )
255       {
256         ret = OptionalRect(Dali::Rect<int>(v[0], v[1], v[2], v[3]));
257       }
258     }
259   }
260   return ret;
261 }
262
263 //
264 //
265 //
266 inline OptionalString IsString( const TreeNode& parent, const std::string& childName)
267 {
268   return IsString( IsChild(&parent, childName) );
269 }
270
271 inline OptionalFloat IsFloat( const TreeNode& parent, const std::string& childName)
272 {
273   return IsFloat( IsChild(&parent, childName) );
274 }
275
276 inline OptionalInteger IsInteger( const TreeNode& parent, const std::string& childName)
277 {
278   return IsInteger( IsChild(&parent, childName) );
279 }
280
281 inline OptionalBoolean IsBoolean( const TreeNode& parent, const std::string& childName)
282 {
283   return IsBoolean( IsChild(parent, childName) );
284 }
285
286 inline OptionalVector4 IsVector4(const TreeNode &parent, const std::string& childName)
287 {
288   return IsVector4( IsChild(parent, childName) );
289 }
290
291 inline OptionalVector3 IsVector3(const TreeNode &parent, const std::string& childName)
292 {
293   return IsVector3( IsChild(parent, childName) );
294 }
295
296 inline OptionalVector2 IsVector2(const TreeNode &parent, const std::string& childName)
297 {
298   return IsVector2( IsChild(parent, childName) );
299 }
300
301 inline OptionalMatrix IsMatrix(const TreeNode &parent, const std::string& childName)
302 {
303   return IsMatrix( IsChild(parent, childName) );
304 }
305
306 inline OptionalMatrix3 IsMatrix3(const TreeNode &parent, const std::string& childName)
307 {
308   return IsMatrix3( IsChild(&parent, childName) );
309 }
310
311 inline OptionalRect IsRect(const TreeNode &parent, const std::string& childName)
312 {
313   return IsRect( IsChild(&parent, childName) );
314 }
315
316 //
317 //
318 //
319 inline OptionalString IsString( const TreeNode& node )
320 {
321   return IsString( OptionalChild( node ) );
322 }
323
324 inline OptionalFloat IsFloat( const TreeNode& node )
325 {
326   return IsFloat( OptionalChild( node ) );
327 }
328
329 inline OptionalInteger IsInteger( const TreeNode& node )
330 {
331   return IsInteger( OptionalChild( node ) );
332 }
333
334 inline OptionalBoolean IsBoolean( const TreeNode& node )
335 {
336   return IsBoolean( OptionalChild( node ) );
337 }
338
339 inline OptionalVector4 IsVector4(const TreeNode &node )
340 {
341   return IsVector4( OptionalChild( node ) );
342 }
343
344 inline OptionalVector3 IsVector3(const TreeNode &node )
345 {
346   return IsVector3( OptionalChild( node ) );
347 }
348
349 inline OptionalVector2 IsVector2(const TreeNode &node )
350 {
351   return IsVector2( OptionalChild( node ) );
352 }
353
354 inline OptionalMatrix IsMatrix(const TreeNode &node )
355 {
356   return IsMatrix( OptionalChild( node ) );
357 }
358
359 inline OptionalMatrix3 IsMatrix3(const TreeNode &node )
360 {
361   return IsMatrix3( OptionalChild( node ) );
362 }
363
364 inline OptionalRect IsRect(const TreeNode &node )
365 {
366   return IsRect( OptionalChild( node ) );
367 }
368
369 //
370 //
371 //
372 inline Dali::Vector4 GetVector4(const TreeNode &child)
373 {
374   OptionalVector4 v( IsVector4( OptionalChild( child ) ) );
375   DALI_ASSERT_ALWAYS(v);
376   return *v;
377 }
378
379 inline Dali::Vector3 GetVector3(const TreeNode &child)
380 {
381   OptionalVector3 v( IsVector3( OptionalChild( child ) ) );
382   DALI_ASSERT_ALWAYS(v);
383   return *v;
384 }
385
386 inline Dali::Vector2 GetVector2(const TreeNode &child)
387 {
388   OptionalVector2 v( IsVector2( OptionalChild( child ) ) );
389   DALI_ASSERT_ALWAYS(v);
390   return *v;
391 }
392
393 inline float GetFloat(const TreeNode &child)
394 {
395   OptionalFloat v( IsFloat( OptionalChild( child ) ) );
396   DALI_ASSERT_ALWAYS(v);
397   return *v;
398 }
399
400 inline bool GetBoolean(const TreeNode &child)
401 {
402   OptionalBoolean v( IsBoolean( OptionalChild( child ) ) );
403   DALI_ASSERT_ALWAYS(v);
404   return *v;
405 }
406
407 inline int GetInteger(const TreeNode &child)
408 {
409   OptionalInteger v( IsInteger( OptionalChild( child ) ) );
410   DALI_ASSERT_ALWAYS(v);
411   return *v;
412 }
413
414
415
416 #endif // __DALI_TOOLKIT_INTERNAL_BUILDER_GET_IS_INL__