blob: 7319af8aa34009926ee26eb622c1b261dc1307f5 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.comc4cae852009-09-23 15:06:10 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11
12#include "gm.h"
13
14using namespace skiagm;
15
reed@android.comc4cae852009-09-23 15:06:10 +000016// need to explicitly declare this, or we get some weird infinite loop llist
17template GMRegistry* GMRegistry::gHead;
18
19class Iter {
20public:
21 Iter() {
22 fReg = GMRegistry::Head();
23 }
24
reed@google.com9b2135a2011-01-11 19:45:38 +000025 void reset() {
26 fReg = GMRegistry::Head();
27 }
28
reed@android.comc4cae852009-09-23 15:06:10 +000029 GM* next() {
30 if (fReg) {
31 GMRegistry::Factory fact = fReg->factory();
32 fReg = fReg->next();
33 return fact(0);
34 }
35 return NULL;
36 }
37
38 static int Count() {
39 const GMRegistry* reg = GMRegistry::Head();
40 int count = 0;
41 while (reg) {
42 count += 1;
43 reg = reg->next();
44 }
45 return count;
46 }
47
48private:
49 const GMRegistry* fReg;
50};
51
52///////////////////////////////////////////////////////////////////////////////
53
reed@google.com0faac1e2011-05-11 05:58:58 +000054class GMView : public SampleView {
reed@android.comc4cae852009-09-23 15:06:10 +000055 Iter fIter;
56 GM* fGM;
57public:
58 GMView() {
59 fGM = fIter.next();
reed@google.com9b2135a2011-01-11 19:45:38 +000060 this->postNextGM();
reed@google.com0faac1e2011-05-11 05:58:58 +000061
62 this->setBGColor(0xFFDDDDDD);
reed@android.comc4cae852009-09-23 15:06:10 +000063 }
reed@google.com1d5aaa82011-05-31 15:35:54 +000064
65 virtual ~GMView() {
66 delete fGM;
67 }
68
reed@android.comc4cae852009-09-23 15:06:10 +000069protected:
70 // overrides from SkEventSink
71 virtual bool onQuery(SkEvent* evt) {
72 if (SampleCode::TitleQ(*evt)) {
73 SampleCode::TitleR(evt, "GM");
74 return true;
75 }
76 return this->INHERITED::onQuery(evt);
77 }
78
reed@google.com9b2135a2011-01-11 19:45:38 +000079 virtual bool onEvent(const SkEvent& evt) {
80 if (evt.isType("next-gm")) {
81 delete fGM;
82 if (!(fGM = fIter.next())) {
83 fIter.reset();
84 fGM = fIter.next();
85 }
86 this->inval(NULL);
87 this->postNextGM();
88 return true;
89 }
90 return this->INHERITED::onEvent(evt);
91 }
92
reed@google.com0faac1e2011-05-11 05:58:58 +000093 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.comc4cae852009-09-23 15:06:10 +000094 fGM->draw(canvas);
95 }
96
97private:
reed@google.com9b2135a2011-01-11 19:45:38 +000098 void postNextGM() {
99 (new SkEvent("next-gm"))->post(this->getSinkID(), 1500);
100 }
101
reed@google.com0faac1e2011-05-11 05:58:58 +0000102 typedef SampleView INHERITED;
reed@android.comc4cae852009-09-23 15:06:10 +0000103};
104
105///////////////////////////////////////////////////////////////////////////////
106
107static SkView* MyFactory() { return new GMView; }
108static SkViewRegister reg(MyFactory);
109
reed@android.comf2b98d62010-12-20 18:26:13 +0000110///////////////////////////////////////////////////////////////////////////////
111
112using namespace skiagm;
113
114GM::GM() {}
115GM::~GM() {}
116
117void GM::draw(SkCanvas* canvas) {
118 this->onDraw(canvas);
119}
120
121