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 | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkCanvas.h" |
| 11 | |
| 12 | #include "gm.h" |
| 13 | |
| 14 | using namespace skiagm; |
| 15 | |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 16 | // need to explicitly declare this, or we get some weird infinite loop llist |
| 17 | template GMRegistry* GMRegistry::gHead; |
| 18 | |
| 19 | class Iter { |
| 20 | public: |
| 21 | Iter() { |
| 22 | fReg = GMRegistry::Head(); |
| 23 | } |
| 24 | |
reed@google.com | 9b2135a | 2011-01-11 19:45:38 +0000 | [diff] [blame] | 25 | void reset() { |
| 26 | fReg = GMRegistry::Head(); |
| 27 | } |
| 28 | |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 29 | GM* next() { |
| 30 | if (fReg) { |
| 31 | GMRegistry::Factory fact = fReg->factory(); |
| 32 | fReg = fReg->next(); |
| 33 | return fact(0); |
| 34 | } |
| 35 | return NULL; |
| 36 | } |
| 37 | |
| 38 | static int Count() { |
| 39 | const GMRegistry* reg = GMRegistry::Head(); |
| 40 | int count = 0; |
| 41 | while (reg) { |
| 42 | count += 1; |
| 43 | reg = reg->next(); |
| 44 | } |
| 45 | return count; |
| 46 | } |
| 47 | |
| 48 | private: |
| 49 | const GMRegistry* fReg; |
| 50 | }; |
| 51 | |
| 52 | /////////////////////////////////////////////////////////////////////////////// |
| 53 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 54 | class GMView : public SampleView { |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 55 | Iter fIter; |
| 56 | GM* fGM; |
| 57 | public: |
| 58 | GMView() { |
| 59 | fGM = fIter.next(); |
reed@google.com | 9b2135a | 2011-01-11 19:45:38 +0000 | [diff] [blame] | 60 | this->postNextGM(); |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 61 | |
| 62 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 63 | } |
reed@google.com | 1d5aaa8 | 2011-05-31 15:35:54 +0000 | [diff] [blame] | 64 | |
| 65 | virtual ~GMView() { |
| 66 | delete fGM; |
| 67 | } |
| 68 | |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 69 | protected: |
| 70 | // overrides from SkEventSink |
| 71 | virtual bool onQuery(SkEvent* evt) { |
| 72 | if (SampleCode::TitleQ(*evt)) { |
| 73 | SampleCode::TitleR(evt, "GM"); |
| 74 | return true; |
| 75 | } |
| 76 | return this->INHERITED::onQuery(evt); |
| 77 | } |
| 78 | |
reed@google.com | 9b2135a | 2011-01-11 19:45:38 +0000 | [diff] [blame] | 79 | virtual bool onEvent(const SkEvent& evt) { |
| 80 | if (evt.isType("next-gm")) { |
| 81 | delete fGM; |
| 82 | if (!(fGM = fIter.next())) { |
| 83 | fIter.reset(); |
| 84 | fGM = fIter.next(); |
| 85 | } |
| 86 | this->inval(NULL); |
| 87 | this->postNextGM(); |
| 88 | return true; |
| 89 | } |
| 90 | return this->INHERITED::onEvent(evt); |
| 91 | } |
| 92 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 93 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 94 | fGM->draw(canvas); |
| 95 | } |
| 96 | |
| 97 | private: |
reed@google.com | 9b2135a | 2011-01-11 19:45:38 +0000 | [diff] [blame] | 98 | void postNextGM() { |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 99 | (new SkEvent("next-gm", this->getSinkID()))->postDelay(1500); |
reed@google.com | 9b2135a | 2011-01-11 19:45:38 +0000 | [diff] [blame] | 100 | } |
| 101 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 102 | typedef SampleView INHERITED; |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | /////////////////////////////////////////////////////////////////////////////// |
| 106 | |
| 107 | static SkView* MyFactory() { return new GMView; } |
| 108 | static SkViewRegister reg(MyFactory); |
| 109 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 110 | /////////////////////////////////////////////////////////////////////////////// |
| 111 | |
| 112 | using namespace skiagm; |
| 113 | |
| 114 | GM::GM() {} |
| 115 | GM::~GM() {} |
| 116 | |
| 117 | void GM::draw(SkCanvas* canvas) { |
| 118 | this->onDraw(canvas); |
| 119 | } |
| 120 | |
| 121 | |