DALi Version 2.1.5
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / builder / builder-set-property.cpp
index 5b079bb..d00ca12 100644 (file)
-//
-// Copyright (c) 2014 Samsung Electronics Co., Ltd.
-//
-// Licensed under the Flora License, Version 1.0 (the License);
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://floralicense.org/license/
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an AS IS BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+// EXTERNAL INCLUDES
+#include <dali/public-api/object/property-array.h>
+#include <dali/public-api/object/property-map.h>
+#include <sstream>
 
 // INTERNAL INCLUDES
-#include <dali-toolkit/internal/builder/builder-impl.h>
 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
+#include <dali-toolkit/internal/builder/builder-impl.h>
+#include <dali-toolkit/internal/builder/builder-set-property.h>
+#include <dali-toolkit/internal/builder/replacement.h>
+#include <dali-toolkit/internal/helpers/color-conversion.h>
 
 namespace Dali
 {
-
 namespace Toolkit
 {
-
 namespace Internal
 {
-
-/*
- * Set a property value from a tree node.
- * This function guesses the type of the property from the format of the string in the node.
- * This is not always possible and could be surprising.
- * @param node  The node string to convert from
- * @param value The property value to set
- * @return true if the string could be converted.
- */
-bool SetPropertyFromNode( const TreeNode& node, Property::Value& value );
-
-/*
- * Set a property value as the given type from a tree node.
- * @param node The node string to convert from
- * @param type The property type to convert to.
- * @param value The property value to set
- * @return true if the string could be converted to the correct type.
- */
-bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value );
-
 /**
  * A property value type can be forced when its unknown by a disambiguation convention in the json
  * ie  "myarray": [1,2,3,4] ; would be a vector but
- *     "myarray": {'type-cast':'array', 'value':[1,2,3,4]} would be an array
- * @param child The node whos string to search for a disambiguated type
+ *     "myarray": {"typeCast":"array", "value":[1,2,3,4]} would be an array
+ * @param child The node whose string to search for a disambiguated type
  * @param value The value to set
+ * @param replacement The user overriding constant map
  * @return True if child contained a disambiguated string that could be converted.
  */
-bool Disambiguated(const TreeNode& child, // ConstantLut& constantLut,
-                   Dali::Property::Value& value)
+bool Disambiguated(const TreeNode&        child,
+                   Dali::Property::Value& value,
+                   const Replacement&     replacement)
 {
-  OptionalString childType = IsString( IsChild(child, "type-cast") );
-  OptionalChild childValue = IsChild(child, "value");
+  OptionalString childType  = IsString(IsChild(child, "typeCast"));
+  OptionalChild  childValue = IsChild(child, "value");
 
-  if( childType && childValue && (2 == child.Size()) )
+  if(childType && childValue && (2 == child.Size()))
   {
     // this case allows disambiguation but normally the type is guessed
     // 2 == child.count() is an extra check as the user could have a user dictionary/map with
     // type-cast and value keys. If they do then a work around is to add a bogus key to not run this case.
     if(*childType == "boolean")
     {
-      return SetPropertyFromNode( *childValue, Dali::Property::BOOLEAN, value);
+      return DeterminePropertyFromNode(*childValue, Dali::Property::BOOLEAN, value, replacement);
     }
     else if(*childType == "float")
     {
-      return SetPropertyFromNode( *childValue, Dali::Property::FLOAT, value);
+      return DeterminePropertyFromNode(*childValue, Dali::Property::FLOAT, value, replacement);
     }
     else if(*childType == "vector2")
     {
-      return SetPropertyFromNode( *childValue, Dali::Property::VECTOR2, value);
+      return DeterminePropertyFromNode(*childValue, Dali::Property::VECTOR2, value, replacement);
     }
     else if(*childType == "vector3")
     {
-      return SetPropertyFromNode( *childValue, Dali::Property::VECTOR3, value);
+      return DeterminePropertyFromNode(*childValue, Dali::Property::VECTOR3, value, replacement);
     }
     else if(*childType == "vector4")
     {
-      return SetPropertyFromNode( *childValue, Dali::Property::VECTOR4, value);
+      return DeterminePropertyFromNode(*childValue, Dali::Property::VECTOR4, value, replacement);
     }
     else if(*childType == "rotation")
     {
-      return SetPropertyFromNode( *childValue, Dali::Property::ROTATION, value);
+      return DeterminePropertyFromNode(*childValue, Dali::Property::ROTATION, value, replacement);
     }
     else if(*childType == "rect")
     {
-      return SetPropertyFromNode( *childValue, Dali::Property::RECTANGLE, value);
+      return DeterminePropertyFromNode(*childValue, Dali::Property::RECTANGLE, value, replacement);
     }
     else if(*childType == "string")
     {
-      return SetPropertyFromNode( *childValue, Dali::Property::STRING, value);
+      return DeterminePropertyFromNode(*childValue, Dali::Property::STRING, value, replacement);
     }
     else if(*childType == "map")
     {
-      return SetPropertyFromNode( *childValue, Dali::Property::MAP, value);
+      return DeterminePropertyFromNode(*childValue, Dali::Property::MAP, value, replacement);
     }
     else if(*childType == "array")
     {
-      return SetPropertyFromNode( *childValue, Dali::Property::ARRAY, value);
+      return DeterminePropertyFromNode(*childValue, Dali::Property::ARRAY, value, replacement);
+    }
+    else if(*childType == "extents")
+    {
+      return DeterminePropertyFromNode(*childValue, Dali::Property::EXTENTS, value, replacement);
     }
   }
 
@@ -111,7 +104,13 @@ bool Disambiguated(const TreeNode& child, // ConstantLut& constantLut,
   return false;
 }
 
-bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value )
+bool DeterminePropertyFromNode(const TreeNode& node, Property::Type type, Property::Value& value)
+{
+  Replacement noReplacement;
+  return DeterminePropertyFromNode(node, type, value, noReplacement);
+}
+
+bool DeterminePropertyFromNode(const TreeNode& node, Property::Type type, Property::Value& value, const Replacement& replacer)
 {
   bool done = false;
 
@@ -119,94 +118,110 @@ bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::V
   {
     case Property::BOOLEAN:
     {
-      if( OptionalBoolean v = IsBoolean(node) )
+      if(OptionalBoolean v = replacer.IsBoolean(node))
       {
         value = *v;
-        done = true;
+        done  = true;
       }
       break;
     }
     case Property::FLOAT:
     {
-      if( OptionalFloat v = IsFloat(node) )
+      if(OptionalFloat v = replacer.IsFloat(node))
       {
         value = *v;
-        done = true;
+        done  = true;
       }
       break;
     }
     case Property::INTEGER:
     {
-      if( OptionalInteger v = IsInteger(node) )
+      if(OptionalInteger v = replacer.IsInteger(node))
       {
         value = *v;
-        done = true;
-      }
-      break;
-    }
-    case Property::UNSIGNED_INTEGER:
-    {
-      if( OptionalInteger v = IsInteger(node) )
-      {
-        if( *v >= 0 ) // with a loss of resolution....
-        {
-          value = *v;
-          done = true;
-        }
+        done  = true;
       }
       break;
     }
     case Property::VECTOR2:
     {
-      if( OptionalVector2 v = IsVector2(node) )
+      if(OptionalVector2 v = replacer.IsVector2(node))
       {
         value = *v;
-        done = true;
+        done  = true;
       }
       break;
     }
     case Property::VECTOR3:
     {
-      if( OptionalVector3 v = IsVector3(node) )
+      if(OptionalVector3 v = replacer.IsVector3(node))
       {
         value = *v;
-        done = true;
+        done  = true;
       }
       break;
     }
     case Property::VECTOR4:
     {
-      if( OptionalVector4 v = IsVector4(node) )
+      if(OptionalVector4 v = replacer.IsVector4(node))
       {
         value = *v;
-        done = true;
-       }
+        done  = true;
+      }
+      else if(OptionalString s = replacer.IsString(node))
+      {
+        Vector4 color;
+        done  = ConvertStringToColor(*s, color);
+        value = color;
+      }
+      else if(TreeNode::OBJECT == node.GetType())
+      {
+        // check for "r", "g" and "b" child color component nodes
+        OptionalInteger r = replacer.IsInteger(IsChild(node, "r"));
+        OptionalInteger g = replacer.IsInteger(IsChild(node, "g"));
+        OptionalInteger b = replacer.IsInteger(IsChild(node, "b"));
+        if(r && g && b)
+        {
+          float red((*r) * (1.0f / 255.0f));
+          float green((*g) * (1.0f / 255.0f));
+          float blue((*b) * (1.0f / 255.0f));
+          // check for optional "a" (alpha) node, default to fully opaque if it is not found.
+          float           alpha(1.0f);
+          OptionalInteger a = replacer.IsInteger(IsChild(node, "a"));
+          if(a)
+          {
+            alpha = (*a) * (1.0f / 255.0f);
+          }
+          value = Vector4(red, green, blue, alpha);
+          done  = true;
+        }
+      }
       break;
     }
     case Property::MATRIX3:
     {
-      if( OptionalMatrix3 v = IsMatrix3(node) )
+      if(OptionalMatrix3 v = replacer.IsMatrix3(node))
       {
         value = *v;
-        done = true;
+        done  = true;
       }
       break;
     }
     case Property::MATRIX:
     {
-      if( OptionalMatrix v = IsMatrix(node) )
+      if(OptionalMatrix v = replacer.IsMatrix(node))
       {
         value = *v;
-        done = true;
+        done  = true;
       }
       break;
     }
     case Property::RECTANGLE:
     {
-      if( OptionalRect v = IsRect(node) )
+      if(OptionalRect v = replacer.IsRect(node))
       {
         value = *v;
-        done = true;
+        done  = true;
       }
       break;
     }
@@ -214,225 +229,199 @@ bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::V
     {
       if(4 == node.Size())
       {
-        if( OptionalVector4 ov = IsVector4(node) )
+        if(OptionalVector4 ov = replacer.IsVector4(node))
         {
           const Vector4& v = *ov;
           // angle, axis as per spec
           value = Quaternion(Radian(Degree(v[3])),
-                             Vector3(v[0],v[1],v[2]));
-          done = true;
+                             Vector3(v[0], v[1], v[2]));
+          done  = true;
         }
       }
       else
       {
         // degrees Euler as per spec
-        if( OptionalVector3 v = IsVector3(node) )
+        if(OptionalVector3 v = replacer.IsVector3(node))
         {
           value = Quaternion(Radian(Degree((*v).x)),
                              Radian(Degree((*v).y)),
                              Radian(Degree((*v).z)));
-          done = true;
+          done  = true;
         }
       }
       break;
     }
     case Property::STRING:
     {
-      if( OptionalString v = IsString(node) )
+      if(OptionalString v = replacer.IsString(node))
       {
         value = *v;
-        done = true;
+        done  = true;
       }
       break;
     }
     case Property::ARRAY:
     {
-      if(node.Size())
+      if(replacer.IsArray(node, value))
+      {
+        done = true;
+      }
+      else if(node.Size())
       {
-        value = Property::Value(Property::ARRAY);
-        unsigned int i = 0;
+        value                  = Property::Value(Property::ARRAY);
+        Property::Array* array = value.GetArray();
+
+        unsigned int            i = 0;
         TreeNode::ConstIterator iter(node.CBegin());
-        for( ; i < node.Size(); ++i, ++iter)
+
+        if(array)
         {
-          Property::Value v;
-          if( SetPropertyFromNode( (*iter).second, v) )
+          for(; i < node.Size(); ++i, ++iter)
           {
-            value.AppendItem(v);
+            Property::Value childValue;
+            DeterminePropertyFromNode((*iter).second, childValue, replacer);
+            array->PushBack(childValue);
           }
-        }
 
-        if( value.GetSize() == static_cast<int>(node.Size()) )
-        {
-          done = true;
-        }
-        else
-        {
-          done = false;
+          done = (array->Count() == node.Size());
         }
       }
       break;
     }
     case Property::MAP:
     {
-      if(node.Size())
+      if(replacer.IsMap(node, value))
       {
-        value = Property::Value(Property::MAP);
-        unsigned int i = 0;
+        done = true;
+      }
+      else if(node.Size())
+      {
+        value              = Property::Value(Property::MAP);
+        Property::Map* map = value.GetMap();
+
+        unsigned int            i = 0;
         TreeNode::ConstIterator iter(node.CBegin());
-        for( ; i < node.Size(); ++i, ++iter)
+
+        if(map)
         {
-          Property::Value v;
-          if( SetPropertyFromNode( (*iter).second, v) )
+          for(; i < node.Size(); ++i, ++iter)
           {
-            value.SetValue( (*iter).first, v );
+            Property::Value childValue;
+            DeterminePropertyFromNode((*iter).second, childValue, replacer);
+            map->Insert((*iter).first, childValue);
           }
-        }
 
-        if( value.GetSize() == static_cast<int>(node.Size()) )
-        {
-          done = true;
-        }
-        else
-        {
-          done = false;
+          done = (map->Count() == node.Size());
         }
       }
       break;
     }
-    case Property::TYPE_COUNT:
+    case Property::EXTENTS:
+    {
+      if(OptionalExtents v = replacer.IsExtents(node))
+      {
+        value = *v;
+        done  = true;
+      }
+      break;
+    }
     case Property::NONE:
     {
       break;
     }
-  }
+  } // switch type
 
   return done;
 }
 
