blob: b1eec87e5087c5b8d1c14c3418574344c81ad9f3 [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
8#include "SampleCode.h"
9#include "Resources.h"
10#include "SkCanvas.h"
11#include "SkDOM.h"
12#include "SkOSFile.h"
13#include "SkOSPath.h"
14#include "SkRect.h"
15#include "SkStream.h"
16#include "SkSVGDOM.h"
17#include "SkView.h"
18
19namespace {
20
21class CowboyView : public SampleView {
22public:
23 CowboyView()
24 : fLabel("SampleCowboy")
25 , fState(kZoomIn)
26 , fAnimationLoop(kAnimationIterations)
27 , fDelta(1) {}
28 ~CowboyView() override = default;
29
30protected:
31 static constexpr auto kAnimationIterations = 5;
Ben Wagner63fd7602017-10-09 15:45:33 -040032
Jim Van Verth712fe732017-09-25 16:53:49 -040033 enum State {
34 kZoomIn,
35 kScroll,
36 kZoomOut
37 };
Ben Wagner63fd7602017-10-09 15:45:33 -040038
Jim Van Verth712fe732017-09-25 16:53:49 -040039 void onOnceBeforeDraw() override {
40 fPath = GetResourcePath("Cowboy.svg");
41 SkFILEStream svgStream(fPath.c_str());
42 if (!svgStream.isValid()) {
43 SkDebugf("file not found: \"path\"\n", fPath.c_str());
44 return;
45 }
46
47 SkDOM xmlDom;
48 if (!xmlDom.build(svgStream)) {
49 SkDebugf("XML parsing failed: \"path\"\n", fPath.c_str());
50 return;
51 }
52
53 fDom = SkSVGDOM::MakeFromDOM(xmlDom);
54 if (fDom) {
55 fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
56 }
57 }
58
59 void onDrawContent(SkCanvas* canvas) override {
60 if (fDom) {
61 canvas->setMatrix(SkMatrix::MakeScale(3));
62 canvas->clipRect(SkRect::MakeLTRB(0, 0, 400, 400));
63 switch (fState) {
64 case kZoomIn:
65 fDelta += 0.2f;
66 canvas->concat(SkMatrix::MakeScale(fDelta));
67 break;
68 case kScroll:
69 if (fAnimationLoop > kAnimationIterations/2) {
70 fDelta += 80.f;
71 } else {
72 fDelta -= 80.f;
73 }
74 canvas->concat(SkMatrix::MakeScale(fDelta));
75 canvas->translate(fDelta, 0);
76 break;
77 case kZoomOut:
78 fDelta += 0.2f;
79 canvas->concat(SkMatrix::MakeScale(fDelta));
80 break;
81 }
Ben Wagner63fd7602017-10-09 15:45:33 -040082
Jim Van Verth712fe732017-09-25 16:53:49 -040083 fDom->render(canvas);
84 }
85 }
86
87 void onSizeChange() override {
88 if (fDom) {
89 fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
90 }
91
92 this->INHERITED::onSizeChange();
93 }
94
95 bool onQuery(SkEvent* evt) override {
96 if (SampleCode::TitleQ(*evt)) {
97 SampleCode::TitleR(evt, fLabel.c_str());
98 return true;
99 }
100
101 return this->INHERITED::onQuery(evt);
102 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400103
Jim Van Verth712fe732017-09-25 16:53:49 -0400104 bool onAnimate(const SkAnimTimer& timer) override {
105 if (!fDom) {
106 return false;
107 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400108
Jim Van Verth712fe732017-09-25 16:53:49 -0400109 --fAnimationLoop;
110 if (fAnimationLoop == 0) {
111 fAnimationLoop = kAnimationIterations;
112 switch (fState) {
113 case kZoomIn:
114 fState = kScroll;
115 fDelta = 0;
116 break;
117 case kScroll:
118 fState = kZoomOut;
119 fDelta = 2;
120 break;
121 case kZoomOut:
122 fState = kZoomIn;
123 fDelta = 1;
124 break;
125 }
126 }
127 return true;
128 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400129
Jim Van Verth712fe732017-09-25 16:53:49 -0400130private:
131 sk_sp<SkSVGDOM> fDom;
132 SkString fPath;
133 SkString fLabel;
134 State fState;
135 int fAnimationLoop;
136 SkScalar fDelta;
137
138 typedef SampleView INHERITED;
139};
140
141} // anonymous namespace
142
143DEF_SAMPLE( return new CowboyView(); )
144