1/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SampleCode.h"
9#include "SkAnimTimer.h"
10#include "SkView.h"
11#include "SkCanvas.h"
12#include "SkUtils.h"
13#include "SkColorPriv.h"
14#include "SkColorFilter.h"
15#include "SkImage.h"
16#include "SkRandom.h"
17#include "SkTime.h"
18#include "SkTypeface.h"
19#include "Timer.h"
20
21#if SK_SUPPORT_GPU
22#include "GrContext.h"
23#endif
24
25// Create an animation of a bunch of letters that rotate in place. This is intended to stress
26// the glyph atlas and test that we don't see corruption or bad slowdowns.
27class FlutterAnimateView : public SampleView {
28public:
29    FlutterAnimateView() : fCurrTime(0), fResetTime(0) {}
30
31protected:
32    void onOnceBeforeDraw() override {
33        fTypeface = SkTypeface::MakeFromFile("/skimages/samplefont.ttf");
34        initChars();
35    }
36
37    // overrides from SkEventSink
38    bool onQuery(SkEvent* evt) override {
39        if (SampleCode::TitleQ(*evt)) {
40            SampleCode::TitleR(evt, "FlutterAnimate");
41            return true;
42        }
43
44        return this->INHERITED::onQuery(evt);
45    }
46
47    void onDrawContent(SkCanvas* canvas) override {
48        SkPaint paint;
49        paint.setTypeface(fTypeface);
50        paint.setAntiAlias(true);
51        paint.setFilterQuality(kMedium_SkFilterQuality);
52        paint.setTextSize(50);
53
54        // rough center of each glyph
55        static constexpr auto kMidX = 35;
56        static constexpr auto kMidY = 50;
57
58        canvas->clear(SK_ColorWHITE);
59        for (int i = 0; i < kNumChars; ++i) {
60            canvas->save();
61            double rot = SkScalarInterp(fChars[i].fStartRotation, fChars[i].fEndRotation,
62                                        fCurrTime/kDuration);
63            canvas->translate(fChars[i].fPosition.fX + kMidX, fChars[i].fPosition.fY - kMidY);
64            canvas->rotate(SkRadiansToDegrees(rot));
65            canvas->translate(-35,+50);
66            canvas->drawString(fChars[i].fChar, 0, 0, paint);
67            canvas->restore();
68        }
69    }
70
71    bool onAnimate(const SkAnimTimer& timer) override {
72        fCurrTime = timer.secs() - fResetTime;
73        if (fCurrTime > kDuration) {
74            this->initChars();
75            fResetTime = timer.secs();
76            fCurrTime = 0;
77        }
78
79        return true;
80    }
81
82private:
83    void initChars() {
84        for (int i = 0; i < kNumChars; ++i) {
85            char c = fRand.nextULessThan(26) + 65;
86            fChars[i].fChar[0] = c;
87            fChars[i].fChar[1] = '\0';
88            fChars[i].fPosition = SkPoint::Make(fRand.nextF()*748 + 10, fRand.nextF()*1004 + 10);
89            fChars[i].fStartRotation = fRand.nextF();
90            fChars[i].fEndRotation = fRand.nextF() * 20 - 10;
91        }
92    }
93
94    static constexpr double kDuration = 5.0;
95    double fCurrTime;
96    double fResetTime;
97    SkRandom fRand;
98
99    struct AnimatedChar {
100        char fChar[2];
101        SkPoint  fPosition;
102        SkScalar fStartRotation;
103        SkScalar fEndRotation;
104    };
105    sk_sp<SkTypeface> fTypeface;
106    static constexpr int kNumChars = 40;
107    AnimatedChar fChars[kNumChars];
108
109    typedef SampleView INHERITED;
110};
111
112//////////////////////////////////////////////////////////////////////////////
113
114static SkView* MyFactory() { return new FlutterAnimateView; }
115static SkViewRegister reg(MyFactory);
116