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