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 );
81 * A property value type can be forced when its unknown by a disambiguation convention in the json
82 * ie "myarray": [1,2,3,4] ; would be a vector but
83 * "myarray": {'type-cast':'array', 'value':[1,2,3,4]} would be an array
84 * @param child The node whos string to search for a disambiguated type
85 * @param value The value to set
86 * @param overrideMap The user overriding constant map
87 * @param defaultMap The default map.
88 * @return True if child contained a disambiguated string that could be converted.
90 bool Disambiguated(const TreeNode& child, // ConstantLut& constantLut,
91 Dali::Property::Value& value,
92 const Replacement& replacement )
94 OptionalString childType = IsString( IsChild(child, "type-cast") );
95 OptionalChild childValue = IsChild(child, "value");
97 if( childType && childValue && (2 == child.Size()) )
99 // this case allows disambiguation but normally the type is guessed
100 // 2 == child.count() is an extra check as the user could have a user dictionary/map with
101 // type-cast and value keys. If they do then a work around is to add a bogus key to not run this case.
102 if(*childType == "boolean")
104 return SetPropertyFromNode( *childValue, Dali::Property::BOOLEAN, value, replacement);
106 else if(*childType == "float")
108 return SetPropertyFromNode( *childValue, Dali::Property::FLOAT, value, replacement);
110 else if(*childType == "vector2")
112 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR2, value, replacement);
114 else if(*childType == "vector3")
116 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR3, value, replacement);
118 else if(*childType == "vector4")
120 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR4, value, replacement);
122 else if(*childType == "rotation")
124 return SetPropertyFromNode( *childValue, Dali::Property::ROTATION, value, replacement);
126 else if(*childType == "rect")
128 return SetPropertyFromNode( *childValue, Dali::Property::RECTANGLE, value, replacement);
130 else if(*childType == "string")
132 return SetPropertyFromNode( *childValue, Dali::Property::STRING, value, replacement);
134 else if(*childType == "map")
136 return SetPropertyFromNode( *childValue, Dali::Property::MAP, value, replacement);
138 else if(*childType == "array")
140 return SetPropertyFromNode( *childValue, Dali::Property::ARRAY, value, replacement);
144 // else we failed to disambiguate
149 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value)
151 Replacement noReplacement;
152 return SetPropertyFromNode( node, type, value, noReplacement );
155 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value,
156 const Replacement& replacer )
162 case Property::BOOLEAN:
164 if( OptionalBoolean v = replacer.IsBoolean(node) )
171 case Property::FLOAT:
173 if( OptionalFloat v = replacer.IsFloat(node) )
180 case Property::INTEGER:
182 if( OptionalInteger v = replacer.IsInteger(node) )
189 case Property::UNSIGNED_INTEGER:
191 if( OptionalInteger v = replacer.IsInteger(node) )
193 if( *v >= 0 ) // with a loss of resolution....
201 case Property::VECTOR2:
203 if( OptionalVector2 v = replacer.IsVector2(node) )
210 case Property::VECTOR3:
212 if( OptionalVector3 v = replacer.IsVector3(node) )
219 case Property::VECTOR4:
221 if( OptionalVector4 v = replacer.IsVector4(node) )
228 case Property::MATRIX3:
230 if( OptionalMatrix3 v = replacer.IsMatrix3(node) )
237 case Property::MATRIX:
239 if( OptionalMatrix v = replacer.IsMatrix(node) )
246 case Property::RECTANGLE:
248 if( OptionalRect v = replacer.IsRect(node) )
255 case Property::ROTATION:
259 if( OptionalVector4 ov = replacer.IsVector4(node) )
261 const Vector4& v = *ov;
262 // angle, axis as per spec
263 value = Quaternion(Radian(Degree(v[3])),
264 Vector3(v[0],v[1],v[2]));
270 // degrees Euler as per spec
271 if( OptionalVector3 v = replacer.IsVector3(node) )
273 value = Quaternion(Radian(Degree((*v).x)),
274 Radian(Degree((*v).y)),
275 Radian(Degree((*v).z)));
281 case Property::STRING:
283 if( OptionalString v = replacer.IsString(node) )
290 case Property::ARRAY:
292 if( replacer.IsArray( node, value ) )
298 value = Property::Value(Property::ARRAY);
300 TreeNode::ConstIterator iter(node.CBegin());
301 for( ; i < node.Size(); ++i, ++iter)
304 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
310 if( value.GetSize() == static_cast<int>(node.Size()) )
323 if( replacer.IsMap( node, value ) )
329 value = Property::Value(Property::MAP);
331 TreeNode::ConstIterator iter(node.CBegin());
332 for( ; i < node.Size(); ++i, ++iter)
335 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
337 value.SetValue( (*iter).first, v );
341 if( value.GetSize() == static_cast<int>(node.Size()) )
352 case Property::TYPE_COUNT:
362 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
365 Replacement replacer;
366 return SetPropertyFromNode( node, value, replacer );
369 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value,
370 const Replacement& replacer )
374 // some values are ambiguous as we have no Property::Type but can be disambiguated in the json
376 // Currently Rotations and Rectangle must always be disambiguated when a type isnt available
377 if( Disambiguated( node, value, replacer ) )
385 // our current heuristic for deciding an array is actually a vector and not say a map
386 // is to check if the values are all floats
387 bool allNumbers = true;
388 for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
390 OptionalFloat f = IsFloat((*iter).second);
400 // prefer finding vectors over presuming composite Property::Array...
401 if( OptionalMatrix v = IsMatrix(node) )
406 else if( OptionalMatrix3 v = IsMatrix3(node) )
411 else if( OptionalVector4 v = IsVector4(node) )
416 else if( OptionalVector3 v = IsVector3(node) )
421 else if( OptionalVector2 v = IsVector2(node) )
426 else if( 4 == node.Size() )
428 if( OptionalVector4 v = IsVector4(node) )
436 value = Property::Value(Property::ARRAY);
439 for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
441 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
452 // presume an array or map
453 // container of size 1
454 TreeNode::ConstIterator iter = node.CBegin();
456 // its seems legal with current json parser for a map to have an empty key
457 // but here we take that to mean the structure is a list
458 if( ((*iter).first) == 0 )
460 value = Property::Value(Property::ARRAY);
462 for(unsigned int i = 0; i < node.Size(); ++i, ++iter)
464 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
473 value = Property::Value(Property::MAP);
475 for(unsigned int i = 0; i < node.Size(); ++i, ++iter)
477 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
479 value.SetValue((*iter).first, v);
486 else // if( 0 == node.size() )
488 // no children so either one of bool, float, integer, string
489 OptionalBoolean aBool = replacer.IsBoolean(node);
490 OptionalInteger anInt = replacer.IsInteger(node);
491 OptionalFloat aFloat = replacer.IsFloat(node);
492 OptionalString aString = replacer.IsString(node);
496 // a bool is also an int but here we presume int
510 // Note: These are both floats and strings
513 // This means we can't have a string with purely numeric content without disambiguation.
526 // string always succeeds with the current json parser so its last
533 } // if( node.size() )
535 } // if Disambiguated()
538 } // bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
541 } // namespace Internal
543 } // namespace Toolkit