2011-07-28 14:26:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2009-10-13 13:33:16 +00:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
|
|
|
|
#include "SkAnimator.h"
|
|
|
|
#include "SkStream.h"
|
2010-05-17 14:50:04 +00:00
|
|
|
#include "SkDOM.h"
|
2009-10-28 14:25:34 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2009-10-13 13:33:16 +00:00
|
|
|
class SkAnimatorView : public SkView {
|
|
|
|
public:
|
|
|
|
SkAnimatorView();
|
|
|
|
virtual ~SkAnimatorView();
|
|
|
|
|
|
|
|
void setURIBase(const char dir[]);
|
|
|
|
|
|
|
|
SkAnimator* getAnimator() const { return fAnimator; }
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-10-13 13:33:16 +00:00
|
|
|
bool decodeFile(const char path[]);
|
|
|
|
bool decodeMemory(const void* buffer, size_t size);
|
|
|
|
bool decodeStream(SkStream* stream);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-10-13 13:33:16 +00:00
|
|
|
protected:
|
|
|
|
// overrides
|
|
|
|
virtual void onDraw(SkCanvas*);
|
2011-07-08 14:41:12 +00:00
|
|
|
virtual bool onQuery(SkEvent* evt);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-10-13 13:33:16 +00:00
|
|
|
private:
|
|
|
|
SkString fBaseURI;
|
|
|
|
SkAnimator* fAnimator;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-10-13 13:33:16 +00:00
|
|
|
typedef SkView INHERITED;
|
|
|
|
};
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
SkAnimatorView::SkAnimatorView() : fAnimator(nullptr) {}
|
2009-10-13 13:33:16 +00:00
|
|
|
|
|
|
|
SkAnimatorView::~SkAnimatorView() {
|
|
|
|
delete fAnimator;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkAnimatorView::setURIBase(const char dir[]) {
|
|
|
|
fBaseURI.set(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SkAnimatorView::decodeFile(const char path[]) {
|
2015-01-21 20:09:53 +00:00
|
|
|
SkAutoTDelete<SkStream> is(SkStream::NewFromFile(path));
|
2015-08-27 14:41:13 +00:00
|
|
|
return is.get() != nullptr && this->decodeStream(is);
|
2009-10-13 13:33:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SkAnimatorView::decodeMemory(const void* buffer, size_t size) {
|
2015-01-21 20:09:53 +00:00
|
|
|
SkMemoryStream is(buffer, size);
|
|
|
|
return this->decodeStream(&is);
|
2009-10-13 13:33:16 +00:00
|
|
|
}
|
|
|
|
|
2010-05-17 14:50:04 +00:00
|
|
|
static const SkDOMNode* find_nodeID(const SkDOM& dom,
|
2012-08-23 18:19:56 +00:00
|
|
|
const SkDOMNode* node, const char name[]) {
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == node) {
|
2012-08-23 18:19:56 +00:00
|
|
|
node = dom.getRootNode();
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
const char* idval = dom.findAttr(node, "id");
|
|
|
|
if (idval && !strcmp(idval, name)) {
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
const SkDOMNode* child = dom.getFirstChild(node);
|
|
|
|
if (child) {
|
|
|
|
const SkDOMNode* found = find_nodeID(dom, child, name);
|
|
|
|
if (found) {
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
} while ((node = dom.getNextSibling(node)) != nullptr);
|
|
|
|
return nullptr;
|
2010-05-17 14:50:04 +00:00
|
|
|
}
|
|
|
|
|
2009-10-13 13:33:16 +00:00
|
|
|
bool SkAnimatorView::decodeStream(SkStream* stream) {
|
|
|
|
delete fAnimator;
|
|
|
|
fAnimator = new SkAnimator;
|
|
|
|
fAnimator->setURIBase(fBaseURI.c_str());
|
2010-05-17 14:50:04 +00:00
|
|
|
#if 0
|
2009-10-13 13:33:16 +00:00
|
|
|
if (!fAnimator->decodeStream(stream)) {
|
|
|
|
delete fAnimator;
|
2015-08-27 14:41:13 +00:00
|
|
|
fAnimator = nullptr;
|
2009-10-13 13:33:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-05-17 14:50:04 +00:00
|
|
|
#else
|
2012-08-23 18:19:56 +00:00
|
|
|
size_t len = stream->getLength();
|
|
|
|
char* text = (char*)sk_malloc_throw(len);
|
|
|
|
stream->read(text, len);
|
|
|
|
SkDOM dom;
|
|
|
|
const SkDOM::Node* root = dom.build(text, len);
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == root) {
|
2012-08-23 18:19:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!fAnimator->decodeDOM(dom, root)) {
|
|
|
|
delete fAnimator;
|
2015-08-27 14:41:13 +00:00
|
|
|
fAnimator = nullptr;
|
2012-08-23 18:19:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (int i = 0; i <= 10; i++) {
|
|
|
|
SkString name("glyph");
|
|
|
|
name.appendS32(i);
|
2015-08-27 14:41:13 +00:00
|
|
|
const SkDOM::Node* node = find_nodeID(dom, nullptr, name.c_str());
|
2012-08-23 18:19:56 +00:00
|
|
|
SkASSERT(node);
|
|
|
|
SkRect r;
|
|
|
|
dom.findScalar(node, "left", &r.fLeft);
|
|
|
|
dom.findScalar(node, "top", &r.fTop);
|
|
|
|
dom.findScalar(node, "width", &r.fRight); r.fRight += r.fLeft;
|
|
|
|
dom.findScalar(node, "height", &r.fBottom); r.fBottom += r.fTop;
|
|
|
|
SkDebugf("--- %s [%g %g %g %g]\n", name.c_str(),
|
|
|
|
r.fLeft, r.fTop, r.fRight, r.fBottom);
|
|
|
|
}
|
2010-05-17 14:50:04 +00:00
|
|
|
#endif
|
2009-10-13 13:33:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "SkTime.h"
|
|
|
|
|
|
|
|
void SkAnimatorView::onDraw(SkCanvas* canvas) {
|
2011-10-10 16:38:58 +00:00
|
|
|
canvas->drawColor(SK_ColorWHITE);
|
2009-10-13 13:33:16 +00:00
|
|
|
if (fAnimator) {
|
2009-10-21 19:41:10 +00:00
|
|
|
fAnimator->draw(canvas, 0);
|
2010-05-17 14:50:04 +00:00
|
|
|
#if 0
|
2009-10-21 19:41:10 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(120, 30);
|
|
|
|
canvas->scale(0.5, 0.5);
|
|
|
|
fAnimator->draw(canvas, 0);
|
|
|
|
canvas->restore();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-10-21 19:41:10 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(190, 40);
|
|
|
|
canvas->scale(0.25, 0.25);
|
|
|
|
fAnimator->draw(canvas, 0);
|
|
|
|
canvas->restore();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
this->inval(nullptr);
|
2010-05-17 14:50:04 +00:00
|
|
|
#endif
|
2009-10-13 13:33:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-08 14:41:12 +00:00
|
|
|
bool SkAnimatorView::onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "Animator");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
2009-10-13 13:33:16 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() {
|
|
|
|
SkAnimatorView* av = new SkAnimatorView;
|
|
|
|
// av->decodeFile("/skimages/test.xml");
|
2010-05-17 14:50:04 +00:00
|
|
|
#if 0
|
2009-10-13 13:33:16 +00:00
|
|
|
av->setURIBase("/skia/trunk/animations/");
|
2009-10-21 19:41:10 +00:00
|
|
|
av->decodeFile("/skia/trunk/animations/checkbox.xml");
|
2010-05-17 14:50:04 +00:00
|
|
|
#else
|
2012-08-23 18:19:56 +00:00
|
|
|
av->setURIBase("/");
|
|
|
|
av->decodeFile("/testanim.txt");
|
2010-05-17 14:50:04 +00:00
|
|
|
#endif
|
2009-10-13 13:33:16 +00:00
|
|
|
return av;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkViewRegister reg(MyFactory);
|