aboutsummaryrefslogtreecommitdiffhomepage
path: root/samplecode/SampleGlyphTransform.cpp
blob: b5d44f8bfca3ffa0f7bcf97c11dcd4993c3f08cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
 * Copyright 2018 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#include "SampleCode.h"
#include "sk_tool_utils.h"

#include "SkAnimTimer.h"
#include "SkCanvas.h"
#include "SkPath.h"
#include "SkRandom.h"
#include "SkRRect.h"
#include "SkTypeface.h"

// Implementation in C++ of Animated Emoji
// See https://t.d3fc.io/status/705212795936247808

class GlyphTransformView : public SampleView {
public:
    GlyphTransformView() {}

protected:
    void onOnceBeforeDraw() override {
        fEmojiFont.fTypeface = sk_tool_utils::emoji_typeface();
        fEmojiFont.fText = sk_tool_utils::emoji_sample_text();
    }

    // overrides from SkEventSink
    bool onQuery(SkEvent* evt) override {
        if (SampleCode::TitleQ(*evt)) {
            SampleCode::TitleR(evt, "Glyph Transform");
            return true;
        }
        return this->INHERITED::onQuery(evt);
    }

    void onDrawContent(SkCanvas* canvas) override {
        SkPaint paint;
        paint.setTypeface(fEmojiFont.fTypeface);
        const char* text = fEmojiFont.fText;

        canvas->scale(4, 4);

        canvas->drawLine(0, 200, 600, 200, paint);
        SkMatrix ctm;
        ctm.setRotate(SkRadiansToDegrees(fRotate));
        ctm.postScale(fScale, fScale);
        ctm.postTranslate(fTranslate.fX, fTranslate.fY);
        canvas->concat(ctm);
        canvas->drawString(text, 0, 0, paint);
    }

    bool onAnimate(const SkAnimTimer& timer) override {
        double t = timer.secs();

        fTranslate.set(99 + sin(t / 3.0e3) - t / 1024, 200 + sin(t / 999) / t);
        fScale = 4.5 - t*t / 99;
        fRotate = sin(t / 734);

        return true;
    }

private:
    struct EmojiFont {
        sk_sp<SkTypeface> fTypeface;
        const char* fText;
    } fEmojiFont;

    SkVector fTranslate;
    SkScalar fScale;
    SkScalar fRotate;

    typedef SampleView INHERITED;
};

//////////////////////////////////////////////////////////////////////////////

static SkView* MyFactory() { return new GlyphTransformView; }
static SkViewRegister reg(MyFactory);