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.
19 #include <dali-toolkit/internal/builder/builder-impl.h>
20 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
21 #include <dali-toolkit/internal/builder/replacement.h>
33 * Set a property value from a tree node.
34 * This function guesses the type of the property from the format of the string in the node.
35 * This is not always possible and could be surprising.
36 * @param node The node string to convert from
37 * @param value The property value to set
38 * @return true if the string could be converted.
40 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value );
43 * Set a property value from a tree node as SetPropertyFromNode() above
44 * @param node The node string to convert from
45 * @param value The property value to set
46 * @param replacement The overriding replacement map (if any)
47 * @return true if the string could be converted.
49 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value,
50 const Replacement& replacement );
53 * Set a property value as the given type from a tree node.
54 * @param node The node string to convert from
55 * @param type The property type to convert to.
56 * @param value The property value to set
57 * @return true if the string could be converted to the correct type.
59 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value );
62 * Set a property value as the given type from a tree node as SetPropertyFromNode() above
63 * @param node The node string to convert from
64 * @param type The property type to convert to.
65 * @param value The property value to set
66 * @param replacement The overriding replacement map (if any)
67 * @return true if the string could be converted to the correct type.
69 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value,
70 const Replacement& replacement );
77 * Converts a HTML style 'color' hex string ("#FF0000" for bright red) to a Vector4.
78 * The Vector4 alpha component will be set to 1.0f
79 * @param hexString The HTML style hex string
80 * @return a Vector4 containing the new color value
82 Vector4 HexStringToVector4( const char* s )
84 unsigned int value(0u);
85 std::istringstream( s ) >> std::hex >> value;
86 return Vector4( ((value >> 16 ) & 0xff ) / 255.0f,
87 ((value >> 8 ) & 0xff ) / 255.0f,
88 (value & 0xff ) / 255.0f,
96 * A property value type can be forced when its unknown by a disambiguation convention in the json
97 * ie "myarray": [1,2,3,4] ; would be a vector but
98 * "myarray": {'type-cast':'array', 'value':[1,2,3,4]} would be an array
99 * @param child The node whos string to search for a disambiguated type
100 * @param value The value to set
101 * @param overrideMap The user overriding constant map
102 * @param defaultMap The default map.
103 * @return True if child contained a disambiguated string that could be converted.
105 bool Disambiguated(const TreeNode& child, // ConstantLut& constantLut,
106 Dali::Property::Value& value,
107 const Replacement& replacement )
109 OptionalString childType = IsString( IsChild(child, "type-cast") );
110 OptionalChild childValue = IsChild(child, "value");
112 if( childType && childValue && (2 == child.Size()) )
114 // this case allows disambiguation but normally the type is guessed
115 // 2 == child.count() is an extra check as the user could have a user dictionary/map with
116 // type-cast and value keys. If they do then a work around is to add a bogus key to not run this case.
117 if(*childType == "boolean")
119 return SetPropertyFromNode( *childValue, Dali::Property::BOOLEAN, value, replacement);
121 else if(*childType == "float")
123 return SetPropertyFromNode( *childValue, Dali::Property::FLOAT, value, replacement);
125 else if(*childType == "vector2")
127 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR2, value, replacement);
129 else if(*childType == "vector3")
131 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR3, value, replacement);
133 else if(*childType == "vector4")
135 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR4, value, replacement);
137 else if(*childType == "rotation")
139 return SetPropertyFromNode( *childValue, Dali::Property::ROTATION, value, replacement);
141 else if(*childType == "rect")
143 return SetPropertyFromNode( *childValue, Dali::Property::RECTANGLE, value, replacement);
145 else if(*childType == "string")
147 return SetPropertyFromNode( *childValue, Dali::Property::STRING, value, replacement);
149 else if(*childType == "map")
151 return SetPropertyFromNode( *childValue, Dali::Property::MAP, value, replacement);
153 else if(*childType == "array")
155 return SetPropertyFromNode( *childValue, Dali::Property::ARRAY, value, replacement);
159 // else we failed to disambiguate
164 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value)
166 Replacement noReplacement;
167 return SetPropertyFromNode( node, type, value, noReplacement );
170 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value,
171 const Replacement& replacer )
177 case Property::BOOLEAN:
179 if( OptionalBoolean v = replacer.IsBoolean(node) )
186 case Property::FLOAT:
188 if( OptionalFloat v = replacer.IsFloat(node) )
195 case Property::INTEGER:
197 if( OptionalInteger v = replacer.IsInteger(node) )
204 case Property::UNSIGNED_INTEGER:
206 if( OptionalInteger v = replacer.IsInteger(node) )
208 if( *v >= 0 ) // with a loss of resolution....
216 case Property::VECTOR2:
218 if( OptionalVector2 v = replacer.IsVector2(node) )
225 case Property::VECTOR3:
227 if( OptionalVector3 v = replacer.IsVector3(node) )
234 case Property::VECTOR4:
236 if( OptionalVector4 v = replacer.IsVector4(node) )
241 else if( OptionalString s = replacer.IsString(node) )
243 if( (*s)[0] == '#' && 7 == (*s).size() )
245 value = HexStringToVector4( &(*s)[1] );
249 else if( TreeNode::OBJECT == node.GetType() )
251 // check for "r", "g" and "b" child color component nodes
252 OptionalInteger r = replacer.IsInteger( IsChild(node, "r") );
253 OptionalInteger g = replacer.IsInteger( IsChild(node, "g") );
254 OptionalInteger b = replacer.IsInteger( IsChild(node, "b") );
257 float red( (*r) * (1.0f/255.0f) );
258 float green( (*g) * (1.0f/255.0f) );
259 float blue( (*b) * (1.0f/255.0f) );
260 // check for optional "a" (alpha) node, default to fully opaque if it is not found.
262 OptionalInteger a = replacer.IsInteger( IsChild(node, "a") );
265 alpha = (*a) * (1.0f/255.0f);
267 value = Vector4( red, green, blue, alpha );
273 case Property::MATRIX3:
275 if( OptionalMatrix3 v = replacer.IsMatrix3(node) )
282 case Property::MATRIX:
284 if( OptionalMatrix v = replacer.IsMatrix(node) )
291 case Property::RECTANGLE:
293 if( OptionalRect v = replacer.IsRect(node) )
300 case Property::ROTATION:
304 if( OptionalVector4 ov = replacer.IsVector4(node) )
306 const Vector4& v = *ov;
307 // angle, axis as per spec
308 value = Quaternion(Radian(Degree(v[3])),
309 Vector3(v[0],v[1],v[2]));
315 // degrees Euler as per spec
316 if( OptionalVector3 v = replacer.IsVector3(node) )
318 value = Quaternion(Radian(Degree((*v).x)),
319 Radian(Degree((*v).y)),
320 Radian(Degree((*v).z)));
326 case Property::STRING:
328 if( OptionalString v = replacer.IsString(node) )
335 case Property::ARRAY:
337 if( replacer.IsArray( node, value ) )
343 value = Property::Value(Property::ARRAY);
345 TreeNode::ConstIterator iter(node.CBegin());
346 for( ; i < node.Size(); ++i, ++iter)
349 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
355 if( value.GetSize() == static_cast<int>(node.Size()) )
368 if( replacer.IsMap( node, value ) )
374 value = Property::Value(Property::MAP);
376 TreeNode::ConstIterator iter(node.CBegin());
377 for( ; i < node.Size(); ++i, ++iter)
380 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
382 value.SetValue( (*iter).first, v );
386 if( value.GetSize() == static_cast<int>(node.Size()) )
397 case Property::TYPE_COUNT:
407 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
410 Replacement replacer;
411 return SetPropertyFromNode( node, value, replacer );
414 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value,
415 const Replacement& replacer )
419 // some values are ambiguous as we have no Property::Type but can be disambiguated in the json
421 // Currently Rotations and Rectangle must always be disambiguated when a type isnt available
422 if( Disambiguated( node, value, replacer ) )
430 // our current heuristic for deciding an array is actually a vector and not say a map
431 // is to check if the values are all floats
432 bool allNumbers = true;
433 for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
435 OptionalFloat f = IsFloat((*iter).second);
445 // prefer finding vectors over presuming composite Property::Array...
446 if( OptionalMatrix v = IsMatrix(node) )
451 else if( OptionalMatrix3 v = IsMatrix3(node) )
456 else if( OptionalVector4 v = IsVector4(node) )
461 else if( OptionalVector3 v = IsVector3(node) )
466 else if( OptionalVector2 v = IsVector2(node) )
471 else if( 4 == node.Size() )
473 if( OptionalVector4 v = IsVector4(node) )
481 value = Property::Value(Property::ARRAY);
484 for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
486 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
497 // presume an array or map
498 // container of size 1
499 TreeNode::ConstIterator iter = node.CBegin();
501 // its seems legal with current json parser for a map to have an empty key
502 // but here we take that to mean the structure is a list
503 if( ((*iter).first) == 0 )
505 value = Property::Value(Property::ARRAY);
507 for(unsigned int i = 0; i < node.Size(); ++i, ++iter)
509 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
518 value = Property::Value(Property::MAP);
520 for(unsigned int i = 0; i < node.Size(); ++i, ++iter)
522 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
524 value.SetValue((*iter).first, v);
531 else // if( 0 == node.size() )
533 // no children so either one of bool, float, integer, string
534 OptionalBoolean aBool = replacer.IsBoolean(node);
535 OptionalInteger anInt = replacer.IsInteger(node);
536 OptionalFloat aFloat = replacer.IsFloat(node);
537 OptionalString aString = replacer.IsString(node);
541 // a bool is also an int but here we presume int
555 // Note: These are both floats and strings
558 // This means we can't have a string with purely numeric content without disambiguation.
571 // string always succeeds with the current json parser so its last
578 } // if( node.size() )
580 } // if Disambiguated()
583 } // bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
586 } // namespace Internal
588 } // namespace Toolkit