epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 7 | #include "SampleCode.h" |
| 8 | #include "SkView.h" |
| 9 | #include "SkCanvas.h" |
| 10 | |
| 11 | #include "SkAnimator.h" |
| 12 | #include "SkStream.h" |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 13 | #include "SkDOM.h" |
reed@android.com | 2ee7c64 | 2009-10-28 14:25:34 +0000 | [diff] [blame] | 14 | |
| 15 | /////////////////////////////////////////////////////////////////////////////// |
| 16 | |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 17 | class SkAnimatorView : public SkView { |
| 18 | public: |
| 19 | SkAnimatorView(); |
| 20 | virtual ~SkAnimatorView(); |
| 21 | |
| 22 | void setURIBase(const char dir[]); |
| 23 | |
| 24 | SkAnimator* getAnimator() const { return fAnimator; } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 25 | |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 26 | bool decodeFile(const char path[]); |
| 27 | bool decodeMemory(const void* buffer, size_t size); |
| 28 | bool decodeStream(SkStream* stream); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 29 | |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 30 | protected: |
| 31 | // overrides |
| 32 | virtual void onDraw(SkCanvas*); |
tomhudson@google.com | d6caf2e | 2011-07-08 14:41:12 +0000 | [diff] [blame] | 33 | virtual bool onQuery(SkEvent* evt); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 34 | |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 35 | private: |
| 36 | SkString fBaseURI; |
| 37 | SkAnimator* fAnimator; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 38 | |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 39 | typedef SkView INHERITED; |
| 40 | }; |
| 41 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 42 | SkAnimatorView::SkAnimatorView() : fAnimator(nullptr) {} |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 43 | |
| 44 | SkAnimatorView::~SkAnimatorView() { |
| 45 | delete fAnimator; |
| 46 | } |
| 47 | |
| 48 | void SkAnimatorView::setURIBase(const char dir[]) { |
| 49 | fBaseURI.set(dir); |
| 50 | } |
| 51 | |
| 52 | bool SkAnimatorView::decodeFile(const char path[]) { |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 53 | SkAutoTDelete<SkStream> is(SkStream::NewFromFile(path)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 54 | return is.get() != nullptr && this->decodeStream(is); |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | bool SkAnimatorView::decodeMemory(const void* buffer, size_t size) { |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 58 | SkMemoryStream is(buffer, size); |
| 59 | return this->decodeStream(&is); |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 60 | } |
| 61 | |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 62 | static const SkDOMNode* find_nodeID(const SkDOM& dom, |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 63 | const SkDOMNode* node, const char name[]) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 64 | if (nullptr == node) { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 65 | 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 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 79 | } while ((node = dom.getNextSibling(node)) != nullptr); |
| 80 | return nullptr; |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 81 | } |
| 82 | |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 83 | bool SkAnimatorView::decodeStream(SkStream* stream) { |
| 84 | delete fAnimator; |
| 85 | fAnimator = new SkAnimator; |
| 86 | fAnimator->setURIBase(fBaseURI.c_str()); |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 87 | #if 0 |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 88 | if (!fAnimator->decodeStream(stream)) { |
| 89 | delete fAnimator; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 90 | fAnimator = nullptr; |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 91 | return false; |
| 92 | } |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 93 | #else |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 94 | 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 99 | if (nullptr == root) { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | if (!fAnimator->decodeDOM(dom, root)) { |
| 103 | delete fAnimator; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 104 | fAnimator = nullptr; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 105 | return false; |
| 106 | } |
| 107 | for (int i = 0; i <= 10; i++) { |
| 108 | SkString name("glyph"); |
| 109 | name.appendS32(i); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 110 | const SkDOM::Node* node = find_nodeID(dom, nullptr, name.c_str()); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 111 | 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.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 120 | #endif |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 121 | return true; |
| 122 | } |
| 123 | |
| 124 | #include "SkTime.h" |
| 125 | |
| 126 | void SkAnimatorView::onDraw(SkCanvas* canvas) { |
suyang1 | bb3f4a8 | 2011-10-10 16:38:58 +0000 | [diff] [blame] | 127 | canvas->drawColor(SK_ColorWHITE); |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 128 | if (fAnimator) { |
reed@android.com | 80b4ebe | 2009-10-21 19:41:10 +0000 | [diff] [blame] | 129 | fAnimator->draw(canvas, 0); |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 130 | #if 0 |
reed@android.com | 80b4ebe | 2009-10-21 19:41:10 +0000 | [diff] [blame] | 131 | canvas->save(); |
| 132 | canvas->translate(120, 30); |
| 133 | canvas->scale(0.5, 0.5); |
| 134 | fAnimator->draw(canvas, 0); |
| 135 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 136 | |
reed@android.com | 80b4ebe | 2009-10-21 19:41:10 +0000 | [diff] [blame] | 137 | canvas->save(); |
| 138 | canvas->translate(190, 40); |
| 139 | canvas->scale(0.25, 0.25); |
| 140 | fAnimator->draw(canvas, 0); |
| 141 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 142 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 143 | this->inval(nullptr); |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 144 | #endif |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
tomhudson@google.com | d6caf2e | 2011-07-08 14:41:12 +0000 | [diff] [blame] | 148 | bool 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.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 156 | ////////////////////////////////////////////////////////////////////////////// |
| 157 | |
| 158 | static SkView* MyFactory() { |
| 159 | SkAnimatorView* av = new SkAnimatorView; |
| 160 | // av->decodeFile("/skimages/test.xml"); |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 161 | #if 0 |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 162 | av->setURIBase("/skia/trunk/animations/"); |
reed@android.com | 80b4ebe | 2009-10-21 19:41:10 +0000 | [diff] [blame] | 163 | av->decodeFile("/skia/trunk/animations/checkbox.xml"); |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 164 | #else |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 165 | av->setURIBase("/"); |
| 166 | av->decodeFile("/testanim.txt"); |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 167 | #endif |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 168 | return av; |
| 169 | } |
| 170 | |
| 171 | static SkViewRegister reg(MyFactory); |