blob: 95efd0fb48f62ae30d86cc6a5547cb830ab6d7bf [file] [log] [blame]
reed@android.comf76bacf2009-05-13 14:00:33 +00001#include "SampleCode.h"
2#include "SkCanvas.h"
3#include "SkPaint.h"
reed@android.com09b84a02009-06-26 20:22:26 +00004#include "SkPicture.h"
reed@android.com0ad336f2009-06-29 16:02:20 +00005#include "SkStream.h"
reed@android.comf76bacf2009-05-13 14:00:33 +00006#include "SkView.h"
7
8#include "SkRectShape.h"
9#include "SkGroupShape.h"
10
11static SkRect make_rect(int l, int t, int r, int b) {
12 SkRect rect;
13 rect.set(SkIntToScalar(l), SkIntToScalar(t),
14 SkIntToScalar(r), SkIntToScalar(b));
15 return rect;
16}
17
reed@android.comf48f2812009-05-18 04:20:55 +000018static SkShape* make_shape0(bool red) {
reed@android.comf76bacf2009-05-13 14:00:33 +000019 SkRectShape* s = new SkRectShape;
20 s->setRect(make_rect(10, 10, 90, 90));
reed@android.comf48f2812009-05-18 04:20:55 +000021 if (red) {
reed@android.comf76bacf2009-05-13 14:00:33 +000022 s->paint().setColor(SK_ColorRED);
23 }
24 return s;
25}
26
reed@android.comf48f2812009-05-18 04:20:55 +000027static SkShape* make_shape1() {
reed@android.comf76bacf2009-05-13 14:00:33 +000028 SkRectShape* s = new SkRectShape;
29 s->setOval(make_rect(10, 10, 90, 90));
reed@android.comf76bacf2009-05-13 14:00:33 +000030 s->paint().setColor(SK_ColorBLUE);
31 return s;
32}
33
reed@android.comf48f2812009-05-18 04:20:55 +000034static SkShape* make_shape2() {
reed@android.comf76bacf2009-05-13 14:00:33 +000035 SkRectShape* s = new SkRectShape;
36 s->setRRect(make_rect(10, 10, 90, 90),
37 SkIntToScalar(20), SkIntToScalar(20));
reed@android.comf76bacf2009-05-13 14:00:33 +000038 s->paint().setColor(SK_ColorGREEN);
39 return s;
40}
41
42///////////////////////////////////////////////////////////////////////////////
43
44class ShapesView : public SkView {
45 SkGroupShape fGroup;
reed@android.com88c2d8e2009-05-27 01:44:24 +000046 SkMatrixRef* fMatrixRefs[4];
reed@android.comf76bacf2009-05-13 14:00:33 +000047public:
48 ShapesView() {
49 SkMatrix m;
reed@android.comf48f2812009-05-18 04:20:55 +000050 fGroup.appendShape(make_shape0(false))->unref();
reed@android.comf76bacf2009-05-13 14:00:33 +000051 m.setRotate(SkIntToScalar(30), SkIntToScalar(50), SkIntToScalar(50));
52 m.postTranslate(0, SkIntToScalar(120));
reed@android.comf48f2812009-05-18 04:20:55 +000053 fGroup.appendShape(make_shape0(true), m)->unref();
reed@android.comf76bacf2009-05-13 14:00:33 +000054
55 m.setTranslate(SkIntToScalar(120), 0);
reed@android.comf48f2812009-05-18 04:20:55 +000056 fGroup.appendShape(make_shape1(), m)->unref();
reed@android.comf76bacf2009-05-13 14:00:33 +000057 m.postTranslate(0, SkIntToScalar(120));
reed@android.comf48f2812009-05-18 04:20:55 +000058 fGroup.appendShape(make_shape2(), m)->unref();
reed@android.com88c2d8e2009-05-27 01:44:24 +000059
60 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) {
61 SkSafeRef(fMatrixRefs[i] = fGroup.getShapeMatrixRef(i));
62 }
63
64 fAngle = 0;
65 }
66
67 virtual ~ShapesView() {
68 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) {
69 SkSafeUnref(fMatrixRefs[i]);
70 }
reed@android.comf76bacf2009-05-13 14:00:33 +000071 }
72
73protected:
74 // overrides from SkEventSink
75 virtual bool onQuery(SkEvent* evt) {
76 if (SampleCode::TitleQ(*evt)) {
77 SampleCode::TitleR(evt, "Shapes");
78 return true;
79 }
80 return this->INHERITED::onQuery(evt);
81 }
82
83 void drawBG(SkCanvas* canvas) {
84 canvas->drawColor(0xFFDDDDDD);
85 }
86
reed@android.com0ad336f2009-06-29 16:02:20 +000087 void drawpicture(SkCanvas* canvas, SkPicture& pict) {
88#if 1
89 SkDynamicMemoryWStream ostream;
90 pict.serialize(&ostream);
91
92 SkMemoryStream istream(ostream.getStream(), ostream.getOffset());
93 SkPicture newPict(&istream);
94
95 canvas->drawPicture(newPict);
96#else
97 canvas->drawPicture(pict);
98#endif
99 }
100
reed@android.com88c2d8e2009-05-27 01:44:24 +0000101 int fAngle;
102
reed@android.comf76bacf2009-05-13 14:00:33 +0000103 virtual void onDraw(SkCanvas* canvas) {
104 this->drawBG(canvas);
105
reed@android.com88c2d8e2009-05-27 01:44:24 +0000106 SkMatrix saveM = *fMatrixRefs[3];
107 fAngle = (fAngle + 5) % 360;
108 SkScalar c = SkIntToScalar(50);
109 fMatrixRefs[3]->preRotate(SkIntToScalar(fAngle), c, c);
110
reed@android.comf76bacf2009-05-13 14:00:33 +0000111 SkMatrix matrix;
reed@android.com09b84a02009-06-26 20:22:26 +0000112
113 SkGroupShape* gs = new SkGroupShape;
114 SkAutoUnref aur(gs);
115 gs->appendShape(&fGroup);
reed@android.com88c2d8e2009-05-27 01:44:24 +0000116 matrix.setScale(-SK_Scalar1, SK_Scalar1);
117 matrix.postTranslate(SkIntToScalar(220), SkIntToScalar(240));
reed@android.com09b84a02009-06-26 20:22:26 +0000118 gs->appendShape(&fGroup, matrix);
reed@android.comf76bacf2009-05-13 14:00:33 +0000119 matrix.setTranslate(SkIntToScalar(240), 0);
120 matrix.preScale(SK_Scalar1*2, SK_Scalar1*2);
reed@android.com09b84a02009-06-26 20:22:26 +0000121 gs->appendShape(&fGroup, matrix);
122
reed@android.com0ad336f2009-06-29 16:02:20 +0000123#if 0
reed@android.com09b84a02009-06-26 20:22:26 +0000124 canvas->drawShape(gs);
reed@android.comf48f2812009-05-18 04:20:55 +0000125#else
reed@android.com09b84a02009-06-26 20:22:26 +0000126 SkPicture pict;
127 SkCanvas* cv = pict.beginRecording(1000, 1000);
128 cv->scale(SK_ScalarHalf, SK_ScalarHalf);
129 cv->drawShape(gs);
130 cv->translate(SkIntToScalar(680), SkIntToScalar(480));
131 cv->scale(-SK_Scalar1, SK_Scalar1);
132 cv->drawShape(gs);
133 pict.endRecording();
reed@android.com0ad336f2009-06-29 16:02:20 +0000134
135 drawpicture(canvas, pict);
reed@android.comf48f2812009-05-18 04:20:55 +0000136#endif
reed@android.com88c2d8e2009-05-27 01:44:24 +0000137
138 *fMatrixRefs[3] = saveM;
139 this->inval(NULL);
140}
reed@android.comf76bacf2009-05-13 14:00:33 +0000141
142private:
143 typedef SkView INHERITED;
144};
145
146///////////////////////////////////////////////////////////////////////////////
147
148static SkView* MyFactory() { return new ShapesView; }
149static SkViewRegister reg(MyFactory);
150