Added actor creation from json snippet 99/26099/5
authorLee Morgan <Lee.morgan@partner.samsung.com>
Fri, 15 Aug 2014 13:13:38 +0000 (14:13 +0100)
committerLee Morgan <Lee.morgan@partner.samsung.com>
Tue, 2 Sep 2014 09:52:32 +0000 (10:52 +0100)
Change-Id: I041c1afd9ec25961c6159df7967cb66cfc7eb225
Signed-off-by: Lee Morgan <Lee.morgan@partner.samsung.com>
automated-tests/src/dali-toolkit-unmanaged/utc-Dali-Builder.cpp
base/dali-toolkit/internal/builder/builder-impl.cpp
base/dali-toolkit/internal/builder/builder-impl.h
base/dali-toolkit/internal/builder/json-parser-impl.cpp
base/dali-toolkit/public-api/builder/builder.cpp
base/dali-toolkit/public-api/builder/builder.h

index 4742bed..833a40e 100644 (file)
@@ -381,3 +381,51 @@ int UtcDaliBuilderSetProperty(void)
 
   END_TEST;
 }
+
+int UtcDaliBuilderCreateFromJson(void)
+{
+  ToolkitTestApplication application;
+
+  tet_infoline(" UtcDaliBuilderCreateFromJson");
+
+  Builder builder = Builder::New();
+
+  TextActor actor = TextActor::DownCast( builder.CreateFromJson("foobar") );
+
+  DALI_TEST_CHECK( !actor );
+
+  actor = TextActor::DownCast(
+    builder.CreateFromJson(
+      ReplaceQuotes("{'type':'TextActor','text':'Hi'}") ) );
+
+  DALI_TEST_CHECK( actor );
+
+  DALI_TEST_CHECK( actor.GetText() == "Hi" );
+
+  END_TEST;
+}
+
+int UtcDaliBuilderApplyFromJson(void)
+{
+  ToolkitTestApplication application;
+
+  tet_infoline(" UtcDaliBuilderApplyFromJson");
+
+  Builder builder = Builder::New();
+
+  TextActor actor = TextActor::DownCast(
+    builder.CreateFromJson(
+      ReplaceQuotes("{'type':'TextActor','text':'Hi'}") ) );
+
+  DALI_TEST_CHECK( actor );
+
+  DALI_TEST_CHECK( actor.GetText() == "Hi" );
+
+  DALI_TEST_CHECK( !builder.ApplyFromJson(actor, ReplaceQuotes("foobar") ) );
+
+  builder.ApplyFromJson(actor, ReplaceQuotes("{'text':'low'}") );
+
+  DALI_TEST_CHECK( actor.GetText() == "low" );
+
+  END_TEST;
+}
index 544c428..6deedb5 100644 (file)
@@ -1029,6 +1029,44 @@ BaseHandle Builder::Create( const std::string& templateName, const Replacement&
   return baseHandle;
 }
 
