blob: 131ba9e73d5d5b104a1d727de7d3fc7c971755c6 [file] [log] [blame]
reed@android.comf76bacf2009-05-13 14:00:33 +00001#include "SampleCode.h"
2#include "SkCanvas.h"
3#include "SkPaint.h"
4#include "SkPorterDuff.h"
5#include "SkView.h"
6
7#include "SkRectShape.h"
8#include "SkGroupShape.h"
9
10static SkRect make_rect(int l, int t, int r, int b) {
11 SkRect rect;
12 rect.set(SkIntToScalar(l), SkIntToScalar(t),
13 SkIntToScalar(r), SkIntToScalar(b));
14 return rect;
15}
16
17static SkShape* make_shape0(const SkMatrix* matrix = NULL) {
18 SkRectShape* s = new SkRectShape;
19 s->setRect(make_rect(10, 10, 90, 90));
20 if (matrix) {
21 s->setMatrix(*matrix);
22 s->paint().setColor(SK_ColorRED);
23 }
24 return s;
25}
26
27static SkShape* make_shape1(const SkMatrix* matrix = NULL) {
28 SkRectShape* s = new SkRectShape;
29 s->setOval(make_rect(10, 10, 90, 90));
30 if (matrix) {
31 s->setMatrix(*matrix);
32 }
33 s->paint().setColor(SK_ColorBLUE);
34 return s;
35}
36
37static SkShape* make_shape2(const SkMatrix* matrix = NULL) {
38 SkRectShape* s = new SkRectShape;
39 s->setRRect(make_rect(10, 10, 90, 90),
40 SkIntToScalar(20), SkIntToScalar(20));
41 if (matrix) {
42 s->setMatrix(*matrix);
43 }
44 s->paint().setColor(SK_ColorGREEN);
45 return s;
46}
47
48///////////////////////////////////////////////////////////////////////////////
49
50class ShapesView : public SkView {
51 SkGroupShape fGroup;
52public:
53 ShapesView() {
54 SkMatrix m;
55 fGroup.appendShape(make_shape0())->unref();
56 m.setRotate(SkIntToScalar(30), SkIntToScalar(50), SkIntToScalar(50));
57 m.postTranslate(0, SkIntToScalar(120));
58 fGroup.appendShape(make_shape0(&m))->unref();
59
60 m.setTranslate(SkIntToScalar(120), 0);
61 fGroup.appendShape(make_shape1(&m))->unref();
62 m.postTranslate(0, SkIntToScalar(120));
63 fGroup.appendShape(make_shape2(&m))->unref();
64 }
65
66protected:
67 // overrides from SkEventSink
68 virtual bool onQuery(SkEvent* evt) {
69 if (SampleCode::TitleQ(*evt)) {
70 SampleCode::TitleR(evt, "Shapes");
71 return true;
72 }
73 return this->INHERITED::onQuery(evt);
74 }
75
76 void drawBG(SkCanvas* canvas) {
77 canvas->drawColor(0xFFDDDDDD);
78 }
79
80 virtual void onDraw(SkCanvas* canvas) {
81 this->drawBG(canvas);
82
83 SkMatrix matrix;
84 matrix.setTranslate(SkIntToScalar(240), 0);
85 matrix.preScale(SK_Scalar1*2, SK_Scalar1*2);
86
87 fGroup.draw(canvas);
88 fGroup.drawXY(canvas, 0, SkIntToScalar(240));
89 fGroup.drawMatrix(canvas, matrix);
90 }
91
92private:
93 typedef SkView INHERITED;
94};
95
96///////////////////////////////////////////////////////////////////////////////
97
98static SkView* MyFactory() { return new ShapesView; }
99static SkViewRegister reg(MyFactory);
100