OLD | NEW |
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 | 8 |
9 #include "SampleCode.h" | 9 #include "SampleCode.h" |
10 #include "SkView.h" | 10 #include "SkView.h" |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 return this->INHERITED::onFindClickHandler(x, y, modi); | 207 return this->INHERITED::onFindClickHandler(x, y, modi); |
208 } | 208 } |
209 | 209 |
210 private: | 210 private: |
211 typedef SampleView INHERITED; | 211 typedef SampleView INHERITED; |
212 }; | 212 }; |
213 DEF_SAMPLE( return new PathView; ) | 213 DEF_SAMPLE( return new PathView; ) |
214 | 214 |
215 ////////////////////////////////////////////////////////////////////////////// | 215 ////////////////////////////////////////////////////////////////////////////// |
216 | 216 |
| 217 #include "SkArcToPathEffect.h" |
217 #include "SkCornerPathEffect.h" | 218 #include "SkCornerPathEffect.h" |
218 #include "SkRandom.h" | 219 #include "SkRandom.h" |
219 | 220 |
220 class ArcToView : public SampleView { | 221 class ArcToView : public SampleView { |
221 SkPaint fPtsPaint, fArcPaint, fSkeletonPaint, fCornerPaint; | 222 bool fDoFrame, fDoArcTo, fDoCorner, fDoConic; |
| 223 SkPaint fPtsPaint, fArcToPaint, fSkeletonPaint, fCornerPaint; |
222 public: | 224 public: |
223 enum { | 225 enum { |
224 N = 4 | 226 N = 4 |
225 }; | 227 }; |
226 SkPoint fPts[N]; | 228 SkPoint fPts[N]; |
227 SkScalar fRadius; | |
228 | 229 |
229 ArcToView() : fRadius(50) { | 230 ArcToView() |
| 231 : fDoFrame(false), fDoArcTo(false), fDoCorner(false), fDoConic(false) |
| 232 { |
230 SkRandom rand; | 233 SkRandom rand; |
231 for (int i = 0; i < N; ++i) { | 234 for (int i = 0; i < N; ++i) { |
232 fPts[i].fX = 20 + rand.nextUScalar1() * 640; | 235 fPts[i].fX = 20 + rand.nextUScalar1() * 640; |
233 fPts[i].fY = 20 + rand.nextUScalar1() * 480; | 236 fPts[i].fY = 20 + rand.nextUScalar1() * 480; |
234 } | 237 } |
| 238 |
| 239 const SkScalar rad = 50; |
235 | 240 |
236 fPtsPaint.setAntiAlias(true); | 241 fPtsPaint.setAntiAlias(true); |
237 fPtsPaint.setStrokeWidth(15); | 242 fPtsPaint.setStrokeWidth(15); |
238 fPtsPaint.setStrokeCap(SkPaint::kRound_Cap); | 243 fPtsPaint.setStrokeCap(SkPaint::kRound_Cap); |
239 | 244 |
240 fArcPaint.setAntiAlias(true); | 245 fArcToPaint.setAntiAlias(true); |
241 fArcPaint.setStyle(SkPaint::kStroke_Style); | 246 fArcToPaint.setStyle(SkPaint::kStroke_Style); |
242 fArcPaint.setStrokeWidth(9); | 247 fArcToPaint.setStrokeWidth(9); |
243 fArcPaint.setColor(0x800000FF); | 248 fArcToPaint.setColor(0x800000FF); |
| 249 fArcToPaint.setPathEffect(SkArcToPathEffect::Create(rad))->unref(); |
244 | 250 |
245 fCornerPaint.setAntiAlias(true); | 251 fCornerPaint.setAntiAlias(true); |
246 fCornerPaint.setStyle(SkPaint::kStroke_Style); | 252 fCornerPaint.setStyle(SkPaint::kStroke_Style); |
247 fCornerPaint.setStrokeWidth(13); | 253 fCornerPaint.setStrokeWidth(13); |
248 fCornerPaint.setColor(SK_ColorGREEN); | 254 fCornerPaint.setColor(SK_ColorGREEN); |
249 fCornerPaint.setPathEffect(SkCornerPathEffect::Create(fRadius*2))->unref
(); | 255 fCornerPaint.setPathEffect(SkCornerPathEffect::Create(rad*2))->unref(); |
250 | 256 |
251 fSkeletonPaint.setAntiAlias(true); | 257 fSkeletonPaint.setAntiAlias(true); |
252 fSkeletonPaint.setStyle(SkPaint::kStroke_Style); | 258 fSkeletonPaint.setStyle(SkPaint::kStroke_Style); |
253 fSkeletonPaint.setColor(SK_ColorRED); | 259 fSkeletonPaint.setColor(SK_ColorRED); |
254 } | 260 } |
255 | 261 |
| 262 void toggle(bool& value) { |
| 263 value = !value; |
| 264 this->inval(NULL); |
| 265 } |
| 266 |
256 protected: | 267 protected: |
257 // overrides from SkEventSink | 268 // overrides from SkEventSink |
258 bool onQuery(SkEvent* evt) SK_OVERRIDE { | 269 bool onQuery(SkEvent* evt) SK_OVERRIDE { |
259 if (SampleCode::TitleQ(*evt)) { | 270 if (SampleCode::TitleQ(*evt)) { |
260 SampleCode::TitleR(evt, "ArcTo"); | 271 SampleCode::TitleR(evt, "ArcTo"); |
261 return true; | 272 return true; |
262 } | 273 } |
| 274 SkUnichar uni; |
| 275 if (SampleCode::CharQ(*evt, &uni)) { |
| 276 switch (uni) { |
| 277 case '1': this->toggle(fDoFrame); return true; |
| 278 case '2': this->toggle(fDoArcTo); return true; |
| 279 case '3': this->toggle(fDoCorner); return true; |
| 280 case '4': this->toggle(fDoConic); return true; |
| 281 default: break; |
| 282 } |
| 283 } |
263 return this->INHERITED::onQuery(evt); | 284 return this->INHERITED::onQuery(evt); |
264 } | 285 } |
| 286 |
| 287 void makePath(SkPath* path) { |
| 288 path->moveTo(fPts[0]); |
| 289 for (int i = 1; i < N; ++i) { |
| 290 path->lineTo(fPts[i]); |
| 291 } |
| 292 if (!fDoFrame) { |
| 293 path->close(); |
| 294 } |
| 295 } |
265 | 296 |
266 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE { | 297 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE { |
267 canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint); | 298 canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint); |
268 | 299 |
269 SkPath path; | 300 SkPath path; |
| 301 this->makePath(&path); |
270 | 302 |
271 path.moveTo(fPts[0]); | 303 if (fDoCorner) { |
272 for (int i = 1; i < N; ++i) { | 304 canvas->drawPath(path, fCornerPaint); |
273 path.lineTo(fPts[i].fX, fPts[i].fY); | |
274 } | 305 } |
275 canvas->drawPath(path, fCornerPaint); | 306 if (fDoArcTo) { |
| 307 canvas->drawPath(path, fArcToPaint); |
| 308 } |
276 | 309 |
277 path.reset(); | 310 canvas->drawPath(path, fSkeletonPaint); |
278 path.moveTo(fPts[0]); | |
279 for (int i = 1; i < N - 1; ++i) { | |
280 path.arcTo(fPts[i].fX, fPts[i].fY, fPts[i+1].fX, fPts[i+1].fY, fRadi
us); | |
281 } | |
282 path.lineTo(fPts[N - 1]); | |
283 canvas->drawPath(path, fArcPaint); | |
284 | |
285 canvas->drawPoints(SkCanvas::kPolygon_PointMode, N, fPts, fSkeletonPaint
); | |
286 } | 311 } |
287 | 312 |
288 bool onClick(Click* click) SK_OVERRIDE { | 313 bool onClick(Click* click) SK_OVERRIDE { |
289 int32_t index; | 314 int32_t index; |
290 if (click->fMeta.findS32("index", &index)) { | 315 if (click->fMeta.findS32("index", &index)) { |
291 SkASSERT((unsigned)index < N); | 316 SkASSERT((unsigned)index < N); |
292 fPts[index] = click->fCurr; | 317 fPts[index] = click->fCurr; |
293 this->inval(NULL); | 318 this->inval(NULL); |
294 return true; | 319 return true; |
295 } | 320 } |
(...skipping 11 matching lines...) Expand all Loading... |
307 } | 332 } |
308 } | 333 } |
309 return this->INHERITED::onFindClickHandler(x, y, modi); | 334 return this->INHERITED::onFindClickHandler(x, y, modi); |
310 } | 335 } |
311 | 336 |
312 private: | 337 private: |
313 typedef SampleView INHERITED; | 338 typedef SampleView INHERITED; |
314 }; | 339 }; |
315 DEF_SAMPLE( return new ArcToView; ) | 340 DEF_SAMPLE( return new ArcToView; ) |
316 | 341 |
OLD | NEW |