Fix prevent issues
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / builder / builder-set-property.cpp
1 /*
2  * Copyright (c) 2015 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 // EXTERNAL INCLUDES
19 #include <sstream>
20 #include <dali/public-api/object/property-array.h>
21 #include <dali/public-api/object/property-map.h>
22 #include <dali/devel-api/adaptor-framework/color-controller.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/internal/builder/builder-impl.h>
26 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
27 #include <dali-toolkit/internal/builder/replacement.h>
28
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Internal
37 {
38
39 /*
40  * Set a property value from a tree node.
41  * This function guesses the type of the property from the format of the string in the node.
42  * This is not always possible and could be surprising.
43  * @param node  The node string to convert from
44  * @param value The property value to set
45  * @return true if the string could be converted.
46  */
47 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value );
48
49 /*
50  * Set a property value from a tree node as SetPropertyFromNode() above
51  * @param node  The node string to convert from
52  * @param value The property value to set
53  * @param replacement The overriding replacement map (if any)
54  * @return true if the string could be converted.
55  */
56 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value,
57                           const Replacement& replacement );
58
59 /*
60  * Set a property value as the given type from a tree node.
61  * @param node The node string to convert from
62  * @param type The property type to convert to.
63  * @param value The property value to set
64  * @return true if the string could be converted to the correct type.
65  */
66 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value );
67
68 /*
69  * Set a property value as the given type from a tree node as SetPropertyFromNode() above
70  * @param node The node string to convert from
71  * @param type The property type to convert to.
72  * @param value The property value to set
73  * @param replacement The overriding replacement map (if any)
74  * @return true if the string could be converted to the correct type.
75  */
76 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value,
77                           const Replacement& replacement );
78
79
80 namespace
81 {
82
83 /**
84  * Converts a HTML style 'color' hex string ("#FF0000" for bright red) to a Vector4.
85  * The Vector4 alpha component will be set to 1.0f
86  * @param hexString The HTML style hex string
87  * @return a Vector4 containing the new color value
88  */
89 Vector4 HexStringToVector4( const char* s )
90 {
91   unsigned int value(0u);
92   std::istringstream( s ) >> std::hex >> value;
93   return Vector4( ((value >> 16 ) & 0xff ) / 255.0f,
94                   ((value >> 8 ) & 0xff ) / 255.0f,
95                   (value & 0xff ) / 255.0f,
96                   1.0f );
97 }
98
99 } // anon namespace
100
101
102 /**
103  * A property value type can be forced when its unknown by a disambiguation convention in the json
104  * ie  "myarray": [1,2,3,4] ; would be a vector but
105  *     "myarray": {'type-cast':'array', 'value':[1,2,3,4]} would be an array
106  * @param child The node whos string to search for a disambiguated type
107  * @param value The value to set
108  * @param overrideMap The user overriding constant map
109  * @param defaultMap The default map.
110  * @return True if child contained a disambiguated string that could be converted.
111  */
112 bool Disambiguated(const TreeNode& child, // ConstantLut& constantLut,
113                    Dali::Property::Value& value,
114                    const Replacement& replacement )
115 {
116   OptionalString childType = IsString( IsChild(child, "type-cast") );
117   OptionalChild childValue = IsChild(child, "value");
118
119   if( childType && childValue && (2 == child.Size()) )
120   {
121     // this case allows disambiguation but normally the type is guessed
122     // 2 == child.count() is an extra check as the user could have a user dictionary/map with
123     // type-cast and value keys. If they do then a work around is to add a bogus key to not run this case.
124     if(*childType == "boolean")
125     {
126       return SetPropertyFromNode( *childValue, Dali::Property::BOOLEAN, value, replacement);
127     }
128     else if(*childType == "float")
129     {
130       return SetPropertyFromNode( *childValue, Dali::Property::FLOAT, value, replacement);
131     }
132     else if(*childType == "vector2")
133     {
134       return SetPropertyFromNode( *childValue, Dali::Property::VECTOR2, value, replacement);
135     }
136     else if(*childType == "vector3")
137     {
138       return SetPropertyFromNode( *childValue, Dali::Property::VECTOR3, value, replacement);
139     }
140     else if(*childType == "vector4")
141     {
142       return SetPropertyFromNode( *childValue, Dali::Property::VECTOR4, value, replacement);
143     }
144     else if(*childType == "rotation")
145     {
146       return SetPropertyFromNode( *childValue, Dali::Property::ROTATION, value, replacement);
147     }
148     else if(*childType == "rect")
149     {
150       return SetPropertyFromNode( *childValue, Dali::Property::RECTANGLE, value, replacement);
151     }
152     else if(*childType == "string")
153     {
154       return SetPropertyFromNode( *childValue, Dali::Property::STRING, value, replacement);
155     }
156     else if(*childType == "map")
157     {
158       return SetPropertyFromNode( *childValue, Dali::Property::MAP, value, replacement);
159     }
160     else if(*childType == "array")
161     {
162       return SetPropertyFromNode( *childValue, Dali::Property::ARRAY, value, replacement);
163     }
164   }
165
166   // else we failed to disambiguate
167   return false;
168 }
169
170
171 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value)
172 {
173   Replacement noReplacement;
174   return SetPropertyFromNode( node, type, value, noReplacement );
175 }
176
177 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value,
178                           const Replacement& replacer )
179 {
180   bool done = false;
181
182   switch(type)
183   {
184     case Property::BOOLEAN:
185     {
186       if( OptionalBoolean v = replacer.IsBoolean(node) )
187       {
188         value = *v;
189         done = true;
190       }
191       break;
192     }
193     case Property::FLOAT:
194     {
195       if( OptionalFloat v = replacer.IsFloat(node) )
196       {
197         value = *v;
198         done = true;
199       }
200       break;
201     }
202     case Property::INTEGER:
203     {
204       if( OptionalInteger v = replacer.IsInteger(node) )
205       {
206         value = *v;
207         done = true;
208       }
209       break;
210     }
211     case Property::UNSIGNED_INTEGER:
212     {
213       if( OptionalUnsignedInt v = replacer.IsUnsignedInteger( node) )
214       {
215         value = *v;
216         done = true;
217       }
218       break;
219     }
220     case Property::VECTOR2:
221     {
222       if( OptionalVector2 v = replacer.IsVector2(node) )
223       {
224         value = *v;
225         done = true;
226       }
227       break;
228     }
229     case Property::VECTOR3:
230     {
231       if( OptionalVector3 v = replacer.IsVector3(node) )
232       {
233         value = *v;
234         done = true;
235       }
236       break;
237     }
238     case Property::VECTOR4:
239     {
240       if( OptionalVector4 v = replacer.IsVector4(node) )
241       {
242         value = *v;
243         done = true;
244       }
245       else if( OptionalString s = replacer.IsString(node) )
246       {
247         if( (*s)[0] == '#' && 7 == (*s).size() )
248         {
249           value = HexStringToVector4( &(*s)[1] );
250           done = true;
251         }
252         else if( Dali::ColorController::Get() )
253         {
254           Vector4 color;
255           done = Dali::ColorController::Get().RetrieveColor( *s, color );
256           value = color;
257         }
258       }
259       else if( TreeNode::OBJECT == node.GetType() )
260       {
261         // check for "r", "g" and "b" child color component nodes
262         OptionalInteger r = replacer.IsInteger( IsChild(node, "r") );
263         OptionalInteger g = replacer.IsInteger( IsChild(node, "g") );
264         OptionalInteger b = replacer.IsInteger( IsChild(node, "b") );
265         if( r && g && b )
266         {
267           float red( (*r) * (1.0f/255.0f) );
268           float green( (*g) * (1.0f/255.0f) );
269           float blue( (*b) * (1.0f/255.0f) );
270           // check for optional "a" (alpha) node, default to fully opaque if it is not found.
271           float alpha( 1.0f );
272           OptionalInteger a = replacer.IsInteger( IsChild(node, "a") );
273           if( a )
274           {
275             alpha = (*a) * (1.0f/255.0f);
276           }
277           value = Vector4( red, green, blue, alpha );
278           done = true;
279         }
280       }
281       break;
282     }
283     case Property::MATRIX3:
284     {
285       if( OptionalMatrix3 v = replacer.IsMatrix3(node) )
286       {
287         value = *v;
288         done = true;
289       }
290       break;
291     }
292     case Property::MATRIX:
293     {
294       if( OptionalMatrix v = replacer.IsMatrix(node) )
295       {
296         value = *v;
297         done = true;
298       }
299       break;
300     }
301     case Property::RECTANGLE:
302     {
303       if( OptionalRect v = replacer.IsRect(node) )
304       {
305         value = *v;
306         done = true;
307       }
308       break;
309     }
310     case Property::ROTATION:
311     {
312       if(4 == node.Size())
313       {
314         if( OptionalVector4 ov = replacer.IsVector4(node) )
315         {
316           const Vector4& v = *ov;
317           // angle, axis as per spec
318           value = Quaternion(Radian(Degree(v[3])),
319                              Vector3(v[0],v[1],v[2]));
320           done = true;
321         }
322       }
323       else
324       {
325         // degrees Euler as per spec
326         if( OptionalVector3 v = replacer.IsVector3(node) )
327         {
328           value = Quaternion(Radian(Degree((*v).x)),
329                              Radian(Degree((*v).y)),
330                              Radian(Degree((*v).z)));
331           done = true;
332         }
333       }
334       break;
335     }
336     case Property::STRING:
337     {
338       if( OptionalString v = replacer.IsString(node) )
339       {
340         value = *v;
341         done = true;
342       }
343       break;
344     }
345     case Property::ARRAY:
346     {
347       if( replacer.IsArray( node, value ) )
348       {
349         done = true;
350       }
351       else if(node.Size())
352       {
353         value = Property::Value(Property::ARRAY);
354         Property::Array* array = value.GetArray();
355         unsigned int i = 0;
356         TreeNode::ConstIterator iter(node.CBegin());
357         for( ; i < node.Size(); ++i, ++iter)
358         {
359           Property::Value childValue;
360           if( SetPropertyFromNode( (*iter).second, childValue, replacer ) )
361           {
362             array->PushBack( childValue );
363           }
364         }
365
366         if( array->Count() == node.Size() )
367         {
368           done = true;
369         }
370         else
371         {
372           done = false;
373         }
374       }
375       break;
376     }
377     case Property::MAP:
378     {
379       if( replacer.IsMap( node, value ) )
380       {
381         done = true;
382       }
383       else if(node.Size())
384       {
385         value = Property::Value(Property::MAP);
386         Property::Map* map = value.GetMap();
387         unsigned int i = 0;
388         TreeNode::ConstIterator iter(node.CBegin());
389         for( ; i < node.Size(); ++i, ++iter)
390         {
391           Property::Value childValue;
392           if( SetPropertyFromNode( (*iter).second, childValue, replacer ) )
393           {
394             map->Insert( (*iter).first, childValue );
395           }
396         }
397
398         if( map->Count() == node.Size() )
399         {
400           done = true;
401         }
402         else
403         {
404           done = false;
405         }
406       }
407       break;
408     }
409     case Property::NONE:
410     {
411       break;
412     }
413   } // switch type
414
415   return done;
416 }
417
418 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
419
420 {
421   Replacement replacer;
422   return SetPropertyFromNode( node, value, replacer );
423 }
424
425 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value,
426                           const Replacement& replacer )
427 {
428   bool done = false;
429
430   // some values are ambiguous as we have no Property::Type but can be disambiguated in the json
431
432   // Currently Rotations and Rectangle must always be disambiguated when a type isnt available
433   if( Disambiguated( node, value, replacer ) )
434   {
435     done = true;
436   }
437   else
438   {
439     if( node.Size() )
440     {
441       // our current heuristic for deciding an array is actually a vector and not say a map
442       // is to check if the values are all floats
443       bool allNumbers = true;
444       for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
445       {
446         OptionalFloat f = IsFloat((*iter).second);
447         if(!f)
448         {
449           allNumbers = false;
450           break;
451         }
452       }
453
454       if( allNumbers )
455       {
456         // prefer finding vectors over presuming composite Property::Array...
457         if( OptionalMatrix v = IsMatrix(node) )
458         {
459           value = *v;
460           done = true;
461         }
462         else if( OptionalMatrix3 v = IsMatrix3(node) )
463         {
464           value = *v;
465           done = true;
466         }
467         else if( OptionalVector4 v = IsVector4(node) )
468         {
469           value = *v;
470           done = true;
471         }
472         else if( OptionalVector3 v = IsVector3(node) )
473         {
474           value = *v;
475           done = true;
476         }
477         else if( OptionalVector2 v = IsVector2(node) )
478         {
479           value = *v;
480           done = true;
481         }
482         else if( 4 == node.Size() )
483         {
484           if( OptionalVector4 v = IsVector4(node) )
485           {
486             value = *v;
487             done = true;
488           }
489         }
490         else
491         {
492           value = Property::Value(Property::ARRAY);
493           Property::Array* array = value.GetArray();
494
495           for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
496           {
497             Property::Value childValue;
498             if( SetPropertyFromNode( (*iter).second, childValue, replacer ) )
499             {
500               array->PushBack( childValue );
501               done = true;
502             }
503           }
504         }
505       }
506
507       if(!done)
508       {
509         // presume an array or map
510         // container of size 1
511         TreeNode::ConstIterator iter = node.CBegin();
512
513         // its seems legal with current json parser for a map to have an empty key
514         // but here we take that to mean the structure is a list
515         if( ((*iter).first) == 0 )
516         {
517           value = Property::Value(Property::ARRAY);
518           Property::Array* array = value.GetArray();
519           for(unsigned int i = 0; i < node.Size(); ++i, ++iter)
520           {
521             Property::Value childValue;
522             if( SetPropertyFromNode( (*iter).second, childValue, replacer ) )
523             {
524               array->PushBack( childValue );
525               done = true;
526             }
527           }
528         }
529         else
530         {
531           value = Property::Value(Property::MAP);
532           Property::Map* map = value.GetMap();
533           for(unsigned int i = 0; i < node.Size(); ++i, ++iter)
534           {
535             Property::Value childValue;
536             if( SetPropertyFromNode( (*iter).second, childValue, replacer ) )
537             {
538               map->Insert( (*iter).first, childValue );
539               done = true;
540             }
541           }
542         }
543       } // if!done
544     } // if node.size()
545     else // if( 0 == node.size() )
546     {
547       // no children so either one of bool, float, integer, string
548       OptionalBoolean aBool    = replacer.IsBoolean(node);
549       OptionalInteger anInt    = replacer.IsInteger(node);
550       OptionalFloat   aFloat   = replacer.IsFloat(node);
551       OptionalString  aString  = replacer.IsString(node);
552
553       if(aBool)
554       {
555         // a bool is also an int but here we presume int
556         if(anInt)
557         {
558           value = *anInt;
559           done = true;
560         }
561         else
562         {
563           value = *aBool;
564           done = true;
565         }
566       }
567       else
568       {
569         // Note: These are both floats and strings
570         // {"value":"123"}
571         // {"value":123}
572         // This means we can't have a string with purely numeric content without disambiguation.
573         if(aFloat)
574         {
575           value = *aFloat;
576           done = true;
577         }
578         else if(anInt)
579         {
580           value = *anInt;
581           done = true;
582         }
583         else
584         {
585           // string always succeeds with the current json parser so its last
586           value = *aString;
587           done = true;
588         }
589
590       } // if aBool
591
592     } // if( node.size() )
593
594   } // if Disambiguated()
595
596   return done;
597 } // bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
598
599
600 } // namespace Internal
601
602 } // namespace Toolkit
603
604 } // namespace Dali