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