+void DeterminePropertyFromNode(const TreeNode& node, Property::Value& value)
 
-bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
 {
-  bool done = false;
+  Replacement replacer;
+  DeterminePropertyFromNode(node, value, replacer);
+}
 
-  // some values are ambiguous as we have no Property::Type but can be disambiguated in the json
+void DeterminePropertyFromNode(const TreeNode& node, Property::Value& value, const Replacement& replacer)
+{
+  TreeNode::NodeType nodeType = node.GetType();
 
-  // Currently Rotations and Rectangle must always be disambiguated when a type isnt available
-  if( Disambiguated( node, value ) )
-  {
-    done = true;
-  }
-  else
+  // Some values are ambiguous as we have no Property::Type but can be disambiguated in the JSON.
+  // Currently Rotations and Rectangle must always be disambiguated when a type isn't available
+  if(!Disambiguated(node, value, replacer))
   {
-    if( node.Size() )
+    bool done = false;
+
+    // Here, nodes are handled with the following precedence order:
+    // 1) Nodes with children, that have type ARRAY: Checked for array types including vectors and matrices.
+    // 2) Nodes without children, that do not have type ARRAY OR OBJECT: Checked for primitive types (int / float /etc).
+    // 3) If no match so far; If type is OBJECT: attempt to create as a Property::Map.
+    // 4) If no match still; Create as array.
+
+    // First handle nodes with children.
+    if(node.Size())
     {
-      // our current heuristic for deciding an array is actually a vector and not say a map
-      // is to check if the values are all floats
-      bool allNumbers = true;
-      for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
+      // Handle array types.
+      if(nodeType == TreeNode::ARRAY)
       {
-        OptionalFloat f = IsFloat((*iter).second);
-        if(!f)
+        // our current heuristic for deciding an array is actually a vector and not say a map
+        // is to check if the values are all floats
+        bool allNumbers = true;
+        for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
         {
-          allNumbers = false;
-          break;
+          OptionalFloat checkFloat = IsFloat((*iter).second);
+          if(!checkFloat)
+          {
+            allNumbers = false;
+            break;
+          }
         }
-      }
 
-      if( allNumbers )
-      {
-        // prefer finding vectors over presuming composite Property::Array...
-        if( OptionalMatrix v = IsMatrix(node) )
-        {
-          value = *v;
-          done = true;
-        }
-        else if( OptionalMatrix3 v = IsMatrix3(node) )
-        {
-          value = *v;
-          done = true;
-        }
-        if( OptionalVector4 v = IsVector4(node) )
-        {
-          value = *v;
-          done = true;
-        }
-        else if( OptionalVector3 v = IsVector3(node) )
-        {
-          value = *v;
-          done = true;
-        }
-        else if( OptionalVector2 v = IsVector2(node) )
+        if(allNumbers)
         {
-          value = *v;
-          done = true;
-        }
-        else if( 4 == node.Size() )
-        {
-          if( OptionalVector4 v = IsVector4(node) )
+          // prefer finding vectors over presuming composite Property::Array...
+          if(OptionalMatrix v = IsMatrix(node))
           {
             value = *v;
-            done = true;
+            done  = true;
           }
-        }
-        else
-        {
-          value = Property::Value(Property::ARRAY);
-          Property::Value v;
-
-          for(TreeConstIter iter = node.CBegin(); iter != node.CEnd(); ++iter)
+          else if(OptionalMatrix3 v = IsMatrix3(node))
           {
-            if( SetPropertyFromNode( (*iter).second, v) )
-            {
-              value.AppendItem(v);
-              done = true;
-            }
+            value = *v;
+            done  = true;
           }
-        }
-      }
-
-      if(!done)
-      {
-        // presume an array or map
-        // container of size 1
-        TreeNode::ConstIterator iter = node.CBegin();
-
-        // its seems legal with current json parser for a map to have an empty key
-        // but here we take that to mean the structure is a list
-        if( ((*iter).first) == 0 )
-        {
-          value = Property::Value(Property::ARRAY);
-          Property::Value v;
-          for(unsigned int i = 0; i < node.Size(); ++i, ++iter)
+          else if(OptionalVector4 v = IsVector4(node))
           {
-            if( SetPropertyFromNode( (*iter).second, v) )
-            {
-              value.AppendItem(v);
-              done = true;
-            }
+            value = *v;
+            done  = true;
           }
-        }
-        else
-        {
-          value = Property::Value(Property::MAP);
-          Property::Value v;
-          for(unsigned int i = 0; i < node.Size(); ++i, ++iter)
+          else if(OptionalVector3 v = IsVector3(node))
           {
-            if( SetPropertyFromNode( (*iter).second, v) )
+            value = *v;
+            done  = true;
+          }
+          else if(OptionalVector2 v = IsVector2(node))
+          {
+            value = *v;
+            done  = true;
+          }
+          else if(4 == node.Size())
+          {
+            if(OptionalVector4 v = IsVector4(node))
             {
-              value.SetValue((*iter).first, v);
-              done = true;
+              value = *v;
+              done  = true;
             }
           }
         }
-      } // if!done
+      }
     } // if node.size()
-    else // if( 0 == node.size() )
+    else if((nodeType != TreeNode::OBJECT) && (nodeType != TreeNode::ARRAY))
     {
       // no children so either one of bool, float, integer, string
-      OptionalBoolean aBool    = IsBoolean(node);
-      OptionalInteger anInt    = IsInteger(node);
-      OptionalFloat   aFloat   = IsFloat(node);
-      OptionalString  aString  = IsString(node);
+      OptionalBoolean aBool   = replacer.IsBoolean(node);
+      OptionalInteger anInt   = replacer.IsInteger(node);
+      OptionalFloat   aFloat  = replacer.IsFloat(node);
+      OptionalString  aString = replacer.IsString(node);
 
       if(aBool)
       {
@@ -440,12 +429,10 @@ bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
         if(anInt)
         {
           value = *anInt;
-          done = true;
         }
         else
         {
           value = *aBool;
-          done = true;
         }
       }
       else
@@ -457,29 +444,67 @@ bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
         if(aFloat)
         {
           value = *aFloat;
-          done = true;
         }
         else if(anInt)
         {
           value = *anInt;
-          done = true;
         }
         else
         {
-          // string always succeeds with the current json parser so its last
           value = *aString;
-          done = true;
         }
-
       } // if aBool
 
+      done = true;
     } // if( node.size() )
 
