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 | |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 12 | #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 | |
| 23 | namespace { |
| 24 | |
| 25 | class CowboyView : public SampleView { |
| 26 | public: |
| 27 | CowboyView() |
| 28 | : fLabel("SampleCowboy") |
| 29 | , fState(kZoomIn) |
| 30 | , fAnimationLoop(kAnimationIterations) |
| 31 | , fDelta(1) {} |
| 32 | ~CowboyView() override = default; |
| 33 | |
| 34 | protected: |
| 35 | static constexpr auto kAnimationIterations = 5; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 36 | |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 37 | enum State { |
| 38 | kZoomIn, |
| 39 | kScroll, |
| 40 | kZoomOut |
| 41 | }; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 42 | |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 43 | void onOnceBeforeDraw() override { |
Mike Reed | 0933bc9 | 2017-12-09 01:27:41 +0000 | [diff] [blame] | 44 | constexpr char path[] = "Cowboy.svg"; |
| 45 | auto data = GetResourceAsData(path); |
| 46 | if (!data) { |
| 47 | SkDebugf("file not found: \"%s\"\n", path); |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 48 | return; |
| 49 | } |
Mike Reed | 0933bc9 | 2017-12-09 01:27:41 +0000 | [diff] [blame] | 50 | SkMemoryStream svgStream(std::move(data)); |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 51 | |
| 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 Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 87 | |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 88 | 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 Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 108 | |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 109 | bool onAnimate(const SkAnimTimer& timer) override { |
| 110 | if (!fDom) { |
| 111 | return false; |
| 112 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 113 | |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 114 | --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 Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 134 | |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 135 | private: |
| 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 | |
| 148 | DEF_SAMPLE( return new CowboyView(); ) |
| 149 | |
Hal Canary | 0f66681 | 2018-03-22 15:21:12 -0400 | [diff] [blame] | 150 | #endif // SK_XML |