blob: 75566b041203cabb58fb6db218f2b30c72074946 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001#include "SampleCode.h"
2#include "SkView.h"
3#include "SkCanvas.h"
4#include "SkGradientShader.h"
5#include "SkPath.h"
6#include "SkRegion.h"
7#include "SkShader.h"
8#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#include "Sk1DPathEffect.h"
10#include "SkCornerPathEffect.h"
11#include "SkPathMeasure.h"
12#include "SkRandom.h"
13#include "SkColorPriv.h"
14#include "SkPixelXorXfermode.h"
15
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#define CORNER_RADIUS 12
17static SkScalar gPhase;
18
19static const int gXY[] = {
20 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
21};
22
reed@android.comf2b98d62010-12-20 18:26:13 +000023static SkPathEffect* make_pe(int flags) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000024 if (flags == 1)
25 return new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS));
26
27 SkPath path;
28 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
29 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
30 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
31 path.close();
32 path.offset(SkIntToScalar(-6), 0);
33
34 SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12), gPhase, SkPath1DPathEffect::kRotate_Style);
35
36 if (flags == 2)
37 return outer;
38
39 SkPathEffect* inner = new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS));
40
41 SkPathEffect* pe = new SkComposePathEffect(outer, inner);
42 outer->unref();
43 inner->unref();
44 return pe;
45}
46
reed@android.comf2b98d62010-12-20 18:26:13 +000047static SkPathEffect* make_warp_pe() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 SkPath path;
49 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
50 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
51 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
52 path.close();
53 path.offset(SkIntToScalar(-6), 0);
54
55 SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12), gPhase, SkPath1DPathEffect::kMorph_Style);
56 SkPathEffect* inner = new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS));
57
58 SkPathEffect* pe = new SkComposePathEffect(outer, inner);
59 outer->unref();
60 inner->unref();
61 return pe;
62}
63
64///////////////////////////////////////////////////////////
65
66#include "SkColorFilter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000067#include "SkLayerRasterizer.h"
68
69class testrast : public SkLayerRasterizer {
70public:
reed@android.comf2b98d62010-12-20 18:26:13 +000071 testrast() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 SkPaint paint;
73 paint.setAntiAlias(true);
74
75#if 0
76 paint.setStyle(SkPaint::kStroke_Style);
77 paint.setStrokeWidth(SK_Scalar1*4);
78 this->addLayer(paint);
79
80 paint.setStrokeWidth(SK_Scalar1*1);
reed@android.com048522d2009-06-23 12:19:41 +000081 paint.setXfermode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 this->addLayer(paint);
83#else
84 paint.setAlpha(0x66);
85 this->addLayer(paint, SkIntToScalar(4), SkIntToScalar(4));
86
87 paint.setAlpha(0xFF);
88 this->addLayer(paint);
89#endif
90 }
91};
92
reed@google.come5ff4392011-06-01 11:59:08 +000093class PathEffectView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 SkPath fPath;
95 SkPoint fClickPt;
96public:
reed@android.comf2b98d62010-12-20 18:26:13 +000097 PathEffectView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 SkRandom rand;
99 int steps = 20;
reed@android.com92a50ea2009-08-14 19:27:37 +0000100 SkScalar dist = SkIntToScalar(400);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 SkScalar x = SkIntToScalar(20);
102 SkScalar y = SkIntToScalar(50);
103
104 fPath.moveTo(x, y);
reed@android.comf2b98d62010-12-20 18:26:13 +0000105 for (int i = 0; i < steps; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 x += dist/steps;
reed@android.com92a50ea2009-08-14 19:27:37 +0000107 SkScalar tmpY = y + SkIntToScalar(rand.nextS() % 25);
108 if (i == steps/2) {
109 fPath.moveTo(x, tmpY);
110 } else {
111 fPath.lineTo(x, tmpY);
112 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 }
114
reed@android.com92a50ea2009-08-14 19:27:37 +0000115 {
116 SkRect oval;
117 oval.set(SkIntToScalar(20), SkIntToScalar(30),
118 SkIntToScalar(100), SkIntToScalar(60));
119 oval.offset(x, 0);
120 fPath.addRoundRect(oval, SkIntToScalar(8), SkIntToScalar(8));
121 }
122
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 fClickPt.set(SkIntToScalar(200), SkIntToScalar(200));
reed@google.come5ff4392011-06-01 11:59:08 +0000124
125 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 }
127
128protected:
129 // overrides from SkEventSink
reed@android.comf2b98d62010-12-20 18:26:13 +0000130 virtual bool onQuery(SkEvent* evt) {
131 if (SampleCode::TitleQ(*evt)) {
132 SampleCode::TitleR(evt, "PathEffects");
133 return true;
134 }
135 return this->INHERITED::onQuery(evt);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 }
137
reed@google.come5ff4392011-06-01 11:59:08 +0000138 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000139 gPhase -= SampleCode::GetAnimSecondsDelta() * 40;
reed@android.com671cd652009-05-22 20:44:12 +0000140 this->inval(NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141
142 SkPaint paint;
143
reed@android.comf2b98d62010-12-20 18:26:13 +0000144#if 0
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 paint.setAntiAlias(true);
146 paint.setStyle(SkPaint::kStroke_Style);
147 paint.setStrokeWidth(SkIntToScalar(5));
148 canvas->drawPath(fPath, paint);
149 paint.setStrokeWidth(0);
150
reed@android.com92a50ea2009-08-14 19:27:37 +0000151 paint.setColor(SK_ColorWHITE);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 paint.setPathEffect(make_pe(1))->unref();
153 canvas->drawPath(fPath, paint);
reed@android.comf2b98d62010-12-20 18:26:13 +0000154#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155
156 canvas->translate(0, SkIntToScalar(50));
157
158 paint.setColor(SK_ColorBLUE);
159 paint.setPathEffect(make_pe(2))->unref();
160 canvas->drawPath(fPath, paint);
161
162 canvas->translate(0, SkIntToScalar(50));
163
164 paint.setARGB(0xFF, 0, 0xBB, 0);
165 paint.setPathEffect(make_pe(3))->unref();
166 canvas->drawPath(fPath, paint);
167
168 canvas->translate(0, SkIntToScalar(50));
169
170 paint.setARGB(0xFF, 0, 0, 0);
171 paint.setPathEffect(make_warp_pe())->unref();
172 paint.setRasterizer(new testrast)->unref();
173 canvas->drawPath(fPath, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174 }
175
176private:
reed@google.come5ff4392011-06-01 11:59:08 +0000177 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178};
179
180//////////////////////////////////////////////////////////////////////////////
181
182static SkView* MyFactory() { return new PathEffectView; }
183static SkViewRegister reg(MyFactory);
184