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>
37 * Set a property value from a tree node.
38 * This function guesses the type of the property from the format of the string in the node.
39 * This is not always possible and could be surprising.
40 * @param node The node string to convert from
41 * @param value The property value to set
42 * @return true if the string could be converted.
44 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value );
47 * Set a property value from a tree node as SetPropertyFromNode() above
48 * @param node The node string to convert from
49 * @param value The property value to set
50 * @param replacement The overriding replacement map (if any)
51 * @return true if the string could be converted.
53 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value,
54 const Replacement& replacement );
57 * Set a property value as the given type from a tree node.
58 * @param node The node string to convert from
59 * @param type The property type to convert to.
60 * @param value The property value to set
61 * @return true if the string could be converted to the correct type.
63 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value );
66 * Set a property value as the given type from a tree node as SetPropertyFromNode() above
67 * @param node The node string to convert from
68 * @param type The property type to convert to.
69 * @param value The property value to set
70 * @param replacement The overriding replacement map (if any)
71 * @return true if the string could be converted to the correct type.
73 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value,
74 const Replacement& replacement );
81 * Converts a HTML style 'color' hex string ("#FF0000" for bright red) to a Vector4.
82 * The Vector4 alpha component will be set to 1.0f
83 * @param hexString The HTML style hex string
84 * @return a Vector4 containing the new color value
86 Vector4 HexStringToVector4( const char* s )
88 unsigned int value(0u);
89 std::istringstream( s ) >> std::hex >> value;
90 return Vector4( ((value >> 16 ) & 0xff ) / 255.0f,
91 ((value >> 8 ) & 0xff ) / 255.0f,
92 (value & 0xff ) / 255.0f,
100 * A property value type can be forced when its unknown by a disambiguation convention in the json
101 * ie "myarray": [1,2,3,4] ; would be a vector but
102 * "myarray": {'type-cast':'array', 'value':[1,2,3,4]} would be an array
103 * @param child The node whos string to search for a disambiguated type
104 * @param value The value to set
105 * @param overrideMap The user overriding constant map
106 * @param defaultMap The default map.
107 * @return True if child contained a disambiguated string that could be converted.
109 bool Disambiguated(const TreeNode& child, // ConstantLut& constantLut,
110 Dali::Property::Value& value,
111 const Replacement& replacement )
113 OptionalString childType = IsString( IsChild(child, "type-cast") );
114 OptionalChild childValue = IsChild(child, "value");
116 if( childType && childValue && (2 == child.Size()) )
118 // this case allows disambiguation but normally the type is guessed
119 // 2 == child.count() is an extra check as the user could have a user dictionary/map with
120 // type-cast and value keys. If they do then a work around is to add a bogus key to not run this case.
121 if(*childType == "boolean")
123 return SetPropertyFromNode( *childValue, Dali::Property::BOOLEAN, value, replacement);
125 else if(*childType == "float")
127 return SetPropertyFromNode( *childValue, Dali::Property::FLOAT, value, replacement);
129 else if(*childType == "vector2")
131 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR2, value, replacement);
133 else if(*childType == "vector3")
135 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR3, value, replacement);
137 else if(*childType == "vector4")
139 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR4, value, replacement);
141 else if(*childType == "rotation")
143 return SetPropertyFromNode( *childValue, Dali::Property::ROTATION, value, replacement);
145 else if(*childType == "rect")
147 return SetPropertyFromNode( *childValue, Dali::Property::RECTANGLE, value, replacement);
149 else if(*childType == "string")
151 return SetPropertyFromNode( *childValue, Dali::Property::STRING, value, replacement);
153 else if(*childType == "map")
155 return SetPropertyFromNode( *childValue, Dali::Property::MAP, value, replacement);
157 else if(*childType == "array")
159 return SetPropertyFromNode( *childValue, Dali::Property::ARRAY, value, replacement);
163 // else we failed to disambiguate
168 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value)
170 Replacement noReplacement;
171 return SetPropertyFromNode( node, type, value, noReplacement );
174 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value,
175 const Replacement& replacer )
181 case Property::BOOLEAN:
183 if( OptionalBoolean v = replacer.IsBoolean(node) )
190 case Property::FLOAT:
192 if( OptionalFloat v = replacer.IsFloat(node) )
199 case Property::INTEGER:
201 if( OptionalInteger v = replacer.IsInteger(node) )
208 case Property::UNSIGNED_INTEGER:
210 if( OptionalUnsignedInt v = replacer.IsUnsignedInteger( node) )
212 if( *v >= 0 ) // with a loss of resolution....
220 case Property::VECTOR2:
222 if( OptionalVector2 v = replacer.IsVector2(node) )
229 case Property::VECTOR3:
231 if( OptionalVector3 v = replacer.IsVector3(node) )
238 case Property::VECTOR4:
240 if( OptionalVector4 v = replacer.IsVector4(node) )
245 else if( OptionalString s = replacer.IsString(node) )
247 if( (*s)[0] == '#' && 7 == (*s).size() )
249 value = HexStringToVector4( &(*s)[1] );
252 else if( Dali::ColorController::Get() )
255 done = Dali::ColorController::Get().RetrieveColor( *s, color );
259 else if( TreeNode::OBJECT == node.GetType() )
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") );
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.
272 OptionalInteger a = replacer.IsInteger( IsChild(node, "a") );
275 alpha = (*a) * (1.0f/255.0f);
277 value = Vector4( red, green, blue, alpha );
283 case Property::MATRIX3:
285 if( OptionalMatrix3 v = replacer.IsMatrix3(node) )
292 case Property::MATRIX:
294 if( OptionalMatrix v = replacer.IsMatrix(node) )
301 case Property::RECTANGLE:
303 if( OptionalRect v = replacer.IsRect(node) )
310 case Property::ROTATION:
314 if( OptionalVector4 ov = replacer.IsVector4(node) )
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]));
325 // degrees Euler as per spec
326 if( OptionalVector3 v = replacer.IsVector3(node) )
328 value = Quaternion(Radian(Degree((*v).x)),
329 Radian(Degree((*v).y)),
330 Radian(Degree((*v).z)));
336 case Property::STRING:
338 if( OptionalString v = replacer.IsString(node) )
345 case Property::ARRAY:
347 if( replacer.IsArray( node, value ) )
353 value = Property::Value(Property::ARRAY);
355 TreeNode::ConstIterator iter(node.CBegin());
356 for( ; i < node.Size(); ++i, ++iter)
359 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
365 if( value.GetSize() == static_cast<int>(node.Size()) )
378 if( replacer.IsMap( node, value ) )
384 value = Property::Value(Property::MAP);
386 TreeNode::ConstIterator iter(node.CBegin());
387 for( ; i < node.Size(); ++i, ++iter)
390 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
392 value.SetValue( (*iter).first, v );
396 if( value.GetSize() == static_cast<int>(node.Size()) )
407 case Property::TYPE_COUNT:
417 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
420 Replacement replacer;
421 return SetPropertyFromNode( node, value, replacer );
424 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value,
425 const Replacement& replacer )
429 // some values are ambiguous as we have no Property::Type but can be disambiguated in the json
431 // Currently Rotations and Rectangle must always be disambiguated when a type isnt available
432 if( Disambiguated( node, value, replacer ) )
440 // our current heuristic for deciding an array is actually a vector and not say a map
441 // is to check if the values are all floats
442 bool allNumbers = true;
443 for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
445 OptionalFloat f = IsFloat((*iter).second);
455 // prefer finding vectors over presuming composite Property::Array...
456 if( OptionalMatrix v = IsMatrix(node) )
461 else if( OptionalMatrix3 v = IsMatrix3(node) )
466 else if( OptionalVector4 v = IsVector4(node) )
471 else if( OptionalVector3 v = IsVector3(node) )
476 else if( OptionalVector2 v = IsVector2(node) )
481 else if( 4 == node.Size() )
483 if( OptionalVector4 v = IsVector4(node) )
491 value = Property::Value(Property::ARRAY);
494 for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
496 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
507 // presume an array or map
508 // container of size 1
509 TreeNode::ConstIterator iter = node.CBegin();
511 // its seems legal with current json parser for a map to have an empty key
512 // but here we take that to mean the structure is a list
513 if( ((*iter).first) == 0 )
515 value = Property::Value(Property::ARRAY);
517 for(unsigned int i = 0; i < node.Size(); ++i, ++iter)
519 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
528 value = Property::Value(Property::MAP);
530 for(unsigned int i = 0; i < node.Size(); ++i, ++iter)
532 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
534 value.SetValue((*iter).first, v);
541 else // if( 0 == node.size() )
543 // no children so either one of bool, float, integer, string
544 OptionalBoolean aBool = replacer.IsBoolean(node);
545 OptionalInteger anInt = replacer.IsInteger(node);
546 OptionalFloat aFloat = replacer.IsFloat(node);
547 OptionalString aString = replacer.IsString(node);
551 // a bool is also an int but here we presume int
565 // Note: These are both floats and strings
568 // This means we can't have a string with purely numeric content without disambiguation.
581 // string always succeeds with the current json parser so its last
588 } // if( node.size() )
590 } // if Disambiguated()
593 } // bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
596 } // namespace Internal
598 } // namespace Toolkit