2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali-toolkit/internal/builder/builder-impl.h>
23 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
24 #include <dali-toolkit/internal/builder/replacement.h>
36 * Set a property value from a tree node.
37 * This function guesses the type of the property from the format of the string in the node.
38 * This is not always possible and could be surprising.
39 * @param node The node string to convert from
40 * @param value The property value to set
41 * @return true if the string could be converted.
43 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value );
46 * Set a property value from a tree node as SetPropertyFromNode() above
47 * @param node The node string to convert from
48 * @param value The property value to set
49 * @param replacement The overriding replacement map (if any)
50 * @return true if the string could be converted.
52 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value,
53 const Replacement& replacement );
56 * Set a property value as the given type from a tree node.
57 * @param node The node string to convert from
58 * @param type The property type to convert to.
59 * @param value The property value to set
60 * @return true if the string could be converted to the correct type.
62 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value );
65 * Set a property value as the given type from a tree node as SetPropertyFromNode() above
66 * @param node The node string to convert from
67 * @param type The property type to convert to.
68 * @param value The property value to set
69 * @param replacement The overriding replacement map (if any)
70 * @return true if the string could be converted to the correct type.
72 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value,
73 const Replacement& replacement );
80 * Converts a HTML style 'color' hex string ("#FF0000" for bright red) to a Vector4.
81 * The Vector4 alpha component will be set to 1.0f
82 * @param hexString The HTML style hex string
83 * @return a Vector4 containing the new color value
85 Vector4 HexStringToVector4( const char* s )
87 unsigned int value(0u);
88 std::istringstream( s ) >> std::hex >> value;
89 return Vector4( ((value >> 16 ) & 0xff ) / 255.0f,
90 ((value >> 8 ) & 0xff ) / 255.0f,
91 (value & 0xff ) / 255.0f,
99 * A property value type can be forced when its unknown by a disambiguation convention in the json
100 * ie "myarray": [1,2,3,4] ; would be a vector but
101 * "myarray": {'type-cast':'array', 'value':[1,2,3,4]} would be an array
102 * @param child The node whos string to search for a disambiguated type
103 * @param value The value to set
104 * @param overrideMap The user overriding constant map
105 * @param defaultMap The default map.
106 * @return True if child contained a disambiguated string that could be converted.
108 bool Disambiguated(const TreeNode& child, // ConstantLut& constantLut,
109 Dali::Property::Value& value,
110 const Replacement& replacement )
112 OptionalString childType = IsString( IsChild(child, "type-cast") );
113 OptionalChild childValue = IsChild(child, "value");
115 if( childType && childValue && (2 == child.Size()) )
117 // this case allows disambiguation but normally the type is guessed
118 // 2 == child.count() is an extra check as the user could have a user dictionary/map with
119 // type-cast and value keys. If they do then a work around is to add a bogus key to not run this case.
120 if(*childType == "boolean")
122 return SetPropertyFromNode( *childValue, Dali::Property::BOOLEAN, value, replacement);
124 else if(*childType == "float")
126 return SetPropertyFromNode( *childValue, Dali::Property::FLOAT, value, replacement);
128 else if(*childType == "vector2")
130 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR2, value, replacement);
132 else if(*childType == "vector3")
134 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR3, value, replacement);
136 else if(*childType == "vector4")
138 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR4, value, replacement);
140 else if(*childType == "rotation")
142 return SetPropertyFromNode( *childValue, Dali::Property::ROTATION, value, replacement);
144 else if(*childType == "rect")
146 return SetPropertyFromNode( *childValue, Dali::Property::RECTANGLE, value, replacement);
148 else if(*childType == "string")
150 return SetPropertyFromNode( *childValue, Dali::Property::STRING, value, replacement);
152 else if(*childType == "map")
154 return SetPropertyFromNode( *childValue, Dali::Property::MAP, value, replacement);
156 else if(*childType == "array")
158 return SetPropertyFromNode( *childValue, Dali::Property::ARRAY, value, replacement);
162 // else we failed to disambiguate
167 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value)
169 Replacement noReplacement;
170 return SetPropertyFromNode( node, type, value, noReplacement );
173 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value,
174 const Replacement& replacer )
180 case Property::BOOLEAN:
182 if( OptionalBoolean v = replacer.IsBoolean(node) )
189 case Property::FLOAT:
191 if( OptionalFloat v = replacer.IsFloat(node) )
198 case Property::INTEGER:
200 if( OptionalInteger v = replacer.IsInteger(node) )
207 case Property::UNSIGNED_INTEGER:
209 if( OptionalUnsignedInt v = replacer.IsUnsignedInteger( node) )
211 if( *v >= 0 ) // with a loss of resolution....
219 case Property::VECTOR2:
221 if( OptionalVector2 v = replacer.IsVector2(node) )
228 case Property::VECTOR3:
230 if( OptionalVector3 v = replacer.IsVector3(node) )
237 case Property::VECTOR4:
239 if( OptionalVector4 v = replacer.IsVector4(node) )
244 else if( OptionalString s = replacer.IsString(node) )
246 if( (*s)[0] == '#' && 7 == (*s).size() )
248 value = HexStringToVector4( &(*s)[1] );
252 else if( TreeNode::OBJECT == node.GetType() )
254 // check for "r", "g" and "b" child color component nodes
255 OptionalInteger r = replacer.IsInteger( IsChild(node, "r") );
256 OptionalInteger g = replacer.IsInteger( IsChild(node, "g") );
257 OptionalInteger b = replacer.IsInteger( IsChild(node, "b") );
260 float red( (*r) * (1.0f/255.0f) );
261 float green( (*g) * (1.0f/255.0f) );
262 float blue( (*b) * (1.0f/255.0f) );
263 // check for optional "a" (alpha) node, default to fully opaque if it is not found.
265 OptionalInteger a = replacer.IsInteger( IsChild(node, "a") );
268 alpha = (*a) * (1.0f/255.0f);
270 value = Vector4( red, green, blue, alpha );
276 case Property::MATRIX3:
278 if( OptionalMatrix3 v = replacer.IsMatrix3(node) )
285 case Property::MATRIX:
287 if( OptionalMatrix v = replacer.IsMatrix(node) )
294 case Property::RECTANGLE:
296 if( OptionalRect v = replacer.IsRect(node) )
303 case Property::ROTATION:
307 if( OptionalVector4 ov = replacer.IsVector4(node) )
309 const Vector4& v = *ov;
310 // angle, axis as per spec
311 value = Quaternion(Radian(Degree(v[3])),
312 Vector3(v[0],v[1],v[2]));
318 // degrees Euler as per spec
319 if( OptionalVector3 v = replacer.IsVector3(node) )
321 value = Quaternion(Radian(Degree((*v).x)),
322 Radian(Degree((*v).y)),
323 Radian(Degree((*v).z)));
329 case Property::STRING:
331 if( OptionalString v = replacer.IsString(node) )
338 case Property::ARRAY:
340 if( replacer.IsArray( node, value ) )
346 value = Property::Value(Property::ARRAY);
348 TreeNode::ConstIterator iter(node.CBegin());
349 for( ; i < node.Size(); ++i, ++iter)
352 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
358 if( value.GetSize() == static_cast<int>(node.Size()) )
371 if( replacer.IsMap( node, value ) )
377 value = Property::Value(Property::MAP);
379 TreeNode::ConstIterator iter(node.CBegin());
380 for( ; i < node.Size(); ++i, ++iter)
383 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
385 value.SetValue( (*iter).first, v );
389 if( value.GetSize() == static_cast<int>(node.Size()) )
400 case Property::TYPE_COUNT:
410 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
413 Replacement replacer;
414 return SetPropertyFromNode( node, value, replacer );
417 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value,
418 const Replacement& replacer )
422 // some values are ambiguous as we have no Property::Type but can be disambiguated in the json
424 // Currently Rotations and Rectangle must always be disambiguated when a type isnt available
425 if( Disambiguated( node, value, replacer ) )
433 // our current heuristic for deciding an array is actually a vector and not say a map
434 // is to check if the values are all floats
435 bool allNumbers = true;
436 for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
438 OptionalFloat f = IsFloat((*iter).second);
448 // prefer finding vectors over presuming composite Property::Array...
449 if( OptionalMatrix v = IsMatrix(node) )
454 else if( OptionalMatrix3 v = IsMatrix3(node) )
459 else if( OptionalVector4 v = IsVector4(node) )
464 else if( OptionalVector3 v = IsVector3(node) )
469 else if( OptionalVector2 v = IsVector2(node) )
474 else if( 4 == node.Size() )
476 if( OptionalVector4 v = IsVector4(node) )
484 value = Property::Value(Property::ARRAY);
487 for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
489 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
500 // presume an array or map
501 // container of size 1
502 TreeNode::ConstIterator iter = node.CBegin();
504 // its seems legal with current json parser for a map to have an empty key
505 // but here we take that to mean the structure is a list
506 if( ((*iter).first) == 0 )
508 value = Property::Value(Property::ARRAY);
510 for(unsigned int i = 0; i < node.Size(); ++i, ++iter)
512 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
521 value = Property::Value(Property::MAP);
523 for(unsigned int i = 0; i < node.Size(); ++i, ++iter)
525 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
527 value.SetValue((*iter).first, v);
534 else // if( 0 == node.size() )
536 // no children so either one of bool, float, integer, string
537 OptionalBoolean aBool = replacer.IsBoolean(node);
538 OptionalInteger anInt = replacer.IsInteger(node);
539 OptionalFloat aFloat = replacer.IsFloat(node);
540 OptionalString aString = replacer.IsString(node);
544 // a bool is also an int but here we presume int
558 // Note: These are both floats and strings
561 // This means we can't have a string with purely numeric content without disambiguation.
574 // string always succeeds with the current json parser so its last
581 } // if( node.size() )
583 } // if Disambiguated()
586 } // bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
589 } // namespace Internal
591 } // namespace Toolkit