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
8#include "SampleCode.h"
9#include "SkCanvas.h"
10#include "SkPath.h"
11#include "SkRandom.h"
12
13class MegaStrokeView : public SampleView {
14public:
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:
29    // overrides from SkEventSink
30    bool onQuery(SkEvent* evt) override {
31        if (SampleCode::TitleQ(*evt)) {
32            SampleCode::TitleR(evt, "MegaStroke");
33            return true;
34        }
35
36        SkUnichar uni;
37        if (SampleCode::CharQ(*evt, &uni)) {
38           fClip.set(0, 0, 950, 600);
39        }
40        if (evt->isType("SampleCode_Key_Event")) {
41           fClip.set(0, 0, 950, 600);
42        }
43        return this->INHERITED::onQuery(evt);
44    }
45
46    void onDrawBackground(SkCanvas* canvas) override {
47    }
48
49    void onDrawContent(SkCanvas* canvas) override {
50        SkPaint paint;
51        paint.setAntiAlias(true);
52        paint.setARGB(255,255,153,0);
53        paint.setStyle(SkPaint::kStroke_Style);
54        paint.setStrokeWidth(1);
55
56        canvas->save();
57        canvas->clipRect(fClip);
58        canvas->clear(SK_ColorWHITE);
59        canvas->drawPath(fMegaPath, paint);
60        canvas->restore();
61
62        SkPaint divSimPaint;
63        divSimPaint.setColor(SK_ColorBLUE);
64	    SkScalar x = SkScalarSin(fAngle * SK_ScalarPI / 180) * 200 + 250;
65	    SkScalar y = SkScalarCos(fAngle * SK_ScalarPI / 180) * 200 + 250;
66
67        if ((fPlusMinus ^= 1)) {
68            fAngle += 5;
69        } else {
70            fAngle -= 5;
71        }
72        SkRect divSim = SkRect::MakeXYWH(x, y, 100, 100);
73        divSim.outset(30, 30);
74        canvas->drawRect(divSim, divSimPaint);
75        fClip = divSim;
76    }
77
78    void onSizeChange() override {
79        fClip.set(0, 0, 950, 600);
80    }
81
82    bool onAnimate(const SkAnimTimer& ) override {
83        return true;
84    }
85
86private:
87    SkPath      fMegaPath;
88    SkRect      fClip;
89    int         fAngle;
90    int         fPlusMinus;
91    typedef SampleView INHERITED;
92};
93
94//////////////////////////////////////////////////////////////////////////////
95
96static SkView* MyFactory() { return new MegaStrokeView; }
97static SkViewRegister reg(MyFactory);
98