jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 7 | |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkCanvas.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 10 | #include "SkPath.h" |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 11 | |
| 12 | // Reproduces https://code.google.com/p/chromium/issues/detail?id=279014 |
| 13 | |
| 14 | // Renders a string art shape. |
| 15 | // The particular shape rendered can be controlled by clicking horizontally, thereby |
| 16 | // generating an angle from 0 to 1. |
| 17 | |
| 18 | class StringArtView : public SampleView { |
| 19 | public: |
| 20 | StringArtView() : fAngle(0.305f) {} |
| 21 | |
| 22 | protected: |
| 23 | // overrides from SkEventSink |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 24 | bool onQuery(SkEvent* evt) override { |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 25 | if (SampleCode::TitleQ(*evt)) { |
| 26 | SampleCode::TitleR(evt, "StringArt"); |
| 27 | return true; |
| 28 | } |
| 29 | return this->INHERITED::onQuery(evt); |
| 30 | } |
| 31 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 32 | void onDrawContent(SkCanvas* canvas) override { |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 33 | SkScalar angle = fAngle*SK_ScalarPI + SkScalarHalf(SK_ScalarPI); |
| 34 | |
| 35 | SkPoint center = SkPoint::Make(SkScalarHalf(this->width()), SkScalarHalf(this->height())); |
| 36 | SkScalar length = 5; |
| 37 | SkScalar step = angle; |
| 38 | |
| 39 | SkPath path; |
| 40 | path.moveTo(center); |
| 41 | |
jvanverth@google.com | 417446a | 2013-09-19 16:32:49 +0000 | [diff] [blame] | 42 | while (length < (SkScalarHalf(SkMinScalar(this->width(), this->height())) - 10.f)) |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 43 | { |
| 44 | SkPoint rp = SkPoint::Make(length*SkScalarCos(step) + center.fX, |
| 45 | length*SkScalarSin(step) + center.fY); |
| 46 | path.lineTo(rp); |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 47 | length += angle / SkScalarHalf(SK_ScalarPI); |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 48 | step += angle; |
| 49 | } |
| 50 | path.close(); |
| 51 | |
| 52 | SkPaint paint; |
| 53 | paint.setAntiAlias(true); |
| 54 | paint.setStyle(SkPaint::kStroke_Style); |
| 55 | paint.setColor(0xFF007700); |
| 56 | |
| 57 | canvas->drawPath(path, paint); |
| 58 | } |
| 59 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 60 | SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override { |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 61 | fAngle = x/width(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 62 | this->inval(nullptr); |
| 63 | return nullptr; |
jvanverth@google.com | 4ea2878 | 2013-09-19 15:32:22 +0000 | [diff] [blame] | 64 | } |
| 65 | private: |
| 66 | |
| 67 | SkScalar fAngle; |
| 68 | typedef SampleView INHERITED; |
| 69 | }; |
| 70 | |
| 71 | ////////////////////////////////////////////////////////////////////////////// |
| 72 | |
| 73 | static SkView* MyFactory() { return new StringArtView; } |
| 74 | static SkViewRegister reg(MyFactory); |