blob: 15579d90eedaa6235719f833933e2bd66519a904 [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() {
Mike Reed92b33352019-08-24 19:39:13 -040016 fClip.setLTRB(0, 0, 950, 600);
caryclark70e6d602016-01-30 10:11:21 -080017 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:
Hal Canary8a027312019-07-03 10:55:44 -040029 SkString name() override { return SkString("MegaStroke"); }
caryclark70e6d602016-01-30 10:11:21 -080030
Hal Canary6cc65e12019-07-03 15:53:04 -040031 bool onChar(SkUnichar uni) override {
Mike Reed92b33352019-08-24 19:39:13 -040032 fClip.setLTRB(0, 0, 950, 600);
Hal Canary6cc65e12019-07-03 15:53:04 -040033 return true;
caryclark70e6d602016-01-30 10:11:21 -080034 }
35
36 void onDrawBackground(SkCanvas* canvas) override {
37 }
38
39 void onDrawContent(SkCanvas* canvas) override {
40 SkPaint paint;
41 paint.setAntiAlias(true);
42 paint.setARGB(255,255,153,0);
43 paint.setStyle(SkPaint::kStroke_Style);
44 paint.setStrokeWidth(1);
45
46 canvas->save();
47 canvas->clipRect(fClip);
48 canvas->clear(SK_ColorWHITE);
49 canvas->drawPath(fMegaPath, paint);
50 canvas->restore();
51
52 SkPaint divSimPaint;
53 divSimPaint.setColor(SK_ColorBLUE);
54 SkScalar x = SkScalarSin(fAngle * SK_ScalarPI / 180) * 200 + 250;
55 SkScalar y = SkScalarCos(fAngle * SK_ScalarPI / 180) * 200 + 250;
56
57 if ((fPlusMinus ^= 1)) {
58 fAngle += 5;
59 } else {
60 fAngle -= 5;
61 }
62 SkRect divSim = SkRect::MakeXYWH(x, y, 100, 100);
63 divSim.outset(30, 30);
64 canvas->drawRect(divSim, divSimPaint);
65 fClip = divSim;
66 }
67
68 void onSizeChange() override {
Mike Reed92b33352019-08-24 19:39:13 -040069 fClip.setWH(950, 600);
caryclark70e6d602016-01-30 10:11:21 -080070 }
71
Hal Canary41248072019-07-11 16:32:53 -040072 bool onAnimate(double /*nanos*/) override { return true; }
caryclark70e6d602016-01-30 10:11:21 -080073
74private:
75 SkPath fMegaPath;
76 SkRect fClip;
77 int fAngle;
78 int fPlusMinus;
John Stiles7571f9e2020-09-02 22:42:33 -040079 using INHERITED = Sample;
caryclark70e6d602016-01-30 10:11:21 -080080};
81
82//////////////////////////////////////////////////////////////////////////////
83
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040084DEF_SAMPLE( return new MegaStrokeView(); )