blob: f9b9d6e10f4bb907733ce5f683a2a4e9695f1201 [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
Jim Van Verth712fe732017-09-25 16:53:49 -040012#include "SampleCode.h"
13#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"
21#include "SkView.h"
22
23namespace {
24
25class CowboyView : public SampleView {
26public:
27 CowboyView()
28 : fLabel("SampleCowboy")
29 , fState(kZoomIn)
30 , fAnimationLoop(kAnimationIterations)
31 , fDelta(1) {}
32 ~CowboyView() override = default;
33
34protected:
35 static constexpr auto kAnimationIterations = 5;
Ben Wagner63fd7602017-10-09 15:45:33 -040036
Jim Van Verth712fe732017-09-25 16:53:49 -040037 enum State {
38 kZoomIn,
39 kScroll,
40 kZoomOut
41 };
Ben Wagner63fd7602017-10-09 15:45:33 -040042
Jim Van Verth712fe732017-09-25 16:53:49 -040043 void onOnceBeforeDraw() override {
Mike Reed0933bc92017-12-09 01:27:41 +000044 constexpr char path[] = "Cowboy.svg";
45 auto data = GetResourceAsData(path);
46 if (!data) {
47 SkDebugf("file not found: \"%s\"\n", path);
Jim Van Verth712fe732017-09-25 16:53:49 -040048 return;
49 }
Mike Reed0933bc92017-12-09 01:27:41 +000050 SkMemoryStream svgStream(std::move(data));
Jim Van Verth712fe732017-09-25 16:53:49 -040051
52 SkDOM xmlDom;
53 if (!xmlDom.build(svgStream)) {
54 SkDebugf("XML parsing failed: \"path\"\n", fPath.c_str());
55 return;
56 }
57
58 fDom = SkSVGDOM::MakeFromDOM(xmlDom);
59 if (fDom) {
60 fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
61 }
62 }
63
64 void onDrawContent(SkCanvas* canvas) override {
65 if (fDom) {
66 canvas->setMatrix(SkMatrix::MakeScale(3));
67 canvas->clipRect(SkRect::MakeLTRB(0, 0, 400, 400));
68 switch (fState) {
69 case kZoomIn:
70 fDelta += 0.2f;
71 canvas->concat(SkMatrix::MakeScale(fDelta));
72 break;
73 case kScroll:
74 if (fAnimationLoop > kAnimationIterations/2) {
75 fDelta += 80.f;
76 } else {
77 fDelta -= 80.f;
78 }
79 canvas->concat(SkMatrix::MakeScale(fDelta));
80 canvas->translate(fDelta, 0);
81 break;
82 case kZoomOut:
83 fDelta += 0.2f;
84 canvas->concat(SkMatrix::MakeScale(fDelta));
85 break;
86 }
Ben Wagner63fd7602017-10-09 15:45:33 -040087
Jim Van Verth712fe732017-09-25 16:53:49 -040088 fDom->render(canvas);
89 }
90 }
91
92 void onSizeChange() override {
93 if (fDom) {
94 fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
95 }
96
97 this->INHERITED::onSizeChange();
98 }
99
100 bool onQuery(SkEvent* evt) override {
101 if (SampleCode::TitleQ(*evt)) {
102 SampleCode::TitleR(evt, fLabel.c_str());
103 return true;
104 }
105
106 return this->INHERITED::onQuery(evt);
107 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400108
Jim Van Verth712fe732017-09-25 16:53:49 -0400109 bool onAnimate(const SkAnimTimer& timer) override {
110 if (!fDom) {
111 return false;
112 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400113
Jim Van Verth712fe732017-09-25 16:53:49 -0400114 --fAnimationLoop;
115 if (fAnimationLoop == 0) {
116 fAnimationLoop = kAnimationIterations;
117 switch (fState) {
118 case kZoomIn:
119 fState = kScroll;
120 fDelta = 0;
121 break;
122 case kScroll:
123 fState = kZoomOut;
124 fDelta = 2;
125 break;
126 case kZoomOut:
127 fState = kZoomIn;
128 fDelta = 1;
129 break;
130 }
131 }
132 return true;
133 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400134
Jim Van Verth712fe732017-09-25 16:53:49 -0400135private:
136 sk_sp<SkSVGDOM> fDom;
137 SkString fPath;
138 SkString fLabel;
139 State fState;
140 int fAnimationLoop;
141 SkScalar fDelta;
142
143 typedef SampleView INHERITED;
144};
145
146} // anonymous namespace
147
148DEF_SAMPLE( return new CowboyView(); )
149
Hal Canary0f666812018-03-22 15:21:12 -0400150#endif // SK_XML