+BaseHandle Builder::CreateFromJson( const std::string& json )
+{
+  BaseHandle ret;
+
+  // merge in new template, hoping no one else has one named '@temp@'
+  std::string newTemplate =
+    std::string("{\"templates\":{\"@temp@\":") +                      \
+    json +                                                            \
+    std::string("}}");
+
+  if( mParser.Parse(newTemplate) )
+  {
+    Replacement replacement( mReplacementMap );
+    ret = Create( "@temp@", replacement );
+  }
+
+  return ret;
+}
+
+bool Builder::ApplyFromJson(  Handle& handle, const std::string& json )
+{
+  bool ret = false;
+
+  // merge new style, hoping no one else has one named '@temp@'
+  std::string newStyle =
+    std::string("{\"styles\":{\"@temp@\":") +                           \
+    json +                                                              \
+    std::string("}}");
+
+  if( mParser.Parse(newStyle) )
+  {
+    Replacement replacement( mReplacementMap );
+    ret = ApplyStyle( "@temp@", handle, replacement );
+  }
+
+  return ret;
+}
+
 
 BaseHandle Builder::Create( const std::string& templateName )
 {
index 62e3e87..d533366 100644 (file)
@@ -129,6 +129,16 @@ public:
   BaseHandle Create( const std::string& templateName, const PropertyValueMap& map );
 
   /**
+   * @copydoc Toolkit::Builder::CreateFromJson( const std::string& json );
+   */
+  BaseHandle CreateFromJson( const std::string& json );
+
+  /**
+   * @copydoc Toolkit::Builder::ApplyFromJson( Handle& handle, const std::string& json );
+   */
+  bool ApplyFromJson(  Handle& handle, const std::string& json );
+
+  /**
    * @copydoc Toolkit::Builder::ApplyStyle
    */
   bool ApplyStyle( const std::string& styleName, Handle& handle );
index 9aea87b..5bce6fb 100644 (file)
@@ -114,6 +114,8 @@ int JsonParser::Parse(const std::string& source)
   }
   else
   {
+    mRoot = NULL;
+
     mErrorDescription   = parserState.GetErrorDescription();
     if(NULL == mErrorDescription)
     {
index e900e5b..79173f1 100644 (file)
@@ -104,11 +104,21 @@ BaseHandle Builder::Create( const std::string& templateName, const PropertyValue
   return GetImpl(*this).Create( templateName, map );
 }
 
+BaseHandle Builder::CreateFromJson( const std::string& json )
+{
+  return GetImpl(*this).CreateFromJson( json );
+}
+
 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle )
 {
   return GetImpl(*this).ApplyStyle( styleName, handle );
 }
 
+bool Builder::ApplyFromJson( Handle& handle, const std::string& json )
+{
+  return GetImpl(*this).ApplyFromJson( handle, json );
+}
+
 void Builder::AddActors( Actor toActor )
 {
   GetImpl(*this).AddActors( toActor );
index 5f33075..ddbb041 100644 (file)
@@ -35,15 +35,14 @@ class Builder;
 typedef std::map<std::string, Property::Value> PropertyValueMap;
 
 /**
- * Builder
- * This class provides the ability to load an actor tree from a string representation.
+ * This class provides the ability to load and style an actor tree from a string representation.
  *
- * The following example is hello world in JSON.
+ * The following is an example in JSON.
  *
  * @code
  *
  *  {
- *    "styles":
+ *    "templates": // are named instantiable actor trees
  *    {
  *      "default-text":
  *      {
@@ -53,6 +52,24 @@ typedef std::map<std::string, Property::Value> PropertyValueMap;
  *        "scale": [50,50,1]
  *      }
  *    },
+ *    "styles": // are named property sets applied to actor trees
+ *    {
+ *     "my-style":
+ *      {
+ *        "size": [10,10,1] // root properties applied to a given root actor
+ *        "actors":         // properties applied to actors found by name from root
+ *        {
+ *          "ok":           // properties for an actor named "ok"
+ *          {
+ *            "scale":[5,5,1],
+ *          },
+ *          "cancel":
+ *          {
+ *            "scale":[50,50,1],
+ *          }
+ *       },
+ *      },
+ *    },
  *    "stage":
  *    [
  *      {
@@ -63,29 +80,43 @@ typedef std::map<std::string, Property::Value> PropertyValueMap;
  *    ]
  *  }
  *
- *
- *
  * @endcode
  *
- * The following is how to load the json data.
- *
+ * The following shows a method to load the json file.
  * @code
- *
  * Builder builder = Builder::New();
- *
  * std::string json_data(ReadFile("layout.json"));
- *
  * builder.LoadFromString(json_data);
- *
- * // 1) load all actors in the "stage" section to the root layer
+ * @endcode
+ * Examples
+ * - Load all actors in the "stage" section to the root layer
+ * @code
  * builder.AddActors( Stage::GetCurrent().GetRootLayer() );
+ * @endcode
  *
- * // or 2) create an actor from the library "templates" section
+ * - Create an actor tree from the "templates" section
+ * @code
  * TextActor actor = TextActor::DownCast( builder.Create( "default-text" ) );
+ * @endcode
  *
+ * - Style an actor tree from the "styles" section
+ * @code
+ * builder.ApplyStyle( "my-style", actor );
  * @endcode
+ *
+ * - Create an actor tree from json
+ * @code
+ * TextActor actor = TextActor::DownCast( builder.CreateFromJson("{\"type\":\"TextActor\",\"font\":\"\",\"scale\":[50,50,1]}") );
+ * @endcode
+ *
+ * - Apply a style to an actor tree from json
+ * @code
+ * builder.ApplyFromJson( textActor, ("{\"scale\":[5,5,1]}") );
+ * @endcode
+ *
  */
- class Builder : public BaseHandle
+
+class Builder : public BaseHandle
  {
  public:
    /**
@@ -286,6 +317,19 @@ typedef std::map<std::string, Property::Value> PropertyValueMap;
   BaseHandle Create( const std::string& templateName, const PropertyValueMap& map );
 
   /**
+   * @brief Creates an object (e.g. an actor) from given json snippet
+   *
+   * e.g.
+   *   Actor a = Actor::DownCast(builder.CreateFromJson( "{\"type\":\"TextActor\"}"));
+   *
+   * @pre The Builder has been initialized.
+   * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
+   * @param json The json snippet used to create the object.
+   * @returns The base handle of the created object if any
+   */
+  BaseHandle CreateFromJson( const std::string& json );
+
+  /**
    * Apply a style (a collection of properties) to an actor.
    * @pre The Builder has been initialized.
    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
@@ -297,6 +341,18 @@ typedef std::map<std::string, Property::Value> PropertyValueMap;
   bool ApplyStyle( const std::string& styleName, Handle& handle );
 
   /**
+   * Apply a style (a collection of properties) to an actor from the given json snippet
+   * @pre The Builder has been initialized.
+   * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
+   * @param handle Then handle of the object on which to set the properties.
+   * @param json The json snippet used to create the object.
+   *
+   * @return Return true if the json snippet was parsed
+   */
+  bool ApplyFromJson(  Handle& handle, const std::string& json );
+
+
+  /**
    * Add the actor tree in the "stage" section to the actor toActor.
    * ie if the representation has a 'stage' section that contains a tree of
    * actors then