Home | History | Annotate | Download | only in samplecode
      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 "Sample.h"
      9 #include "SkCanvas.h"
     10 #include "SkPath.h"
     11 #include "SkRandom.h"
     12 
     13 class MegaStrokeView : public Sample {
     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:
     29     bool onQuery(Sample::Event* evt) override {
     30         if (Sample::TitleQ(*evt)) {
     31             Sample::TitleR(evt, "MegaStroke");
     32             return true;
     33         }
     34 
     35         SkUnichar uni;
     36         if (Sample::CharQ(*evt, &uni)) {
     37            fClip.set(0, 0, 950, 600);
     38         }
     39         if (evt->isType("SampleCode_Key_Event")) {
     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;
     90     typedef Sample INHERITED;
     91 };
     92 
     93 //////////////////////////////////////////////////////////////////////////////
     94 
     95 DEF_SAMPLE( return new MegaStrokeView(); )
     96