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 | } |
reed@google.com | 1d5aaa8 | 2011-05-31 15:35:54 +0000 | [diff] [blame] | 57 | |
| 58 | virtual ~GMView() { |
| 59 | delete fGM; |
| 60 | } |
| 61 | |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 62 | protected: |
| 63 | // overrides from SkEventSink |
| 64 | virtual bool onQuery(SkEvent* evt) { |
| 65 | if (SampleCode::TitleQ(*evt)) { |
| 66 | SampleCode::TitleR(evt, "GM"); |
| 67 | return true; |
| 68 | } |
| 69 | return this->INHERITED::onQuery(evt); |
| 70 | } |
| 71 | |
reed@google.com | 9b2135a | 2011-01-11 19:45:38 +0000 | [diff] [blame] | 72 | virtual bool onEvent(const SkEvent& evt) { |
| 73 | if (evt.isType("next-gm")) { |
| 74 | delete fGM; |
| 75 | if (!(fGM = fIter.next())) { |
| 76 | fIter.reset(); |
| 77 | fGM = fIter.next(); |
| 78 | } |
| 79 | this->inval(NULL); |
| 80 | this->postNextGM(); |
| 81 | return true; |
| 82 | } |
| 83 | return this->INHERITED::onEvent(evt); |
| 84 | } |
| 85 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 86 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 87 | fGM->draw(canvas); |
| 88 | } |
| 89 | |
| 90 | private: |
reed@google.com | 9b2135a | 2011-01-11 19:45:38 +0000 | [diff] [blame] | 91 | void postNextGM() { |
| 92 | (new SkEvent("next-gm"))->post(this->getSinkID(), 1500); |
| 93 | } |
| 94 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 95 | typedef SampleView INHERITED; |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | /////////////////////////////////////////////////////////////////////////////// |
| 99 | |
| 100 | static SkView* MyFactory() { return new GMView; } |
| 101 | static SkViewRegister reg(MyFactory); |
| 102 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 103 | /////////////////////////////////////////////////////////////////////////////// |
| 104 | |
| 105 | using namespace skiagm; |
| 106 | |
| 107 | GM::GM() {} |
| 108 | GM::~GM() {} |
| 109 | |
| 110 | void GM::draw(SkCanvas* canvas) { |
| 111 | this->onDraw(canvas); |
| 112 | } |
| 113 | |
| 114 | |