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.
21 #include <dali-toolkit/internal/builder/builder-impl.h>
22 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
23 #include <dali-toolkit/internal/builder/replacement.h>
35 * Set a property value from a tree node.
36 * This function guesses the type of the property from the format of the string in the node.
37 * This is not always possible and could be surprising.
38 * @param node The node string to convert from
39 * @param value The property value to set
40 * @return true if the string could be converted.
42 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value );
45 * Set a property value from a tree node as SetPropertyFromNode() above
46 * @param node The node string to convert from
47 * @param value The property value to set
48 * @param replacement The overriding replacement map (if any)
49 * @return true if the string could be converted.
51 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value,
52 const Replacement& replacement );
55 * Set a property value as the given type from a tree node.
56 * @param node The node string to convert from
57 * @param type The property type to convert to.
58 * @param value The property value to set
59 * @return true if the string could be converted to the correct type.
61 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value );
64 * Set a property value as the given type from a tree node as SetPropertyFromNode() above
65 * @param node The node string to convert from
66 * @param type The property type to convert to.
67 * @param value The property value to set
68 * @param replacement The overriding replacement map (if any)
69 * @return true if the string could be converted to the correct type.
71 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value,
72 const Replacement& replacement );
79 * Converts a HTML style 'color' hex string ("#FF0000" for bright red) to a Vector4.
80 * The Vector4 alpha component will be set to 1.0f
81 * @param hexString The HTML style hex string
82 * @return a Vector4 containing the new color value
84 Vector4 HexStringToVector4( const char* s )
86 unsigned int value(0u);
87 std::istringstream( s ) >> std::hex >> value;
88 return Vector4( ((value >> 16 ) & 0xff ) / 255.0f,
89 ((value >> 8 ) & 0xff ) / 255.0f,
90 (value & 0xff ) / 255.0f,
98 * A property value type can be forced when its unknown by a disambiguation convention in the json
99 * ie "myarray": [1,2,3,4] ; would be a vector but
100 * "myarray": {'type-cast':'array', 'value':[1,2,3,4]} would be an array
101 * @param child The node whos string to search for a disambiguated type
102 * @param value The value to set
103 * @param overrideMap The user overriding constant map
104 * @param defaultMap The default map.
105 * @return True if child contained a disambiguated string that could be converted.
107 bool Disambiguated(const TreeNode& child, // ConstantLut& constantLut,
108 Dali::Property::Value& value,
109 const Replacement& replacement )
111 OptionalString childType = IsString( IsChild(child, "type-cast") );
112 OptionalChild childValue = IsChild(child, "value");
114 if( childType && childValue && (2 == child.Size()) )
116 // this case allows disambiguation but normally the type is guessed
117 // 2 == child.count() is an extra check as the user could have a user dictionary/map with
118 // type-cast and value keys. If they do then a work around is to add a bogus key to not run this case.
119 if(*childType == "boolean")
121 return SetPropertyFromNode( *childValue, Dali::Property::BOOLEAN, value, replacement);
123 else if(*childType == "float")
125 return SetPropertyFromNode( *childValue, Dali::Property::FLOAT, value, replacement);
127 else if(*childType == "vector2")
129 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR2, value, replacement);
131 else if(*childType == "vector3")
133 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR3, value, replacement);
135 else if(*childType == "vector4")
137 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR4, value, replacement);
139 else if(*childType == "rotation")
141 return SetPropertyFromNode( *childValue, Dali::Property::ROTATION, value, replacement);
143 else if(*childType == "rect")
145 return SetPropertyFromNode( *childValue, Dali::Property::RECTANGLE, value, replacement);
147 else if(*childType == "string")
149 return SetPropertyFromNode( *childValue, Dali::Property::STRING, value, replacement);
151 else if(*childType == "map")
153 return SetPropertyFromNode( *childValue, Dali::Property::MAP, value, replacement);
155 else if(*childType == "array")
157 return SetPropertyFromNode( *childValue, Dali::Property::ARRAY, value, replacement);
161 // else we failed to disambiguate
166 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value)
168 Replacement noReplacement;
169 return SetPropertyFromNode( node, type, value, noReplacement );
172 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value,
173 const Replacement& replacer )
179 case Property::BOOLEAN:
181 if( OptionalBoolean v = replacer.IsBoolean(node) )
188 case Property::FLOAT:
190 if( OptionalFloat v = replacer.IsFloat(node) )
197 case Property::INTEGER:
199 if( OptionalInteger v = replacer.IsInteger(node) )
206 case Property::UNSIGNED_INTEGER:
208 if( OptionalInteger v = replacer.IsInteger(node) )
210 if( *v >= 0 ) // with a loss of resolution....
218 case Property::VECTOR2:
220 if( OptionalVector2 v = replacer.IsVector2(node) )
227 case Property::VECTOR3:
229 if( OptionalVector3 v = replacer.IsVector3(node) )
236 case Property::VECTOR4:
238 if( OptionalVector4 v = replacer.IsVector4(node) )
243 else if( OptionalString s = replacer.IsString(node) )
245 if( (*s)[0] == '#' && 7 == (*s).size() )
247 value = HexStringToVector4( &(*s)[1] );
251 else if( TreeNode::OBJECT == node.GetType() )
253 // check for "r", "g" and "b" child color component nodes
254 OptionalInteger r = replacer.IsInteger( IsChild(node, "r") );
255 OptionalInteger g = replacer.IsInteger( IsChild(node, "g") );
256 OptionalInteger b = replacer.IsInteger( IsChild(node, "b") );
259 float red( (*r) * (1.0f/255.0f) );
260 float green( (*g) * (1.0f/255.0f) );
261 float blue( (*b) * (1.0f/255.0f) );
262 // check for optional "a" (alpha) node, default to fully opaque if it is not found.
264 OptionalInteger a = replacer.IsInteger( IsChild(node, "a") );
267 alpha = (*a) * (1.0f/255.0f);
269 value = Vector4( red, green, blue, alpha );
275 case Property::MATRIX3:
277 if( OptionalMatrix3 v = replacer.IsMatrix3(node) )
284 case Property::MATRIX:
286 if( OptionalMatrix v = replacer.IsMatrix(node) )
293 case Property::RECTANGLE:
295 if( OptionalRect v = replacer.IsRect(node) )
302 case Property::ROTATION:
306 if( OptionalVector4 ov = replacer.IsVector4(node) )
308 const Vector4& v = *ov;
309 // angle, axis as per spec
310 value = Quaternion(Radian(Degree(v[3])),
311 Vector3(v[0],v[1],v[2]));
317 // degrees Euler as per spec
318 if( OptionalVector3 v = replacer.IsVector3(node) )
320 value = Quaternion(Radian(Degree((*v).x)),
321 Radian(Degree((*v).y)),
322 Radian(Degree((*v).z)));
328 case Property::STRING:
330 if( OptionalString v = replacer.IsString(node) )
337 case Property::ARRAY:
339 if( replacer.IsArray( node, value ) )
345 value = Property::Value(Property::ARRAY);
347 TreeNode::ConstIterator iter(node.CBegin());
348 for( ; i < node.Size(); ++i, ++iter)
351 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
357 if( value.GetSize() == static_cast<int>(node.Size()) )
370 if( replacer.IsMap( node, value ) )
376 value = Property::Value(Property::MAP);
378 TreeNode::ConstIterator iter(node.CBegin());
379 for( ; i < node.Size(); ++i, ++iter)
382 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
384 value.SetValue( (*iter).first, v );
388 if( value.GetSize() == static_cast<int>(node.Size()) )
399 case Property::TYPE_COUNT:
409 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
412 Replacement replacer;
413 return SetPropertyFromNode( node, value, replacer );
416 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value,
417 const Replacement& replacer )
421 // some values are ambiguous as we have no Property::Type but can be disambiguated in the json
423 // Currently Rotations and Rectangle must always be disambiguated when a type isnt available
424 if( Disambiguated( node, value, replacer ) )
432 // our current heuristic for deciding an array is actually a vector and not say a map
433 // is to check if the values are all floats
434 bool allNumbers = true;
435 for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
437 OptionalFloat f = IsFloat((*iter).second);
447 // prefer finding vectors over presuming composite Property::Array...
448 if( OptionalMatrix v = IsMatrix(node) )
453 else if( OptionalMatrix3 v = IsMatrix3(node) )
458 else if( OptionalVector4 v = IsVector4(node) )
463 else if( OptionalVector3 v = IsVector3(node) )
468 else if( OptionalVector2 v = IsVector2(node) )
473 else if( 4 == node.Size() )
475 if( OptionalVector4 v = IsVector4(node) )
483 value = Property::Value(Property::ARRAY);
486 for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
488 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
499 // presume an array or map
500 // container of size 1
501 TreeNode::ConstIterator iter = node.CBegin();
503 // its seems legal with current json parser for a map to have an empty key
504 // but here we take that to mean the structure is a list
505 if( ((*iter).first) == 0 )
507 value = Property::Value(Property::ARRAY);
509 for(unsigned int i = 0; i < node.Size(); ++i, ++iter)
511 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
520 value = Property::Value(Property::MAP);
522 for(unsigned int i = 0; i < node.Size(); ++i, ++iter)
524 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
526 value.SetValue((*iter).first, v);
533 else // if( 0 == node.size() )
535 // no children so either one of bool, float, integer, string
536 OptionalBoolean aBool = replacer.IsBoolean(node);
537 OptionalInteger anInt = replacer.IsInteger(node);
538 OptionalFloat aFloat = replacer.IsFloat(node);
539 OptionalString aString = replacer.IsString(node);
543 // a bool is also an int but here we presume int
557 // Note: These are both floats and strings
560 // This means we can't have a string with purely numeric content without disambiguation.
573 // string always succeeds with the current json parser so its last
580 } // if( node.size() )
582 } // if Disambiguated()
585 } // bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
588 } // namespace Internal
590 } // namespace Toolkit