blob: a8f349a012bdeffb6bb0db96d5634878be2f0b44 [file] [log] [blame]
caryclark70e6d602016-01-30 10:11:21 -08001/*
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 Wagnerb2c4ea62018-08-08 11:36:17 -04008#include "Sample.h"
caryclark70e6d602016-01-30 10:11:21 -08009#include "SkCanvas.h"
10#include "SkPath.h"
11#include "SkRandom.h"
12
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040013class MegaStrokeView : public Sample {
caryclark70e6d602016-01-30 10:11:21 -080014public:
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
28protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040029 bool onQuery(Sample::Event* evt) override {
30 if (Sample::TitleQ(*evt)) {
31 Sample::TitleR(evt, "MegaStroke");
caryclark70e6d602016-01-30 10:11:21 -080032 return true;
33 }
34
35 SkUnichar uni;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040036 if (Sample::CharQ(*evt, &uni)) {
caryclark70e6d602016-01-30 10:11:21 -080037 fClip.set(0, 0, 950, 600);
38 }
Brian Osman8ceee432017-12-01 10:52:28 -050039 if (evt->isType("SampleCode_Key_Event")) {
caryclark70e6d602016-01-30 10:11:21 -080040 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
85private:
86 SkPath fMegaPath;
87 SkRect fClip;
88 int fAngle;
89 int fPlusMinus;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040090 typedef Sample INHERITED;
caryclark70e6d602016-01-30 10:11:21 -080091};
92
93//////////////////////////////////////////////////////////////////////////////
94
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040095DEF_SAMPLE( return new MegaStrokeView(); )