2 * Copyright (c) 2016 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>
28 #include <dali-toolkit/internal/builder/builder-set-property.h>
44 * Converts a HTML style 'color' hex string ("#FF0000" for bright red) to a Vector4.
45 * The Vector4 alpha component will be set to 1.0f
46 * @param hexString The HTML style hex string
47 * @return a Vector4 containing the new color value
49 Vector4 HexStringToVector4( const char* s )
51 unsigned int value(0u);
52 std::istringstream( s ) >> std::hex >> value;
53 return Vector4( ((value >> 16 ) & 0xff ) / 255.0f,
54 ((value >> 8 ) & 0xff ) / 255.0f,
55 (value & 0xff ) / 255.0f,
63 * A property value type can be forced when its unknown by a disambiguation convention in the json
64 * ie "myarray": [1,2,3,4] ; would be a vector but
65 * "myarray": {"typeCast":"array", "value":[1,2,3,4]} would be an array
66 * @param child The node whose string to search for a disambiguated type
67 * @param value The value to set
68 * @param replacement The user overriding constant map
69 * @return True if child contained a disambiguated string that could be converted.
71 bool Disambiguated(const TreeNode& child,
72 Dali::Property::Value& value,
73 const Replacement& replacement )
75 OptionalString childType = IsString( IsChild(child, "typeCast") );
76 OptionalChild childValue = IsChild(child, "value");
78 if( childType && childValue && (2 == child.Size()) )
80 // this case allows disambiguation but normally the type is guessed
81 // 2 == child.count() is an extra check as the user could have a user dictionary/map with
82 // type-cast and value keys. If they do then a work around is to add a bogus key to not run this case.
83 if(*childType == "boolean")
85 return DeterminePropertyFromNode( *childValue, Dali::Property::BOOLEAN, value, replacement);
87 else if(*childType == "float")
89 return DeterminePropertyFromNode( *childValue, Dali::Property::FLOAT, value, replacement);
91 else if(*childType == "vector2")
93 return DeterminePropertyFromNode( *childValue, Dali::Property::VECTOR2, value, replacement);
95 else if(*childType == "vector3")
97 return DeterminePropertyFromNode( *childValue, Dali::Property::VECTOR3, value, replacement);
99 else if(*childType == "vector4")
101 return DeterminePropertyFromNode( *childValue, Dali::Property::VECTOR4, value, replacement);
103 else if(*childType == "rotation")
105 return DeterminePropertyFromNode( *childValue, Dali::Property::ROTATION, value, replacement);
107 else if(*childType == "rect")
109 return DeterminePropertyFromNode( *childValue, Dali::Property::RECTANGLE, value, replacement);
111 else if(*childType == "string")
113 return DeterminePropertyFromNode( *childValue, Dali::Property::STRING, value, replacement);
115 else if(*childType == "map")
117 return DeterminePropertyFromNode( *childValue, Dali::Property::MAP, value, replacement);
119 else if(*childType == "array")
121 return DeterminePropertyFromNode( *childValue, Dali::Property::ARRAY, value, replacement);
125 // else we failed to disambiguate
130 bool DeterminePropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value)
132 Replacement noReplacement;
133 return DeterminePropertyFromNode( node, type, value, noReplacement );
136 bool DeterminePropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value,
137 const Replacement& replacer )
143 case Property::BOOLEAN:
145 if( OptionalBoolean v = replacer.IsBoolean(node) )
152 case Property::FLOAT:
154 if( OptionalFloat v = replacer.IsFloat(node) )
161 case Property::INTEGER:
163 if( OptionalInteger v = replacer.IsInteger(node) )
170 case Property::VECTOR2:
172 if( OptionalVector2 v = replacer.IsVector2(node) )
179 case Property::VECTOR3:
181 if( OptionalVector3 v = replacer.IsVector3(node) )
188 case Property::VECTOR4:
190 if( OptionalVector4 v = replacer.IsVector4(node) )
195 else if( OptionalString s = replacer.IsString(node) )
197 if( (*s)[0] == '#' && 7 == (*s).size() )
199 value = HexStringToVector4( &(*s)[1] );
202 else if( Dali::ColorController::Get() )
205 done = Dali::ColorController::Get().RetrieveColor( *s, color );
209 else if( TreeNode::OBJECT == node.GetType() )
211 // check for "r", "g" and "b" child color component nodes
212 OptionalInteger r = replacer.IsInteger( IsChild(node, "r") );
213 OptionalInteger g = replacer.IsInteger( IsChild(node, "g") );
214 OptionalInteger b = replacer.IsInteger( IsChild(node, "b") );
217 float red( (*r) * (1.0f/255.0f) );
218 float green( (*g) * (1.0f/255.0f) );
219 float blue( (*b) * (1.0f/255.0f) );
220 // check for optional "a" (alpha) node, default to fully opaque if it is not found.
222 OptionalInteger a = replacer.IsInteger( IsChild(node, "a") );
225 alpha = (*a) * (1.0f/255.0f);
227 value = Vector4( red, green, blue, alpha );
233 case Property::MATRIX3:
235 if( OptionalMatrix3 v = replacer.IsMatrix3(node) )
242 case Property::MATRIX:
244 if( OptionalMatrix v = replacer.IsMatrix(node) )
251 case Property::RECTANGLE:
253 if( OptionalRect v = replacer.IsRect(node) )
260 case Property::ROTATION:
264 if( OptionalVector4 ov = replacer.IsVector4(node) )
266 const Vector4& v = *ov;
267 // angle, axis as per spec
268 value = Quaternion(Radian(Degree(v[3])),
269 Vector3(v[0],v[1],v[2]));
275 // degrees Euler as per spec
276 if( OptionalVector3 v = replacer.IsVector3(node) )
278 value = Quaternion(Radian(Degree((*v).x)),
279 Radian(Degree((*v).y)),
280 Radian(Degree((*v).z)));
286 case Property::STRING:
288 if( OptionalString v = replacer.IsString(node) )
295 case Property::ARRAY:
297 if( replacer.IsArray( node, value ) )
303 value = Property::Value(Property::ARRAY);
304 Property::Array* array = value.GetArray();
307 TreeNode::ConstIterator iter(node.CBegin());
311 for( ; i < node.Size(); ++i, ++iter)
313 Property::Value childValue;
314 DeterminePropertyFromNode( (*iter).second, childValue, replacer );
315 array->PushBack( childValue );
318 done = ( array->Count() == node.Size() );
325 if( replacer.IsMap( node, value ) )
331 value = Property::Value(Property::MAP);
332 Property::Map* map = value.GetMap();
335 TreeNode::ConstIterator iter(node.CBegin());
339 for( ; i < node.Size(); ++i, ++iter)
341 Property::Value childValue;
342 DeterminePropertyFromNode( (*iter).second, childValue, replacer );
343 map->Insert( (*iter).first, childValue );
346 done = ( map->Count() == node.Size() );
360 void DeterminePropertyFromNode( const TreeNode& node, Property::Value& value )
363 Replacement replacer;
364 DeterminePropertyFromNode( node, value, replacer );
367 void DeterminePropertyFromNode( const TreeNode& node, Property::Value& value,
368 const Replacement& replacer )
371 TreeNode::NodeType nodeType = node.GetType();
373 // Some values are ambiguous as we have no Property::Type but can be disambiguated in the JSON.
374 // Currently Rotations and Rectangle must always be disambiguated when a type isn't available
375 if( !Disambiguated( node, value, replacer ) )
379 // Here, nodes are handled with the following precedence order:
380 // 1) Nodes with children, that have type ARRAY: Checked for array types including vectors and matrices.
381 // 2) Nodes without children, that do not have type ARRAY OR OBJECT: Checked for primitive types (int / float /etc).
382 // 3) If no match so far; If type is OBJECT: attempt to create as a Property::Map.
383 // 4) If no match still; Create as array.
385 // First handle nodes with children.
388 // Handle array types.
389 if( nodeType == TreeNode::ARRAY )
391 // our current heuristic for deciding an array is actually a vector and not say a map
392 // is to check if the values are all floats
393 bool allNumbers = true;
394 for( TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter )
396 OptionalFloat checkFloat = IsFloat( ( *iter ).second );
406 // prefer finding vectors over presuming composite Property::Array...
407 if( OptionalMatrix v = IsMatrix( node ) )
412 else if( OptionalMatrix3 v = IsMatrix3( node ) )
417 else if( OptionalVector4 v = IsVector4( node ) )
422 else if( OptionalVector3 v = IsVector3( node ) )
427 else if( OptionalVector2 v = IsVector2( node ) )
432 else if( 4 == node.Size() )
434 if( OptionalVector4 v = IsVector4( node ) )
443 else if( ( nodeType != TreeNode::OBJECT ) && ( nodeType != TreeNode::ARRAY ) )
445 // no children so either one of bool, float, integer, string
446 OptionalBoolean aBool = replacer.IsBoolean( node );
447 OptionalInteger anInt = replacer.IsInteger( node );
448 OptionalFloat aFloat = replacer.IsFloat( node );
449 OptionalString aString = replacer.IsString( node );
453 // a bool is also an int but here we presume int
465 // Note: These are both floats and strings
468 // This means we can't have a string with purely numeric content without disambiguation.
484 } // if( node.size() )
486 // If we have not created a value so far, attempt to create a Map or Array.
489 // We are guaranteed to have at least one entry as this has been checked already.
490 TreeConstIter containerIterator = node.CBegin();
491 TreeConstIter containerEnd = node.CEnd();
493 // The TreeNode::OBJECT type implies a Property::Map.
494 if( nodeType == TreeNode::OBJECT )
496 // We have a key, treat container as a Map.
497 value = Property::Value( Property::MAP );
498 Property::Map* map = value.GetMap();
502 // Iterate through container to add all entries.
503 for( ; containerIterator != containerEnd; ++containerIterator )
505 Property::Value childValue;
506 DeterminePropertyFromNode( ( *containerIterator ).second, childValue, replacer );
507 map->Insert( ( *containerIterator ).first, childValue );
513 // We don't have a key, treat container as an array.
514 // Note: We don't check if the node type is array here, as we want to cope with unknowns by creating an array also.
515 // This is the fall-back if no other types could be created.
516 value = Property::Value( Property::ARRAY );
517 Property::Array* array = value.GetArray();
521 // Iterate through container to add all entries.
522 for( ; containerIterator != containerEnd; ++containerIterator )
524 Property::Value childValue;
525 DeterminePropertyFromNode( ( *containerIterator ).second, childValue, replacer );
526 array->PushBack( childValue );
531 } // if !Disambiguated()
535 } // namespace Internal
537 } // namespace Toolkit