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.
|
|
|
|
*/
|
2015-02-02 20:55:02 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColorPriv.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkPathMeasure.h"
|
|
|
|
#include "include/core/SkRegion.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/effects/Sk1DPathEffect.h"
|
|
|
|
#include "include/effects/SkCornerPathEffect.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "src/utils/SkUTF.h"
|
2019-07-11 20:32:53 +00:00
|
|
|
#include "tools/timer/TimeUtils.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
#define CORNER_RADIUS 12
|
|
|
|
|
|
|
|
static const int gXY[] = {
|
|
|
|
4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
|
|
|
|
};
|
|
|
|
|
2016-03-18 18:22:57 +00:00
|
|
|
static sk_sp<SkPathEffect> make_pe(int flags, SkScalar phase) {
|
|
|
|
if (flags == 1) {
|
|
|
|
return SkCornerPathEffect::Make(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);
|
|
|
|
|
2016-03-18 18:22:57 +00:00
|
|
|
auto outer = SkPath1DPathEffect::Make(path, 12, phase, SkPath1DPathEffect::kRotate_Style);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
if (flags == 2)
|
|
|
|
return outer;
|
|
|
|
|
2016-03-18 18:22:57 +00:00
|
|
|
auto inner = SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS));
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2017-02-26 03:34:32 +00:00
|
|
|
return SkPathEffect::MakeCompose(outer, inner);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2016-03-18 18:22:57 +00:00
|
|
|
static sk_sp<SkPathEffect> make_warp_pe(SkScalar phase) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPath path;
|
|
|
|
path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
|
2016-03-18 18:22:57 +00:00
|
|
|
for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) {
|
2008-12-17 15:59:43 +00:00
|
|
|
path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
|
2016-03-18 18:22:57 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
path.close();
|
|
|
|
path.offset(SkIntToScalar(-6), 0);
|
|
|
|
|
2016-03-18 18:22:57 +00:00
|
|
|
auto outer = SkPath1DPathEffect::Make(
|
2015-02-02 03:01:04 +00:00
|
|
|
path, 12, phase, SkPath1DPathEffect::kMorph_Style);
|
2016-03-18 18:22:57 +00:00
|
|
|
auto inner = SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS));
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2017-02-26 03:34:32 +00:00
|
|
|
return SkPathEffect::MakeCompose(outer, inner);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkColorFilter.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class PathEffectView : public Sample {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPath fPath;
|
|
|
|
SkPoint fClickPt;
|
2015-02-02 03:01:04 +00:00
|
|
|
SkScalar fPhase;
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2015-02-02 03:01:04 +00:00
|
|
|
PathEffectView() : fPhase(0) {
|
2015-02-25 17:04:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
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
|
|
|
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString("PathEffects"); }
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-02-02 03:01:04 +00:00
|
|
|
canvas->translate(0, 50);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setColor(SK_ColorBLUE);
|
2016-03-18 18:22:57 +00:00
|
|
|
paint.setPathEffect(make_pe(2, fPhase));
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->drawPath(fPath, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-02-02 03:01:04 +00:00
|
|
|
canvas->translate(0, 50);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setARGB(0xFF, 0, 0xBB, 0);
|
2016-03-18 18:22:57 +00:00
|
|
|
paint.setPathEffect(make_pe(3, fPhase));
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->drawPath(fPath, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-02-02 03:01:04 +00:00
|
|
|
canvas->translate(0, 50);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
paint.setARGB(0xFF, 0, 0, 0);
|
2016-03-18 18:22:57 +00:00
|
|
|
paint.setPathEffect(make_warp_pe(fPhase));
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->drawPath(fPath, paint);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
bool onAnimate(double nanos) override {
|
|
|
|
fPhase = TimeUtils::Scaled(1e-9 * nanos, 40);
|
2015-02-02 03:01:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new PathEffectView(); )
|