• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2011 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 #include "SampleCode.h"
9 #include "SkView.h"
10 #include "SkCanvas.h"
11 #include "SkGradientShader.h"
12 #include "SkPath.h"
13 #include "SkRegion.h"
14 #include "SkShader.h"
15 #include "SkUtils.h"
16 #include "Sk1DPathEffect.h"
17 #include "SkCornerPathEffect.h"
18 #include "SkPathMeasure.h"
19 #include "SkRandom.h"
20 #include "SkColorPriv.h"
21 #include "SkPixelXorXfermode.h"
22 
23 #define CORNER_RADIUS   12
24 static SkScalar gPhase;
25 
26 static const int gXY[] = {
27     4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
28 };
29 
make_pe(int flags)30 static SkPathEffect* make_pe(int flags) {
31     if (flags == 1)
32         return new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS));
33 
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 
41     SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12), gPhase, SkPath1DPathEffect::kRotate_Style);
42 
43     if (flags == 2)
44         return outer;
45 
46     SkPathEffect* inner = new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS));
47 
48     SkPathEffect* pe = new SkComposePathEffect(outer, inner);
49     outer->unref();
50     inner->unref();
51     return pe;
52 }
53 
make_warp_pe()54 static SkPathEffect* make_warp_pe() {
55     SkPath  path;
56     path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
57     for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
58         path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
59     path.close();
60     path.offset(SkIntToScalar(-6), 0);
61 
62     SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12), gPhase, SkPath1DPathEffect::kMorph_Style);
63     SkPathEffect* inner = new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS));
64 
65     SkPathEffect* pe = new SkComposePathEffect(outer, inner);
66     outer->unref();
67     inner->unref();
68     return pe;
69 }
70 
71 ///////////////////////////////////////////////////////////
72 
73 #include "SkColorFilter.h"
74 #include "SkLayerRasterizer.h"
75 
76 class testrast : public SkLayerRasterizer {
77 public:
testrast()78     testrast() {
79         SkPaint paint;
80         paint.setAntiAlias(true);
81 
82 #if 0
83         paint.setStyle(SkPaint::kStroke_Style);
84         paint.setStrokeWidth(SK_Scalar1*4);
85         this->addLayer(paint);
86 
87         paint.setStrokeWidth(SK_Scalar1*1);
88         paint.setXfermode(SkXfermode::kClear_Mode);
89         this->addLayer(paint);
90 #else
91         paint.setAlpha(0x66);
92         this->addLayer(paint, SkIntToScalar(4), SkIntToScalar(4));
93 
94         paint.setAlpha(0xFF);
95         this->addLayer(paint);
96 #endif
97     }
98 };
99 
100 class PathEffectView : public SampleView {
101     SkPath  fPath;
102     SkPoint fClickPt;
103 public:
PathEffectView()104 	PathEffectView() {
105         SkRandom    rand;
106         int         steps = 20;
107         SkScalar    dist = SkIntToScalar(400);
108         SkScalar    x = SkIntToScalar(20);
109         SkScalar    y = SkIntToScalar(50);
110 
111         fPath.moveTo(x, y);
112         for (int i = 0; i < steps; i++) {
113             x += dist/steps;
114             SkScalar tmpY = y + SkIntToScalar(rand.nextS() % 25);
115             if (i == steps/2) {
116                 fPath.moveTo(x, tmpY);
117             } else {
118                 fPath.lineTo(x, tmpY);
119             }
120         }
121 
122         {
123             SkRect  oval;
124             oval.set(SkIntToScalar(20), SkIntToScalar(30),
125                      SkIntToScalar(100), SkIntToScalar(60));
126             oval.offset(x, 0);
127             fPath.addRoundRect(oval, SkIntToScalar(8), SkIntToScalar(8));
128         }
129 
130         fClickPt.set(SkIntToScalar(200), SkIntToScalar(200));
131 
132         this->setBGColor(0xFFDDDDDD);
133     }
134 
135 protected:
136     // overrides from SkEventSink
onQuery(SkEvent * evt)137     virtual bool onQuery(SkEvent* evt) {
138         if (SampleCode::TitleQ(*evt)) {
139             SampleCode::TitleR(evt, "PathEffects");
140             return true;
141         }
142         return this->INHERITED::onQuery(evt);
143     }
144 
onDrawContent(SkCanvas * canvas)145     virtual void onDrawContent(SkCanvas* canvas) {
146         gPhase -= SampleCode::GetAnimSecondsDelta() * 40;
147         this->inval(NULL);
148 
149         SkPaint paint;
150 
151 #if 0
152         paint.setAntiAlias(true);
153         paint.setStyle(SkPaint::kStroke_Style);
154         paint.setStrokeWidth(SkIntToScalar(5));
155         canvas->drawPath(fPath, paint);
156         paint.setStrokeWidth(0);
157 
158         paint.setColor(SK_ColorWHITE);
159         paint.setPathEffect(make_pe(1))->unref();
160         canvas->drawPath(fPath, paint);
161 #endif
162 
163         canvas->translate(0, SkIntToScalar(50));
164 
165         paint.setColor(SK_ColorBLUE);
166         paint.setPathEffect(make_pe(2))->unref();
167         canvas->drawPath(fPath, paint);
168 
169         canvas->translate(0, SkIntToScalar(50));
170 
171         paint.setARGB(0xFF, 0, 0xBB, 0);
172         paint.setPathEffect(make_pe(3))->unref();
173         canvas->drawPath(fPath, paint);
174 
175         canvas->translate(0, SkIntToScalar(50));
176 
177         paint.setARGB(0xFF, 0, 0, 0);
178         paint.setPathEffect(make_warp_pe())->unref();
179         paint.setRasterizer(new testrast)->unref();
180         canvas->drawPath(fPath, paint);
181     }
182 
183 private:
184     typedef SampleView INHERITED;
185 };
186 
187 //////////////////////////////////////////////////////////////////////////////
188 
MyFactory()189 static SkView* MyFactory() { return new PathEffectView; }
190 static SkViewRegister reg(MyFactory);
191 
192