blob: 2f9f046d3f904c456ef7115a4d6cf3e5bf911f9a [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 }
57
58protected:
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.com9b2135a2011-01-11 19:45:38 +000068 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.com0faac1e2011-05-11 05:58:58 +000082 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.comc4cae852009-09-23 15:06:10 +000083 fGM->draw(canvas);
84 }
85
86private:
reed@google.com9b2135a2011-01-11 19:45:38 +000087 void postNextGM() {
88 (new SkEvent("next-gm"))->post(this->getSinkID(), 1500);
89 }
90
reed@google.com0faac1e2011-05-11 05:58:58 +000091 typedef SampleView INHERITED;
reed@android.comc4cae852009-09-23 15:06:10 +000092};
93
94///////////////////////////////////////////////////////////////////////////////
95
96static SkView* MyFactory() { return new GMView; }
97static SkViewRegister reg(MyFactory);
98
reed@android.comf2b98d62010-12-20 18:26:13 +000099///////////////////////////////////////////////////////////////////////////////
100
101using namespace skiagm;
102
103GM::GM() {}
104GM::~GM() {}
105
106void GM::draw(SkCanvas* canvas) {
107 this->onDraw(canvas);
108}
109
110