2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SampleCode.h"
9 #include "SkAnimTimer.h"
12 #include "SkGradientShader.h"
17 #include "Sk1DPathEffect.h"
18 #include "SkCornerPathEffect.h"
19 #include "SkPathMeasure.h"
21 #include "SkColorPriv.h"
22 #include "SkPixelXorXfermode.h"
24 #define CORNER_RADIUS 12
26 static const int gXY[] = {
27 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
30 static SkPathEffect* make_pe(int flags, SkScalar phase) {
32 return SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS));
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]));
39 path.offset(SkIntToScalar(-6), 0);
41 SkPathEffect* outer = SkPath1DPathEffect::Create(path, 12, phase,
42 SkPath1DPathEffect::kRotate_Style);
47 SkPathEffect* inner = SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS));
49 SkPathEffect* pe = SkComposePathEffect::Create(outer, inner);
55 static SkPathEffect* make_warp_pe(SkScalar phase) {
57 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
58 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
59 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
61 path.offset(SkIntToScalar(-6), 0);
63 SkPathEffect* outer = SkPath1DPathEffect::Create(
64 path, 12, phase, SkPath1DPathEffect::kMorph_Style);
65 SkPathEffect* inner = SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS));
67 SkPathEffect* pe = SkComposePathEffect::Create(outer, inner);
73 ///////////////////////////////////////////////////////////
75 #include "SkColorFilter.h"
76 #include "SkLayerRasterizer.h"
78 class TestRastBuilder : public SkLayerRasterizer::Builder {
82 paint.setAntiAlias(true);
85 this->addLayer(paint, SkIntToScalar(4), SkIntToScalar(4));
88 this->addLayer(paint);
92 class PathEffectView : public SampleView {
98 PathEffectView() : fPhase(0) {
102 void onOnceBeforeDraw() SK_OVERRIDE {
105 SkScalar dist = SkIntToScalar(400);
106 SkScalar x = SkIntToScalar(20);
107 SkScalar y = SkIntToScalar(50);
110 for (int i = 0; i < steps; i++) {
112 SkScalar tmpY = y + SkIntToScalar(rand.nextS() % 25);
114 fPath.moveTo(x, tmpY);
116 fPath.lineTo(x, tmpY);
122 oval.set(SkIntToScalar(20), SkIntToScalar(30),
123 SkIntToScalar(100), SkIntToScalar(60));
125 fPath.addRoundRect(oval, SkIntToScalar(8), SkIntToScalar(8));
128 fClickPt.set(SkIntToScalar(200), SkIntToScalar(200));
130 this->setBGColor(0xFFDDDDDD);
133 bool onQuery(SkEvent* evt) SK_OVERRIDE {
134 if (SampleCode::TitleQ(*evt)) {
135 SampleCode::TitleR(evt, "PathEffects");
138 return this->INHERITED::onQuery(evt);
141 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
144 canvas->translate(0, 50);
146 paint.setColor(SK_ColorBLUE);
147 paint.setPathEffect(make_pe(2, fPhase))->unref();
148 canvas->drawPath(fPath, paint);
150 canvas->translate(0, 50);
152 paint.setARGB(0xFF, 0, 0xBB, 0);
153 paint.setPathEffect(make_pe(3, fPhase))->unref();
154 canvas->drawPath(fPath, paint);
156 canvas->translate(0, 50);
158 paint.setARGB(0xFF, 0, 0, 0);
159 paint.setPathEffect(make_warp_pe(fPhase))->unref();
160 TestRastBuilder testRastBuilder;
161 paint.setRasterizer(testRastBuilder.detachRasterizer())->unref();
162 canvas->drawPath(fPath, paint);
165 bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
166 fPhase = timer.scaled(40);
171 typedef SampleView INHERITED;
174 //////////////////////////////////////////////////////////////////////////////
176 static SkView* MyFactory() { return new PathEffectView; }
177 static SkViewRegister reg(MyFactory);