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)
|
2014-02-20 20:40:19 +00:00
|
|
|
return SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS));
|
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);
|
|
|
|
|
2014-02-20 20:40:19 +00:00
|
|
|
SkPathEffect* outer = SkPath1DPathEffect::Create(path, SkIntToScalar(12), gPhase, SkPath1DPathEffect::kRotate_Style);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
if (flags == 2)
|
|
|
|
return outer;
|
|
|
|
|
2014-02-20 20:40:19 +00:00
|
|
|
SkPathEffect* inner = SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS));
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2014-02-20 20:40:19 +00:00
|
|
|
SkPathEffect* pe = SkComposePathEffect::Create(outer, inner);
|
2008-12-17 15:59:43 +00:00
|
|
|
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);
|
|
|
|
|
2014-02-20 20:40:19 +00:00
|
|
|
SkPathEffect* outer = SkPath1DPathEffect::Create(
|
|
|
|
path, SkIntToScalar(12), gPhase, SkPath1DPathEffect::kMorph_Style);
|
|
|
|
SkPathEffect* inner = SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS));
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2014-02-20 20:40:19 +00:00
|
|
|
SkPathEffect* pe = SkComposePathEffect::Create(outer, inner);
|
2008-12-17 15:59:43 +00:00
|
|
|
outer->unref();
|
|
|
|
inner->unref();
|
|
|
|
return pe;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkLayerRasterizer.h"
|
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
class TestRastBuilder : public SkLayerRasterizer::Builder {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2014-02-26 13:27:37 +00:00
|
|
|
TestRastBuilder() {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
2012-08-23 18:19:56 +00:00
|
|
|
#if 0
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(SK_Scalar1*4);
|
|
|
|
this->addLayer(paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
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));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
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:
|
2012-08-23 18:19:56 +00:00
|
|
|
PathEffectView() {
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2008-12-17 15:59:43 +00:00
|
|
|
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);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
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));
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
fClickPt.set(SkIntToScalar(200), SkIntToScalar(200));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-06-01 11:59:08 +00:00
|
|
|
this->setBGColor(0xFFDDDDDD);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
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
|
|
|
}
|
2012-08-23 18:19:56 +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);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
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);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
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
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->translate(0, SkIntToScalar(50));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
paint.setPathEffect(make_pe(2))->unref();
|
|
|
|
canvas->drawPath(fPath, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->translate(0, SkIntToScalar(50));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setARGB(0xFF, 0, 0xBB, 0);
|
|
|
|
paint.setPathEffect(make_pe(3))->unref();
|
|
|
|
canvas->drawPath(fPath, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->translate(0, SkIntToScalar(50));
|
|
|
|
|
|
|
|
paint.setARGB(0xFF, 0, 0, 0);
|
|
|
|
paint.setPathEffect(make_warp_pe())->unref();
|
2014-02-26 13:27:37 +00:00
|
|
|
TestRastBuilder testRastBuilder;
|
|
|
|
paint.setRasterizer(testRastBuilder.detachRasterizer())->unref();
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->drawPath(fPath, paint);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
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);
|