blob: 5d80e5a08d2a5a74d48b0ff2fdf77ebb2b5a9b62 [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
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
reed76113a92015-02-02 12:55:02 -08009#include "SkAnimTimer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkView.h"
11#include "SkCanvas.h"
12#include "SkGradientShader.h"
13#include "SkPath.h"
14#include "SkRegion.h"
15#include "SkShader.h"
16#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "Sk1DPathEffect.h"
18#include "SkCornerPathEffect.h"
19#include "SkPathMeasure.h"
20#include "SkRandom.h"
21#include "SkColorPriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022
reed@android.com8a1c16f2008-12-17 15:59:43 +000023#define CORNER_RADIUS 12
reed@android.com8a1c16f2008-12-17 15:59:43 +000024
25static const int gXY[] = {
26 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
27};
28
reeda4393342016-03-18 11:22:57 -070029static sk_sp<SkPathEffect> make_pe(int flags, SkScalar phase) {
30 if (flags == 1) {
31 return SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS));
32 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000033
34 SkPath path;
35 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
36 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
37 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
38 path.close();
39 path.offset(SkIntToScalar(-6), 0);
40
reeda4393342016-03-18 11:22:57 -070041 auto outer = SkPath1DPathEffect::Make(path, 12, phase, SkPath1DPathEffect::kRotate_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +000042
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 if (flags == 2)
44 return outer;
45
reeda4393342016-03-18 11:22:57 -070046 auto inner = SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS));
reed@android.com8a1c16f2008-12-17 15:59:43 +000047
Mike Reeda07741a2017-02-25 22:34:32 -050048 return SkPathEffect::MakeCompose(outer, inner);
reed@android.com8a1c16f2008-12-17 15:59:43 +000049}
50
reeda4393342016-03-18 11:22:57 -070051static sk_sp<SkPathEffect> make_warp_pe(SkScalar phase) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 SkPath path;
53 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
reeda4393342016-03-18 11:22:57 -070054 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
reeda4393342016-03-18 11:22:57 -070056 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 path.close();
58 path.offset(SkIntToScalar(-6), 0);
59
reeda4393342016-03-18 11:22:57 -070060 auto outer = SkPath1DPathEffect::Make(
reedd9adfe62015-02-01 19:01:04 -080061 path, 12, phase, SkPath1DPathEffect::kMorph_Style);
reeda4393342016-03-18 11:22:57 -070062 auto inner = SkCornerPathEffect::Make(SkIntToScalar(CORNER_RADIUS));
reed@android.com8a1c16f2008-12-17 15:59:43 +000063
Mike Reeda07741a2017-02-25 22:34:32 -050064 return SkPathEffect::MakeCompose(outer, inner);
reed@android.com8a1c16f2008-12-17 15:59:43 +000065}
66
67///////////////////////////////////////////////////////////
68
69#include "SkColorFilter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000070
reed@google.come5ff4392011-06-01 11:59:08 +000071class PathEffectView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 SkPath fPath;
73 SkPoint fClickPt;
reedd9adfe62015-02-01 19:01:04 -080074 SkScalar fPhase;
75
reed@android.com8a1c16f2008-12-17 15:59:43 +000076public:
reedd9adfe62015-02-01 19:01:04 -080077 PathEffectView() : fPhase(0) {
caryclark63c684a2015-02-25 09:04:04 -080078 }
79
80protected:
mtklein36352bf2015-03-25 18:17:31 -070081 void onOnceBeforeDraw() override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000082 SkRandom rand;
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 int steps = 20;
reed@android.com92a50ea2009-08-14 19:27:37 +000084 SkScalar dist = SkIntToScalar(400);
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 SkScalar x = SkIntToScalar(20);
86 SkScalar y = SkIntToScalar(50);
rmistry@google.comae933ce2012-08-23 18:19:56 +000087
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 fPath.moveTo(x, y);
reed@android.comf2b98d62010-12-20 18:26:13 +000089 for (int i = 0; i < steps; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 x += dist/steps;
reed@android.com92a50ea2009-08-14 19:27:37 +000091 SkScalar tmpY = y + SkIntToScalar(rand.nextS() % 25);
92 if (i == steps/2) {
93 fPath.moveTo(x, tmpY);
94 } else {
95 fPath.lineTo(x, tmpY);
96 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 }
98
reed@android.com92a50ea2009-08-14 19:27:37 +000099 {
100 SkRect oval;
101 oval.set(SkIntToScalar(20), SkIntToScalar(30),
102 SkIntToScalar(100), SkIntToScalar(60));
103 oval.offset(x, 0);
104 fPath.addRoundRect(oval, SkIntToScalar(8), SkIntToScalar(8));
105 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000106
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 fClickPt.set(SkIntToScalar(200), SkIntToScalar(200));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000108
reed@google.come5ff4392011-06-01 11:59:08 +0000109 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000111
mtklein36352bf2015-03-25 18:17:31 -0700112 bool onQuery(SkEvent* evt) override {
reed@android.comf2b98d62010-12-20 18:26:13 +0000113 if (SampleCode::TitleQ(*evt)) {
114 SampleCode::TitleR(evt, "PathEffects");
115 return true;
116 }
117 return this->INHERITED::onQuery(evt);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000119
mtklein36352bf2015-03-25 18:17:31 -0700120 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000122
reedd9adfe62015-02-01 19:01:04 -0800123 canvas->translate(0, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 paint.setColor(SK_ColorBLUE);
reeda4393342016-03-18 11:22:57 -0700126 paint.setPathEffect(make_pe(2, fPhase));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 canvas->drawPath(fPath, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000128
reedd9adfe62015-02-01 19:01:04 -0800129 canvas->translate(0, 50);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000130
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 paint.setARGB(0xFF, 0, 0xBB, 0);
reeda4393342016-03-18 11:22:57 -0700132 paint.setPathEffect(make_pe(3, fPhase));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 canvas->drawPath(fPath, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000134
reedd9adfe62015-02-01 19:01:04 -0800135 canvas->translate(0, 50);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136
137 paint.setARGB(0xFF, 0, 0, 0);
reeda4393342016-03-18 11:22:57 -0700138 paint.setPathEffect(make_warp_pe(fPhase));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 canvas->drawPath(fPath, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000141
mtklein36352bf2015-03-25 18:17:31 -0700142 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -0800143 fPhase = timer.scaled(40);
reedd9adfe62015-02-01 19:01:04 -0800144 return true;
145 }
146
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147private:
reed@google.come5ff4392011-06-01 11:59:08 +0000148 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149};
150
151//////////////////////////////////////////////////////////////////////////////
152
153static SkView* MyFactory() { return new PathEffectView; }
154static SkViewRegister reg(MyFactory);