Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 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 | |
Hal Canary | 0f66681 | 2018-03-22 15:21:12 -0400 | [diff] [blame] | 8 | #include "SkTypes.h" |
| 9 | |
| 10 | #ifdef SK_XML |
| 11 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 12 | #include "Sample.h" |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 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" |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 21 | |
| 22 | namespace { |
| 23 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 24 | class CowboyView : public Sample { |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 25 | public: |
| 26 | CowboyView() |
| 27 | : fLabel("SampleCowboy") |
| 28 | , fState(kZoomIn) |
| 29 | , fAnimationLoop(kAnimationIterations) |
| 30 | , fDelta(1) {} |
| 31 | ~CowboyView() override = default; |
| 32 | |
| 33 | protected: |
| 34 | static constexpr auto kAnimationIterations = 5; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 35 | |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 36 | enum State { |
| 37 | kZoomIn, |
| 38 | kScroll, |
| 39 | kZoomOut |
| 40 | }; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 41 | |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 42 | void onOnceBeforeDraw() override { |
Mike Reed | 0933bc9 | 2017-12-09 01:27:41 +0000 | [diff] [blame] | 43 | constexpr char path[] = "Cowboy.svg"; |
| 44 | auto data = GetResourceAsData(path); |
| 45 | if (!data) { |
| 46 | SkDebugf("file not found: \"%s\"\n", path); |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 47 | return; |
| 48 | } |
Mike Reed | 0933bc9 | 2017-12-09 01:27:41 +0000 | [diff] [blame] | 49 | SkMemoryStream svgStream(std::move(data)); |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 50 | |
| 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 Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 86 | |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 87 | 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 Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 99 | bool onQuery(Sample::Event* evt) override { |
| 100 | if (Sample::TitleQ(*evt)) { |
| 101 | Sample::TitleR(evt, fLabel.c_str()); |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 102 | return true; |
| 103 | } |
| 104 | |
| 105 | return this->INHERITED::onQuery(evt); |
| 106 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 107 | |
Mike Klein | cd5104e | 2019-03-20 11:55:08 -0500 | [diff] [blame] | 108 | bool onAnimate(const AnimTimer& timer) override { |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 109 | if (!fDom) { |
| 110 | return false; |
| 111 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 112 | |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 113 | --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 Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 133 | |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 134 | private: |
| 135 | sk_sp<SkSVGDOM> fDom; |
| 136 | SkString fPath; |
| 137 | SkString fLabel; |
| 138 | State fState; |
| 139 | int fAnimationLoop; |
| 140 | SkScalar fDelta; |
| 141 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 142 | typedef Sample INHERITED; |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | } // anonymous namespace |
| 146 | |
| 147 | DEF_SAMPLE( return new CowboyView(); ) |
| 148 | |
Hal Canary | 0f66681 | 2018-03-22 15:21:12 -0400 | [diff] [blame] | 149 | #endif // SK_XML |