blob: 06bbf888981d495eca0d1983fcc49e29856da97f [file] [log] [blame]
Jim Van Verth712fe732017-09-25 16:53:49 -04001/*
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
Hal Canary0f666812018-03-22 15:21:12 -04008#include "SkTypes.h"
9
10#ifdef SK_XML
11
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040012#include "Sample.h"
Jim Van Verth712fe732017-09-25 16:53:49 -040013#include "Resources.h"
14#include "SkCanvas.h"
15#include "SkDOM.h"
16#include "SkOSFile.h"
17#include "SkOSPath.h"
18#include "SkRect.h"
19#include "SkStream.h"
20#include "SkSVGDOM.h"
Jim Van Verth712fe732017-09-25 16:53:49 -040021
22namespace {
23
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040024class CowboyView : public Sample {
Jim Van Verth712fe732017-09-25 16:53:49 -040025public:
26 CowboyView()
27 : fLabel("SampleCowboy")
28 , fState(kZoomIn)
29 , fAnimationLoop(kAnimationIterations)
30 , fDelta(1) {}
31 ~CowboyView() override = default;
32
33protected:
34 static constexpr auto kAnimationIterations = 5;
Ben Wagner63fd7602017-10-09 15:45:33 -040035
Jim Van Verth712fe732017-09-25 16:53:49 -040036 enum State {
37 kZoomIn,
38 kScroll,
39 kZoomOut
40 };
Ben Wagner63fd7602017-10-09 15:45:33 -040041
Jim Van Verth712fe732017-09-25 16:53:49 -040042 void onOnceBeforeDraw() override {
Mike Reed0933bc92017-12-09 01:27:41 +000043 constexpr char path[] = "Cowboy.svg";
44 auto data = GetResourceAsData(path);
45 if (!data) {
46 SkDebugf("file not found: \"%s\"\n", path);
Jim Van Verth712fe732017-09-25 16:53:49 -040047 return;
48 }
Mike Reed0933bc92017-12-09 01:27:41 +000049 SkMemoryStream svgStream(std::move(data));
Jim Van Verth712fe732017-09-25 16:53:49 -040050
51 SkDOM xmlDom;
52 if (!xmlDom.build(svgStream)) {
53 SkDebugf("XML parsing failed: \"path\"\n", fPath.c_str());
54 return;
55 }
56
57 fDom = SkSVGDOM::MakeFromDOM(xmlDom);
58 if (fDom) {
59 fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
60 }
61 }
62
63 void onDrawContent(SkCanvas* canvas) override {
64 if (fDom) {
65 canvas->setMatrix(SkMatrix::MakeScale(3));
66 canvas->clipRect(SkRect::MakeLTRB(0, 0, 400, 400));
67 switch (fState) {
68 case kZoomIn:
69 fDelta += 0.2f;
70 canvas->concat(SkMatrix::MakeScale(fDelta));
71 break;
72 case kScroll:
73 if (fAnimationLoop > kAnimationIterations/2) {
74 fDelta += 80.f;
75 } else {
76 fDelta -= 80.f;
77 }
78 canvas->concat(SkMatrix::MakeScale(fDelta));
79 canvas->translate(fDelta, 0);
80 break;
81 case kZoomOut:
82 fDelta += 0.2f;
83 canvas->concat(SkMatrix::MakeScale(fDelta));
84 break;
85 }
Ben Wagner63fd7602017-10-09 15:45:33 -040086
Jim Van Verth712fe732017-09-25 16:53:49 -040087 fDom->render(canvas);
88 }
89 }
90
91 void onSizeChange() override {
92 if (fDom) {
93 fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
94 }
95
96 this->INHERITED::onSizeChange();
97 }
98
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040099 bool onQuery(Sample::Event* evt) override {
100 if (Sample::TitleQ(*evt)) {
101 Sample::TitleR(evt, fLabel.c_str());
Jim Van Verth712fe732017-09-25 16:53:49 -0400102 return true;
103 }
104
105 return this->INHERITED::onQuery(evt);
106 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400107
Jim Van Verth712fe732017-09-25 16:53:49 -0400108 bool onAnimate(const SkAnimTimer& timer) override {
109 if (!fDom) {
110 return false;
111 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400112
Jim Van Verth712fe732017-09-25 16:53:49 -0400113 --fAnimationLoop;
114 if (fAnimationLoop == 0) {
115 fAnimationLoop = kAnimationIterations;
116 switch (fState) {
117 case kZoomIn:
118 fState = kScroll;
119 fDelta = 0;
120 break;
121 case kScroll:
122 fState = kZoomOut;
123 fDelta = 2;
124 break;
125 case kZoomOut:
126 fState = kZoomIn;
127 fDelta = 1;
128 break;
129 }
130 }
131 return true;
132 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400133
Jim Van Verth712fe732017-09-25 16:53:49 -0400134private:
135 sk_sp<SkSVGDOM> fDom;
136 SkString fPath;
137 SkString fLabel;
138 State fState;
139 int fAnimationLoop;
140 SkScalar fDelta;
141
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400142 typedef Sample INHERITED;
Jim Van Verth712fe732017-09-25 16:53:49 -0400143};
144
145} // anonymous namespace
146
147DEF_SAMPLE( return new CowboyView(); )
148
Hal Canary0f666812018-03-22 15:21:12 -0400149#endif // SK_XML