blob: ec5b22a936b24bc83da03b3847fcffe14ce91d5a [file] [log] [blame]
reed@android.comc4cae852009-09-23 15:06:10 +00001#include "SampleCode.h"
2#include "SkView.h"
3#include "SkCanvas.h"
4
5#include "gm.h"
6
7using namespace skiagm;
8
reed@android.comc4cae852009-09-23 15:06:10 +00009// need to explicitly declare this, or we get some weird infinite loop llist
10template GMRegistry* GMRegistry::gHead;
11
12class Iter {
13public:
14 Iter() {
15 fReg = GMRegistry::Head();
16 }
17
reed@google.com9b2135a2011-01-11 19:45:38 +000018 void reset() {
19 fReg = GMRegistry::Head();
20 }
21
reed@android.comc4cae852009-09-23 15:06:10 +000022 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
41private:
42 const GMRegistry* fReg;
43};
44
45///////////////////////////////////////////////////////////////////////////////
46
reed@google.com0faac1e2011-05-11 05:58:58 +000047class GMView : public SampleView {
reed@android.comc4cae852009-09-23 15:06:10 +000048 Iter fIter;
49 GM* fGM;
50public:
51 GMView() {
52 fGM = fIter.next();
reed@google.com9b2135a2011-01-11 19:45:38 +000053 this->postNextGM();
reed@google.com0faac1e2011-05-11 05:58:58 +000054
55 this->setBGColor(0xFFDDDDDD);
reed@android.comc4cae852009-09-23 15:06:10 +000056 }
reed@google.com1d5aaa82011-05-31 15:35:54 +000057
58 virtual ~GMView() {
59 delete fGM;
60 }
61
reed@android.comc4cae852009-09-23 15:06:10 +000062protected:
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.com9b2135a2011-01-11 19:45:38 +000072 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.com0faac1e2011-05-11 05:58:58 +000086 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.comc4cae852009-09-23 15:06:10 +000087 fGM->draw(canvas);
88 }
89
90private:
reed@google.com9b2135a2011-01-11 19:45:38 +000091 void postNextGM() {
92 (new SkEvent("next-gm"))->post(this->getSinkID(), 1500);
93 }
94
reed@google.com0faac1e2011-05-11 05:58:58 +000095 typedef SampleView INHERITED;
reed@android.comc4cae852009-09-23 15:06:10 +000096};
97
98///////////////////////////////////////////////////////////////////////////////
99
100static SkView* MyFactory() { return new GMView; }
101static SkViewRegister reg(MyFactory);
102
reed@android.comf2b98d62010-12-20 18:26:13 +0000103///////////////////////////////////////////////////////////////////////////////
104
105using namespace skiagm;
106
107GM::GM() {}
108GM::~GM() {}
109
110void GM::draw(SkCanvas* canvas) {
111 this->onDraw(canvas);
112}
113
114