blob: 50b61bad93fe2a5992a516deb7e3a4099280795d [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
reed@android.com0bb6d062010-05-17 14:50:04 +000043SkAnimatorView::SkAnimatorView() : fAnimator(NULL) {}
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[]) {
54 SkFILEStream* is = new SkFILEStream(path);
55 SkAutoUnref aur(is);
56 return is->isValid() && this->decodeStream(is);
57}
58
59bool SkAnimatorView::decodeMemory(const void* buffer, size_t size) {
60 SkMemoryStream* is = new SkMemoryStream(buffer, size);
61 SkAutoUnref aur(is);
62 return this->decodeStream(is);
63}
64
reed@android.com0bb6d062010-05-17 14:50:04 +000065static const SkDOMNode* find_nodeID(const SkDOM& dom,
rmistry@google.comae933ce2012-08-23 18:19:56 +000066 const SkDOMNode* node, const char name[]) {
67 if (NULL == node) {
68 node = dom.getRootNode();
69 }
70 do {
71 const char* idval = dom.findAttr(node, "id");
72 if (idval && !strcmp(idval, name)) {
73 return node;
74 }
75 const SkDOMNode* child = dom.getFirstChild(node);
76 if (child) {
77 const SkDOMNode* found = find_nodeID(dom, child, name);
78 if (found) {
79 return found;
80 }
81 }
82 } while ((node = dom.getNextSibling(node)) != NULL);
83 return NULL;
reed@android.com0bb6d062010-05-17 14:50:04 +000084}
85
reed@android.com0f0cfae2009-10-13 13:33:16 +000086bool SkAnimatorView::decodeStream(SkStream* stream) {
87 delete fAnimator;
88 fAnimator = new SkAnimator;
89 fAnimator->setURIBase(fBaseURI.c_str());
reed@android.com0bb6d062010-05-17 14:50:04 +000090#if 0
reed@android.com0f0cfae2009-10-13 13:33:16 +000091 if (!fAnimator->decodeStream(stream)) {
92 delete fAnimator;
93 fAnimator = NULL;
94 return false;
95 }
reed@android.com0bb6d062010-05-17 14:50:04 +000096#else
rmistry@google.comae933ce2012-08-23 18:19:56 +000097 size_t len = stream->getLength();
98 char* text = (char*)sk_malloc_throw(len);
99 stream->read(text, len);
100 SkDOM dom;
101 const SkDOM::Node* root = dom.build(text, len);
102 if (NULL == root) {
103 return false;
104 }
105 if (!fAnimator->decodeDOM(dom, root)) {
106 delete fAnimator;
107 fAnimator = NULL;
108 return false;
109 }
110 for (int i = 0; i <= 10; i++) {
111 SkString name("glyph");
112 name.appendS32(i);
113 const SkDOM::Node* node = find_nodeID(dom, NULL, name.c_str());
114 SkASSERT(node);
115 SkRect r;
116 dom.findScalar(node, "left", &r.fLeft);
117 dom.findScalar(node, "top", &r.fTop);
118 dom.findScalar(node, "width", &r.fRight); r.fRight += r.fLeft;
119 dom.findScalar(node, "height", &r.fBottom); r.fBottom += r.fTop;
120 SkDebugf("--- %s [%g %g %g %g]\n", name.c_str(),
121 r.fLeft, r.fTop, r.fRight, r.fBottom);
122 }
reed@android.com0bb6d062010-05-17 14:50:04 +0000123#endif
reed@android.com0f0cfae2009-10-13 13:33:16 +0000124 return true;
125}
126
127#include "SkTime.h"
128
129void SkAnimatorView::onDraw(SkCanvas* canvas) {
suyang1bb3f4a82011-10-10 16:38:58 +0000130 canvas->drawColor(SK_ColorWHITE);
reed@android.com0f0cfae2009-10-13 13:33:16 +0000131 if (fAnimator) {
reed@android.com80b4ebe2009-10-21 19:41:10 +0000132 fAnimator->draw(canvas, 0);
reed@android.com0bb6d062010-05-17 14:50:04 +0000133#if 0
reed@android.com80b4ebe2009-10-21 19:41:10 +0000134 canvas->save();
135 canvas->translate(120, 30);
136 canvas->scale(0.5, 0.5);
137 fAnimator->draw(canvas, 0);
138 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000139
reed@android.com80b4ebe2009-10-21 19:41:10 +0000140 canvas->save();
141 canvas->translate(190, 40);
142 canvas->scale(0.25, 0.25);
143 fAnimator->draw(canvas, 0);
144 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000145
reed@android.com0f0cfae2009-10-13 13:33:16 +0000146 this->inval(NULL);
reed@android.com0bb6d062010-05-17 14:50:04 +0000147#endif
reed@android.com0f0cfae2009-10-13 13:33:16 +0000148 }
149}
150
tomhudson@google.comd6caf2e2011-07-08 14:41:12 +0000151bool SkAnimatorView::onQuery(SkEvent* evt) {
152 if (SampleCode::TitleQ(*evt)) {
153 SampleCode::TitleR(evt, "Animator");
154 return true;
155 }
156 return this->INHERITED::onQuery(evt);
157}
158
reed@android.com0f0cfae2009-10-13 13:33:16 +0000159//////////////////////////////////////////////////////////////////////////////
160
161static SkView* MyFactory() {
162 SkAnimatorView* av = new SkAnimatorView;
163// av->decodeFile("/skimages/test.xml");
reed@android.com0bb6d062010-05-17 14:50:04 +0000164#if 0
reed@android.com0f0cfae2009-10-13 13:33:16 +0000165 av->setURIBase("/skia/trunk/animations/");
reed@android.com80b4ebe2009-10-21 19:41:10 +0000166 av->decodeFile("/skia/trunk/animations/checkbox.xml");
reed@android.com0bb6d062010-05-17 14:50:04 +0000167#else
rmistry@google.comae933ce2012-08-23 18:19:56 +0000168 av->setURIBase("/");
169 av->decodeFile("/testanim.txt");
reed@android.com0bb6d062010-05-17 14:50:04 +0000170#endif
reed@android.com0f0cfae2009-10-13 13:33:16 +0000171 return av;
172}
173
174static SkViewRegister reg(MyFactory);