blob: 5a5bb4c06ca68e8cc61dc00b9a4a7df7b7b20ab1 [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.comf76bacf2009-05-13 14:00:33 +00008#include "SampleCode.h"
9#include "SkCanvas.h"
10#include "SkPaint.h"
reed@android.com09b84a02009-06-26 20:22:26 +000011#include "SkPicture.h"
reed@android.com0ad336f2009-06-29 16:02:20 +000012#include "SkStream.h"
reed@android.comf76bacf2009-05-13 14:00:33 +000013#include "SkView.h"
14
reed@google.coma09368c2011-02-24 21:42:29 +000015#define DO_AA true
reed@android.comf2b98d62010-12-20 18:26:13 +000016
reed@android.comf76bacf2009-05-13 14:00:33 +000017#include "SkRectShape.h"
18#include "SkGroupShape.h"
19
20static SkRect make_rect(int l, int t, int r, int b) {
21 SkRect rect;
22 rect.set(SkIntToScalar(l), SkIntToScalar(t),
23 SkIntToScalar(r), SkIntToScalar(b));
24 return rect;
25}
26
reed@android.comf48f2812009-05-18 04:20:55 +000027static SkShape* make_shape0(bool red) {
reed@android.comf76bacf2009-05-13 14:00:33 +000028 SkRectShape* s = new SkRectShape;
29 s->setRect(make_rect(10, 10, 90, 90));
reed@android.comf48f2812009-05-18 04:20:55 +000030 if (red) {
reed@android.comf76bacf2009-05-13 14:00:33 +000031 s->paint().setColor(SK_ColorRED);
32 }
reed@android.comf2b98d62010-12-20 18:26:13 +000033 s->paint().setAntiAlias(DO_AA);
reed@android.comf76bacf2009-05-13 14:00:33 +000034 return s;
35}
36
reed@android.comf48f2812009-05-18 04:20:55 +000037static SkShape* make_shape1() {
reed@android.comf76bacf2009-05-13 14:00:33 +000038 SkRectShape* s = new SkRectShape;
39 s->setOval(make_rect(10, 10, 90, 90));
reed@android.comf76bacf2009-05-13 14:00:33 +000040 s->paint().setColor(SK_ColorBLUE);
reed@android.comf2b98d62010-12-20 18:26:13 +000041 s->paint().setAntiAlias(DO_AA);
reed@android.comf76bacf2009-05-13 14:00:33 +000042 return s;
43}
44
reed@android.comf48f2812009-05-18 04:20:55 +000045static SkShape* make_shape2() {
reed@android.comf76bacf2009-05-13 14:00:33 +000046 SkRectShape* s = new SkRectShape;
47 s->setRRect(make_rect(10, 10, 90, 90),
48 SkIntToScalar(20), SkIntToScalar(20));
reed@android.comf76bacf2009-05-13 14:00:33 +000049 s->paint().setColor(SK_ColorGREEN);
reed@android.comf2b98d62010-12-20 18:26:13 +000050 s->paint().setAntiAlias(DO_AA);
reed@android.comf76bacf2009-05-13 14:00:33 +000051 return s;
52}
53
54///////////////////////////////////////////////////////////////////////////////
55
reed@google.comf2183392011-04-22 14:10:48 +000056class ShapesView : public SampleView {
reed@android.comf76bacf2009-05-13 14:00:33 +000057 SkGroupShape fGroup;
reed@android.com88c2d8e2009-05-27 01:44:24 +000058 SkMatrixRef* fMatrixRefs[4];
reed@android.comf76bacf2009-05-13 14:00:33 +000059public:
60 ShapesView() {
61 SkMatrix m;
reed@android.comf48f2812009-05-18 04:20:55 +000062 fGroup.appendShape(make_shape0(false))->unref();
reed@android.comf76bacf2009-05-13 14:00:33 +000063 m.setRotate(SkIntToScalar(30), SkIntToScalar(50), SkIntToScalar(50));
64 m.postTranslate(0, SkIntToScalar(120));
reed@android.comf48f2812009-05-18 04:20:55 +000065 fGroup.appendShape(make_shape0(true), m)->unref();
reed@android.comf76bacf2009-05-13 14:00:33 +000066
67 m.setTranslate(SkIntToScalar(120), 0);
reed@android.comf48f2812009-05-18 04:20:55 +000068 fGroup.appendShape(make_shape1(), m)->unref();
reed@android.comf76bacf2009-05-13 14:00:33 +000069 m.postTranslate(0, SkIntToScalar(120));
reed@android.comf48f2812009-05-18 04:20:55 +000070 fGroup.appendShape(make_shape2(), m)->unref();
reed@android.com88c2d8e2009-05-27 01:44:24 +000071
72 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) {
73 SkSafeRef(fMatrixRefs[i] = fGroup.getShapeMatrixRef(i));
74 }
reed@google.comf2183392011-04-22 14:10:48 +000075
76 this->setBGColor(0xFFDDDDDD);
reed@android.com88c2d8e2009-05-27 01:44:24 +000077 }
78
79 virtual ~ShapesView() {
80 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) {
81 SkSafeUnref(fMatrixRefs[i]);
82 }
reed@android.comf76bacf2009-05-13 14:00:33 +000083 }
84
85protected:
86 // overrides from SkEventSink
87 virtual bool onQuery(SkEvent* evt) {
88 if (SampleCode::TitleQ(*evt)) {
89 SampleCode::TitleR(evt, "Shapes");
90 return true;
91 }
92 return this->INHERITED::onQuery(evt);
93 }
94
reed@android.com0ad336f2009-06-29 16:02:20 +000095 void drawpicture(SkCanvas* canvas, SkPicture& pict) {
reed@android.comf2b98d62010-12-20 18:26:13 +000096#if 0
reed@android.com0ad336f2009-06-29 16:02:20 +000097 SkDynamicMemoryWStream ostream;
98 pict.serialize(&ostream);
99
100 SkMemoryStream istream(ostream.getStream(), ostream.getOffset());
reed@android.com8433b5d2009-07-03 02:52:27 +0000101 SkPicture* newPict = new SkPicture(&istream);
102 canvas->drawPicture(*newPict);
103 newPict->unref();
reed@android.com0ad336f2009-06-29 16:02:20 +0000104#else
105 canvas->drawPicture(pict);
106#endif
107 }
108
reed@google.comf2183392011-04-22 14:10:48 +0000109 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000110 SkScalar angle = SampleCode::GetAnimScalar(SkIntToScalar(180),
reed@android.com44177402009-11-23 21:07:51 +0000111 SkIntToScalar(360));
112
reed@android.com88c2d8e2009-05-27 01:44:24 +0000113 SkMatrix saveM = *fMatrixRefs[3];
reed@android.com88c2d8e2009-05-27 01:44:24 +0000114 SkScalar c = SkIntToScalar(50);
reed@android.com44177402009-11-23 21:07:51 +0000115 fMatrixRefs[3]->preRotate(angle, c, c);
reed@android.com88c2d8e2009-05-27 01:44:24 +0000116
reed@android.comf2b98d62010-12-20 18:26:13 +0000117 const SkScalar dx = 350;
118 const SkScalar dy = 500;
119 const int N = 1;
120 for (int v = -N; v <= N; v++) {
121 for (int h = -N; h <= N; h++) {
122 SkAutoCanvasRestore acr(canvas, true);
123 canvas->translate(h * dx, v * dy);
124
reed@android.comf76bacf2009-05-13 14:00:33 +0000125 SkMatrix matrix;
reed@android.com09b84a02009-06-26 20:22:26 +0000126
127 SkGroupShape* gs = new SkGroupShape;
128 SkAutoUnref aur(gs);
129 gs->appendShape(&fGroup);
reed@android.com88c2d8e2009-05-27 01:44:24 +0000130 matrix.setScale(-SK_Scalar1, SK_Scalar1);
131 matrix.postTranslate(SkIntToScalar(220), SkIntToScalar(240));
reed@android.com09b84a02009-06-26 20:22:26 +0000132 gs->appendShape(&fGroup, matrix);
reed@android.comf76bacf2009-05-13 14:00:33 +0000133 matrix.setTranslate(SkIntToScalar(240), 0);
134 matrix.preScale(SK_Scalar1*2, SK_Scalar1*2);
reed@android.com09b84a02009-06-26 20:22:26 +0000135 gs->appendShape(&fGroup, matrix);
136
reed@google.com5667afc2011-06-27 14:42:15 +0000137#if 1
reed@android.comf2b98d62010-12-20 18:26:13 +0000138 SkPicture* pict = new SkPicture;
139 SkCanvas* cv = pict->beginRecording(1000, 1000);
reed@android.com09b84a02009-06-26 20:22:26 +0000140 cv->scale(SK_ScalarHalf, SK_ScalarHalf);
reed@google.com5667afc2011-06-27 14:42:15 +0000141 gs->draw(cv);
reed@android.com09b84a02009-06-26 20:22:26 +0000142 cv->translate(SkIntToScalar(680), SkIntToScalar(480));
143 cv->scale(-SK_Scalar1, SK_Scalar1);
reed@google.com5667afc2011-06-27 14:42:15 +0000144 gs->draw(cv);
reed@android.comf2b98d62010-12-20 18:26:13 +0000145 pict->endRecording();
reed@android.com0ad336f2009-06-29 16:02:20 +0000146
reed@android.comf2b98d62010-12-20 18:26:13 +0000147 drawpicture(canvas, *pict);
148 pict->unref();
reed@android.comf48f2812009-05-18 04:20:55 +0000149#endif
reed@android.com88c2d8e2009-05-27 01:44:24 +0000150
reed@android.comf2b98d62010-12-20 18:26:13 +0000151 }}
152
reed@android.com88c2d8e2009-05-27 01:44:24 +0000153 *fMatrixRefs[3] = saveM;
154 this->inval(NULL);
155}
reed@android.comf76bacf2009-05-13 14:00:33 +0000156
157private:
reed@google.comf2183392011-04-22 14:10:48 +0000158 typedef SampleView INHERITED;
reed@android.comf76bacf2009-05-13 14:00:33 +0000159};
160
161///////////////////////////////////////////////////////////////////////////////
162
163static SkView* MyFactory() { return new ShapesView; }
164static SkViewRegister reg(MyFactory);
165