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);
100 else if(*childType == "extents")
102 return DeterminePropertyFromNode( *childValue, Dali::Property::EXTENTS, value, replacement);
106 // else we failed to disambiguate
111 bool DeterminePropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value)
113 Replacement noReplacement;
114 return DeterminePropertyFromNode( node, type, value, noReplacement );
117 bool DeterminePropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value,
118 const Replacement& replacer )
124 case Property::BOOLEAN:
126 if( OptionalBoolean v = replacer.IsBoolean(node) )
133 case Property::FLOAT:
135 if( OptionalFloat v = replacer.IsFloat(node) )
142 case Property::INTEGER:
144 if( OptionalInteger v = replacer.IsInteger(node) )
151 case Property::VECTOR2:
153 if( OptionalVector2 v = replacer.IsVector2(node) )
160 case Property::VECTOR3:
162 if( OptionalVector3 v = replacer.IsVector3(node) )
169 case Property::VECTOR4:
171 if( OptionalVector4 v = replacer.IsVector4(node) )
176 else if( OptionalString s = replacer.IsString(node) )
179 done = ConvertStringToColor( *s, color );
182 else if( TreeNode::OBJECT == node.GetType() )
184 // check for "r", "g" and "b" child color component nodes
185 OptionalInteger r = replacer.IsInteger( IsChild(node, "r") );
186 OptionalInteger g = replacer.IsInteger( IsChild(node, "g") );
187 OptionalInteger b = replacer.IsInteger( IsChild(node, "b") );
190 float red( (*r) * (1.0f/255.0f) );
191 float green( (*g) * (1.0f/255.0f) );
192 float blue( (*b) * (1.0f/255.0f) );
193 // check for optional "a" (alpha) node, default to fully opaque if it is not found.
195 OptionalInteger a = replacer.IsInteger( IsChild(node, "a") );
198 alpha = (*a) * (1.0f/255.0f);
200 value = Vector4( red, green, blue, alpha );
206 case Property::MATRIX3:
208 if( OptionalMatrix3 v = replacer.IsMatrix3(node) )
215 case Property::MATRIX:
217 if( OptionalMatrix v = replacer.IsMatrix(node) )
224 case Property::RECTANGLE:
226 if( OptionalRect v = replacer.IsRect(node) )
233 case Property::ROTATION:
237 if( OptionalVector4 ov = replacer.IsVector4(node) )
239 const Vector4& v = *ov;
240 // angle, axis as per spec
241 value = Quaternion(Radian(Degree(v[3])),
242 Vector3(v[0],v[1],v[2]));
248 // degrees Euler as per spec
249 if( OptionalVector3 v = replacer.IsVector3(node) )
251 value = Quaternion(Radian(Degree((*v).x)),
252 Radian(Degree((*v).y)),
253 Radian(Degree((*v).z)));
259 case Property::STRING:
261 if( OptionalString v = replacer.IsString(node) )
268 case Property::ARRAY:
270 if( replacer.IsArray( node, value ) )
276 value = Property::Value(Property::ARRAY);
277 Property::Array* array = value.GetArray();
280 TreeNode::ConstIterator iter(node.CBegin());
284 for( ; i < node.Size(); ++i, ++iter)
286 Property::Value childValue;
287 DeterminePropertyFromNode( (*iter).second, childValue, replacer );
288 array->PushBack( childValue );
291 done = ( array->Count() == node.Size() );
298 if( replacer.IsMap( node, value ) )
304 value = Property::Value(Property::MAP);
305 Property::Map* map = value.GetMap();
308 TreeNode::ConstIterator iter(node.CBegin());
312 for( ; i < node.Size(); ++i, ++iter)
314 Property::Value childValue;
315 DeterminePropertyFromNode( (*iter).second, childValue, replacer );
316 map->Insert( (*iter).first, childValue );
319 done = ( map->Count() == node.Size() );
324 case Property::EXTENTS:
326 if( OptionalExtents v = replacer.IsExtents(node) )
342 void DeterminePropertyFromNode( const TreeNode& node, Property::Value& value )
345 Replacement replacer;
346 DeterminePropertyFromNode( node, value, replacer );
349 void DeterminePropertyFromNode( const TreeNode& node, Property::Value& value,
350 const Replacement& replacer )
353 TreeNode::NodeType nodeType = node.GetType();
355 // Some values are ambiguous as we have no Property::Type but can be disambiguated in the JSON.
356 // Currently Rotations and Rectangle must always be disambiguated when a type isn't available
357 if( !Disambiguated( node, value, replacer ) )
361 // Here, nodes are handled with the following precedence order:
362 // 1) Nodes with children, that have type ARRAY: Checked for array types including vectors and matrices.
363 // 2) Nodes without children, that do not have type ARRAY OR OBJECT: Checked for primitive types (int / float /etc).
364 // 3) If no match so far; If type is OBJECT: attempt to create as a Property::Map.
365 // 4) If no match still; Create as array.
367 // First handle nodes with children.
370 // Handle array types.
371 if( nodeType == TreeNode::ARRAY )
373 // our current heuristic for deciding an array is actually a vector and not say a map
374 // is to check if the values are all floats
375 bool allNumbers = true;
376 for( TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter )
378 OptionalFloat checkFloat = IsFloat( ( *iter ).second );
388 // prefer finding vectors over presuming composite Property::Array...
389 if( OptionalMatrix v = IsMatrix( node ) )
394 else if( OptionalMatrix3 v = IsMatrix3( node ) )
399 else if( OptionalVector4 v = IsVector4( node ) )
404 else if( OptionalVector3 v = IsVector3( node ) )
409 else if( OptionalVector2 v = IsVector2( node ) )
414 else if( 4 == node.Size() )
416 if( OptionalVector4 v = IsVector4( node ) )
425 else if( ( nodeType != TreeNode::OBJECT ) && ( nodeType != TreeNode::ARRAY ) )
427 // no children so either one of bool, float, integer, string
428 OptionalBoolean aBool = replacer.IsBoolean( node );
429 OptionalInteger anInt = replacer.IsInteger( node );
430 OptionalFloat aFloat = replacer.IsFloat( node );
431 OptionalString aString = replacer.IsString( node );
435 // a bool is also an int but here we presume int
447 // Note: These are both floats and strings
450 // This means we can't have a string with purely numeric content without disambiguation.
466 } // if( node.size() )
468 // If we have not created a value so far, attempt to create a Map or Array.
471 // We are guaranteed to have at least one entry as this has been checked already.
472 TreeConstIter containerIterator = node.CBegin();
473 TreeConstIter containerEnd = node.CEnd();
475 // The TreeNode::OBJECT type implies a Property::Map.
476 if( nodeType == TreeNode::OBJECT )
478 // We have a key, treat container as a Map.
479 value = Property::Value( Property::MAP );
480 Property::Map* map = value.GetMap();
484 // Iterate through container to add all entries.
485 for( ; containerIterator != containerEnd; ++containerIterator )
487 Property::Value childValue;
488 DeterminePropertyFromNode( ( *containerIterator ).second, childValue, replacer );
489 map->Insert( ( *containerIterator ).first, childValue );
495 // We don't have a key, treat container as an array.
496 // Note: We don't check if the node type is array here, as we want to cope with unknowns by creating an array also.
497 // This is the fall-back if no other types could be created.
498 value = Property::Value( Property::ARRAY );
499 Property::Array* array = value.GetArray();
503 // Iterate through container to add all entries.
504 for( ; containerIterator != containerEnd; ++containerIterator )
506 Property::Value childValue;
507 DeterminePropertyFromNode( ( *containerIterator ).second, childValue, replacer );
508 array->PushBack( childValue );
513 } // if !Disambiguated()
517 } // namespace Internal
519 } // namespace Toolkit