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 | |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 15 | #include <memory> |
| 16 | |
reed@android.com | 2ee7c64 | 2009-10-28 14:25:34 +0000 | [diff] [blame] | 17 | /////////////////////////////////////////////////////////////////////////////// |
| 18 | |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 19 | class SkAnimatorView : public SkView { |
| 20 | public: |
| 21 | SkAnimatorView(); |
| 22 | virtual ~SkAnimatorView(); |
| 23 | |
| 24 | void setURIBase(const char dir[]); |
| 25 | |
| 26 | SkAnimator* getAnimator() const { return fAnimator; } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 27 | |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 28 | bool decodeFile(const char path[]); |
| 29 | bool decodeMemory(const void* buffer, size_t size); |
| 30 | bool decodeStream(SkStream* stream); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 31 | |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 32 | protected: |
| 33 | // overrides |
| 34 | virtual void onDraw(SkCanvas*); |
tomhudson@google.com | d6caf2e | 2011-07-08 14:41:12 +0000 | [diff] [blame] | 35 | virtual bool onQuery(SkEvent* evt); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 36 | |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 37 | private: |
| 38 | SkString fBaseURI; |
| 39 | SkAnimator* fAnimator; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 40 | |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 41 | typedef SkView INHERITED; |
| 42 | }; |
| 43 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 44 | SkAnimatorView::SkAnimatorView() : fAnimator(nullptr) {} |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 45 | |
| 46 | SkAnimatorView::~SkAnimatorView() { |
| 47 | delete fAnimator; |
| 48 | } |
| 49 | |
| 50 | void SkAnimatorView::setURIBase(const char dir[]) { |
| 51 | fBaseURI.set(dir); |
| 52 | } |
| 53 | |
| 54 | bool SkAnimatorView::decodeFile(const char path[]) { |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 55 | std::unique_ptr<SkStream> is = SkStream::MakeFromFile(path); |
| 56 | return is && this->decodeStream(is.get()); |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | bool SkAnimatorView::decodeMemory(const void* buffer, size_t size) { |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 60 | SkMemoryStream is(buffer, size); |
| 61 | return this->decodeStream(&is); |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 62 | } |
| 63 | |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 64 | static const SkDOMNode* find_nodeID(const SkDOM& dom, |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 65 | const SkDOMNode* node, const char name[]) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 66 | if (nullptr == node) { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 67 | node = dom.getRootNode(); |
| 68 | } |
| 69 | do { |
| 70 | const char* idval = dom.findAttr(node, "id"); |
| 71 | if (idval && !strcmp(idval, name)) { |
| 72 | return node; |
| 73 | } |
| 74 | const SkDOMNode* child = dom.getFirstChild(node); |
| 75 | if (child) { |
| 76 | const SkDOMNode* found = find_nodeID(dom, child, name); |
| 77 | if (found) { |
| 78 | return found; |
| 79 | } |
| 80 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 81 | } while ((node = dom.getNextSibling(node)) != nullptr); |
| 82 | return nullptr; |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 83 | } |
| 84 | |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 85 | bool SkAnimatorView::decodeStream(SkStream* stream) { |
| 86 | delete fAnimator; |
| 87 | fAnimator = new SkAnimator; |
| 88 | fAnimator->setURIBase(fBaseURI.c_str()); |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 89 | #if 0 |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 90 | if (!fAnimator->decodeStream(stream)) { |
| 91 | delete fAnimator; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 92 | fAnimator = nullptr; |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 93 | return false; |
| 94 | } |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 95 | #else |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 96 | size_t len = stream->getLength(); |
| 97 | char* text = (char*)sk_malloc_throw(len); |
| 98 | stream->read(text, len); |
| 99 | SkDOM dom; |
| 100 | const SkDOM::Node* root = dom.build(text, len); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 101 | if (nullptr == root) { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 102 | return false; |
| 103 | } |
| 104 | if (!fAnimator->decodeDOM(dom, root)) { |
| 105 | delete fAnimator; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 106 | fAnimator = nullptr; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 107 | return false; |
| 108 | } |
| 109 | for (int i = 0; i <= 10; i++) { |
| 110 | SkString name("glyph"); |
| 111 | name.appendS32(i); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 112 | const SkDOM::Node* node = find_nodeID(dom, nullptr, name.c_str()); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 113 | SkASSERT(node); |
| 114 | SkRect r; |
| 115 | dom.findScalar(node, "left", &r.fLeft); |
| 116 | dom.findScalar(node, "top", &r.fTop); |
| 117 | dom.findScalar(node, "width", &r.fRight); r.fRight += r.fLeft; |
| 118 | dom.findScalar(node, "height", &r.fBottom); r.fBottom += r.fTop; |
| 119 | SkDebugf("--- %s [%g %g %g %g]\n", name.c_str(), |
| 120 | r.fLeft, r.fTop, r.fRight, r.fBottom); |
| 121 | } |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 122 | #endif |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 123 | return true; |
| 124 | } |
| 125 | |
| 126 | #include "SkTime.h" |
| 127 | |
| 128 | void SkAnimatorView::onDraw(SkCanvas* canvas) { |
suyang1 | bb3f4a8 | 2011-10-10 16:38:58 +0000 | [diff] [blame] | 129 | canvas->drawColor(SK_ColorWHITE); |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 130 | if (fAnimator) { |
reed@android.com | 80b4ebe | 2009-10-21 19:41:10 +0000 | [diff] [blame] | 131 | fAnimator->draw(canvas, 0); |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 132 | #if 0 |
reed@android.com | 80b4ebe | 2009-10-21 19:41:10 +0000 | [diff] [blame] | 133 | canvas->save(); |
| 134 | canvas->translate(120, 30); |
| 135 | canvas->scale(0.5, 0.5); |
| 136 | fAnimator->draw(canvas, 0); |
| 137 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 138 | |
reed@android.com | 80b4ebe | 2009-10-21 19:41:10 +0000 | [diff] [blame] | 139 | canvas->save(); |
| 140 | canvas->translate(190, 40); |
| 141 | canvas->scale(0.25, 0.25); |
| 142 | fAnimator->draw(canvas, 0); |
| 143 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 144 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 145 | this->inval(nullptr); |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 146 | #endif |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
tomhudson@google.com | d6caf2e | 2011-07-08 14:41:12 +0000 | [diff] [blame] | 150 | bool SkAnimatorView::onQuery(SkEvent* evt) { |
| 151 | if (SampleCode::TitleQ(*evt)) { |
| 152 | SampleCode::TitleR(evt, "Animator"); |
| 153 | return true; |
| 154 | } |
| 155 | return this->INHERITED::onQuery(evt); |
| 156 | } |
| 157 | |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 158 | ////////////////////////////////////////////////////////////////////////////// |
| 159 | |
| 160 | static SkView* MyFactory() { |
| 161 | SkAnimatorView* av = new SkAnimatorView; |
| 162 | // av->decodeFile("/skimages/test.xml"); |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 163 | #if 0 |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 164 | av->setURIBase("/skia/trunk/animations/"); |
reed@android.com | 80b4ebe | 2009-10-21 19:41:10 +0000 | [diff] [blame] | 165 | av->decodeFile("/skia/trunk/animations/checkbox.xml"); |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 166 | #else |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 167 | av->setURIBase("/"); |
| 168 | av->decodeFile("/testanim.txt"); |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 169 | #endif |
reed@android.com | 0f0cfae | 2009-10-13 13:33:16 +0000 | [diff] [blame] | 170 | return av; |
| 171 | } |
| 172 | |
| 173 | static SkViewRegister reg(MyFactory); |