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>
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>
27 #include <dali-toolkit/internal/builder/builder-set-property.h>
28 #include <dali-toolkit/internal/helpers/color-conversion.h>
40 * A property value type can be forced when its unknown by a disambiguation convention in the json
41 * ie "myarray": [1,2,3,4] ; would be a vector but
42 * "myarray": {"typeCast":"array", "value":[1,2,3,4]} would be an array
43 * @param child The node whose string to search for a disambiguated type
44 * @param value The value to set
45 * @param replacement The user overriding constant map
46 * @return True if child contained a disambiguated string that could be converted.
48 bool Disambiguated(const TreeNode& child,
49 Dali::Property::Value& value,
50 const Replacement& replacement )
52 OptionalString childType = IsString( IsChild(child, "typeCast") );
53 OptionalChild childValue = IsChild(child, "value");
55 if( childType && childValue && (2 == child.Size()) )
57 // this case allows disambiguation but normally the type is guessed
58 // 2 == child.count() is an extra check as the user could have a user dictionary/map with
59 // type-cast and value keys. If they do then a work around is to add a bogus key to not run this case.
60 if(*childType == "boolean")
62 return DeterminePropertyFromNode( *childValue, Dali::Property::BOOLEAN, value, replacement);
64 else if(*childType == "float")
66 return DeterminePropertyFromNode( *childValue, Dali::Property::FLOAT, value, replacement);
68 else if(*childType == "vector2")
70 return DeterminePropertyFromNode( *childValue, Dali::Property::VECTOR2, value, replacement);
72 else if(*childType == "vector3")
74 return DeterminePropertyFromNode( *childValue, Dali::Property::VECTOR3, value, replacement);
76 else if(*childType == "vector4")
78 return DeterminePropertyFromNode( *childValue, Dali::Property::VECTOR4, value, replacement);
80 else if(*childType == "rotation")
82 return DeterminePropertyFromNode( *childValue, Dali::Property::ROTATION, value, replacement);
84 else if(*childType == "rect")
86 return DeterminePropertyFromNode( *childValue, Dali::Property::RECTANGLE, value, replacement);
88 else if(*childType == "string")
90 return DeterminePropertyFromNode( *childValue, Dali::Property::STRING, value, replacement);
92 else if(*childType == "map")
94 return DeterminePropertyFromNode( *childValue, Dali::Property::MAP, value, replacement);
96 else if(*childType == "array")
98 return DeterminePropertyFromNode( *childValue, Dali::Property::ARRAY, value, replacement);
102 // else we failed to disambiguate
107 bool DeterminePropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value)
109 Replacement noReplacement;
110 return DeterminePropertyFromNode( node, type, value, noReplacement );
113 bool DeterminePropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value,
114 const Replacement& replacer )
120 case Property::BOOLEAN:
122 if( OptionalBoolean v = replacer.IsBoolean(node) )
129 case Property::FLOAT:
131 if( OptionalFloat v = replacer.IsFloat(node) )
138 case Property::INTEGER:
140 if( OptionalInteger v = replacer.IsInteger(node) )
147 case Property::VECTOR2:
149 if( OptionalVector2 v = replacer.IsVector2(node) )
156 case Property::VECTOR3:
158 if( OptionalVector3 v = replacer.IsVector3(node) )
165 case Property::VECTOR4:
167 if( OptionalVector4 v = replacer.IsVector4(node) )
172 else if( OptionalString s = replacer.IsString(node) )
175 done = ConvertStringToColor( *s, color );
178 else if( TreeNode::OBJECT == node.GetType() )
180 // check for "r", "g" and "b" child color component nodes
181 OptionalInteger r = replacer.IsInteger( IsChild(node, "r") );
182 OptionalInteger g = replacer.IsInteger( IsChild(node, "g") );
183 OptionalInteger b = replacer.IsInteger( IsChild(node, "b") );
186 float red( (*r) * (1.0f/255.0f) );
187 float green( (*g) * (1.0f/255.0f) );
188 float blue( (*b) * (1.0f/255.0f) );
189 // check for optional "a" (alpha) node, default to fully opaque if it is not found.
191 OptionalInteger a = replacer.IsInteger( IsChild(node, "a") );
194 alpha = (*a) * (1.0f/255.0f);
196 value = Vector4( red, green, blue, alpha );
202 case Property::MATRIX3:
204 if( OptionalMatrix3 v = replacer.IsMatrix3(node) )
211 case Property::MATRIX:
213 if( OptionalMatrix v = replacer.IsMatrix(node) )
220 case Property::RECTANGLE:
222 if( OptionalRect v = replacer.IsRect(node) )
229 case Property::ROTATION:
233 if( OptionalVector4 ov = replacer.IsVector4(node) )
235 const Vector4& v = *ov;
236 // angle, axis as per spec
237 value = Quaternion(Radian(Degree(v[3])),
238 Vector3(v[0],v[1],v[2]));
244 // degrees Euler as per spec
245 if( OptionalVector3 v = replacer.IsVector3(node) )
247 value = Quaternion(Radian(Degree((*v).x)),
248 Radian(Degree((*v).y)),
249 Radian(Degree((*v).z)));
255 case Property::STRING:
257 if( OptionalString v = replacer.IsString(node) )
264 case Property::ARRAY:
266 if( replacer.IsArray( node, value ) )
272 value = Property::Value(Property::ARRAY);
273 Property::Array* array = value.GetArray();
276 TreeNode::ConstIterator iter(node.CBegin());
280 for( ; i < node.Size(); ++i, ++iter)
282 Property::Value childValue;
283 DeterminePropertyFromNode( (*iter).second, childValue, replacer );
284 array->PushBack( childValue );
287 done = ( array->Count() == node.Size() );
294 if( replacer.IsMap( node, value ) )
300 value = Property::Value(Property::MAP);
301 Property::Map* map = value.GetMap();
304 TreeNode::ConstIterator iter(node.CBegin());
308 for( ; i < node.Size(); ++i, ++iter)
310 Property::Value childValue;
311 DeterminePropertyFromNode( (*iter).second, childValue, replacer );
312 map->Insert( (*iter).first, childValue );
315 done = ( map->Count() == node.Size() );
329 void DeterminePropertyFromNode( const TreeNode& node, Property::Value& value )
332 Replacement replacer;
333 DeterminePropertyFromNode( node, value, replacer );
336 void DeterminePropertyFromNode( const TreeNode& node, Property::Value& value,
337 const Replacement& replacer )
340 TreeNode::NodeType nodeType = node.GetType();
342 // Some values are ambiguous as we have no Property::Type but can be disambiguated in the JSON.
343 // Currently Rotations and Rectangle must always be disambiguated when a type isn't available
344 if( !Disambiguated( node, value, replacer ) )
348 // Here, nodes are handled with the following precedence order:
349 // 1) Nodes with children, that have type ARRAY: Checked for array types including vectors and matrices.
350 // 2) Nodes without children, that do not have type ARRAY OR OBJECT: Checked for primitive types (int / float /etc).
351 // 3) If no match so far; If type is OBJECT: attempt to create as a Property::Map.
352 // 4) If no match still; Create as array.
354 // First handle nodes with children.
357 // Handle array types.
358 if( nodeType == TreeNode::ARRAY )
360 // our current heuristic for deciding an array is actually a vector and not say a map
361 // is to check if the values are all floats
362 bool allNumbers = true;
363 for( TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter )
365 OptionalFloat checkFloat = IsFloat( ( *iter ).second );
375 // prefer finding vectors over presuming composite Property::Array...
376 if( OptionalMatrix v = IsMatrix( node ) )
381 else if( OptionalMatrix3 v = IsMatrix3( node ) )
386 else if( OptionalVector4 v = IsVector4( node ) )
391 else if( OptionalVector3 v = IsVector3( node ) )
396 else if( OptionalVector2 v = IsVector2( node ) )
401 else if( 4 == node.Size() )
403 if( OptionalVector4 v = IsVector4( node ) )
412 else if( ( nodeType != TreeNode::OBJECT ) && ( nodeType != TreeNode::ARRAY ) )
414 // no children so either one of bool, float, integer, string
415 OptionalBoolean aBool = replacer.IsBoolean( node );
416 OptionalInteger anInt = replacer.IsInteger( node );
417 OptionalFloat aFloat = replacer.IsFloat( node );
418 OptionalString aString = replacer.IsString( node );
422 // a bool is also an int but here we presume int
434 // Note: These are both floats and strings
437 // This means we can't have a string with purely numeric content without disambiguation.
453 } // if( node.size() )
455 // If we have not created a value so far, attempt to create a Map or Array.
458 // We are guaranteed to have at least one entry as this has been checked already.
459 TreeConstIter containerIterator = node.CBegin();
460 TreeConstIter containerEnd = node.CEnd();
462 // The TreeNode::OBJECT type implies a Property::Map.
463 if( nodeType == TreeNode::OBJECT )
465 // We have a key, treat container as a Map.
466 value = Property::Value( Property::MAP );
467 Property::Map* map = value.GetMap();
471 // Iterate through container to add all entries.
472 for( ; containerIterator != containerEnd; ++containerIterator )
474 Property::Value childValue;
475 DeterminePropertyFromNode( ( *containerIterator ).second, childValue, replacer );
476 map->Insert( ( *containerIterator ).first, childValue );
482 // We don't have a key, treat container as an array.
483 // Note: We don't check if the node type is array here, as we want to cope with unknowns by creating an array also.
484 // This is the fall-back if no other types could be created.
485 value = Property::Value( Property::ARRAY );
486 Property::Array* array = value.GetArray();
490 // Iterate through container to add all entries.
491 for( ; containerIterator != containerEnd; ++containerIterator )
493 Property::Value childValue;
494 DeterminePropertyFromNode( ( *containerIterator ).second, childValue, replacer );
495 array->PushBack( childValue );
500 } // if !Disambiguated()
504 } // namespace Internal
506 } // namespace Toolkit