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