2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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
8 // http://floralicense.org/license/
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.
18 #include <dali-toolkit/internal/builder/builder-impl.h>
19 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
20 #include <dali-toolkit/internal/builder/replacement.h>
32 * Set a property value from a tree node.
33 * This function guesses the type of the property from the format of the string in the node.
34 * This is not always possible and could be surprising.
35 * @param node The node string to convert from
36 * @param value The property value to set
37 * @return true if the string could be converted.
39 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value );
42 * Set a property value from a tree node as SetPropertyFromNode() above
43 * @param node The node string to convert from
44 * @param value The property value to set
45 * @param replacement The overriding replacement map (if any)
46 * @return true if the string could be converted.
48 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value,
49 const Replacement& replacement );
52 * Set a property value as the given type from a tree node.
53 * @param node The node string to convert from
54 * @param type The property type to convert to.
55 * @param value The property value to set
56 * @return true if the string could be converted to the correct type.
58 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value );
61 * Set a property value as the given type from a tree node as SetPropertyFromNode() above
62 * @param node The node string to convert from
63 * @param type The property type to convert to.
64 * @param value The property value to set
65 * @param replacement The overriding replacement map (if any)
66 * @return true if the string could be converted to the correct type.
68 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value,
69 const Replacement& replacement );
76 * Converts a HTML style 'color' hex string ("#FF0000" for bright red) to a Vector4.
77 * The Vector4 alpha component will be set to 1.0f
78 * @param hexString The HTML style hex string
79 * @return a Vector4 containing the new color value
81 Vector4 HexStringToVector4( const char* s )
83 unsigned int value(0u);
84 std::istringstream( s ) >> std::hex >> value;
85 return Vector4( ((value >> 16 ) & 0xff ) / 255.0f,
86 ((value >> 8 ) & 0xff ) / 255.0f,
87 (value & 0xff ) / 255.0f,
95 * A property value type can be forced when its unknown by a disambiguation convention in the json
96 * ie "myarray": [1,2,3,4] ; would be a vector but
97 * "myarray": {'type-cast':'array', 'value':[1,2,3,4]} would be an array
98 * @param child The node whos string to search for a disambiguated type
99 * @param value The value to set
100 * @param overrideMap The user overriding constant map
101 * @param defaultMap The default map.
102 * @return True if child contained a disambiguated string that could be converted.
104 bool Disambiguated(const TreeNode& child, // ConstantLut& constantLut,
105 Dali::Property::Value& value,
106 const Replacement& replacement )
108 OptionalString childType = IsString( IsChild(child, "type-cast") );
109 OptionalChild childValue = IsChild(child, "value");
111 if( childType && childValue && (2 == child.Size()) )
113 // this case allows disambiguation but normally the type is guessed
114 // 2 == child.count() is an extra check as the user could have a user dictionary/map with
115 // type-cast and value keys. If they do then a work around is to add a bogus key to not run this case.
116 if(*childType == "boolean")
118 return SetPropertyFromNode( *childValue, Dali::Property::BOOLEAN, value, replacement);
120 else if(*childType == "float")
122 return SetPropertyFromNode( *childValue, Dali::Property::FLOAT, value, replacement);
124 else if(*childType == "vector2")
126 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR2, value, replacement);
128 else if(*childType == "vector3")
130 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR3, value, replacement);
132 else if(*childType == "vector4")
134 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR4, value, replacement);
136 else if(*childType == "rotation")
138 return SetPropertyFromNode( *childValue, Dali::Property::ROTATION, value, replacement);
140 else if(*childType == "rect")
142 return SetPropertyFromNode( *childValue, Dali::Property::RECTANGLE, value, replacement);
144 else if(*childType == "string")
146 return SetPropertyFromNode( *childValue, Dali::Property::STRING, value, replacement);
148 else if(*childType == "map")
150 return SetPropertyFromNode( *childValue, Dali::Property::MAP, value, replacement);
152 else if(*childType == "array")
154 return SetPropertyFromNode( *childValue, Dali::Property::ARRAY, value, replacement);
158 // else we failed to disambiguate
163 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value)
165 Replacement noReplacement;
166 return SetPropertyFromNode( node, type, value, noReplacement );
169 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value,
170 const Replacement& replacer )
176 case Property::BOOLEAN:
178 if( OptionalBoolean v = replacer.IsBoolean(node) )
185 case Property::FLOAT:
187 if( OptionalFloat v = replacer.IsFloat(node) )
194 case Property::INTEGER:
196 if( OptionalInteger v = replacer.IsInteger(node) )
203 case Property::UNSIGNED_INTEGER:
205 if( OptionalInteger v = replacer.IsInteger(node) )
207 if( *v >= 0 ) // with a loss of resolution....
215 case Property::VECTOR2:
217 if( OptionalVector2 v = replacer.IsVector2(node) )
224 case Property::VECTOR3:
226 if( OptionalVector3 v = replacer.IsVector3(node) )
233 case Property::VECTOR4:
235 if( OptionalVector4 v = replacer.IsVector4(node) )
240 else if( OptionalString s = replacer.IsString(node) )
242 if( (*s)[0] == '#' && 7 == (*s).size() )
244 value = HexStringToVector4( &(*s)[1] );
248 else if( TreeNode::OBJECT == node.GetType() )
250 // check for "r", "g" and "b" child color component nodes
251 OptionalInteger r = replacer.IsInteger( IsChild(node, "r") );
252 OptionalInteger g = replacer.IsInteger( IsChild(node, "g") );
253 OptionalInteger b = replacer.IsInteger( IsChild(node, "b") );
256 float red( (*r) * (1.0f/255.0f) );
257 float green( (*g) * (1.0f/255.0f) );
258 float blue( (*b) * (1.0f/255.0f) );
259 // check for optional "a" (alpha) node, default to fully opaque if it is not found.
261 OptionalInteger a = replacer.IsInteger( IsChild(node, "a") );
264 alpha = (*a) * (1.0f/255.0f);
266 value = Vector4( red, green, blue, alpha );
272 case Property::MATRIX3:
274 if( OptionalMatrix3 v = replacer.IsMatrix3(node) )
281 case Property::MATRIX:
283 if( OptionalMatrix v = replacer.IsMatrix(node) )
290 case Property::RECTANGLE:
292 if( OptionalRect v = replacer.IsRect(node) )
299 case Property::ROTATION:
303 if( OptionalVector4 ov = replacer.IsVector4(node) )
305 const Vector4& v = *ov;
306 // angle, axis as per spec
307 value = Quaternion(Radian(Degree(v[3])),
308 Vector3(v[0],v[1],v[2]));
314 // degrees Euler as per spec
315 if( OptionalVector3 v = replacer.IsVector3(node) )
317 value = Quaternion(Radian(Degree((*v).x)),
318 Radian(Degree((*v).y)),
319 Radian(Degree((*v).z)));
325 case Property::STRING:
327 if( OptionalString v = replacer.IsString(node) )
334 case Property::ARRAY:
336 if( replacer.IsArray( node, value ) )
342 value = Property::Value(Property::ARRAY);
344 TreeNode::ConstIterator iter(node.CBegin());
345 for( ; i < node.Size(); ++i, ++iter)
348 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
354 if( value.GetSize() == static_cast<int>(node.Size()) )
367 if( replacer.IsMap( node, value ) )
373 value = Property::Value(Property::MAP);
375 TreeNode::ConstIterator iter(node.CBegin());
376 for( ; i < node.Size(); ++i, ++iter)
379 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
381 value.SetValue( (*iter).first, v );
385 if( value.GetSize() == static_cast<int>(node.Size()) )
396 case Property::TYPE_COUNT:
406 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
409 Replacement replacer;
410 return SetPropertyFromNode( node, value, replacer );
413 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value,
414 const Replacement& replacer )
418 // some values are ambiguous as we have no Property::Type but can be disambiguated in the json
420 // Currently Rotations and Rectangle must always be disambiguated when a type isnt available
421 if( Disambiguated( node, value, replacer ) )
429 // our current heuristic for deciding an array is actually a vector and not say a map
430 // is to check if the values are all floats
431 bool allNumbers = true;
432 for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
434 OptionalFloat f = IsFloat((*iter).second);
444 // prefer finding vectors over presuming composite Property::Array...
445 if( OptionalMatrix v = IsMatrix(node) )
450 else if( OptionalMatrix3 v = IsMatrix3(node) )
455 else if( OptionalVector4 v = IsVector4(node) )
460 else if( OptionalVector3 v = IsVector3(node) )
465 else if( OptionalVector2 v = IsVector2(node) )
470 else if( 4 == node.Size() )
472 if( OptionalVector4 v = IsVector4(node) )
480 value = Property::Value(Property::ARRAY);
483 for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
485 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
496 // presume an array or map
497 // container of size 1
498 TreeNode::ConstIterator iter = node.CBegin();
500 // its seems legal with current json parser for a map to have an empty key
501 // but here we take that to mean the structure is a list
502 if( ((*iter).first) == 0 )
504 value = Property::Value(Property::ARRAY);
506 for(unsigned int i = 0; i < node.Size(); ++i, ++iter)
508 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
517 value = Property::Value(Property::MAP);
519 for(unsigned int i = 0; i < node.Size(); ++i, ++iter)
521 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
523 value.SetValue((*iter).first, v);
530 else // if( 0 == node.size() )
532 // no children so either one of bool, float, integer, string
533 OptionalBoolean aBool = replacer.IsBoolean(node);
534 OptionalInteger anInt = replacer.IsInteger(node);
535 OptionalFloat aFloat = replacer.IsFloat(node);
536 OptionalString aString = replacer.IsString(node);
540 // a bool is also an int but here we presume int
554 // Note: These are both floats and strings
557 // This means we can't have a string with purely numeric content without disambiguation.
570 // string always succeeds with the current json parser so its last
577 } // if( node.size() )
579 } // if Disambiguated()
582 } // bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
585 } // namespace Internal
587 } // namespace Toolkit