blob: a175af640a5cab7935aa51e7aa553eb75cfe70ed [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
reed@google.coma09368c2011-02-24 21:42:29 +00008#define DO_AA true
reed@android.comf2b98d62010-12-20 18:26:13 +00009
reed@android.comf76bacf2009-05-13 14:00:33 +000010#include "SkRectShape.h"
11#include "SkGroupShape.h"
12
13static SkRect make_rect(int l, int t, int r, int b) {
14 SkRect rect;
15 rect.set(SkIntToScalar(l), SkIntToScalar(t),
16 SkIntToScalar(r), SkIntToScalar(b));
17 return rect;
18}
19
reed@android.comf48f2812009-05-18 04:20:55 +000020static SkShape* make_shape0(bool red) {
reed@android.comf76bacf2009-05-13 14:00:33 +000021 SkRectShape* s = new SkRectShape;
22 s->setRect(make_rect(10, 10, 90, 90));
reed@android.comf48f2812009-05-18 04:20:55 +000023 if (red) {
reed@android.comf76bacf2009-05-13 14:00:33 +000024 s->paint().setColor(SK_ColorRED);
25 }
reed@android.comf2b98d62010-12-20 18:26:13 +000026 s->paint().setAntiAlias(DO_AA);
reed@android.comf76bacf2009-05-13 14:00:33 +000027 return s;
28}
29
reed@android.comf48f2812009-05-18 04:20:55 +000030static SkShape* make_shape1() {
reed@android.comf76bacf2009-05-13 14:00:33 +000031 SkRectShape* s = new SkRectShape;
32 s->setOval(make_rect(10, 10, 90, 90));
reed@android.comf76bacf2009-05-13 14:00:33 +000033 s->paint().setColor(SK_ColorBLUE);
reed@android.comf2b98d62010-12-20 18:26:13 +000034 s->paint().setAntiAlias(DO_AA);
reed@android.comf76bacf2009-05-13 14:00:33 +000035 return s;
36}
37
reed@android.comf48f2812009-05-18 04:20:55 +000038static SkShape* make_shape2() {
reed@android.comf76bacf2009-05-13 14:00:33 +000039 SkRectShape* s = new SkRectShape;
40 s->setRRect(make_rect(10, 10, 90, 90),
41 SkIntToScalar(20), SkIntToScalar(20));
reed@android.comf76bacf2009-05-13 14:00:33 +000042 s->paint().setColor(SK_ColorGREEN);
reed@android.comf2b98d62010-12-20 18:26:13 +000043 s->paint().setAntiAlias(DO_AA);
reed@android.comf76bacf2009-05-13 14:00:33 +000044 return s;
45}
46
47///////////////////////////////////////////////////////////////////////////////
48
49class ShapesView : public SkView {
50 SkGroupShape fGroup;
reed@android.com88c2d8e2009-05-27 01:44:24 +000051 SkMatrixRef* fMatrixRefs[4];
reed@android.comf76bacf2009-05-13 14:00:33 +000052public:
53 ShapesView() {
54 SkMatrix m;
reed@android.comf48f2812009-05-18 04:20:55 +000055 fGroup.appendShape(make_shape0(false))->unref();
reed@android.comf76bacf2009-05-13 14:00:33 +000056 m.setRotate(SkIntToScalar(30), SkIntToScalar(50), SkIntToScalar(50));
57 m.postTranslate(0, SkIntToScalar(120));
reed@android.comf48f2812009-05-18 04:20:55 +000058 fGroup.appendShape(make_shape0(true), m)->unref();
reed@android.comf76bacf2009-05-13 14:00:33 +000059
60 m.setTranslate(SkIntToScalar(120), 0);
reed@android.comf48f2812009-05-18 04:20:55 +000061 fGroup.appendShape(make_shape1(), m)->unref();
reed@android.comf76bacf2009-05-13 14:00:33 +000062 m.postTranslate(0, SkIntToScalar(120));
reed@android.comf48f2812009-05-18 04:20:55 +000063 fGroup.appendShape(make_shape2(), m)->unref();
reed@android.com88c2d8e2009-05-27 01:44:24 +000064
65 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) {
66 SkSafeRef(fMatrixRefs[i] = fGroup.getShapeMatrixRef(i));
67 }
reed@android.com88c2d8e2009-05-27 01:44:24 +000068 }
69
70 virtual ~ShapesView() {
71 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) {
72 SkSafeUnref(fMatrixRefs[i]);
73 }
reed@android.comf76bacf2009-05-13 14:00:33 +000074 }
75
76protected:
77 // overrides from SkEventSink
78 virtual bool onQuery(SkEvent* evt) {
79 if (SampleCode::TitleQ(*evt)) {
80 SampleCode::TitleR(evt, "Shapes");
81 return true;
82 }
83 return this->INHERITED::onQuery(evt);
84 }
85
86 void drawBG(SkCanvas* canvas) {
87 canvas->drawColor(0xFFDDDDDD);
88 }
89
reed@android.com0ad336f2009-06-29 16:02:20 +000090 void drawpicture(SkCanvas* canvas, SkPicture& pict) {
reed@android.comf2b98d62010-12-20 18:26:13 +000091#if 0
reed@android.com0ad336f2009-06-29 16:02:20 +000092 SkDynamicMemoryWStream ostream;
93 pict.serialize(&ostream);
94
95 SkMemoryStream istream(ostream.getStream(), ostream.getOffset());
reed@android.com8433b5d2009-07-03 02:52:27 +000096 SkPicture* newPict = new SkPicture(&istream);
97 canvas->drawPicture(*newPict);
98 newPict->unref();
reed@android.com0ad336f2009-06-29 16:02:20 +000099#else
100 canvas->drawPicture(pict);
101#endif
102 }
103
reed@android.comf76bacf2009-05-13 14:00:33 +0000104 virtual void onDraw(SkCanvas* canvas) {
105 this->drawBG(canvas);
106
reed@android.comf2b98d62010-12-20 18:26:13 +0000107 SkScalar angle = SampleCode::GetAnimScalar(SkIntToScalar(180),
reed@android.com44177402009-11-23 21:07:51 +0000108 SkIntToScalar(360));
109
reed@android.com88c2d8e2009-05-27 01:44:24 +0000110 SkMatrix saveM = *fMatrixRefs[3];
reed@android.com88c2d8e2009-05-27 01:44:24 +0000111 SkScalar c = SkIntToScalar(50);
reed@android.com44177402009-11-23 21:07:51 +0000112 fMatrixRefs[3]->preRotate(angle, c, c);
reed@android.com88c2d8e2009-05-27 01:44:24 +0000113
reed@android.comf2b98d62010-12-20 18:26:13 +0000114 const SkScalar dx = 350;
115 const SkScalar dy = 500;
116 const int N = 1;
117 for (int v = -N; v <= N; v++) {
118 for (int h = -N; h <= N; h++) {
119 SkAutoCanvasRestore acr(canvas, true);
120 canvas->translate(h * dx, v * dy);
121
reed@android.comf76bacf2009-05-13 14:00:33 +0000122 SkMatrix matrix;
reed@android.com09b84a02009-06-26 20:22:26 +0000123
124 SkGroupShape* gs = new SkGroupShape;
125 SkAutoUnref aur(gs);
126 gs->appendShape(&fGroup);
reed@android.com88c2d8e2009-05-27 01:44:24 +0000127 matrix.setScale(-SK_Scalar1, SK_Scalar1);
128 matrix.postTranslate(SkIntToScalar(220), SkIntToScalar(240));
reed@android.com09b84a02009-06-26 20:22:26 +0000129 gs->appendShape(&fGroup, matrix);
reed@android.comf76bacf2009-05-13 14:00:33 +0000130 matrix.setTranslate(SkIntToScalar(240), 0);
131 matrix.preScale(SK_Scalar1*2, SK_Scalar1*2);
reed@android.com09b84a02009-06-26 20:22:26 +0000132 gs->appendShape(&fGroup, matrix);
133
reed@android.com0ad336f2009-06-29 16:02:20 +0000134#if 0
reed@android.com09b84a02009-06-26 20:22:26 +0000135 canvas->drawShape(gs);
reed@android.comf48f2812009-05-18 04:20:55 +0000136#else
reed@android.comf2b98d62010-12-20 18:26:13 +0000137 SkPicture* pict = new SkPicture;
138 SkCanvas* cv = pict->beginRecording(1000, 1000);
reed@android.com09b84a02009-06-26 20:22:26 +0000139 cv->scale(SK_ScalarHalf, SK_ScalarHalf);
140 cv->drawShape(gs);
141 cv->translate(SkIntToScalar(680), SkIntToScalar(480));
142 cv->scale(-SK_Scalar1, SK_Scalar1);
143 cv->drawShape(gs);
reed@android.comf2b98d62010-12-20 18:26:13 +0000144 pict->endRecording();
reed@android.com0ad336f2009-06-29 16:02:20 +0000145
reed@android.comf2b98d62010-12-20 18:26:13 +0000146 drawpicture(canvas, *pict);
147 pict->unref();
reed@android.comf48f2812009-05-18 04:20:55 +0000148#endif
reed@android.com88c2d8e2009-05-27 01:44:24 +0000149
reed@android.comf2b98d62010-12-20 18:26:13 +0000150 }}
151
reed@android.com88c2d8e2009-05-27 01:44:24 +0000152 *fMatrixRefs[3] = saveM;
153 this->inval(NULL);
154}
reed@android.comf76bacf2009-05-13 14:00:33 +0000155
156private:
157 typedef SkView INHERITED;
158};
159
160///////////////////////////////////////////////////////////////////////////////
161
162static SkView* MyFactory() { return new ShapesView; }
163static SkViewRegister reg(MyFactory);
164