Merge PathText samples into one, with different modes

Change-Id: I640cd522c1c83f7534c86ebfd6a6f3904fb90206
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/292734
Reviewed-by: Adlai Holler <adlai@google.com>
Commit-Queue: Chris Dalton <csmartdalton@google.com>
diff --git a/samplecode/SamplePathText.cpp b/samplecode/SamplePathText.cpp
index d9fb7c0..b8927b9 100644
--- a/samplecode/SamplePathText.cpp
+++ b/samplecode/SamplePathText.cpp
@@ -30,6 +30,7 @@
         for (Glyph& glyph : fGlyphs) {
             glyph.reset(fRand, this->width(), this->height());
         }
+        fGlyphAnimator->reset(&fRand, this->width(), this->height());
     }
 
     void onOnceBeforeDraw() final {
@@ -49,19 +50,17 @@
             fGlyphs[i].init(fRand, p);
         }
 
-        this->INHERITED::onOnceBeforeDraw();
+        this->Sample::onOnceBeforeDraw();
         this->reset();
     }
-    void onSizeChange() final { this->INHERITED::onSizeChange(); this->reset(); }
+    void onSizeChange() final { this->Sample::onSizeChange(); this->reset(); }
 
     SkString name() override { return SkString(this->getName()); }
 
-    bool onChar(SkUnichar unichar) override {
-            if (unichar == 'X') {
-                fDoClip = !fDoClip;
-                return true;
-            }
-            return false;
+    bool onChar(SkUnichar) override;
+
+    bool onAnimate(double nanos) final {
+        return fGlyphAnimator->animate(nanos, this->width(), this->height());
     }
 
     void onDrawContent(SkCanvas* canvas) override {
@@ -74,18 +73,7 @@
             canvas->restore();
             canvas->clipPath(deviceSpaceClipPath, SkClipOp::kIntersect, true);
         }
-        this->drawGlyphs(canvas);
-    }
-
-    virtual void drawGlyphs(SkCanvas* canvas) {
-        for (Glyph& glyph : fGlyphs) {
-            SkAutoCanvasRestore acr(canvas, true);
-            canvas->translate(glyph.fPosition.x(), glyph.fPosition.y());
-            canvas->scale(glyph.fZoom, glyph.fZoom);
-            canvas->rotate(glyph.fSpin);
-            canvas->translate(-glyph.fMidpt.x(), -glyph.fMidpt.y());
-            canvas->drawPath(glyph.fPath, glyph.fPaint);
-        }
+        fGlyphAnimator->draw(canvas);
     }
 
 protected:
@@ -101,12 +89,36 @@
         SkPoint    fMidpt;
     };
 