-  } // if Disambiguated()
+    // If we have not created a value so far, attempt to create a Map or Array.
+    if(!done)
+    {
+      // We are guaranteed to have at least one entry as this has been checked already.
+      TreeConstIter containerIterator = node.CBegin();
+      TreeConstIter containerEnd      = node.CEnd();
 
-  return done;
-} // bool SetPropertyFromNode( const TreeNode& node, Property::Value& value )
+      // The TreeNode::OBJECT type implies a Property::Map.
+      if(nodeType == TreeNode::OBJECT)
+      {
+        // We have a key, treat container as a Map.
+        value              = Property::Value(Property::MAP);
+        Property::Map* map = value.GetMap();
 
+        if(map)
+        {
+          // Iterate through container to add all entries.
+          for(; containerIterator != containerEnd; ++containerIterator)
+          {
+            Property::Value childValue;
+            DeterminePropertyFromNode((*containerIterator).second, childValue, replacer);
+            map->Insert((*containerIterator).first, childValue);
+          }
+        }
+      }
+      else
+      {
+        // We don't have a key, treat container as an array.
+        // Note: We don't check if the node type is array here, as we want to cope with unknowns by creating an array also.
+        // This is the fall-back if no other types could be created.
+        value                  = Property::Value(Property::ARRAY);
+        Property::Array* array = value.GetArray();
+
+        if(array)
+        {
+          // Iterate through container to add all entries.
+          for(; containerIterator != containerEnd; ++containerIterator)
+          {
+            Property::Value childValue;
+            DeterminePropertyFromNode((*containerIterator).second, childValue, replacer);
+            array->PushBack(childValue);
+          }
+        }
+      }
+    } // if !done
+  }   // if !Disambiguated()
+}
 
 } // namespace Internal