blob: 19912a39959d9416d4fbd9219cffcaf6687efc55 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkPath.h"
10#include "include/utils/SkRandom.h"
11#include "samplecode/Sample.h"
caryclark70e6d602016-01-30 10:11:21 -080012
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
Mike Kleincd5104e2019-03-20 11:55:08 -050081 bool onAnimate(const AnimTimer&) override { return true; }
caryclark70e6d602016-01-30 10:11:21 -080082
83private:
84 SkPath fMegaPath;
85 SkRect fClip;
86 int fAngle;
87 int fPlusMinus;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040088 typedef Sample INHERITED;
caryclark70e6d602016-01-30 10:11:21 -080089};
90
91//////////////////////////////////////////////////////////////////////////////
92
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040093DEF_SAMPLE( return new MegaStrokeView(); )