common scene: implement duplicate() method.
authorHermet Park <chuneon.park@samsung.com>
Tue, 22 Sep 2020 04:20:13 +0000 (13:20 +0900)
committerHermet Park <chuneon.park@samsung.com>
Tue, 22 Sep 2020 05:00:24 +0000 (14:00 +0900)
Change-Id: I132776aa42a4f170bd3dd0c36a02dbdeaa9613f7

src/examples/testDuplicate.cpp
src/lib/tvgPaint.h
src/lib/tvgSceneImpl.h

index ba889e3..b963abf 100644 (file)
@@ -8,52 +8,76 @@ void tvgDrawCmds(tvg::Canvas* canvas)
 {
     if (!canvas) return;
 
-    //Prepare first shape, which will not be drawn
-    auto shape1 = tvg::Shape::gen();
-    shape1->appendRect(10, 10, 200, 200, 0, 0);
-    shape1->appendRect(220, 10, 100, 100, 0, 0);
+    //Duplicate Shapes
+    {
+        //Original Shape
+        auto shape1 = tvg::Shape::gen();
+        shape1->appendRect(10, 10, 200, 200, 0, 0);
+        shape1->appendRect(220, 10, 100, 100, 0, 0);
 
-    shape1->stroke(3);
-    shape1->stroke(0, 255, 0, 255);
+        shape1->stroke(3);
+        shape1->stroke(0, 255, 0, 255);
 
-    float dashPattern[2] = {4, 4};
-    shape1->stroke(dashPattern, 2);
-    shape1->fill(255, 0, 0, 255);
+        float dashPattern[2] = {4, 4};
+        shape1->stroke(dashPattern, 2);
+        shape1->fill(255, 0, 0, 255);
 
-    //Create second shape and duplicate parameters
-    auto shape2 = unique_ptr<tvg::Shape>(static_cast<tvg::Shape*>(shape1->duplicate()));
+        //Duplicate Shape, Switch fill method
+        auto shape2 = unique_ptr<tvg::Shape>(static_cast<tvg::Shape*>(shape1->duplicate()));
+        shape2->translate(0, 220);
 
-    //Create third shape, duplicate from first and add some new parameters
-    auto shape3 = unique_ptr<tvg::Shape>(static_cast<tvg::Shape*>(shape1->duplicate()));
+        auto fill = tvg::LinearGradient::gen();
+        fill->linear(10, 10, 440, 200);
 
-    //Move shape3 to new postion to don't cover second shape
-    shape3->translate(0, 220);
+        tvg::Fill::ColorStop colorStops[2];
+        colorStops[0] = {0, 0, 0, 0, 255};
+        colorStops[1] = {1, 255, 255, 255, 255};
+        fill->colorStops(colorStops, 2);
 
-    //Append New paths
-    shape3->appendRect(340, 10, 100, 100, 0, 0);
+        shape2->fill(move(fill));
 
-    //Fill shap3 with Linear Gradient
-    auto fill = tvg::LinearGradient::gen();
-    fill->linear(10, 10, 440, 200);
+        //Duplicate Shape 2
+        auto shape3 = unique_ptr<tvg::Shape>(static_cast<tvg::Shape*>(shape2->duplicate()));
+        shape3->translate(0, 440);
 
-    //Gradient Color Stops
-    tvg::Fill::ColorStop colorStops[2];
-    colorStops[0] = {0, 0, 0, 0, 255};
-    colorStops[1] = {1, 255, 255, 255, 255};
+        canvas->push(move(shape1));
+        canvas->push(move(shape2));
+        canvas->push(move(shape3));
+    }
+
+    //Duplicate Scene
+    {
+        //Create a Scene1
+        auto scene1 = tvg::Scene::gen();
+        scene1->reserve(3);
+
+        auto shape1 = tvg::Shape::gen();
+        shape1->appendRect(0, 0, 400, 400, 50, 50);
+        shape1->fill(0, 255, 0, 255);
+        scene1->push(move(shape1));
 
-    fill->colorStops(colorStops, 2);
+        auto shape2 = tvg::Shape::gen();
+        shape2->appendCircle(400, 400, 200, 200);
+        shape2->fill(255, 255, 0, 255);
+        scene1->push(move(shape2));
 
-    shape3->fill(move(fill));
+        auto shape3 = tvg::Shape::gen();
+        shape3->appendCircle(600, 600, 150, 100);
+        shape3->fill(0, 255, 255, 255);
+        scene1->push(move(shape3));
 
-    //Create fourth shape, duplicate from third and move position
-    auto shape4 = unique_ptr<tvg::Shape>(static_cast<tvg::Shape*>(shape3->duplicate()));
+        scene1->scale(0.25);
+        scene1->translate(400, 0);
+
+        //Duplicate Scene1
+        auto scene2 = unique_ptr<tvg::Scene>(static_cast<tvg::Scene*>(scene1->duplicate()));
+        scene2->translate(600, 200);
+
+        canvas->push(move(scene1));
+        canvas->push(move(scene2));
+    }
 
-    //Move shape3 to new postion to don't cover second shape
-    shape4->translate(0, 440);
 
-    canvas->push(move(shape2));
-    canvas->push(move(shape3));
-    canvas->push(move(shape4));
 }
 
 
index 3bdba81..d47ba28 100644 (file)
@@ -150,7 +150,18 @@ namespace tvg
 
         Paint* duplicate()
         {
-            return smethod->duplicate();
+            auto ret = smethod->duplicate();
+
+            //duplicate Transform
+            if (rTransform) {
+                ret->pImpl->rTransform = new RenderTransform();
+                if (ret->pImpl->rTransform) {
+                    *ret->pImpl->rTransform = *rTransform;
+                    ret->pImpl->flag |= RenderUpdateFlag::Transform;
+                }
+            }
+
+            return ret;
         }
     };
 
index d9d6094..8073a70 100644 (file)
@@ -91,8 +91,17 @@ struct Scene::Impl
 
     Paint* duplicate()
     {
-        //TODO:
-        return nullptr;
+        auto ret = Scene::gen();
+        if (!ret) return nullptr;
+        auto dup = ret.get()->pImpl;
+
+        dup->paints.reserve(paints.size());
+
+        for (auto paint: paints) {
+            dup->paints.push_back(paint->duplicate());
+        }
+
+        return ret.release();
     }
 };