Home | History | Annotate | Download | only in samplecode
      1 /*
      2  * Copyright 2011 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 #include "Sample.h"
      8 #include "SkCanvas.h"
      9 #include "SkGraphics.h"
     10 #include "SkRandom.h"
     11 #include "SkBlurDrawLooper.h"
     12 #include "SkGradientShader.h"
     13 
     14 typedef SkScalar (*MakePathProc)(SkPath*);
     15 
     16 static SkScalar make_frame(SkPath* path) {
     17     SkRect r = { 10, 10, 630, 470 };
     18     path->addRoundRect(r, 15, 15);
     19 
     20     SkPaint paint;
     21     paint.setStyle(SkPaint::kStroke_Style);
     22     paint.setStrokeWidth(5);
     23     paint.getFillPath(*path, path);
     24     return 15;
     25 }
     26 
     27 static SkScalar make_triangle(SkPath* path) {
     28     static const int gCoord[] = {
     29         10, 20, 15, 5, 30, 30
     30     };
     31     path->moveTo(SkIntToScalar(gCoord[0]), SkIntToScalar(gCoord[1]));
     32     path->lineTo(SkIntToScalar(gCoord[2]), SkIntToScalar(gCoord[3]));
     33     path->lineTo(SkIntToScalar(gCoord[4]), SkIntToScalar(gCoord[5]));
     34     path->close();
     35     path->offset(10, 0);
     36     return SkIntToScalar(30);
     37 }
     38 
     39 static SkScalar make_rect(SkPath* path) {
     40     SkRect r = { 10, 10, 30, 30 };
     41     path->addRect(r);
     42     path->offset(10, 0);
     43     return SkIntToScalar(30);
     44 }
     45 
     46 static SkScalar make_oval(SkPath* path) {
     47     SkRect r = { 10, 10, 30, 30 };
     48     path->addOval(r);
     49     path->offset(10, 0);
     50     return SkIntToScalar(30);
     51 }
     52 
     53 static SkScalar make_sawtooth(SkPath* path) {
     54     SkScalar x = SkIntToScalar(20);
     55     SkScalar y = SkIntToScalar(20);
     56     const SkScalar x0 = x;
     57     const SkScalar dx = SK_Scalar1 * 5;
     58     const SkScalar dy = SK_Scalar1 * 10;
     59 
     60     path->moveTo(x, y);
     61     for (int i = 0; i < 32; i++) {
     62         x += dx;
     63         path->lineTo(x, y - dy);
     64         x += dx;
     65         path->lineTo(x, y + dy);
     66     }
     67     path->lineTo(x, y + 2 * dy);
     68     path->lineTo(x0, y + 2 * dy);
     69     path->close();
     70     return SkIntToScalar(30);
     71 }
     72 
     73 static SkScalar make_star(SkPath* path, int n) {
     74     const SkScalar c = SkIntToScalar(45);
     75     const SkScalar r = SkIntToScalar(20);
     76 
     77     SkScalar rad = -SK_ScalarPI / 2;
     78     const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
     79 
     80     path->moveTo(c, c - r);
     81     for (int i = 1; i < n; i++) {
     82         rad += drad;
     83         SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV);
     84         path->lineTo(c + cosV * r, c + sinV * r);
     85     }
     86     path->close();
     87     return r * 2 * 6 / 5;
     88 }
     89 
     90 static SkScalar make_star_5(SkPath* path) { return make_star(path, 5); }
     91 static SkScalar make_star_13(SkPath* path) { return make_star(path, 13); }
     92 
     93 static const MakePathProc gProcs[] = {
     94     make_frame,
     95     make_triangle,
     96     make_rect,
     97     make_oval,
     98     make_sawtooth,
     99     make_star_5,
    100     make_star_13
    101 };
    102 
    103 #define N   SK_ARRAY_COUNT(gProcs)
    104 
    105 class PathFillView : public Sample {
    106     SkPath  fPath[N];
    107     SkScalar fDY[N];
    108 
    109 public:
    110     PathFillView() {
    111         for (size_t i = 0; i < N; i++) {
    112             fDY[i] = gProcs[i](&fPath[i]);
    113         }
    114         this->setBGColor(0xFFDDDDDD);
    115     }
    116 
    117 protected:
    118     virtual bool onQuery(Sample::Event* evt) {
    119         if (Sample::TitleQ(*evt)) {
    120             Sample::TitleR(evt, "PathFill");
    121             return true;
    122         }
    123         return this->INHERITED::onQuery(evt);
    124     }
    125 
    126     virtual void onDrawContent(SkCanvas* canvas) {
    127         SkPaint paint;
    128         paint.setAntiAlias(true);
    129 
    130         for (size_t i = 0; i < N; i++) {
    131             canvas->drawPath(fPath[i], paint);
    132             canvas->translate(0, fDY[i]);
    133         }
    134     }
    135 
    136 private:
    137     typedef Sample INHERITED;
    138 };
    139 
    140 //////////////////////////////////////////////////////////////////////////////
    141 
    142 static Sample* MyFactory() { return new PathFillView; }
    143 static SampleRegister reg(MyFactory);
    144