Home | History | Annotate | Download | only in samplecode
      1 
      2 /*
      3  * Copyright 2011 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 #include "SampleCode.h"
      9 #include "SkView.h"
     10 #include "SkCanvas.h"
     11 #include "SkPaint.h"
     12 #include "SkRandom.h"
     13 #include "SkString.h"
     14 
     15 class EmptyPathView : public SampleView {
     16 public:
     17     EmptyPathView() {}
     18 
     19 protected:
     20     // overrides from SkEventSink
     21     virtual bool onQuery(SkEvent* evt) {
     22         if (SampleCode::TitleQ(*evt)) {
     23             SampleCode::TitleR(evt, "EmptyPath");
     24             return true;
     25         }
     26         return this->INHERITED::onQuery(evt);
     27     }
     28 
     29     void drawEmpty(SkCanvas* canvas,
     30                    SkColor color,
     31                    const SkRect& clip,
     32                    SkPaint::Style style,
     33                    SkPath::FillType fill) {
     34         SkPath path;
     35         path.setFillType(fill);
     36         SkPaint paint;
     37         paint.setColor(color);
     38         paint.setStyle(style);
     39         canvas->save();
     40         canvas->clipRect(clip);
     41         canvas->drawPath(path, paint);
     42         canvas->restore();
     43     }
     44 
     45     virtual void onDrawContent(SkCanvas* canvas) {
     46         struct FillAndName {
     47             SkPath::FillType fFill;
     48             const char*      fName;
     49         };
     50         static const FillAndName gFills[] = {
     51             {SkPath::kWinding_FillType, "Winding"},
     52             {SkPath::kEvenOdd_FillType, "Even / Odd"},
     53             {SkPath::kInverseWinding_FillType, "Inverse Winding"},
     54             {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
     55         };
     56         struct StyleAndName {
     57             SkPaint::Style fStyle;
     58             const char*    fName;
     59         };
     60         static const StyleAndName gStyles[] = {
     61             {SkPaint::kFill_Style, "Fill"},
     62             {SkPaint::kStroke_Style, "Stroke"},
     63             {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
     64         };
     65 
     66         SkPaint titlePaint;
     67         titlePaint.setColor(SK_ColorBLACK);
     68         titlePaint.setAntiAlias(true);
     69         titlePaint.setLCDRenderText(true);
     70         titlePaint.setTextSize(24 * SK_Scalar1);
     71         const char title[] = "Empty Paths Drawn Into Rectangle Clips With Indicated Style and Fill";
     72         canvas->drawText(title, strlen(title),
     73                          40 * SK_Scalar1,
     74                          100*SK_Scalar1,
     75                          titlePaint);
     76 
     77         SkRandom rand;
     78         SkRect rect = SkRect::MakeWH(125*SK_Scalar1, 100*SK_Scalar1);
     79         int i = 0;
     80         canvas->save();
     81         canvas->translate(80 * SK_Scalar1, 0);
     82         canvas->save();
     83         for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
     84             for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
     85                 if (0 == i % 4) {
     86                     canvas->restore();
     87                     canvas->translate(0, rect.height() + 50 * SK_Scalar1);
     88                     canvas->save();
     89                 } else {
     90                     canvas->translate(rect.width() + 100 * SK_Scalar1, 0);
     91                 }
     92                 ++i;
     93 
     94 
     95                 SkColor color = rand.nextU();
     96                 color = 0xff000000| color; // force solid
     97                 this->drawEmpty(canvas, color, rect,
     98                                 gStyles[style].fStyle, gFills[fill].fFill);
     99 
    100                 SkPaint rectPaint;
    101                 rectPaint.setColor(SK_ColorBLACK);
    102                 rectPaint.setStyle(SkPaint::kStroke_Style);
    103                 rectPaint.setStrokeWidth(-1);
    104                 rectPaint.setAntiAlias(true);
    105                 canvas->drawRect(rect, rectPaint);
    106 
    107                 SkString label;
    108                 label.appendf("%s, %s", gStyles[style].fName, gFills[fill].fName);
    109 
    110                 SkPaint labelPaint;
    111                 labelPaint.setColor(color);
    112                 labelPaint.setAntiAlias(true);
    113                 labelPaint.setLCDRenderText(true);
    114                 canvas->drawText(label.c_str(), label.size(),
    115                                  0, rect.height() + 15 * SK_Scalar1,
    116                                  labelPaint);
    117             }
    118         }
    119         canvas->restore();
    120         canvas->restore();
    121     }
    122 
    123 private:
    124     typedef SampleView INHERITED;
    125 };
    126 
    127 //////////////////////////////////////////////////////////////////////////////
    128 
    129 static SkView* MyFactory() { return new EmptyPathView; }
    130 static SkViewRegister reg(MyFactory);
    131