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