blob: 5c9ea76c9367d226be05bd9cd86cb6aa69ac5d92 [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
47class GMView : public SkView {
48 Iter fIter;
49 GM* fGM;
50public:
51 GMView() {
52 fGM = fIter.next();
reed@google.com9b2135a2011-01-11 19:45:38 +000053 this->postNextGM();
reed@android.comc4cae852009-09-23 15:06:10 +000054 }
55
56protected:
57 // overrides from SkEventSink
58 virtual bool onQuery(SkEvent* evt) {
59 if (SampleCode::TitleQ(*evt)) {
60 SampleCode::TitleR(evt, "GM");
61 return true;
62 }
63 return this->INHERITED::onQuery(evt);
64 }
65
reed@google.com9b2135a2011-01-11 19:45:38 +000066 virtual bool onEvent(const SkEvent& evt) {
67 if (evt.isType("next-gm")) {
68 delete fGM;
69 if (!(fGM = fIter.next())) {
70 fIter.reset();
71 fGM = fIter.next();
72 }
73 this->inval(NULL);
74 this->postNextGM();
75 return true;
76 }
77 return this->INHERITED::onEvent(evt);
78 }
79
reed@android.comc4cae852009-09-23 15:06:10 +000080 void drawBG(SkCanvas* canvas) {
81 canvas->drawColor(0xFFDDDDDD);
82 }
83
84 virtual void onDraw(SkCanvas* canvas) {
85 fGM->draw(canvas);
86 }
87
88private:
reed@google.com9b2135a2011-01-11 19:45:38 +000089 void postNextGM() {
90 (new SkEvent("next-gm"))->post(this->getSinkID(), 1500);
91 }
92
reed@android.comc4cae852009-09-23 15:06:10 +000093 typedef SkView INHERITED;
94};
95
96///////////////////////////////////////////////////////////////////////////////
97
98static SkView* MyFactory() { return new GMView; }
99static SkViewRegister reg(MyFactory);
100
reed@android.comf2b98d62010-12-20 18:26:13 +0000101///////////////////////////////////////////////////////////////////////////////
102
103using namespace skiagm;
104
105GM::GM() {}
106GM::~GM() {}
107
108void GM::draw(SkCanvas* canvas) {
109 this->onDraw(canvas);
110}
111
112