Jim Van Verth | 7027691 | 2018-06-01 13:46:46 -0400 | [diff] [blame^] | 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 | #include "SampleCode.h" |
| 8 | #include "sk_tool_utils.h" |
| 9 | |
| 10 | #include "SkAnimTimer.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkPath.h" |
| 13 | #include "SkRandom.h" |
| 14 | #include "SkRRect.h" |
| 15 | #include "SkTypeface.h" |
| 16 | |
| 17 | // Implementation in C++ of Animated Emoji |
| 18 | // See https://t.d3fc.io/status/705212795936247808 |
| 19 | |
| 20 | class GlyphTransformView : public SampleView { |
| 21 | public: |
| 22 | GlyphTransformView() {} |
| 23 | |
| 24 | protected: |
| 25 | void onOnceBeforeDraw() override { |
| 26 | fEmojiFont.fTypeface = sk_tool_utils::emoji_typeface(); |
| 27 | fEmojiFont.fText = sk_tool_utils::emoji_sample_text(); |
| 28 | } |
| 29 | |
| 30 | // overrides from SkEventSink |
| 31 | bool onQuery(SkEvent* evt) override { |
| 32 | if (SampleCode::TitleQ(*evt)) { |
| 33 | SampleCode::TitleR(evt, "Glyph Transform"); |
| 34 | return true; |
| 35 | } |
| 36 | return this->INHERITED::onQuery(evt); |
| 37 | } |
| 38 | |
| 39 | void onDrawContent(SkCanvas* canvas) override { |
| 40 | SkPaint paint; |
| 41 | paint.setTypeface(fEmojiFont.fTypeface); |
| 42 | const char* text = fEmojiFont.fText; |
| 43 | |
| 44 | canvas->scale(4, 4); |
| 45 | |
| 46 | canvas->drawLine(0, 200, 600, 200, paint); |
| 47 | SkMatrix ctm; |
| 48 | ctm.setRotate(SkRadiansToDegrees(fRotate)); |
| 49 | ctm.postScale(fScale, fScale); |
| 50 | ctm.postTranslate(fTranslate.fX, fTranslate.fY); |
| 51 | canvas->concat(ctm); |
| 52 | canvas->drawString(text, 0, 0, paint); |
| 53 | } |
| 54 | |
| 55 | bool onAnimate(const SkAnimTimer& timer) override { |
| 56 | double t = timer.secs(); |
| 57 | |
| 58 | fTranslate.set(99 + sin(t / 3.0e3) - t / 1024, 200 + sin(t / 999) / t); |
| 59 | fScale = 4.5 - t*t / 99; |
| 60 | fRotate = sin(t / 734); |
| 61 | |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | struct EmojiFont { |
| 67 | sk_sp<SkTypeface> fTypeface; |
| 68 | const char* fText; |
| 69 | } fEmojiFont; |
| 70 | |
| 71 | SkVector fTranslate; |
| 72 | SkScalar fScale; |
| 73 | SkScalar fRotate; |
| 74 | |
| 75 | typedef SampleView INHERITED; |
| 76 | }; |
| 77 | |
| 78 | ////////////////////////////////////////////////////////////////////////////// |
| 79 | |
| 80 | static SkView* MyFactory() { return new GlyphTransformView; } |
| 81 | static SkViewRegister reg(MyFactory); |