Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: samplecode/SamplePathEffects.cpp

Issue 888283002: allow GMs to animate (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: switch all existing animations to use animatePulse Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « samplecode/SampleHairline.cpp ('k') | samplecode/SamplePicture.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2011 Google Inc. 3 * Copyright 2011 Google Inc.
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 #include "SampleCode.h" 8 #include "SampleCode.h"
9 #include "SkView.h" 9 #include "SkView.h"
10 #include "SkCanvas.h" 10 #include "SkCanvas.h"
11 #include "SkGradientShader.h" 11 #include "SkGradientShader.h"
12 #include "SkPath.h" 12 #include "SkPath.h"
13 #include "SkRegion.h" 13 #include "SkRegion.h"
14 #include "SkShader.h" 14 #include "SkShader.h"
15 #include "SkUtils.h" 15 #include "SkUtils.h"
16 #include "Sk1DPathEffect.h" 16 #include "Sk1DPathEffect.h"
17 #include "SkCornerPathEffect.h" 17 #include "SkCornerPathEffect.h"
18 #include "SkPathMeasure.h" 18 #include "SkPathMeasure.h"
19 #include "SkRandom.h" 19 #include "SkRandom.h"
20 #include "SkColorPriv.h" 20 #include "SkColorPriv.h"
21 #include "SkPixelXorXfermode.h" 21 #include "SkPixelXorXfermode.h"
22 22
23 #define CORNER_RADIUS 12 23 #define CORNER_RADIUS 12
24 static SkScalar gPhase;
25 24
26 static const int gXY[] = { 25 static const int gXY[] = {
27 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4 26 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
28 }; 27 };
29 28
30 static SkPathEffect* make_pe(int flags) { 29 static SkPathEffect* make_pe(int flags, SkScalar phase) {
31 if (flags == 1) 30 if (flags == 1)
32 return SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS)); 31 return SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS));
33 32
34 SkPath path; 33 SkPath path;
35 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); 34 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
36 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) 35 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
37 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); 36 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
38 path.close(); 37 path.close();
39 path.offset(SkIntToScalar(-6), 0); 38 path.offset(SkIntToScalar(-6), 0);
40 39
41 SkPathEffect* outer = SkPath1DPathEffect::Create(path, SkIntToScalar(12), gP hase, SkPath1DPathEffect::kRotate_Style); 40 SkPathEffect* outer = SkPath1DPathEffect::Create(path, 12, phase,
41 SkPath1DPathEffect::kRotate _Style);
42 42
43 if (flags == 2) 43 if (flags == 2)
44 return outer; 44 return outer;
45 45
46 SkPathEffect* inner = SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS )); 46 SkPathEffect* inner = SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS ));
47 47
48 SkPathEffect* pe = SkComposePathEffect::Create(outer, inner); 48 SkPathEffect* pe = SkComposePathEffect::Create(outer, inner);
49 outer->unref(); 49 outer->unref();
50 inner->unref(); 50 inner->unref();
51 return pe; 51 return pe;
52 } 52 }
53 53
54 static SkPathEffect* make_warp_pe() { 54 static SkPathEffect* make_warp_pe(SkScalar phase) {
55 SkPath path; 55 SkPath path;
56 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); 56 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
57 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) 57 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
58 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); 58 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
59 path.close(); 59 path.close();
60 path.offset(SkIntToScalar(-6), 0); 60 path.offset(SkIntToScalar(-6), 0);
61 61
62 SkPathEffect* outer = SkPath1DPathEffect::Create( 62 SkPathEffect* outer = SkPath1DPathEffect::Create(
63 path, SkIntToScalar(12), gPhase, SkPath1DPathEffect::kMorph_Style); 63 path, 12, phase, SkPath1DPathEffect::kMorph_Style);
64 SkPathEffect* inner = SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS )); 64 SkPathEffect* inner = SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS ));
65 65
66 SkPathEffect* pe = SkComposePathEffect::Create(outer, inner); 66 SkPathEffect* pe = SkComposePathEffect::Create(outer, inner);
67 outer->unref(); 67 outer->unref();
68 inner->unref(); 68 inner->unref();
69 return pe; 69 return pe;
70 } 70 }
71 71
72 /////////////////////////////////////////////////////////// 72 ///////////////////////////////////////////////////////////
73 73
74 #include "SkColorFilter.h" 74 #include "SkColorFilter.h"
75 #include "SkLayerRasterizer.h" 75 #include "SkLayerRasterizer.h"
76 76
77 class TestRastBuilder : public SkLayerRasterizer::Builder { 77 class TestRastBuilder : public SkLayerRasterizer::Builder {
78 public: 78 public:
79 TestRastBuilder() { 79 TestRastBuilder() {
80 SkPaint paint; 80 SkPaint paint;
81 paint.setAntiAlias(true); 81 paint.setAntiAlias(true);
82 82
83 #if 0
84 paint.setStyle(SkPaint::kStroke_Style);
85 paint.setStrokeWidth(SK_Scalar1*4);
86 this->addLayer(paint);
87
88 paint.setStrokeWidth(SK_Scalar1*1);
89 paint.setXfermode(SkXfermode::kClear_Mode);
90 this->addLayer(paint);
91 #else
92 paint.setAlpha(0x66); 83 paint.setAlpha(0x66);
93 this->addLayer(paint, SkIntToScalar(4), SkIntToScalar(4)); 84 this->addLayer(paint, SkIntToScalar(4), SkIntToScalar(4));
94 85
95 paint.setAlpha(0xFF); 86 paint.setAlpha(0xFF);
96 this->addLayer(paint); 87 this->addLayer(paint);
97 #endif
98 } 88 }
99 }; 89 };
100 90
101 class PathEffectView : public SampleView { 91 class PathEffectView : public SampleView {
102 SkPath fPath; 92 SkPath fPath;
103 SkPoint fClickPt; 93 SkPoint fClickPt;
94 SkScalar fPhase;
95
104 public: 96 public:
105 PathEffectView() { 97 PathEffectView() : fPhase(0) {
106 SkRandom rand; 98 SkRandom rand;
107 int steps = 20; 99 int steps = 20;
108 SkScalar dist = SkIntToScalar(400); 100 SkScalar dist = SkIntToScalar(400);
109 SkScalar x = SkIntToScalar(20); 101 SkScalar x = SkIntToScalar(20);
110 SkScalar y = SkIntToScalar(50); 102 SkScalar y = SkIntToScalar(50);
111 103
112 fPath.moveTo(x, y); 104 fPath.moveTo(x, y);
113 for (int i = 0; i < steps; i++) { 105 for (int i = 0; i < steps; i++) {
114 x += dist/steps; 106 x += dist/steps;
115 SkScalar tmpY = y + SkIntToScalar(rand.nextS() % 25); 107 SkScalar tmpY = y + SkIntToScalar(rand.nextS() % 25);
(...skipping 11 matching lines...) Expand all
127 oval.offset(x, 0); 119 oval.offset(x, 0);
128 fPath.addRoundRect(oval, SkIntToScalar(8), SkIntToScalar(8)); 120 fPath.addRoundRect(oval, SkIntToScalar(8), SkIntToScalar(8));
129 } 121 }
130 122
131 fClickPt.set(SkIntToScalar(200), SkIntToScalar(200)); 123 fClickPt.set(SkIntToScalar(200), SkIntToScalar(200));
132 124
133 this->setBGColor(0xFFDDDDDD); 125 this->setBGColor(0xFFDDDDDD);
134 } 126 }
135 127
136 protected: 128 protected:
137 // overrides from SkEventSink 129 bool onQuery(SkEvent* evt) SK_OVERRIDE {
138 virtual bool onQuery(SkEvent* evt) {
139 if (SampleCode::TitleQ(*evt)) { 130 if (SampleCode::TitleQ(*evt)) {
140 SampleCode::TitleR(evt, "PathEffects"); 131 SampleCode::TitleR(evt, "PathEffects");
141 return true; 132 return true;
142 } 133 }
143 return this->INHERITED::onQuery(evt); 134 return this->INHERITED::onQuery(evt);
144 } 135 }
145 136
146 virtual void onDrawContent(SkCanvas* canvas) { 137 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
147 gPhase -= SampleCode::GetAnimSecondsDelta() * 40;
148 this->inval(NULL);
149
150 SkPaint paint; 138 SkPaint paint;
151 139
152 #if 0 140 canvas->translate(0, 50);
153 paint.setAntiAlias(true);
154 paint.setStyle(SkPaint::kStroke_Style);
155 paint.setStrokeWidth(SkIntToScalar(5));
156 canvas->drawPath(fPath, paint);
157 paint.setStrokeWidth(0);
158
159 paint.setColor(SK_ColorWHITE);
160 paint.setPathEffect(make_pe(1))->unref();
161 canvas->drawPath(fPath, paint);
162 #endif
163
164 canvas->translate(0, SkIntToScalar(50));
165 141
166 paint.setColor(SK_ColorBLUE); 142 paint.setColor(SK_ColorBLUE);
167 paint.setPathEffect(make_pe(2))->unref(); 143 paint.setPathEffect(make_pe(2, fPhase))->unref();
168 canvas->drawPath(fPath, paint); 144 canvas->drawPath(fPath, paint);
169 145
170 canvas->translate(0, SkIntToScalar(50)); 146 canvas->translate(0, 50);
171 147
172 paint.setARGB(0xFF, 0, 0xBB, 0); 148 paint.setARGB(0xFF, 0, 0xBB, 0);
173 paint.setPathEffect(make_pe(3))->unref(); 149 paint.setPathEffect(make_pe(3, fPhase))->unref();
174 canvas->drawPath(fPath, paint); 150 canvas->drawPath(fPath, paint);
175 151
176 canvas->translate(0, SkIntToScalar(50)); 152 canvas->translate(0, 50);
177 153
178 paint.setARGB(0xFF, 0, 0, 0); 154 paint.setARGB(0xFF, 0, 0, 0);
179 paint.setPathEffect(make_warp_pe())->unref(); 155 paint.setPathEffect(make_warp_pe(fPhase))->unref();
180 TestRastBuilder testRastBuilder; 156 TestRastBuilder testRastBuilder;
181 paint.setRasterizer(testRastBuilder.detachRasterizer())->unref(); 157 paint.setRasterizer(testRastBuilder.detachRasterizer())->unref();
182 canvas->drawPath(fPath, paint); 158 canvas->drawPath(fPath, paint);
183 } 159 }
184 160
161 bool onAnimatePulse(SkMSec curr, SkMSec prev) SK_OVERRIDE {
162 fPhase -= (curr - prev) * 0.04f;
163 return true;
164 }
165
185 private: 166 private:
186 typedef SampleView INHERITED; 167 typedef SampleView INHERITED;
187 }; 168 };
188 169
189 ////////////////////////////////////////////////////////////////////////////// 170 //////////////////////////////////////////////////////////////////////////////
190 171
191 static SkView* MyFactory() { return new PathEffectView; } 172 static SkView* MyFactory() { return new PathEffectView; }
192 static SkViewRegister reg(MyFactory); 173 static SkViewRegister reg(MyFactory);
OLDNEW
« no previous file with comments | « samplecode/SampleHairline.cpp ('k') | samplecode/SamplePicture.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698