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.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "Sk1DPathEffect.h"
|
|
|
|
#include "SkCornerPathEffect.h"
|
|
|
|
#include "SkPathMeasure.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkPixelXorXfermode.h"
|
|
|
|
|
|
|
|
#define CORNER_RADIUS 12
|
|
|
|
static SkScalar gPhase;
|
|
|
|
|
|
|
|
static const int gXY[] = {
|
|
|
|
4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
|
|
|
|
};
|
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
static SkPathEffect* make_pe(int flags) {
|
2008-12-17 15:59:43 +00:00
|
|
|
if (flags == 1)
|
|
|
|
return new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS));
|
|
|
|
|
|
|
|
SkPath path;
|
|
|
|
path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
|
|
|
|
for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
|
|
|
|
path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
|
|
|
|
path.close();
|
|
|
|
path.offset(SkIntToScalar(-6), 0);
|
|
|
|
|
|
|
|
SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12), gPhase, SkPath1DPathEffect::kRotate_Style);
|
|
|
|
|
|
|
|
if (flags == 2)
|
|
|
|
return outer;
|
|
|
|
|
|
|
|
SkPathEffect* inner = new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS));
|
|
|
|
|
|
|
|
SkPathEffect* pe = new SkComposePathEffect(outer, inner);
|
|
|
|
outer->unref();
|
|
|
|
inner->unref();
|
|
|
|
return pe;
|
|
|
|
}
|
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
static SkPathEffect* make_warp_pe() {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPath path;
|
|
|
|
path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
|
|
|
|
for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
|
|
|
|
path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
|
|
|
|
path.close();
|
|
|
|
path.offset(SkIntToScalar(-6), 0);
|
|
|
|
|
|
|
|
SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12), gPhase, SkPath1DPathEffect::kMorph_Style);
|
|
|
|
SkPathEffect* inner = new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS));
|
|
|
|
|
|
|
|
SkPathEffect* pe = new SkComposePathEffect(outer, inner);
|
|
|
|
outer->unref();
|
|
|
|
inner->unref();
|
|
|
|
return pe;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkLayerRasterizer.h"
|
|
|
|
|
|
|
|
class testrast : public SkLayerRasterizer {
|
|
|
|
public:
|
2010-12-20 18:26:13 +00:00
|
|
|
testrast() {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(SK_Scalar1*4);
|
|
|
|
this->addLayer(paint);
|
|
|
|
|
|
|
|
paint.setStrokeWidth(SK_Scalar1*1);
|
2009-06-23 12:19:41 +00:00
|
|
|
paint.setXfermode(SkXfermode::kClear_Mode);
|
2008-12-17 15:59:43 +00:00
|
|
|
this->addLayer(paint);
|
|
|
|
#else
|
|
|
|
paint.setAlpha(0x66);
|
|
|
|
this->addLayer(paint, SkIntToScalar(4), SkIntToScalar(4));
|
|
|
|
|
|
|
|
paint.setAlpha(0xFF);
|
|
|
|
this->addLayer(paint);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-06-01 11:59:08 +00:00
|
|
|
class PathEffectView : public SampleView {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPath fPath;
|
|
|
|
SkPoint fClickPt;
|
|
|
|
public:
|
2010-12-20 18:26:13 +00:00
|
|
|
PathEffectView() {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkRandom rand;
|
|
|
|
int steps = 20;
|
2009-08-14 19:27:37 +00:00
|
|
|
SkScalar dist = SkIntToScalar(400);
|
2008-12-17 15:59:43 +00:00
|
|
|
SkScalar x = SkIntToScalar(20);
|
|
|
|
SkScalar y = SkIntToScalar(50);
|
|
|
|
|
|
|
|
fPath.moveTo(x, y);
|
2010-12-20 18:26:13 +00:00
|
|
|
for (int i = 0; i < steps; i++) {
|
2008-12-17 15:59:43 +00:00
|
|
|
x += dist/steps;
|
2009-08-14 19:27:37 +00:00
|
|
|
SkScalar tmpY = y + SkIntToScalar(rand.nextS() % 25);
|
|
|
|
if (i == steps/2) {
|
|
|
|
fPath.moveTo(x, tmpY);
|
|
|
|
} else {
|
|
|
|
fPath.lineTo(x, tmpY);
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2009-08-14 19:27:37 +00:00
|
|
|
{
|
|
|
|
SkRect oval;
|
|
|
|
oval.set(SkIntToScalar(20), SkIntToScalar(30),
|
|
|
|
SkIntToScalar(100), SkIntToScalar(60));
|
|
|
|
oval.offset(x, 0);
|
|
|
|
fPath.addRoundRect(oval, SkIntToScalar(8), SkIntToScalar(8));
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
fClickPt.set(SkIntToScalar(200), SkIntToScalar(200));
|
2011-06-01 11:59:08 +00:00
|
|
|
|
|
|
|
this->setBGColor(0xFFDDDDDD);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
2010-12-20 18:26:13 +00:00
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "PathEffects");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2011-06-01 11:59:08 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2010-12-20 18:26:13 +00:00
|
|
|
gPhase -= SampleCode::GetAnimSecondsDelta() * 40;
|
2009-05-22 20:44:12 +00:00
|
|
|
this->inval(NULL);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
#if 0
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(SkIntToScalar(5));
|
|
|
|
canvas->drawPath(fPath, paint);
|
|
|
|
paint.setStrokeWidth(0);
|
|
|
|
|
2009-08-14 19:27:37 +00:00
|
|
|
paint.setColor(SK_ColorWHITE);
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setPathEffect(make_pe(1))->unref();
|
|
|
|
canvas->drawPath(fPath, paint);
|
2010-12-20 18:26:13 +00:00
|
|
|
#endif
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
canvas->translate(0, SkIntToScalar(50));
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
paint.setPathEffect(make_pe(2))->unref();
|
|
|
|
canvas->drawPath(fPath, paint);
|
|
|
|
|
|
|
|
canvas->translate(0, SkIntToScalar(50));
|
|
|
|
|
|
|
|
paint.setARGB(0xFF, 0, 0xBB, 0);
|
|
|
|
paint.setPathEffect(make_pe(3))->unref();
|
|
|
|
canvas->drawPath(fPath, paint);
|
|
|
|
|
|
|
|
canvas->translate(0, SkIntToScalar(50));
|
|
|
|
|
|
|
|
paint.setARGB(0xFF, 0, 0, 0);
|
|
|
|
paint.setPathEffect(make_warp_pe())->unref();
|
|
|
|
paint.setRasterizer(new testrast)->unref();
|
|
|
|
canvas->drawPath(fPath, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-06-01 11:59:08 +00:00
|
|
|
typedef SampleView INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new PathEffectView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|
|
|
|
|