2 * Copyright (c) 2021 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.
19 #include <dali/public-api/object/property-array.h>
20 #include <dali/public-api/object/property-map.h>
24 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
25 #include <dali-toolkit/internal/builder/builder-impl.h>
26 #include <dali-toolkit/internal/builder/builder-set-property.h>
27 #include <dali-toolkit/internal/builder/replacement.h>
28 #include <dali-toolkit/internal/helpers/color-conversion.h>
37 * A property value type can be forced when its unknown by a disambiguation convention in the json
38 * ie "myarray": [1,2,3,4] ; would be a vector but
39 * "myarray": {"typeCast":"array", "value":[1,2,3,4]} would be an array
40 * @param child The node whose string to search for a disambiguated type
41 * @param value The value to set
42 * @param replacement The user overriding constant map
43 * @return True if child contained a disambiguated string that could be converted.
45 bool Disambiguated(const TreeNode& child,
46 Dali::Property::Value& value,
47 const Replacement& replacement)
49 OptionalString childType = IsString(IsChild(child, "typeCast"));
50 OptionalChild childValue = IsChild(child, "value");
52 if(childType && childValue && (2 == child.Size()))
54 // this case allows disambiguation but normally the type is guessed
55 // 2 == child.count() is an extra check as the user could have a user dictionary/map with
56 // type-cast and value keys. If they do then a work around is to add a bogus key to not run this case.
57 if(*childType == "boolean")
59 return DeterminePropertyFromNode(*childValue, Dali::Property::BOOLEAN, value, replacement);
61 else if(*childType == "float")
63 return DeterminePropertyFromNode(*childValue, Dali::Property::FLOAT, value, replacement);
65 else if(*childType == "vector2")
67 return DeterminePropertyFromNode(*childValue, Dali::Property::VECTOR2, value, replacement);
69 else if(*childType == "vector3")
71 return DeterminePropertyFromNode(*childValue, Dali::Property::VECTOR3, value, replacement);
73 else if(*childType == "vector4")
75 return DeterminePropertyFromNode(*childValue, Dali::Property::VECTOR4, value, replacement);
77 else if(*childType == "rotation")
79 return DeterminePropertyFromNode(*childValue, Dali::Property::ROTATION, value, replacement);
81 else if(*childType == "rect")
83 return DeterminePropertyFromNode(*childValue, Dali::Property::RECTANGLE, value, replacement);
85 else if(*childType == "string")
87 return DeterminePropertyFromNode(*childValue, Dali::Property::STRING, value, replacement);
89 else if(*childType == "map")
91 return DeterminePropertyFromNode(*childValue, Dali::Property::MAP, value, replacement);
93 else if(*childType == "array")
95 return DeterminePropertyFromNode(*childValue, Dali::Property::ARRAY, value, replacement);
97 else if(*childType == "extents")
99 return DeterminePropertyFromNode(*childValue, Dali::Property::EXTENTS, value, replacement);
103 // 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, const Replacement& replacer)
119 case Property::BOOLEAN:
121 if(OptionalBoolean v = replacer.IsBoolean(node))
128 case Property::FLOAT:
130 if(OptionalFloat v = replacer.IsFloat(node))
137 case Property::INTEGER:
139 if(OptionalInteger v = replacer.IsInteger(node))
146 case Property::VECTOR2:
148 if(OptionalVector2 v = replacer.IsVector2(node))
155 case Property::VECTOR3:
157 if(OptionalVector3 v = replacer.IsVector3(node))
164 case Property::VECTOR4:
166 if(OptionalVector4 v = replacer.IsVector4(node))
171 else if(OptionalString s = replacer.IsString(node))
174 done = ConvertStringToColor(*s, color);
177 else if(TreeNode::OBJECT == node.GetType())
179 // check for "r", "g" and "b" child color component nodes
180 OptionalInteger r = replacer.IsInteger(IsChild(node, "r"));
181 OptionalInteger g = replacer.IsInteger(IsChild(node, "g"));
182 OptionalInteger b = replacer.IsInteger(IsChild(node, "b"));
185 float red((*r) * (1.0f / 255.0f));
186 float green((*g) * (1.0f / 255.0f));
187 float blue((*b) * (1.0f / 255.0f));
188 // check for optional "a" (alpha) node, default to fully opaque if it is not found.
190 OptionalInteger a = replacer.IsInteger(IsChild(node, "a"));
193 alpha = (*a) * (1.0f / 255.0f);
195 value = Vector4(red, green, blue, alpha);
201 case Property::MATRIX3:
203 if(OptionalMatrix3 v = replacer.IsMatrix3(node))
210 case Property::MATRIX:
212 if(OptionalMatrix v = replacer.IsMatrix(node))
219 case Property::RECTANGLE:
221 if(OptionalRect v = replacer.IsRect(node))
228 case Property::ROTATION:
232 if(OptionalVector4 ov = replacer.IsVector4(node))
234 const Vector4& v = *ov;
235 // angle, axis as per spec
236 value = Quaternion(Radian(Degree(v[3])),
237 Vector3(v[0], v[1], v[2]));
243 // degrees Euler as per spec
244 if(OptionalVector3 v = replacer.IsVector3(node))
246 value = Quaternion(Radian(Degree((*v).x)),
247 Radian(Degree((*v).y)),
248 Radian(Degree((*v).z)));
254 case Property::STRING:
256 if(OptionalString v = replacer.IsString(node))
263 case Property::ARRAY:
265 if(replacer.IsArray(node, value))
271 value = Property::Value(Property::ARRAY);
272 Property::Array* array = value.GetArray();
275 TreeNode::ConstIterator iter(node.CBegin());
279 for(; i < node.Size(); ++i, ++iter)
281 Property::Value childValue;
282 DeterminePropertyFromNode((*iter).second, childValue, replacer);
283 array->PushBack(childValue);
286 done = (array->Count() == node.Size());
293 if(replacer.IsMap(node, value))
299 value = Property::Value(Property::MAP);
300 Property::Map* map = value.GetMap();
303 TreeNode::ConstIterator iter(node.CBegin());
307 for(; i < node.Size(); ++i, ++iter)
309 Property::Value childValue;
310 DeterminePropertyFromNode((*iter).second, childValue, replacer);
311 map->Insert((*iter).first, childValue);
314 done = (map->Count() == node.Size());
319 case Property::EXTENTS:
321 if(OptionalExtents v = replacer.IsExtents(node))
337 void DeterminePropertyFromNode(const TreeNode& node, Property::Value& value)
340 Replacement replacer;
341 DeterminePropertyFromNode(node, value, replacer);
344 void DeterminePropertyFromNode(const TreeNode& node, Property::Value& value, const Replacement& replacer)
346 TreeNode::NodeType nodeType = node.GetType();
348 // Some values are ambiguous as we have no Property::Type but can be disambiguated in the JSON.
349 // Currently Rotations and Rectangle must always be disambiguated when a type isn't available
350 if(!Disambiguated(node, value, replacer))
354 // Here, nodes are handled with the following precedence order:
355 // 1) Nodes with children, that have type ARRAY: Checked for array types including vectors and matrices.
356 // 2) Nodes without children, that do not have type ARRAY OR OBJECT: Checked for primitive types (int / float /etc).
357 // 3) If no match so far; If type is OBJECT: attempt to create as a Property::Map.
358 // 4) If no match still; Create as array.
360 // First handle nodes with children.
363 // Handle array types.
364 if(nodeType == TreeNode::ARRAY)
366 // our current heuristic for deciding an array is actually a vector and not say a map
367 // is to check if the values are all floats
368 bool allNumbers = true;
369 for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
371 OptionalFloat checkFloat = IsFloat((*iter).second);
381 // prefer finding vectors over presuming composite Property::Array...
382 if(OptionalMatrix v = IsMatrix(node))
387 else if(OptionalMatrix3 v = IsMatrix3(node))
392 else if(OptionalVector4 v = IsVector4(node))
397 else if(OptionalVector3 v = IsVector3(node))
402 else if(OptionalVector2 v = IsVector2(node))
407 else if(4 == node.Size())
409 if(OptionalVector4 v = IsVector4(node))
418 else if((nodeType != TreeNode::OBJECT) && (nodeType != TreeNode::ARRAY))
420 // no children so either one of bool, float, integer, string
421 OptionalBoolean aBool = replacer.IsBoolean(node);
422 OptionalInteger anInt = replacer.IsInteger(node);
423 OptionalFloat aFloat = replacer.IsFloat(node);
424 OptionalString aString = replacer.IsString(node);
428 // a bool is also an int but here we presume int
440 // Note: These are both floats and strings
443 // This means we can't have a string with purely numeric content without disambiguation.
459 } // if( node.size() )
461 // If we have not created a value so far, attempt to create a Map or Array.
464 // We are guaranteed to have at least one entry as this has been checked already.
465 TreeConstIter containerIterator = node.CBegin();
466 TreeConstIter containerEnd = node.CEnd();
468 // The TreeNode::OBJECT type implies a Property::Map.
469 if(nodeType == TreeNode::OBJECT)
471 // We have a key, treat container as a Map.
472 value = Property::Value(Property::MAP);
473 Property::Map* map = value.GetMap();
477 // Iterate through container to add all entries.
478 for(; containerIterator != containerEnd; ++containerIterator)
480 Property::Value childValue;
481 DeterminePropertyFromNode((*containerIterator).second, childValue, replacer);
482 map->Insert((*containerIterator).first, childValue);
488 // We don't have a key, treat container as an array.
489 // Note: We don't check if the node type is array here, as we want to cope with unknowns by creating an array also.
490 // This is the fall-back if no other types could be created.
491 value = Property::Value(Property::ARRAY);
492 Property::Array* array = value.GetArray();
496 // Iterate through container to add all entries.
497 for(; containerIterator != containerEnd; ++containerIterator)
499 Property::Value childValue;
500 DeterminePropertyFromNode((*containerIterator).second, childValue, replacer);
501 array->PushBack(childValue);
506 } // if !Disambiguated()
509 } // namespace Internal
511 } // namespace Toolkit