blob: e59b64d45b12f5799815e6735749e9e90caf8d1f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed76113a92015-02-02 12:55:02 -08007
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04008#include "Sample.h"
reed76113a92015-02-02 12:55:02 -08009#include "SkAnimTimer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkCanvas.h"
11#include "SkGradientShader.h"
12#include "SkPath.h"
13#include "SkRegion.h"
14#include "SkShader.h"
Hal Canaryea60b952018-08-21 11:45:46 -040015#include "SkUTF.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include "Sk1DPathEffect.h"
17#include "SkCornerPathEffect.h"
18#include "SkPathMeasure.h"
19#include "SkRandom.h"
20#include "SkColorPriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#define CORNER_RADIUS 12
reed@android.com8a1c16f2008-12-17 15:59:43 +000023
24static const int gXY[] = {
25 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
26};
27
reeda4393342016-03-18 11:22:57 -070028static sk_sp<SkPathEffect> make_pe(int flags, SkScalar phase) {
29 if (flags == 1) {
30 return SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS));
31 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000032
33 SkPath path;
34 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
35 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
36 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
37 path.close();
38 path.offset(SkIntToScalar(-6), 0);
39
reeda4393342016-03-18 11:22:57 -070040 auto outer = SkPath1DPathEffect::Make(path, 12, phase, SkPath1DPathEffect::kRotate_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +000041
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 if (flags == 2)
43 return outer;
44
reeda4393342016-03-18 11:22:57 -070045 auto inner = SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS));
reed@android.com8a1c16f2008-12-17 15:59:43 +000046
Mike Reeda07741a2017-02-25 22:34:32 -050047 return SkPathEffect::MakeCompose(outer, inner);
reed@android.com8a1c16f2008-12-17 15:59:43 +000048}
49
reeda4393342016-03-18 11:22:57 -070050static sk_sp<SkPathEffect> make_warp_pe(SkScalar phase) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 SkPath path;
52 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
reeda4393342016-03-18 11:22:57 -070053 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
reeda4393342016-03-18 11:22:57 -070055 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 path.close();
57 path.offset(SkIntToScalar(-6), 0);
58
reeda4393342016-03-18 11:22:57 -070059 auto outer = SkPath1DPathEffect::Make(
reedd9adfe62015-02-01 19:01:04 -080060 path, 12, phase, SkPath1DPathEffect::kMorph_Style);
reeda4393342016-03-18 11:22:57 -070061 auto inner = SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS));
reed@android.com8a1c16f2008-12-17 15:59:43 +000062
Mike Reeda07741a2017-02-25 22:34:32 -050063 return SkPathEffect::MakeCompose(outer, inner);
reed@android.com8a1c16f2008-12-17 15:59:43 +000064}
65
66///////////////////////////////////////////////////////////
67
68#include "SkColorFilter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000069
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040070class PathEffectView : public Sample {
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 SkPath fPath;
72 SkPoint fClickPt;
reedd9adfe62015-02-01 19:01:04 -080073 SkScalar fPhase;
74
reed@android.com8a1c16f2008-12-17 15:59:43 +000075public:
reedd9adfe62015-02-01 19:01:04 -080076 PathEffectView() : fPhase(0) {
caryclark63c684a2015-02-25 09:04:04 -080077 }
78
79protected:
mtklein36352bf2015-03-25 18:17:31 -070080 void onOnceBeforeDraw() override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000081 SkRandom rand;
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 int steps = 20;
reed@android.com92a50ea2009-08-14 19:27:37 +000083 SkScalar dist = SkIntToScalar(400);
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 SkScalar x = SkIntToScalar(20);
85 SkScalar y = SkIntToScalar(50);
rmistry@google.comae933ce2012-08-23 18:19:56 +000086
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 fPath.moveTo(x, y);
reed@android.comf2b98d62010-12-20 18:26:13 +000088 for (int i = 0; i < steps; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 x += dist/steps;
reed@android.com92a50ea2009-08-14 19:27:37 +000090 SkScalar tmpY = y + SkIntToScalar(rand.nextS() % 25);
91 if (i == steps/2) {
92 fPath.moveTo(x, tmpY);
93 } else {
94 fPath.lineTo(x, tmpY);
95 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 }
97
reed@android.com92a50ea2009-08-14 19:27:37 +000098 {
99 SkRect oval;
100 oval.set(SkIntToScalar(20), SkIntToScalar(30),
101 SkIntToScalar(100), SkIntToScalar(60));
102 oval.offset(x, 0);
103 fPath.addRoundRect(oval, SkIntToScalar(8), SkIntToScalar(8));
104 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000105
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 fClickPt.set(SkIntToScalar(200), SkIntToScalar(200));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000107
reed@google.come5ff4392011-06-01 11:59:08 +0000108 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000110
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400111 bool onQuery(Sample::Event* evt) override {
112 if (Sample::TitleQ(*evt)) {
113 Sample::TitleR(evt, "PathEffects");
reed@android.comf2b98d62010-12-20 18:26:13 +0000114 return true;
115 }
116 return this->INHERITED::onQuery(evt);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000118
mtklein36352bf2015-03-25 18:17:31 -0700119 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000121
reedd9adfe62015-02-01 19:01:04 -0800122 canvas->translate(0, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000123
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 paint.setColor(SK_ColorBLUE);
reeda4393342016-03-18 11:22:57 -0700125 paint.setPathEffect(make_pe(2, fPhase));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 canvas->drawPath(fPath, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000127
reedd9adfe62015-02-01 19:01:04 -0800128 canvas->translate(0, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000129
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 paint.setARGB(0xFF, 0, 0xBB, 0);
reeda4393342016-03-18 11:22:57 -0700131 paint.setPathEffect(make_pe(3, fPhase));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 canvas->drawPath(fPath, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000133
reedd9adfe62015-02-01 19:01:04 -0800134 canvas->translate(0, 50);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135
136 paint.setARGB(0xFF, 0, 0, 0);
reeda4393342016-03-18 11:22:57 -0700137 paint.setPathEffect(make_warp_pe(fPhase));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 canvas->drawPath(fPath, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000140
mtklein36352bf2015-03-25 18:17:31 -0700141 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -0800142 fPhase = timer.scaled(40);
reedd9adfe62015-02-01 19:01:04 -0800143 return true;
144 }
145
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400147 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148};
149
150//////////////////////////////////////////////////////////////////////////////
151
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400152DEF_SAMPLE( return new PathEffectView(); )