caryclark | 70e6d60 | 2016-01-30 10:11:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | */ |
| 7 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 8 | #include "Sample.h" |
caryclark | 70e6d60 | 2016-01-30 10:11:21 -0800 | [diff] [blame] | 9 | #include "SkCanvas.h" |
| 10 | #include "SkPath.h" |
| 11 | #include "SkRandom.h" |
| 12 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 13 | class MegaStrokeView : public Sample { |
caryclark | 70e6d60 | 2016-01-30 10:11:21 -0800 | [diff] [blame] | 14 | public: |
| 15 | MegaStrokeView() { |
| 16 | fClip.set(0, 0, 950, 600); |
| 17 | fAngle = 0; |
| 18 | fPlusMinus = 0; |
| 19 | SkRandom rand; |
| 20 | fMegaPath.reset(); |
| 21 | for (int index = 0; index < 921; ++index) { |
| 22 | for (int segs = 0; segs < 40; ++segs) { |
| 23 | fMegaPath.lineTo(SkIntToScalar(index), SkIntToScalar(rand.nextRangeU(500, 600))); |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 29 | bool onQuery(Sample::Event* evt) override { |
| 30 | if (Sample::TitleQ(*evt)) { |
| 31 | Sample::TitleR(evt, "MegaStroke"); |
caryclark | 70e6d60 | 2016-01-30 10:11:21 -0800 | [diff] [blame] | 32 | return true; |
| 33 | } |
| 34 | |
| 35 | SkUnichar uni; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 36 | if (Sample::CharQ(*evt, &uni)) { |
caryclark | 70e6d60 | 2016-01-30 10:11:21 -0800 | [diff] [blame] | 37 | fClip.set(0, 0, 950, 600); |
| 38 | } |
Brian Osman | 8ceee43 | 2017-12-01 10:52:28 -0500 | [diff] [blame] | 39 | if (evt->isType("SampleCode_Key_Event")) { |
caryclark | 70e6d60 | 2016-01-30 10:11:21 -0800 | [diff] [blame] | 40 | fClip.set(0, 0, 950, 600); |
| 41 | } |
| 42 | return this->INHERITED::onQuery(evt); |
| 43 | } |
| 44 | |
| 45 | void onDrawBackground(SkCanvas* canvas) override { |
| 46 | } |
| 47 | |
| 48 | void onDrawContent(SkCanvas* canvas) override { |
| 49 | SkPaint paint; |
| 50 | paint.setAntiAlias(true); |
| 51 | paint.setARGB(255,255,153,0); |
| 52 | paint.setStyle(SkPaint::kStroke_Style); |
| 53 | paint.setStrokeWidth(1); |
| 54 | |
| 55 | canvas->save(); |
| 56 | canvas->clipRect(fClip); |
| 57 | canvas->clear(SK_ColorWHITE); |
| 58 | canvas->drawPath(fMegaPath, paint); |
| 59 | canvas->restore(); |
| 60 | |
| 61 | SkPaint divSimPaint; |
| 62 | divSimPaint.setColor(SK_ColorBLUE); |
| 63 | SkScalar x = SkScalarSin(fAngle * SK_ScalarPI / 180) * 200 + 250; |
| 64 | SkScalar y = SkScalarCos(fAngle * SK_ScalarPI / 180) * 200 + 250; |
| 65 | |
| 66 | if ((fPlusMinus ^= 1)) { |
| 67 | fAngle += 5; |
| 68 | } else { |
| 69 | fAngle -= 5; |
| 70 | } |
| 71 | SkRect divSim = SkRect::MakeXYWH(x, y, 100, 100); |
| 72 | divSim.outset(30, 30); |
| 73 | canvas->drawRect(divSim, divSimPaint); |
| 74 | fClip = divSim; |
| 75 | } |
| 76 | |
| 77 | void onSizeChange() override { |
| 78 | fClip.set(0, 0, 950, 600); |
| 79 | } |
| 80 | |
| 81 | bool onAnimate(const SkAnimTimer& ) override { |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | SkPath fMegaPath; |
| 87 | SkRect fClip; |
| 88 | int fAngle; |
| 89 | int fPlusMinus; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 90 | typedef Sample INHERITED; |
caryclark | 70e6d60 | 2016-01-30 10:11:21 -0800 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | ////////////////////////////////////////////////////////////////////////////// |
| 94 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 95 | DEF_SAMPLE( return new MegaStrokeView(); ) |