aboutsummaryrefslogtreecommitdiffhomepage
path: root/samplecode/SampleCowboy.cpp
diff options
context:
space:
mode:
authorGravatar Jim Van Verth <jvanverth@google.com>2017-09-25 16:53:49 -0400
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2017-09-25 21:14:09 +0000
commit712fe73cafb824f0d30a57ab0301afce52d1dc49 (patch)
treeac2ba0abefe586a94d5c4fa89760a1d23473ce1b /samplecode/SampleCowboy.cpp
parentf40ae1a4b5365620463bd63b5140bd3fc78894a1 (diff)
Add animated cowboy sample from WebKit tests, and fix.
Bug: chromium:712455 Change-Id: Ic9bb9b862abe01f112cc41d28589733460b15bc1 Reviewed-on: https://skia-review.googlesource.com/50181 Commit-Queue: Jim Van Verth <jvanverth@google.com> Reviewed-by: Robert Phillips <robertphillips@google.com>
Diffstat (limited to 'samplecode/SampleCowboy.cpp')
-rw-r--r--samplecode/SampleCowboy.cpp144
1 files changed, 144 insertions, 0 deletions
diff --git a/samplecode/SampleCowboy.cpp b/samplecode/SampleCowboy.cpp
new file mode 100644
index 0000000000..1191cd4d3e
--- /dev/null
+++ b/samplecode/SampleCowboy.cpp
@@ -0,0 +1,144 @@
+/*
+ * Copyright 2017 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SampleCode.h"
+#include "Resources.h"
+#include "SkCanvas.h"
+#include "SkDOM.h"
+#include "SkOSFile.h"
+#include "SkOSPath.h"
+#include "SkRect.h"
+#include "SkStream.h"
+#include "SkSVGDOM.h"
+#include "SkView.h"
+
+namespace {
+
+class CowboyView : public SampleView {
+public:
+ CowboyView()
+ : fLabel("SampleCowboy")
+ , fState(kZoomIn)
+ , fAnimationLoop(kAnimationIterations)
+ , fDelta(1) {}
+ ~CowboyView() override = default;
+
+protected:
+ static constexpr auto kAnimationIterations = 5;
+
+ enum State {
+ kZoomIn,
+ kScroll,
+ kZoomOut
+ };
+
+ void onOnceBeforeDraw() override {
+ fPath = GetResourcePath("Cowboy.svg");
+ SkFILEStream svgStream(fPath.c_str());
+ if (!svgStream.isValid()) {
+ SkDebugf("file not found: \"path\"\n", fPath.c_str());
+ return;
+ }
+
+ SkDOM xmlDom;
+ if (!xmlDom.build(svgStream)) {
+ SkDebugf("XML parsing failed: \"path\"\n", fPath.c_str());
+ return;
+ }
+
+ fDom = SkSVGDOM::MakeFromDOM(xmlDom);
+ if (fDom) {
+ fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
+ }
+ }
+
+ void onDrawContent(SkCanvas* canvas) override {
+ if (fDom) {
+ canvas->setMatrix(SkMatrix::MakeScale(3));
+ canvas->clipRect(SkRect::MakeLTRB(0, 0, 400, 400));
+ switch (fState) {
+ case kZoomIn:
+ fDelta += 0.2f;
+ canvas->concat(SkMatrix::MakeScale(fDelta));
+ break;
+ case kScroll:
+ if (fAnimationLoop > kAnimationIterations/2) {
+ fDelta += 80.f;
+ } else {
+ fDelta -= 80.f;
+ }
+ canvas->concat(SkMatrix::MakeScale(fDelta));
+ canvas->translate(fDelta, 0);
+ break;
+ case kZoomOut:
+ fDelta += 0.2f;
+ canvas->concat(SkMatrix::MakeScale(fDelta));
+ break;
+ }
+
+ fDom->render(canvas);
+ }
+ }
+
+ void onSizeChange() override {
+ if (fDom) {
+ fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
+ }
+
+ this->INHERITED::onSizeChange();
+ }
+
+ bool onQuery(SkEvent* evt) override {
+ if (SampleCode::TitleQ(*evt)) {
+ SampleCode::TitleR(evt, fLabel.c_str());
+ return true;
+ }
+
+ return this->INHERITED::onQuery(evt);
+ }
+
+ bool onAnimate(const SkAnimTimer& timer) override {
+ if (!fDom) {
+ return false;
+ }
+
+ --fAnimationLoop;
+ if (fAnimationLoop == 0) {
+ fAnimationLoop = kAnimationIterations;
+ switch (fState) {
+ case kZoomIn:
+ fState = kScroll;
+ fDelta = 0;
+ break;
+ case kScroll:
+ fState = kZoomOut;
+ fDelta = 2;
+ break;
+ case kZoomOut:
+ fState = kZoomIn;
+ fDelta = 1;
+ break;
+ }
+ }
+ return true;
+ }
+
+private:
+ sk_sp<SkSVGDOM> fDom;
+ SkString fPath;
+ SkString fLabel;
+ State fState;
+ int fAnimationLoop;
+ SkScalar fDelta;
+
+ typedef SampleView INHERITED;
+};
+
+} // anonymous namespace
+
+DEF_SAMPLE( return new CowboyView(); )
+