blob: 8b7af68cb332a0b46afefe6afed27b925711cb3d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com0f0cfae2009-10-13 13:33:16 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11
12#include "SkAnimator.h"
13#include "SkStream.h"
reed@android.com0bb6d062010-05-17 14:50:04 +000014#include "SkDOM.h"
reed@android.com2ee7c642009-10-28 14:25:34 +000015
16///////////////////////////////////////////////////////////////////////////////
17
reed@android.com0f0cfae2009-10-13 13:33:16 +000018class SkAnimatorView : public SkView {
19public:
20 SkAnimatorView();
21 virtual ~SkAnimatorView();
22
23 void setURIBase(const char dir[]);
24
25 SkAnimator* getAnimator() const { return fAnimator; }
rmistry@google.comae933ce2012-08-23 18:19:56 +000026
reed@android.com0f0cfae2009-10-13 13:33:16 +000027 bool decodeFile(const char path[]);
28 bool decodeMemory(const void* buffer, size_t size);
29 bool decodeStream(SkStream* stream);
rmistry@google.comae933ce2012-08-23 18:19:56 +000030
reed@android.com0f0cfae2009-10-13 13:33:16 +000031protected:
32 // overrides
33 virtual void onDraw(SkCanvas*);
tomhudson@google.comd6caf2e2011-07-08 14:41:12 +000034 virtual bool onQuery(SkEvent* evt);
rmistry@google.comae933ce2012-08-23 18:19:56 +000035
reed@android.com0f0cfae2009-10-13 13:33:16 +000036private:
37 SkString fBaseURI;
38 SkAnimator* fAnimator;
rmistry@google.comae933ce2012-08-23 18:19:56 +000039
reed@android.com0f0cfae2009-10-13 13:33:16 +000040 typedef SkView INHERITED;
41};
42
halcanary96fcdcc2015-08-27 07:41:13 -070043SkAnimatorView::SkAnimatorView() : fAnimator(nullptr) {}
reed@android.com0f0cfae2009-10-13 13:33:16 +000044
45SkAnimatorView::~SkAnimatorView() {
46 delete fAnimator;
47}
48
49void SkAnimatorView::setURIBase(const char dir[]) {
50 fBaseURI.set(dir);
51}
52
53bool SkAnimatorView::decodeFile(const char path[]) {
scroggoa1193e42015-01-21 12:09:53 -080054 SkAutoTDelete<SkStream> is(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -070055 return is.get() != nullptr && this->decodeStream(is);
reed@android.com0f0cfae2009-10-13 13:33:16 +000056}
57
58bool SkAnimatorView::decodeMemory(const void* buffer, size_t size) {
scroggoa1193e42015-01-21 12:09:53 -080059 SkMemoryStream is(buffer, size);
60 return this->decodeStream(&is);
reed@android.com0f0cfae2009-10-13 13:33:16 +000061}
62
reed@android.com0bb6d062010-05-17 14:50:04 +000063static const SkDOMNode* find_nodeID(const SkDOM& dom,
rmistry@google.comae933ce2012-08-23 18:19:56 +000064 const SkDOMNode* node, const char name[]) {
halcanary96fcdcc2015-08-27 07:41:13 -070065 if (nullptr == node) {
rmistry@google.comae933ce2012-08-23 18:19:56 +000066 node = dom.getRootNode();
67 }
68 do {
69 const char* idval = dom.findAttr(node, "id");
70 if (idval && !strcmp(idval, name)) {
71 return node;
72 }
73 const SkDOMNode* child = dom.getFirstChild(node);
74 if (child) {
75 const SkDOMNode* found = find_nodeID(dom, child, name);
76 if (found) {
77 return found;
78 }
79 }
halcanary96fcdcc2015-08-27 07:41:13 -070080 } while ((node = dom.getNextSibling(node)) != nullptr);
81 return nullptr;
reed@android.com0bb6d062010-05-17 14:50:04 +000082}
83
reed@android.com0f0cfae2009-10-13 13:33:16 +000084bool SkAnimatorView::decodeStream(SkStream* stream) {
85 delete fAnimator;
86 fAnimator = new SkAnimator;
87 fAnimator->setURIBase(fBaseURI.c_str());
reed@android.com0bb6d062010-05-17 14:50:04 +000088#if 0
reed@android.com0f0cfae2009-10-13 13:33:16 +000089 if (!fAnimator->decodeStream(stream)) {
90 delete fAnimator;
halcanary96fcdcc2015-08-27 07:41:13 -070091 fAnimator = nullptr;
reed@android.com0f0cfae2009-10-13 13:33:16 +000092 return false;
93 }
reed@android.com0bb6d062010-05-17 14:50:04 +000094#else
rmistry@google.comae933ce2012-08-23 18:19:56 +000095 size_t len = stream->getLength();
96 char* text = (char*)sk_malloc_throw(len);
97 stream->read(text, len);
98 SkDOM dom;
99 const SkDOM::Node* root = dom.build(text, len);
halcanary96fcdcc2015-08-27 07:41:13 -0700100 if (nullptr == root) {
rmistry@google.comae933ce2012-08-23 18:19:56 +0000101 return false;
102 }
103 if (!fAnimator->decodeDOM(dom, root)) {
104 delete fAnimator;
halcanary96fcdcc2015-08-27 07:41:13 -0700105 fAnimator = nullptr;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000106 return false;
107 }
108 for (int i = 0; i <= 10; i++) {
109 SkString name("glyph");
110 name.appendS32(i);
halcanary96fcdcc2015-08-27 07:41:13 -0700111 const SkDOM::Node* node = find_nodeID(dom, nullptr, name.c_str());
rmistry@google.comae933ce2012-08-23 18:19:56 +0000112 SkASSERT(node);
113 SkRect r;
114 dom.findScalar(node, "left", &r.fLeft);
115 dom.findScalar(node, "top", &r.fTop);
116 dom.findScalar(node, "width", &r.fRight); r.fRight += r.fLeft;
117 dom.findScalar(node, "height", &r.fBottom); r.fBottom += r.fTop;
118 SkDebugf("--- %s [%g %g %g %g]\n", name.c_str(),
119 r.fLeft, r.fTop, r.fRight, r.fBottom);
120 }
reed@android.com0bb6d062010-05-17 14:50:04 +0000121#endif
reed@android.com0f0cfae2009-10-13 13:33:16 +0000122 return true;
123}
124
125#include "SkTime.h"
126
127void SkAnimatorView::onDraw(SkCanvas* canvas) {
suyang1bb3f4a82011-10-10 16:38:58 +0000128 canvas->drawColor(SK_ColorWHITE);
reed@android.com0f0cfae2009-10-13 13:33:16 +0000129 if (fAnimator) {
reed@android.com80b4ebe2009-10-21 19:41:10 +0000130 fAnimator->draw(canvas, 0);
reed@android.com0bb6d062010-05-17 14:50:04 +0000131#if 0
reed@android.com80b4ebe2009-10-21 19:41:10 +0000132 canvas->save();
133 canvas->translate(120, 30);
134 canvas->scale(0.5, 0.5);
135 fAnimator->draw(canvas, 0);
136 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000137
reed@android.com80b4ebe2009-10-21 19:41:10 +0000138 canvas->save();
139 canvas->translate(190, 40);
140 canvas->scale(0.25, 0.25);
141 fAnimator->draw(canvas, 0);
142 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000143
halcanary96fcdcc2015-08-27 07:41:13 -0700144 this->inval(nullptr);
reed@android.com0bb6d062010-05-17 14:50:04 +0000145#endif
reed@android.com0f0cfae2009-10-13 13:33:16 +0000146 }
147}
148
tomhudson@google.comd6caf2e2011-07-08 14:41:12 +0000149bool SkAnimatorView::onQuery(SkEvent* evt) {
150 if (SampleCode::TitleQ(*evt)) {
151 SampleCode::TitleR(evt, "Animator");
152 return true;
153 }
154 return this->INHERITED::onQuery(evt);
155}
156
reed@android.com0f0cfae2009-10-13 13:33:16 +0000157//////////////////////////////////////////////////////////////////////////////
158
159static SkView* MyFactory() {
160 SkAnimatorView* av = new SkAnimatorView;
161// av->decodeFile("/skimages/test.xml");
reed@android.com0bb6d062010-05-17 14:50:04 +0000162#if 0
reed@android.com0f0cfae2009-10-13 13:33:16 +0000163 av->setURIBase("/skia/trunk/animations/");
reed@android.com80b4ebe2009-10-21 19:41:10 +0000164 av->decodeFile("/skia/trunk/animations/checkbox.xml");
reed@android.com0bb6d062010-05-17 14:50:04 +0000165#else
rmistry@google.comae933ce2012-08-23 18:19:56 +0000166 av->setURIBase("/");
167 av->decodeFile("/testanim.txt");
reed@android.com0bb6d062010-05-17 14:50:04 +0000168#endif
reed@android.com0f0cfae2009-10-13 13:33:16 +0000169 return av;
170}
171
172static SkViewRegister reg(MyFactory);