blob: d4a4cdd2887f150c405310f742b67f65c9ec494e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
reed@android.com0f0cfae2009-10-13 13:33:16 +00007#include "SampleCode.h"
8#include "SkView.h"
9#include "SkCanvas.h"
10
11#include "SkAnimator.h"
12#include "SkStream.h"
reed@android.com0bb6d062010-05-17 14:50:04 +000013#include "SkDOM.h"
reed@android.com2ee7c642009-10-28 14:25:34 +000014
15///////////////////////////////////////////////////////////////////////////////
16
reed@android.com0f0cfae2009-10-13 13:33:16 +000017class SkAnimatorView : public SkView {
18public:
19 SkAnimatorView();
20 virtual ~SkAnimatorView();
21
22 void setURIBase(const char dir[]);
23
24 SkAnimator* getAnimator() const { return fAnimator; }
rmistry@google.comae933ce2012-08-23 18:19:56 +000025
reed@android.com0f0cfae2009-10-13 13:33:16 +000026 bool decodeFile(const char path[]);
27 bool decodeMemory(const void* buffer, size_t size);
28 bool decodeStream(SkStream* stream);
rmistry@google.comae933ce2012-08-23 18:19:56 +000029
reed@android.com0f0cfae2009-10-13 13:33:16 +000030protected:
31 // overrides
32 virtual void onDraw(SkCanvas*);
tomhudson@google.comd6caf2e2011-07-08 14:41:12 +000033 virtual bool onQuery(SkEvent* evt);
rmistry@google.comae933ce2012-08-23 18:19:56 +000034
reed@android.com0f0cfae2009-10-13 13:33:16 +000035private:
36 SkString fBaseURI;
37 SkAnimator* fAnimator;
rmistry@google.comae933ce2012-08-23 18:19:56 +000038
reed@android.com0f0cfae2009-10-13 13:33:16 +000039 typedef SkView INHERITED;
40};
41
halcanary96fcdcc2015-08-27 07:41:13 -070042SkAnimatorView::SkAnimatorView() : fAnimator(nullptr) {}
reed@android.com0f0cfae2009-10-13 13:33:16 +000043
44SkAnimatorView::~SkAnimatorView() {
45 delete fAnimator;
46}
47
48void SkAnimatorView::setURIBase(const char dir[]) {
49 fBaseURI.set(dir);
50}
51
52bool SkAnimatorView::decodeFile(const char path[]) {
scroggoa1193e42015-01-21 12:09:53 -080053 SkAutoTDelete<SkStream> is(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -070054 return is.get() != nullptr && this->decodeStream(is);
reed@android.com0f0cfae2009-10-13 13:33:16 +000055}
56
57bool SkAnimatorView::decodeMemory(const void* buffer, size_t size) {
scroggoa1193e42015-01-21 12:09:53 -080058 SkMemoryStream is(buffer, size);
59 return this->decodeStream(&is);
reed@android.com0f0cfae2009-10-13 13:33:16 +000060}
61
reed@android.com0bb6d062010-05-17 14:50:04 +000062static const SkDOMNode* find_nodeID(const SkDOM& dom,
rmistry@google.comae933ce2012-08-23 18:19:56 +000063 const SkDOMNode* node, const char name[]) {
halcanary96fcdcc2015-08-27 07:41:13 -070064 if (nullptr == node) {
rmistry@google.comae933ce2012-08-23 18:19:56 +000065 node = dom.getRootNode();
66 }
67 do {
68 const char* idval = dom.findAttr(node, "id");
69 if (idval && !strcmp(idval, name)) {
70 return node;
71 }
72 const SkDOMNode* child = dom.getFirstChild(node);
73 if (child) {
74 const SkDOMNode* found = find_nodeID(dom, child, name);
75 if (found) {
76 return found;
77 }
78 }
halcanary96fcdcc2015-08-27 07:41:13 -070079 } while ((node = dom.getNextSibling(node)) != nullptr);
80 return nullptr;
reed@android.com0bb6d062010-05-17 14:50:04 +000081}
82
reed@android.com0f0cfae2009-10-13 13:33:16 +000083bool SkAnimatorView::decodeStream(SkStream* stream) {
84 delete fAnimator;
85 fAnimator = new SkAnimator;
86 fAnimator->setURIBase(fBaseURI.c_str());
reed@android.com0bb6d062010-05-17 14:50:04 +000087#if 0
reed@android.com0f0cfae2009-10-13 13:33:16 +000088 if (!fAnimator->decodeStream(stream)) {
89 delete fAnimator;
halcanary96fcdcc2015-08-27 07:41:13 -070090 fAnimator = nullptr;
reed@android.com0f0cfae2009-10-13 13:33:16 +000091 return false;
92 }
reed@android.com0bb6d062010-05-17 14:50:04 +000093#else
rmistry@google.comae933ce2012-08-23 18:19:56 +000094 size_t len = stream->getLength();
95 char* text = (char*)sk_malloc_throw(len);
96 stream->read(text, len);
97 SkDOM dom;
98 const SkDOM::Node* root = dom.build(text, len);
halcanary96fcdcc2015-08-27 07:41:13 -070099 if (nullptr == root) {
rmistry@google.comae933ce2012-08-23 18:19:56 +0000100 return false;
101 }
102 if (!fAnimator->decodeDOM(dom, root)) {
103 delete fAnimator;
halcanary96fcdcc2015-08-27 07:41:13 -0700104 fAnimator = nullptr;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000105 return false;
106 }
107 for (int i = 0; i <= 10; i++) {
108 SkString name("glyph");
109 name.appendS32(i);
halcanary96fcdcc2015-08-27 07:41:13 -0700110 const SkDOM::Node* node = find_nodeID(dom, nullptr, name.c_str());
rmistry@google.comae933ce2012-08-23 18:19:56 +0000111 SkASSERT(node);
112 SkRect r;
113 dom.findScalar(node, "left", &r.fLeft);
114 dom.findScalar(node, "top", &r.fTop);
115 dom.findScalar(node, "width", &r.fRight); r.fRight += r.fLeft;
116 dom.findScalar(node, "height", &r.fBottom); r.fBottom += r.fTop;
117 SkDebugf("--- %s [%g %g %g %g]\n", name.c_str(),
118 r.fLeft, r.fTop, r.fRight, r.fBottom);
119 }
reed@android.com0bb6d062010-05-17 14:50:04 +0000120#endif
reed@android.com0f0cfae2009-10-13 13:33:16 +0000121 return true;
122}
123
124#include "SkTime.h"
125
126void SkAnimatorView::onDraw(SkCanvas* canvas) {
suyang1bb3f4a82011-10-10 16:38:58 +0000127 canvas->drawColor(SK_ColorWHITE);
reed@android.com0f0cfae2009-10-13 13:33:16 +0000128 if (fAnimator) {
reed@android.com80b4ebe2009-10-21 19:41:10 +0000129 fAnimator->draw(canvas, 0);
reed@android.com0bb6d062010-05-17 14:50:04 +0000130#if 0
reed@android.com80b4ebe2009-10-21 19:41:10 +0000131 canvas->save();
132 canvas->translate(120, 30);
133 canvas->scale(0.5, 0.5);
134 fAnimator->draw(canvas, 0);
135 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000136
reed@android.com80b4ebe2009-10-21 19:41:10 +0000137 canvas->save();
138 canvas->translate(190, 40);
139 canvas->scale(0.25, 0.25);
140 fAnimator->draw(canvas, 0);
141 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000142
halcanary96fcdcc2015-08-27 07:41:13 -0700143 this->inval(nullptr);
reed@android.com0bb6d062010-05-17 14:50:04 +0000144#endif
reed@android.com0f0cfae2009-10-13 13:33:16 +0000145 }
146}
147
tomhudson@google.comd6caf2e2011-07-08 14:41:12 +0000148bool SkAnimatorView::onQuery(SkEvent* evt) {
149 if (SampleCode::TitleQ(*evt)) {
150 SampleCode::TitleR(evt, "Animator");
151 return true;
152 }
153 return this->INHERITED::onQuery(evt);
154}
155
reed@android.com0f0cfae2009-10-13 13:33:16 +0000156//////////////////////////////////////////////////////////////////////////////
157
158static SkView* MyFactory() {
159 SkAnimatorView* av = new SkAnimatorView;
160// av->decodeFile("/skimages/test.xml");
reed@android.com0bb6d062010-05-17 14:50:04 +0000161#if 0
reed@android.com0f0cfae2009-10-13 13:33:16 +0000162 av->setURIBase("/skia/trunk/animations/");
reed@android.com80b4ebe2009-10-21 19:41:10 +0000163 av->decodeFile("/skia/trunk/animations/checkbox.xml");
reed@android.com0bb6d062010-05-17 14:50:04 +0000164#else
rmistry@google.comae933ce2012-08-23 18:19:56 +0000165 av->setURIBase("/");
166 av->decodeFile("/testanim.txt");
reed@android.com0bb6d062010-05-17 14:50:04 +0000167#endif
reed@android.com0f0cfae2009-10-13 13:33:16 +0000168 return av;
169}
170
171static SkViewRegister reg(MyFactory);