2 * Copyright (c) 2015 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.
20 #include <dali/public-api/object/property-array.h>
21 #include <dali/public-api/object/property-map.h>
22 #include <dali/devel-api/adaptor-framework/color-controller.h>
25 #include <dali-toolkit/internal/builder/builder-impl.h>
26 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
27 #include <dali-toolkit/internal/builder/replacement.h>
40 * Set a property value from a tree node.
41 * This function guesses the type of the property from the format of the string in the node.
42 * This is not always possible and could be surprising.
43 * @param node The node string to convert from
44 * @param value The property value to set
45 * @return true if the string could be converted.
47 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value );
50 * Set a property value from a tree node as SetPropertyFromNode() above
51 * @param node The node string to convert from
52 * @param value The property value to set
53 * @param replacement The overriding replacement map (if any)
54 * @return true if the string could be converted.
56 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value,
57 const Replacement& replacement );
60 * Set a property value as the given type from a tree node.
61 * @param node The node string to convert from
62 * @param type The property type to convert to.
63 * @param value The property value to set
64 * @return true if the string could be converted to the correct type.
66 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value );
69 * Set a property value as the given type from a tree node as SetPropertyFromNode() above
70 * @param node The node string to convert from
71 * @param type The property type to convert to.
72 * @param value The property value to set
73 * @param replacement The overriding replacement map (if any)
74 * @return true if the string could be converted to the correct type.
76 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value,
77 const Replacement& replacement );
84 * Converts a HTML style 'color' hex string ("#FF0000" for bright red) to a Vector4.
85 * The Vector4 alpha component will be set to 1.0f
86 * @param hexString The HTML style hex string
87 * @return a Vector4 containing the new color value
89 Vector4 HexStringToVector4( const char* s )
91 unsigned int value(0u);
92 std::istringstream( s ) >> std::hex >> value;
93 return Vector4( ((value >> 16 ) & 0xff ) / 255.0f,
94 ((value >> 8 ) & 0xff ) / 255.0f,
95 (value & 0xff ) / 255.0f,
103 * A property value type can be forced when its unknown by a disambiguation convention in the json
104 * ie "myarray": [1,2,3,4] ; would be a vector but
105 * "myarray": {'type-cast':'array', 'value':[1,2,3,4]} would be an array
106 * @param child The node whos string to search for a disambiguated type
107 * @param value The value to set
108 * @param overrideMap The user overriding constant map
109 * @param defaultMap The default map.
110 * @return True if child contained a disambiguated string that could be converted.
112 bool Disambiguated(const TreeNode& child, // ConstantLut& constantLut,
113 Dali::Property::Value& value,
114 const Replacement& replacement )
116 OptionalString childType = IsString( IsChild(child, "type-cast") );
117 OptionalChild childValue = IsChild(child, "value");
119 if( childType && childValue && (2 == child.Size()) )
121 // this case allows disambiguation but normally the type is guessed
122 // 2 == child.count() is an extra check as the user could have a user dictionary/map with
123 // type-cast and value keys. If they do then a work around is to add a bogus key to not run this case.
124 if(*childType == "boolean")
126 return SetPropertyFromNode( *childValue, Dali::Property::BOOLEAN, value, replacement);
128 else if(*childType == "float")
130 return SetPropertyFromNode( *childValue, Dali::Property::FLOAT, value, replacement);
132 else if(*childType == "vector2")
134 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR2, value, replacement);
136 else if(*childType == "vector3")
138 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR3, value, replacement);
140 else if(*childType == "vector4")
142 return SetPropertyFromNode( *childValue, Dali::Property::VECTOR4, value, replacement);
144 else if(*childType == "rotation")
146 return SetPropertyFromNode( *childValue, Dali::Property::ROTATION, value, replacement);
148 else if(*childType == "rect")
150 return SetPropertyFromNode( *childValue, Dali::Property::RECTANGLE, value, replacement);
152 else if(*childType == "string")
154 return SetPropertyFromNode( *childValue, Dali::Property::STRING, value, replacement);
156 else if(*childType == "map")
158 return SetPropertyFromNode( *childValue, Dali::Property::MAP, value, replacement);
160 else if(*childType == "array")
162 return SetPropertyFromNode( *childValue, Dali::Property::ARRAY, value, replacement);
166 // else we failed to disambiguate
171 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value)
173 Replacement noReplacement;
174 return SetPropertyFromNode( node, type, value, noReplacement );
177 bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value,
178 const Replacement& replacer )
184 case Property::BOOLEAN:
186 if( OptionalBoolean v = replacer.IsBoolean(node) )
193 case Property::FLOAT:
195 if( OptionalFloat v = replacer.IsFloat(node) )
202 case Property::INTEGER:
204 if( OptionalInteger v = replacer.IsInteger(node) )
211 case Property::UNSIGNED_INTEGER:
213 if( OptionalUnsignedInt v = replacer.IsUnsignedInteger( node) )
215 if( *v >= 0 ) // with a loss of resolution....
223 case Property::VECTOR2:
225 if( OptionalVector2 v = replacer.IsVector2(node) )
232 case Property::VECTOR3:
234 if( OptionalVector3 v = replacer.IsVector3(node) )
241 case Property::VECTOR4:
243 if( OptionalVector4 v = replacer.IsVector4(node) )
248 else if( OptionalString s = replacer.IsString(node) )
250 if( (*s)[0] == '#' && 7 == (*s).size() )
252 value = HexStringToVector4( &(*s)[1] );
255 else if( Dali::ColorController::Get() )
258 done = Dali::ColorController::Get().RetrieveColor( *s, color );
262 else if( TreeNode::OBJECT == node.GetType() )
264 // check for "r", "g" and "b" child color component nodes
265 OptionalInteger r = replacer.IsInteger( IsChild(node, "r") );
266 OptionalInteger g = replacer.IsInteger( IsChild(node, "g") );
267 OptionalInteger b = replacer.IsInteger( IsChild(node, "b") );
270 float red( (*r) * (1.0f/255.0f) );
271 float green( (*g) * (1.0f/255.0f) );
272 float blue( (*b) * (1.0f/255.0f) );
273 // check for optional "a" (alpha) node, default to fully opaque if it is not found.
275 OptionalInteger a = replacer.IsInteger( IsChild(node, "a") );
278 alpha = (*a) * (1.0f/255.0f);
280 value = Vector4( red, green, blue, alpha );
286 case Property::MATRIX3:
288 if( OptionalMatrix3 v = replacer.IsMatrix3(node) )
295 case Property::MATRIX:
297 if( OptionalMatrix v = replacer.IsMatrix(node) )
304 case Property::RECTANGLE:
306 if( OptionalRect v = replacer.IsRect(node) )
313 case Property::ROTATION:
317 if( OptionalVector4 ov = replacer.IsVector4(node) )
319 const Vector4& v = *ov;
320 // angle, axis as per spec
321 value = Quaternion(Radian(Degree(v[3])),
322 Vector3(v[0],v[1],v[2]));
328 // degrees Euler as per spec
329 if( OptionalVector3 v = replacer.IsVector3(node) )
331 value = Quaternion(Radian(Degree((*v).x)),
332 Radian(Degree((*v).y)),
333 Radian(Degree((*v).z)));
339 case Property::STRING:
341 if( OptionalString v = replacer.IsString(node) )
348 case Property::ARRAY:
350 if( replacer.IsArray( node, value ) )
356 value = Property::Value(Property::ARRAY);
357 Property::Array* array = value.GetArray();
359 TreeNode::ConstIterator iter(node.CBegin());
360 for( ; i < node.Size(); ++i, ++iter)
362 Property::Value childValue;
363 if( SetPropertyFromNode( (*iter).second, childValue, replacer ) )
365 array->PushBack( childValue );
369 if( array->Count() == node.Size() )
382 if( replacer.IsMap( node, value ) )
388 value = Property::Value(Property::MAP);
389 Property::Map* map = value.GetMap();
391 TreeNode::ConstIterator iter(node.CBegin());
392 for( ; i < node.Size(); ++i, ++iter)
394 Property::Value childValue;
395 if( SetPropertyFromNode( (*iter).second, childValue, replacer ) )
397 map->Insert( (*iter).first, childValue );
401 if( map->Count() == node.Size() )
421 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
424 Replacement replacer;
425 return SetPropertyFromNode( node, value, replacer );
428 bool SetPropertyFromNode( const TreeNode& node, Property::Value& value,
429 const Replacement& replacer )
433 // some values are ambiguous as we have no Property::Type but can be disambiguated in the json
435 // Currently Rotations and Rectangle must always be disambiguated when a type isnt available
436 if( Disambiguated( node, value, replacer ) )
444 // our current heuristic for deciding an array is actually a vector and not say a map
445 // is to check if the values are all floats
446 bool allNumbers = true;
447 for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
449 OptionalFloat f = IsFloat((*iter).second);
459 // prefer finding vectors over presuming composite Property::Array...
460 if( OptionalMatrix v = IsMatrix(node) )
465 else if( OptionalMatrix3 v = IsMatrix3(node) )
470 else if( OptionalVector4 v = IsVector4(node) )
475 else if( OptionalVector3 v = IsVector3(node) )
480 else if( OptionalVector2 v = IsVector2(node) )
485 else if( 4 == node.Size() )
487 if( OptionalVector4 v = IsVector4(node) )
495 value = Property::Value(Property::ARRAY);
496 Property::Array* array = value.GetArray();
498 for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
500 Property::Value childValue;
501 if( SetPropertyFromNode( (*iter).second, childValue, replacer ) )
503 array->PushBack( childValue );
512 // presume an array or map
513 // container of size 1
514 TreeNode::ConstIterator iter = node.CBegin();
516 // its seems legal with current json parser for a map to have an empty key
517 // but here we take that to mean the structure is a list
518 if( ((*iter).first) == 0 )
520 value = Property::Value(Property::ARRAY);
521 Property::Array* array = value.GetArray();
522 for(unsigned int i = 0; i < node.Size(); ++i, ++iter)
524 Property::Value childValue;
525 if( SetPropertyFromNode( (*iter).second, childValue, replacer ) )
527 array->PushBack( childValue );
534 value = Property::Value(Property::MAP);
535 Property::Map* map = value.GetMap();
536 for(unsigned int i = 0; i < node.Size(); ++i, ++iter)
538 Property::Value childValue;
539 if( SetPropertyFromNode( (*iter).second, childValue, replacer ) )
541 map->Insert( (*iter).first, childValue );
548 else // if( 0 == node.size() )
550 // no children so either one of bool, float, integer, string
551 OptionalBoolean aBool = replacer.IsBoolean(node);
552 OptionalInteger anInt = replacer.IsInteger(node);
553 OptionalFloat aFloat = replacer.IsFloat(node);
554 OptionalString aString = replacer.IsString(node);
558 // a bool is also an int but here we presume int
572 // Note: These are both floats and strings
575 // This means we can't have a string with purely numeric content without disambiguation.
588 // string always succeeds with the current json parser so its last
595 } // if( node.size() )
597 } // if Disambiguated()
600 } // bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
603 } // namespace Internal
605 } // namespace Toolkit