blob: 53e219985328028c9d64876664f9313b2efcbb6c [file] [log] [blame]
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001
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"
bungeman@google.comfab44db2013-10-11 18:50:45 +000013#include "SkString.h"
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000014
15class EmptyPathView : public SampleView {
16public:
17 EmptyPathView() {}
18
19protected:
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 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000028
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000029 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 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000044
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000045 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
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000077 SkRandom rand;
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000078 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();
bsalomon@google.comf12449b2011-10-10 14:03:33 +000083 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
84 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000085 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
bungeman@google.comfab44db2013-10-11 18:50:45 +0000107 SkString label;
108 label.appendf("%s, %s", gStyles[style].fName, gFills[fill].fName);
109
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000110 SkPaint labelPaint;
111 labelPaint.setColor(color);
112 labelPaint.setAntiAlias(true);
113 labelPaint.setLCDRenderText(true);
bungeman@google.comfab44db2013-10-11 18:50:45 +0000114 canvas->drawText(label.c_str(), label.size(),
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000115 0, rect.height() + 15 * SK_Scalar1,
116 labelPaint);
117 }
118 }
119 canvas->restore();
120 canvas->restore();
121 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000122
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000123private:
124 typedef SampleView INHERITED;
125};
126
127//////////////////////////////////////////////////////////////////////////////
128
129static SkView* MyFactory() { return new EmptyPathView; }
130static SkViewRegister reg(MyFactory);