-    Glyph      fGlyphs[kNumPaths];
-    SkRandom   fRand{25};
-    SkPath     fClipPath = ToolUtils::make_star(SkRect{0, 0, 1, 1}, 11, 3);
-    bool       fDoClip = false;
+    class GlyphAnimator {
+    public:
+        GlyphAnimator(Glyph* glyphs) : fGlyphs(glyphs) {}
+        virtual void reset(SkRandom*, int screenWidth, int screenHeight) {}
+        virtual bool animate(double nanos, int screenWidth, int screenHeight) { return false; }
+        virtual void draw(SkCanvas* canvas) {
+            for (int i = 0; i < kNumPaths; ++i) {
+                Glyph& glyph = fGlyphs[i];
+                SkAutoCanvasRestore acr(canvas, true);
+                canvas->translate(glyph.fPosition.x(), glyph.fPosition.y());
+                canvas->scale(glyph.fZoom, glyph.fZoom);
+                canvas->rotate(glyph.fSpin);
+                canvas->translate(-glyph.fMidpt.x(), -glyph.fMidpt.y());
+                canvas->drawPath(glyph.fPath, glyph.fPaint);
+            }
+        }
+        virtual ~GlyphAnimator() {}
 
-    typedef Sample INHERITED;
+    protected:
+        Glyph* const fGlyphs;
+    };
+
+    class MovingGlyphAnimator;
+    class WavyGlyphAnimator;
+
+    Glyph fGlyphs[kNumPaths];
+    SkRandom fRand{25};
+    SkPath fClipPath = ToolUtils::make_star(SkRect{0, 0, 1, 1}, 11, 3);
+    bool fDoClip = false;
+    std::unique_ptr<GlyphAnimator> fGlyphAnimator = std::make_unique<GlyphAnimator>(fGlyphs);
 };
 
 void PathText::Glyph::init(SkRandom& rand, const SkPath& path) {
@@ -130,48 +142,46 @@
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 // Text from paths with animated transformation matrices.
-class MovingPathText : public PathText {
+class PathText::MovingGlyphAnimator : public PathText::GlyphAnimator {
 public:
-    const char* getName() const override { return "MovingPathText"; }
-
-    MovingPathText()
-        : fFrontMatrices(kNumPaths)
-        , fBackMatrices(kNumPaths) {
+    MovingGlyphAnimator(Glyph* glyphs)
+            : GlyphAnimator(glyphs)
+            , fFrontMatrices(kNumPaths)
+            , fBackMatrices(kNumPaths) {
     }
 
-    ~MovingPathText() override {
+    ~MovingGlyphAnimator() override {
         fBackgroundAnimationTask.wait();
     }
 
-    void reset() override {
-        const SkScalar screensize = static_cast<SkScalar>(std::max(this->width(), this->height()));
-        this->INHERITED::reset();
+    void reset(SkRandom* rand, int screenWidth, int screenHeight) override {
+        const SkScalar screensize = static_cast<SkScalar>(std::max(screenWidth, screenHeight));
 
         for (auto& v : fVelocities) {
             for (SkScalar* d : {&v.fDx, &v.fDy}) {
-                SkScalar t = pow(fRand.nextF(), 3);
-                *d = ((1 - t) / 60 + t / 10) * (fRand.nextBool() ? screensize : -screensize);
+                SkScalar t = pow(rand->nextF(), 3);
+                *d = ((1 - t) / 60 + t / 10) * (rand->nextBool() ? screensize : -screensize);
             }
 
-            SkScalar t = pow(fRand.nextF(), 25);
-            v.fDSpin = ((1 - t) * 360 / 7.5 + t * 360 / 1.5) * (fRand.nextBool() ? 1 : -1);
+            SkScalar t = pow(rand->nextF(), 25);
+            v.fDSpin = ((1 - t) * 360 / 7.5 + t * 360 / 1.5) * (rand->nextBool() ? 1 : -1);
         }
 
         // Get valid front data.
         fBackgroundAnimationTask.wait();
-        this->runAnimationTask(0, 0, this->width(), this->height());
+        this->runAnimationTask(0, 0, screenWidth, screenHeight);
         memcpy(fFrontMatrices, fBackMatrices, kNumPaths * sizeof(SkMatrix));
         fLastTick = 0;
     }
 
-    bool onAnimate(double nanos) final {
+    bool animate(double nanos, int screenWidth, int screenHeight) final {
         fBackgroundAnimationTask.wait();
         this->swapAnimationBuffers();
 
         const double tsec = 1e-9 * nanos;
         const double dt = fLastTick ? (1e-9 * nanos - fLastTick) : 0;
-        fBackgroundAnimationTask.add(std::bind(&MovingPathText::runAnimationTask, this, tsec,
-                                               dt, this->width(), this->height()));
+        fBackgroundAnimationTask.add(std::bind(&MovingGlyphAnimator::runAnimationTask, this, tsec,
+                                               dt, screenWidth, screenHeight));
         fLastTick = 1e-9 * nanos;
         return true;
     }
@@ -216,7 +226,7 @@
         std::swap(fFrontMatrices, fBackMatrices);
     }
 
-    void drawGlyphs(SkCanvas* canvas) override {
+    void draw(SkCanvas* canvas) override {
         for (int i = 0; i < kNumPaths; ++i) {
             SkAutoCanvasRestore acr(canvas, true);
             canvas->concat(fFrontMatrices[i]);
@@ -230,33 +240,31 @@
         SkScalar fDSpin;
     };
 
-    Velocity                  fVelocities[kNumPaths];
-    SkAutoTMalloc<SkMatrix>   fFrontMatrices;
-    SkAutoTMalloc<SkMatrix>   fBackMatrices;
-    SkTaskGroup               fBackgroundAnimationTask;
-    double                    fLastTick;
-
-    typedef PathText INHERITED;
+    Velocity fVelocities[kNumPaths];
+    SkAutoTMalloc<SkMatrix> fFrontMatrices;
+    SkAutoTMalloc<SkMatrix> fBackMatrices;
+    SkTaskGroup fBackgroundAnimationTask;
+    double fLastTick;
 };
 
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 // Text from paths with animated control points.
-class WavyPathText : public MovingPathText {
+class PathText::WavyGlyphAnimator : public PathText::MovingGlyphAnimator {
 public:
-    const char* getName() const override { return "WavyPathText"; }
+    WavyGlyphAnimator(Glyph* glyphs)
+            : MovingGlyphAnimator(glyphs)
+            , fFrontPaths(kNumPaths)
+            , fBackPaths(kNumPaths) {
+    }
 
-    WavyPathText()
-        : fFrontPaths(kNumPaths)
-        , fBackPaths(kNumPaths) {}
-
-    ~WavyPathText() override {
+    ~WavyGlyphAnimator() override {
         fBackgroundAnimationTask.wait();
     }
 
-    void reset() override {
-        fWaves.reset(fRand, this->width(), this->height());
-        this->INHERITED::reset();
+    void reset(SkRandom* rand, int screenWidth, int screenHeight) override {
+        fWaves.reset(*rand, screenWidth, screenHeight);
+        this->MovingGlyphAnimator::reset(rand, screenWidth, screenHeight);
         std::copy(fBackPaths.get(), fBackPaths.get() + kNumPaths, fFrontPaths.get());
     }
 
@@ -265,7 +273,7 @@
      */
     void runAnimationTask(double t, double dt, int w, int h) override {
         const float tsec = static_cast<float>(t);
-        this->INHERITED::runAnimationTask(t, 0.5 * dt, w, h);
+        this->MovingGlyphAnimator::runAnimationTask(t, 0.5 * dt, w, h);
 
         for (int i = 0; i < kNumPaths; ++i) {
             const Glyph& glyph = fGlyphs[i];
@@ -313,11 +321,11 @@
     }
 
     void swapAnimationBuffers() override {
-        this->INHERITED::swapAnimationBuffers();
+        this->MovingGlyphAnimator::swapAnimationBuffers();
         std::swap(fFrontPaths, fBackPaths);
     }
 
-    void drawGlyphs(SkCanvas* canvas) override {
+    void draw(SkCanvas* canvas) override {
         for (int i = 0; i < kNumPaths; ++i) {
             canvas->drawPath(fFrontPaths[i], fGlyphs[i].fPaint);
         }
@@ -344,14 +352,12 @@
         float fOffsets[4];
     };
 
-    SkAutoTArray<SkPath>   fFrontPaths;
-    SkAutoTArray<SkPath>   fBackPaths;
-    Waves                  fWaves;
-
-    typedef MovingPathText INHERITED;
+    SkAutoTArray<SkPath> fFrontPaths;
+    SkAutoTArray<SkPath> fBackPaths;
+    Waves fWaves;
 };
 
-void WavyPathText::Waves::reset(SkRandom& rand, int w, int h) {
+void PathText::WavyGlyphAnimator::Waves::reset(SkRandom& rand, int w, int h) {
     const double pixelsPerMeter = 0.06 * std::max(w, h);
     const double medianWavelength = 8 * pixelsPerMeter;
     const double medianWaveAmplitude = 0.05 * 4 * pixelsPerMeter;
@@ -371,7 +377,8 @@
     }
 }
 
-SkPoint WavyPathText::Waves::apply(float tsec, const Sk2f matrix[3], const SkPoint& pt) const {
+SkPoint PathText::WavyGlyphAnimator::Waves::apply(float tsec, const Sk2f matrix[3],
+                                                  const SkPoint& pt) const {
     constexpr static int kTablePeriod = 1 << 12;
     static float sin2table[kTablePeriod + 1];
     static SkOnce initTable;
@@ -418,10 +425,28 @@
     return {devicePt[0] + offsetY[0] + offsetY[1], devicePt[1] - offsetX[0] - offsetX[1]};
 }
 
+bool PathText::onChar(SkUnichar unichar) {
+    switch (unichar) {
+        case 'X':
+            fDoClip = !fDoClip;
+            return true;
+        case 'S':
+            fGlyphAnimator = std::make_unique<GlyphAnimator>(fGlyphs);
+            fGlyphAnimator->reset(&fRand, this->width(), this->height());
+            return true;
+        case 'M':
+            fGlyphAnimator = std::make_unique<MovingGlyphAnimator>(fGlyphs);
+            fGlyphAnimator->reset(&fRand, this->width(), this->height());
+            return true;
+        case 'W':
+            fGlyphAnimator = std::make_unique<WavyGlyphAnimator>(fGlyphs);
+            fGlyphAnimator->reset(&fRand, this->width(), this->height());
+            return true;
+    }
+    return false;
+}
+
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 
-DEF_SAMPLE( return new PathText; )
-DEF_SAMPLE( return new MovingPathText; )
-
-Sample* MakeWavyPathTextSample() { return new WavyPathText; }
-static SampleRegistry WavyPathText(MakeWavyPathTextSample);
+Sample* MakePathTextSample() { return new PathText; }
+static SampleRegistry gPathTextSample(MakePathTextSample);