blob: a3ec83afe673968e45d09a91824eb8552e8ce1de [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
bungemanf93d7112016-09-16 06:24:20 -070015#include <memory>
16
reed@android.com2ee7c642009-10-28 14:25:34 +000017///////////////////////////////////////////////////////////////////////////////
18
reed@android.com0f0cfae2009-10-13 13:33:16 +000019class SkAnimatorView : public SkView {
20public:
21 SkAnimatorView();
22 virtual ~SkAnimatorView();
23
24 void setURIBase(const char dir[]);
25
26 SkAnimator* getAnimator() const { return fAnimator; }
rmistry@google.comae933ce2012-08-23 18:19:56 +000027
reed@android.com0f0cfae2009-10-13 13:33:16 +000028 bool decodeFile(const char path[]);
29 bool decodeMemory(const void* buffer, size_t size);
30 bool decodeStream(SkStream* stream);
rmistry@google.comae933ce2012-08-23 18:19:56 +000031
reed@android.com0f0cfae2009-10-13 13:33:16 +000032protected:
33 // overrides
34 virtual void onDraw(SkCanvas*);
tomhudson@google.comd6caf2e2011-07-08 14:41:12 +000035 virtual bool onQuery(SkEvent* evt);
rmistry@google.comae933ce2012-08-23 18:19:56 +000036
reed@android.com0f0cfae2009-10-13 13:33:16 +000037private:
38 SkString fBaseURI;
39 SkAnimator* fAnimator;
rmistry@google.comae933ce2012-08-23 18:19:56 +000040
reed@android.com0f0cfae2009-10-13 13:33:16 +000041 typedef SkView INHERITED;
42};
43
halcanary96fcdcc2015-08-27 07:41:13 -070044SkAnimatorView::SkAnimatorView() : fAnimator(nullptr) {}
reed@android.com0f0cfae2009-10-13 13:33:16 +000045
46SkAnimatorView::~SkAnimatorView() {
47 delete fAnimator;
48}
49
50void SkAnimatorView::setURIBase(const char dir[]) {
51 fBaseURI.set(dir);
52}
53
54bool SkAnimatorView::decodeFile(const char path[]) {
bungemanf93d7112016-09-16 06:24:20 -070055 std::unique_ptr<SkStream> is = SkStream::MakeFromFile(path);
56 return is && this->decodeStream(is.get());
reed@android.com0f0cfae2009-10-13 13:33:16 +000057}
58
59bool SkAnimatorView::decodeMemory(const void* buffer, size_t size) {
scroggoa1193e42015-01-21 12:09:53 -080060 SkMemoryStream is(buffer, size);
61 return this->decodeStream(&is);
reed@android.com0f0cfae2009-10-13 13:33:16 +000062}
63
reed@android.com0bb6d062010-05-17 14:50:04 +000064static const SkDOMNode* find_nodeID(const SkDOM& dom,
rmistry@google.comae933ce2012-08-23 18:19:56 +000065 const SkDOMNode* node, const char name[]) {
halcanary96fcdcc2015-08-27 07:41:13 -070066 if (nullptr == node) {
rmistry@google.comae933ce2012-08-23 18:19:56 +000067 node = dom.getRootNode();
68 }
69 do {
70 const char* idval = dom.findAttr(node, "id");
71 if (idval && !strcmp(idval, name)) {
72 return node;
73 }
74 const SkDOMNode* child = dom.getFirstChild(node);
75 if (child) {
76 const SkDOMNode* found = find_nodeID(dom, child, name);
77 if (found) {
78 return found;
79 }
80 }
halcanary96fcdcc2015-08-27 07:41:13 -070081 } while ((node = dom.getNextSibling(node)) != nullptr);
82 return nullptr;
reed@android.com0bb6d062010-05-17 14:50:04 +000083}
84
reed@android.com0f0cfae2009-10-13 13:33:16 +000085bool SkAnimatorView::decodeStream(SkStream* stream) {
86 delete fAnimator;
87 fAnimator = new SkAnimator;
88 fAnimator->setURIBase(fBaseURI.c_str());
reed@android.com0bb6d062010-05-17 14:50:04 +000089#if 0
reed@android.com0f0cfae2009-10-13 13:33:16 +000090 if (!fAnimator->decodeStream(stream)) {
91 delete fAnimator;
halcanary96fcdcc2015-08-27 07:41:13 -070092 fAnimator = nullptr;
reed@android.com0f0cfae2009-10-13 13:33:16 +000093 return false;
94 }
reed@android.com0bb6d062010-05-17 14:50:04 +000095#else
rmistry@google.comae933ce2012-08-23 18:19:56 +000096 size_t len = stream->getLength();
97 char* text = (char*)sk_malloc_throw(len);
98 stream->read(text, len);
99 SkDOM dom;
100 const SkDOM::Node* root = dom.build(text, len);
halcanary96fcdcc2015-08-27 07:41:13 -0700101 if (nullptr == root) {
rmistry@google.comae933ce2012-08-23 18:19:56 +0000102 return false;
103 }
104 if (!fAnimator->decodeDOM(dom, root)) {
105 delete fAnimator;
halcanary96fcdcc2015-08-27 07:41:13 -0700106 fAnimator = nullptr;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000107 return false;
108 }
109 for (int i = 0; i <= 10; i++) {
110 SkString name("glyph");
111 name.appendS32(i);
halcanary96fcdcc2015-08-27 07:41:13 -0700112 const SkDOM::Node* node = find_nodeID(dom, nullptr, name.c_str());
rmistry@google.comae933ce2012-08-23 18:19:56 +0000113 SkASSERT(node);
114 SkRect r;
115 dom.findScalar(node, "left", &r.fLeft);
116 dom.findScalar(node, "top", &r.fTop);
117 dom.findScalar(node, "width", &r.fRight); r.fRight += r.fLeft;
118 dom.findScalar(node, "height", &r.fBottom); r.fBottom += r.fTop;
119 SkDebugf("--- %s [%g %g %g %g]\n", name.c_str(),
120 r.fLeft, r.fTop, r.fRight, r.fBottom);
121 }
reed@android.com0bb6d062010-05-17 14:50:04 +0000122#endif
reed@android.com0f0cfae2009-10-13 13:33:16 +0000123 return true;
124}
125
126#include "SkTime.h"
127
128void SkAnimatorView::onDraw(SkCanvas* canvas) {
suyang1bb3f4a82011-10-10 16:38:58 +0000129 canvas->drawColor(SK_ColorWHITE);
reed@android.com0f0cfae2009-10-13 13:33:16 +0000130 if (fAnimator) {
reed@android.com80b4ebe2009-10-21 19:41:10 +0000131 fAnimator->draw(canvas, 0);
reed@android.com0bb6d062010-05-17 14:50:04 +0000132#if 0
reed@android.com80b4ebe2009-10-21 19:41:10 +0000133 canvas->save();
134 canvas->translate(120, 30);
135 canvas->scale(0.5, 0.5);
136 fAnimator->draw(canvas, 0);
137 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000138
reed@android.com80b4ebe2009-10-21 19:41:10 +0000139 canvas->save();
140 canvas->translate(190, 40);
141 canvas->scale(0.25, 0.25);
142 fAnimator->draw(canvas, 0);
143 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000144
halcanary96fcdcc2015-08-27 07:41:13 -0700145 this->inval(nullptr);
reed@android.com0bb6d062010-05-17 14:50:04 +0000146#endif
reed@android.com0f0cfae2009-10-13 13:33:16 +0000147 }
148}
149
tomhudson@google.comd6caf2e2011-07-08 14:41:12 +0000150bool SkAnimatorView::onQuery(SkEvent* evt) {
151 if (SampleCode::TitleQ(*evt)) {
152 SampleCode::TitleR(evt, "Animator");
153 return true;
154 }
155 return this->INHERITED::onQuery(evt);
156}
157
reed@android.com0f0cfae2009-10-13 13:33:16 +0000158//////////////////////////////////////////////////////////////////////////////
159
160static SkView* MyFactory() {
161 SkAnimatorView* av = new SkAnimatorView;
162// av->decodeFile("/skimages/test.xml");
reed@android.com0bb6d062010-05-17 14:50:04 +0000163#if 0
reed@android.com0f0cfae2009-10-13 13:33:16 +0000164 av->setURIBase("/skia/trunk/animations/");
reed@android.com80b4ebe2009-10-21 19:41:10 +0000165 av->decodeFile("/skia/trunk/animations/checkbox.xml");
reed@android.com0bb6d062010-05-17 14:50:04 +0000166#else
rmistry@google.comae933ce2012-08-23 18:19:56 +0000167 av->setURIBase("/");
168 av->decodeFile("/testanim.txt");
reed@android.com0bb6d062010-05-17 14:50:04 +0000169#endif
reed@android.com0f0cfae2009-10-13 13:33:16 +0000170 return av;
171}
172
173static SkViewRegister reg(MyFactory);