Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
CowboySlide.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2017 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
9
10#if defined(SK_ENABLE_SVG)
11
13#include "include/core/SkRect.h"
18#include "src/core/SkOSFile.h"
19#include "src/utils/SkOSPath.h"
20#include "src/xml/SkDOM.h"
21#include "tools/Resources.h"
23#include "tools/viewer/Slide.h"
24
25namespace {
26class AnimatedSVGSlide : public Slide {
27 inline static constexpr auto kAnimationIterations = 5;
28 enum State {
29 kZoomIn,
30 kScroll,
31 kZoomOut
32 };
33 sk_sp<SkSVGDOM> fDom;
34 const char* fResource = nullptr;
35 State fState = kZoomIn;
36 int fAnimationLoop = kAnimationIterations;
37 SkScalar fDelta = 1;
38
39public:
40 AnimatedSVGSlide(const char* r, const char* n) : fResource(r) { fName = n; }
41
42 void load(SkScalar w, SkScalar h) override {
43 SkASSERT(fResource);
44 auto data = GetResourceAsData(fResource);
45 if (!data) {
46 SkDebugf("Resource not found: \"%s\"\n", fResource);
47 return;
48 }
49 SkMemoryStream svgStream(std::move(data));
50
51 fDom = SkSVGDOM::Builder()
54 .make(svgStream);
55 if (fDom) {
56 fDom->setContainerSize(SkSize::Make(w, h));
57 }
58 }
59
60 void draw(SkCanvas* canvas) override {
61 if (fDom) {
62 canvas->setMatrix(SkMatrix::Scale(3, 3));
63 canvas->clipRect(SkRect::MakeLTRB(0, 0, 400, 400));
64 switch (fState) {
65 case kZoomIn:
66 fDelta += 0.2f;
67 canvas->scale(fDelta, fDelta);
68 break;
69 case kScroll:
70 if (fAnimationLoop > kAnimationIterations/2) {
71 fDelta += 80.f;
72 } else {
73 fDelta -= 80.f;
74 }
75 canvas->scale(fDelta, fDelta);
76 canvas->translate(fDelta, 0);
77 break;
78 case kZoomOut:
79 fDelta += 0.2f;
80 canvas->scale(fDelta, fDelta);
81 break;
82 }
83
84 fDom->render(canvas);
85 }
86 }
87
88 void resize(SkScalar w, SkScalar h) override {
89 if (fDom) {
90 fDom->setContainerSize(SkSize::Make(w, h));
91 }
92 }
93
94 bool animate(double nanos) override {
95 if (!fDom) {
96 return false;
97 }
98
99 --fAnimationLoop;
100 if (fAnimationLoop == 0) {
101 fAnimationLoop = kAnimationIterations;
102 switch (fState) {
103 case kZoomIn:
104 fState = kScroll;
105 fDelta = 0;
106 break;
107 case kScroll:
108 fState = kZoomOut;
109 fDelta = 2;
110 break;
111 case kZoomOut:
112 fState = kZoomIn;
113 fDelta = 1;
114 break;
115 }
116 }
117 return true;
118 }
119};
120} // namespace
121
122DEF_SLIDE( return new AnimatedSVGSlide("Cowboy.svg", "SampleCowboy"); )
123
124#endif // defined(SK_ENABLE_SVG)
const char * fName
sk_sp< SkData > GetResourceAsData(const char *resource)
Definition Resources.cpp:42
#define SkASSERT(cond)
Definition SkAssert.h:116
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
#define DEF_SLIDE(code)
Definition Slide.h:25
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
void translate(SkScalar dx, SkScalar dy)
void setMatrix(const SkM44 &matrix)
void scale(SkScalar sx, SkScalar sy)
static SkMatrix Scale(SkScalar sx, SkScalar sy)
Definition SkMatrix.h:75
Builder & setTextShapingFactory(sk_sp< SkShapers::Factory >)
Definition SkSVGDOM.cpp:400
sk_sp< SkSVGDOM > make(SkStream &) const
Definition SkSVGDOM.cpp:405
Builder & setFontManager(sk_sp< SkFontMgr >)
Definition SkSVGDOM.cpp:390
Definition Slide.h:29
virtual void resize(SkScalar winWidth, SkScalar winHeight)
Definition Slide.h:41
virtual void load(SkScalar winWidth, SkScalar winHeight)
Definition Slide.h:40
virtual bool animate(double nanos)
Definition Slide.h:39
virtual void draw(SkCanvas *canvas)=0
float SkScalar
Definition extension.cpp:12
sk_sp< Factory > BestAvailable()
sk_sp< SkFontMgr > TestFontMgr()
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition switches.h:41
SkScalar w
SkScalar h
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition SkRect.h:646
static constexpr SkSize Make(SkScalar w, SkScalar h)
Definition SkSize.h:56