blob: ccdc5412f51f39b9a5a42df0015c0b08b7e73ba3 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkColorPriv.h"
10#include "include/core/SkPath.h"
11#include "include/core/SkPathMeasure.h"
12#include "include/core/SkRegion.h"
13#include "include/core/SkShader.h"
14#include "include/effects/Sk1DPathEffect.h"
15#include "include/effects/SkCornerPathEffect.h"
16#include "include/effects/SkGradientShader.h"
17#include "include/utils/SkRandom.h"
18#include "samplecode/Sample.h"
19#include "src/utils/SkUTF.h"
Hal Canary41248072019-07-11 16:32:53 -040020#include "tools/timer/TimeUtils.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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050068#include "include/core/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;
Mike Reed92b33352019-08-24 19:39:13 -0400100 oval.setLTRB(20, 30, 100, 60);
reed@android.com92a50ea2009-08-14 19:27:37 +0000101 oval.offset(x, 0);
Mike Reed4241f5e2019-09-14 19:13:23 +0000102 fPath.addRoundRect(oval, SkIntToScalar(8), SkIntToScalar(8));
reed@android.com92a50ea2009-08-14 19:27:37 +0000103 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000104
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 fClickPt.set(SkIntToScalar(200), SkIntToScalar(200));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000106
reed@google.come5ff4392011-06-01 11:59:08 +0000107 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000109
Hal Canary8a027312019-07-03 10:55:44 -0400110 SkString name() override { return SkString("PathEffects"); }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000111
mtklein36352bf2015-03-25 18:17:31 -0700112 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000114
reedd9adfe62015-02-01 19:01:04 -0800115 canvas->translate(0, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000116
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 paint.setColor(SK_ColorBLUE);
reeda4393342016-03-18 11:22:57 -0700118 paint.setPathEffect(make_pe(2, fPhase));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 canvas->drawPath(fPath, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000120
reedd9adfe62015-02-01 19:01:04 -0800121 canvas->translate(0, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000122
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 paint.setARGB(0xFF, 0, 0xBB, 0);
reeda4393342016-03-18 11:22:57 -0700124 paint.setPathEffect(make_pe(3, fPhase));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 canvas->drawPath(fPath, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000126
reedd9adfe62015-02-01 19:01:04 -0800127 canvas->translate(0, 50);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128
129 paint.setARGB(0xFF, 0, 0, 0);
reeda4393342016-03-18 11:22:57 -0700130 paint.setPathEffect(make_warp_pe(fPhase));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 canvas->drawPath(fPath, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000133
Hal Canary41248072019-07-11 16:32:53 -0400134 bool onAnimate(double nanos) override {
135 fPhase = TimeUtils::Scaled(1e-9 * nanos, 40);
reedd9adfe62015-02-01 19:01:04 -0800136 return true;
137 }
138
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400140 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141};
142
143//////////////////////////////////////////////////////////////////////////////
144
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400145DEF_SAMPLE( return new PathEffectView(); )