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