2011-07-28 14:26:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2009-05-13 14:00:33 +00:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkPaint.h"
|
2009-06-26 20:22:26 +00:00
|
|
|
#include "SkPicture.h"
|
2009-06-29 16:02:20 +00:00
|
|
|
#include "SkStream.h"
|
2009-05-13 14:00:33 +00:00
|
|
|
#include "SkView.h"
|
|
|
|
|
2011-02-24 21:42:29 +00:00
|
|
|
#define DO_AA true
|
2010-12-20 18:26:13 +00:00
|
|
|
|
2009-05-13 14:00:33 +00:00
|
|
|
#include "SkRectShape.h"
|
|
|
|
#include "SkGroupShape.h"
|
|
|
|
|
|
|
|
static SkRect make_rect(int l, int t, int r, int b) {
|
|
|
|
SkRect rect;
|
|
|
|
rect.set(SkIntToScalar(l), SkIntToScalar(t),
|
|
|
|
SkIntToScalar(r), SkIntToScalar(b));
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2009-05-18 04:20:55 +00:00
|
|
|
static SkShape* make_shape0(bool red) {
|
2009-05-13 14:00:33 +00:00
|
|
|
SkRectShape* s = new SkRectShape;
|
|
|
|
s->setRect(make_rect(10, 10, 90, 90));
|
2009-05-18 04:20:55 +00:00
|
|
|
if (red) {
|
2009-05-13 14:00:33 +00:00
|
|
|
s->paint().setColor(SK_ColorRED);
|
|
|
|
}
|
2010-12-20 18:26:13 +00:00
|
|
|
s->paint().setAntiAlias(DO_AA);
|
2009-05-13 14:00:33 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2009-05-18 04:20:55 +00:00
|
|
|
static SkShape* make_shape1() {
|
2009-05-13 14:00:33 +00:00
|
|
|
SkRectShape* s = new SkRectShape;
|
|
|
|
s->setOval(make_rect(10, 10, 90, 90));
|
|
|
|
s->paint().setColor(SK_ColorBLUE);
|
2010-12-20 18:26:13 +00:00
|
|
|
s->paint().setAntiAlias(DO_AA);
|
2009-05-13 14:00:33 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2009-05-18 04:20:55 +00:00
|
|
|
static SkShape* make_shape2() {
|
2009-05-13 14:00:33 +00:00
|
|
|
SkRectShape* s = new SkRectShape;
|
|
|
|
s->setRRect(make_rect(10, 10, 90, 90),
|
|
|
|
SkIntToScalar(20), SkIntToScalar(20));
|
|
|
|
s->paint().setColor(SK_ColorGREEN);
|
2010-12-20 18:26:13 +00:00
|
|
|
s->paint().setAntiAlias(DO_AA);
|
2009-05-13 14:00:33 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
class ShapesView : public SampleView {
|
2009-05-13 14:00:33 +00:00
|
|
|
SkGroupShape fGroup;
|
2009-05-27 01:44:24 +00:00
|
|
|
SkMatrixRef* fMatrixRefs[4];
|
2009-05-13 14:00:33 +00:00
|
|
|
public:
|
|
|
|
ShapesView() {
|
|
|
|
SkMatrix m;
|
2009-05-18 04:20:55 +00:00
|
|
|
fGroup.appendShape(make_shape0(false))->unref();
|
2009-05-13 14:00:33 +00:00
|
|
|
m.setRotate(SkIntToScalar(30), SkIntToScalar(50), SkIntToScalar(50));
|
|
|
|
m.postTranslate(0, SkIntToScalar(120));
|
2009-05-18 04:20:55 +00:00
|
|
|
fGroup.appendShape(make_shape0(true), m)->unref();
|
2009-05-13 14:00:33 +00:00
|
|
|
|
|
|
|
m.setTranslate(SkIntToScalar(120), 0);
|
2009-05-18 04:20:55 +00:00
|
|
|
fGroup.appendShape(make_shape1(), m)->unref();
|
2009-05-13 14:00:33 +00:00
|
|
|
m.postTranslate(0, SkIntToScalar(120));
|
2009-05-18 04:20:55 +00:00
|
|
|
fGroup.appendShape(make_shape2(), m)->unref();
|
2009-05-27 01:44:24 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) {
|
|
|
|
SkSafeRef(fMatrixRefs[i] = fGroup.getShapeMatrixRef(i));
|
|
|
|
}
|
2011-04-22 14:10:48 +00:00
|
|
|
|
|
|
|
this->setBGColor(0xFFDDDDDD);
|
2009-05-27 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~ShapesView() {
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) {
|
|
|
|
SkSafeUnref(fMatrixRefs[i]);
|
|
|
|
}
|
2009-05-13 14:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "Shapes");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
2009-06-29 16:02:20 +00:00
|
|
|
void drawpicture(SkCanvas* canvas, SkPicture& pict) {
|
2010-12-20 18:26:13 +00:00
|
|
|
#if 0
|
2009-06-29 16:02:20 +00:00
|
|
|
SkDynamicMemoryWStream ostream;
|
|
|
|
pict.serialize(&ostream);
|
|
|
|
|
|
|
|
SkMemoryStream istream(ostream.getStream(), ostream.getOffset());
|
2009-07-03 02:52:27 +00:00
|
|
|
SkPicture* newPict = new SkPicture(&istream);
|
|
|
|
canvas->drawPicture(*newPict);
|
|
|
|
newPict->unref();
|
2009-06-29 16:02:20 +00:00
|
|
|
#else
|
|
|
|
canvas->drawPicture(pict);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2010-12-20 18:26:13 +00:00
|
|
|
SkScalar angle = SampleCode::GetAnimScalar(SkIntToScalar(180),
|
2009-11-23 21:07:51 +00:00
|
|
|
SkIntToScalar(360));
|
|
|
|
|
2009-05-27 01:44:24 +00:00
|
|
|
SkMatrix saveM = *fMatrixRefs[3];
|
|
|
|
SkScalar c = SkIntToScalar(50);
|
2009-11-23 21:07:51 +00:00
|
|
|
fMatrixRefs[3]->preRotate(angle, c, c);
|
2009-05-27 01:44:24 +00:00
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
const SkScalar dx = 350;
|
|
|
|
const SkScalar dy = 500;
|
|
|
|
const int N = 1;
|
|
|
|
for (int v = -N; v <= N; v++) {
|
|
|
|
for (int h = -N; h <= N; h++) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->translate(h * dx, v * dy);
|
|
|
|
|
2009-05-13 14:00:33 +00:00
|
|
|
SkMatrix matrix;
|
2009-06-26 20:22:26 +00:00
|
|
|
|
|
|
|
SkGroupShape* gs = new SkGroupShape;
|
|
|
|
SkAutoUnref aur(gs);
|
|
|
|
gs->appendShape(&fGroup);
|
2009-05-27 01:44:24 +00:00
|
|
|
matrix.setScale(-SK_Scalar1, SK_Scalar1);
|
|
|
|
matrix.postTranslate(SkIntToScalar(220), SkIntToScalar(240));
|
2009-06-26 20:22:26 +00:00
|
|
|
gs->appendShape(&fGroup, matrix);
|
2009-05-13 14:00:33 +00:00
|
|
|
matrix.setTranslate(SkIntToScalar(240), 0);
|
|
|
|
matrix.preScale(SK_Scalar1*2, SK_Scalar1*2);
|
2009-06-26 20:22:26 +00:00
|
|
|
gs->appendShape(&fGroup, matrix);
|
|
|
|
|
2011-06-27 14:42:15 +00:00
|
|
|
#if 1
|
2010-12-20 18:26:13 +00:00
|
|
|
SkPicture* pict = new SkPicture;
|
|
|
|
SkCanvas* cv = pict->beginRecording(1000, 1000);
|
2009-06-26 20:22:26 +00:00
|
|
|
cv->scale(SK_ScalarHalf, SK_ScalarHalf);
|
2011-06-27 14:42:15 +00:00
|
|
|
gs->draw(cv);
|
2009-06-26 20:22:26 +00:00
|
|
|
cv->translate(SkIntToScalar(680), SkIntToScalar(480));
|
|
|
|
cv->scale(-SK_Scalar1, SK_Scalar1);
|
2011-06-27 14:42:15 +00:00
|
|
|
gs->draw(cv);
|
2010-12-20 18:26:13 +00:00
|
|
|
pict->endRecording();
|
2009-06-29 16:02:20 +00:00
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
drawpicture(canvas, *pict);
|
|
|
|
pict->unref();
|
2009-05-18 04:20:55 +00:00
|
|
|
#endif
|
2009-05-27 01:44:24 +00:00
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
}}
|
|
|
|
|
2009-05-27 01:44:24 +00:00
|
|
|
*fMatrixRefs[3] = saveM;
|
|
|
|
this->inval(NULL);
|
|
|
|
}
|
2009-05-13 14:00:33 +00:00
|
|
|
|
|
|
|
private:
|
2011-04-22 14:10:48 +00:00
|
|
|
typedef SampleView INHERITED;
|
2009-05-13 14:00:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new ShapesView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|
|
|
|
|