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.
21 #include <dali/public-api/adaptor-framework/common/color-controller.h>
24 #include <dali-toolkit/internal/builder/builder-impl.h>
25 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
26 #include <dali-toolkit/internal/builder/replacement.h>
39 * Set a property value from a tree node.
40 * This function guesses the type of the property from the format of the string in the node.
41 * This is not always possible and could be surprising.
42 * @param node The node string to convert from
43 * @param value The property value to set
44 * @return true if the string could be converted.
46 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value );
49 * Set a property value from a tree node as SetPropertyFromNode() above
50 * @param node The node string to convert from
51 * @param value The property value to set
52 * @param replacement The overriding replacement map (if any)
53 * @return true if the string could be converted.
55 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value,
56 const Replacement& replacement );
59 * Set a property value as the given type from a tree node.
60 * @param node The node string to convert from
61 * @param type The property type to convert to.
62 * @param value The property value to set
63 * @return true if the string could be converted to the correct type.
65 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value );
68 * Set a property value as the given type from a tree node as SetPropertyFromNode() above
69 * @param node The node string to convert from
70 * @param type The property type to convert to.
71 * @param value The property value to set
72 * @param replacement The overriding replacement map (if any)
73 * @return true if the string could be converted to the correct type.
75 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value,
76 const Replacement& replacement );
83 * Converts a HTML style 'color' hex string ("#FF0000" for bright red) to a Vector4.
84 * The Vector4 alpha component will be set to 1.0f
85 * @param hexString The HTML style hex string
86 * @return a Vector4 containing the new color value
88 Vector4 HexStringToVector4( const char* s )
90 unsigned int value(0u);
91 std::istringstream( s ) >> std::hex >> value;
92 return Vector4( ((value >> 16 ) & 0xff ) / 255.0f,
93 ((value >> 8 ) & 0xff ) / 255.0f,
94 (value & 0xff ) / 255.0f,
102 * A property value type can be forced when its unknown by a disambiguation convention in the json
103 * ie "myarray": [1,2,3,4] ; would be a vector but
104 * "myarray": {'type-cast':'array', 'value':[1,2,3,4]} would be an array
105 * @param child The node whos string to search for a disambiguated type
106 * @param value The value to set
107 * @param overrideMap The user overriding constant map
108 * @param defaultMap The default map.
109 * @return True if child contained a disambiguated string that could be converted.
111 bool Disambiguated(const TreeNode& child, // ConstantLut& constantLut,
112 Dali::Property::Value& value,
113 const Replacement& replacement )
115 OptionalString childType = IsString( IsChild(child, "type-cast") );
116 OptionalChild childValue = IsChild(child, "value");
118 if( childType && childValue && (2 == child.Size()) )
120 // this case allows disambiguation but normally the type is guessed
121 // 2 == child.count() is an extra check as the user could have a user dictionary/map with
122 // type-cast and value keys. If they do then a work around is to add a bogus key to not run this case.
123 if(*childType == "boolean")
125 return SetPropertyFromNode( *childValue, Dali::Property::BOOLEAN, value, replacement);
127 else if(*childType == "float")
129 return SetPropertyFromNode( *childValue, Dali::Property::FLOAT, value, replacement);
131 else if(*childType == "vector2")
133 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR2, value, replacement);
135 else if(*childType == "vector3")
137 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR3, value, replacement);
139 else if(*childType == "vector4")
141 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR4, value, replacement);
143 else if(*childType == "rotation")
145 return SetPropertyFromNode( *childValue, Dali::Property::ROTATION, value, replacement);
147 else if(*childType == "rect")
149 return SetPropertyFromNode( *childValue, Dali::Property::RECTANGLE, value, replacement);
151 else if(*childType == "string")
153 return SetPropertyFromNode( *childValue, Dali::Property::STRING, value, replacement);
155 else if(*childType == "map")
157 return SetPropertyFromNode( *childValue, Dali::Property::MAP, value, replacement);
159 else if(*childType == "array")
161 return SetPropertyFromNode( *childValue, Dali::Property::ARRAY, value, replacement);
165 // else we failed to disambiguate
170 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value)
172 Replacement noReplacement;
173 return SetPropertyFromNode( node, type, value, noReplacement );
176 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value,
177 const Replacement& replacer )
183 case Property::BOOLEAN:
185 if( OptionalBoolean v = replacer.IsBoolean(node) )
192 case Property::FLOAT:
194 if( OptionalFloat v = replacer.IsFloat(node) )
201 case Property::INTEGER:
203 if( OptionalInteger v = replacer.IsInteger(node) )
210 case Property::UNSIGNED_INTEGER:
212 if( OptionalUnsignedInt v = replacer.IsUnsignedInteger( node) )
214 if( *v >= 0 ) // with a loss of resolution....
222 case Property::VECTOR2:
224 if( OptionalVector2 v = replacer.IsVector2(node) )
231 case Property::VECTOR3:
233 if( OptionalVector3 v = replacer.IsVector3(node) )
240 case Property::VECTOR4:
242 if( OptionalVector4 v = replacer.IsVector4(node) )
247 else if( OptionalString s = replacer.IsString(node) )
249 if( (*s)[0] == '#' && 7 == (*s).size() )
251 value = HexStringToVector4( &(*s)[1] );
254 else if( Dali::ColorController::Get() )
257 done = Dali::ColorController::Get().RetrieveColor( *s, color );
261 else if( TreeNode::OBJECT == node.GetType() )
263 // check for "r", "g" and "b" child color component nodes
264 OptionalInteger r = replacer.IsInteger( IsChild(node, "r") );
265 OptionalInteger g = replacer.IsInteger( IsChild(node, "g") );
266 OptionalInteger b = replacer.IsInteger( IsChild(node, "b") );
269 float red( (*r) * (1.0f/255.0f) );
270 float green( (*g) * (1.0f/255.0f) );
271 float blue( (*b) * (1.0f/255.0f) );
272 // check for optional "a" (alpha) node, default to fully opaque if it is not found.
274 OptionalInteger a = replacer.IsInteger( IsChild(node, "a") );
277 alpha = (*a) * (1.0f/255.0f);
279 value = Vector4( red, green, blue, alpha );
285 case Property::MATRIX3:
287 if( OptionalMatrix3 v = replacer.IsMatrix3(node) )
294 case Property::MATRIX:
296 if( OptionalMatrix v = replacer.IsMatrix(node) )
303 case Property::RECTANGLE:
305 if( OptionalRect v = replacer.IsRect(node) )
312 case Property::ROTATION:
316 if( OptionalVector4 ov = replacer.IsVector4(node) )
318 const Vector4& v = *ov;
319 // angle, axis as per spec
320 value = Quaternion(Radian(Degree(v[3])),
321 Vector3(v[0],v[1],v[2]));
327 // degrees Euler as per spec
328 if( OptionalVector3 v = replacer.IsVector3(node) )
330 value = Quaternion(Radian(Degree((*v).x)),
331 Radian(Degree((*v).y)),
332 Radian(Degree((*v).z)));
338 case Property::STRING:
340 if( OptionalString v = replacer.IsString(node) )
347 case Property::ARRAY:
349 if( replacer.IsArray( node, value ) )
355 value = Property::Value(Property::ARRAY);
357 TreeNode::ConstIterator iter(node.CBegin());
358 for( ; i < node.Size(); ++i, ++iter)
361 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
367 if( value.GetSize() == static_cast<int>(node.Size()) )
380 if( replacer.IsMap( node, value ) )
386 value = Property::Value(Property::MAP);
388 TreeNode::ConstIterator iter(node.CBegin());
389 for( ; i < node.Size(); ++i, ++iter)
392 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
394 value.SetValue( (*iter).first, v );
398 if( value.GetSize() == static_cast<int>(node.Size()) )
409 case Property::TYPE_COUNT:
419 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
422 Replacement replacer;
423 return SetPropertyFromNode( node, value, replacer );
426 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value,
427 const Replacement& replacer )
431 // some values are ambiguous as we have no Property::Type but can be disambiguated in the json
433 // Currently Rotations and Rectangle must always be disambiguated when a type isnt available
434 if( Disambiguated( node, value, replacer ) )
442 // our current heuristic for deciding an array is actually a vector and not say a map
443 // is to check if the values are all floats
444 bool allNumbers = true;
445 for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
447 OptionalFloat f = IsFloat((*iter).second);
457 // prefer finding vectors over presuming composite Property::Array...
458 if( OptionalMatrix v = IsMatrix(node) )
463 else if( OptionalMatrix3 v = IsMatrix3(node) )
468 else if( OptionalVector4 v = IsVector4(node) )
473 else if( OptionalVector3 v = IsVector3(node) )
478 else if( OptionalVector2 v = IsVector2(node) )
483 else if( 4 == node.Size() )
485 if( OptionalVector4 v = IsVector4(node) )
493 value = Property::Value(Property::ARRAY);
496 for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
498 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
509 // presume an array or map
510 // container of size 1
511 TreeNode::ConstIterator iter = node.CBegin();
513 // its seems legal with current json parser for a map to have an empty key
514 // but here we take that to mean the structure is a list
515 if( ((*iter).first) == 0 )
517 value = Property::Value(Property::ARRAY);
519 for(unsigned int i = 0; i < node.Size(); ++i, ++iter)
521 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
530 value = Property::Value(Property::MAP);
532 for(unsigned int i = 0; i < node.Size(); ++i, ++iter)
534 if( SetPropertyFromNode( (*iter).second, v, replacer ) )
536 value.SetValue((*iter).first, v);
543 else // if( 0 == node.size() )
545 // no children so either one of bool, float, integer, string
546 OptionalBoolean aBool = replacer.IsBoolean(node);
547 OptionalInteger anInt = replacer.IsInteger(node);
548 OptionalFloat aFloat = replacer.IsFloat(node);
549 OptionalString aString = replacer.IsString(node);
553 // a bool is also an int but here we presume int
567 // Note: These are both floats and strings
570 // This means we can't have a string with purely numeric content without disambiguation.
583 // string always succeeds with the current json parser so its last
590 } // if( node.size() )
592 } // if Disambiguated()
595 } // bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
598 } // namespace Internal
600 } // namespace Toolkit