blob: 9c7cdc6df4c4a51a6e03f375fd6b5058f362f0b1 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +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 */
reed@google.comad89fbd2011-04-11 13:57:34 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkGraphics.h"
12#include "SkRandom.h"
13#include "SkBlurDrawLooper.h"
14#include "SkGradientShader.h"
15
16typedef SkScalar (*MakePathProc)(SkPath*);
17
18static SkScalar make_frame(SkPath* path) {
19 SkRect r = { 10, 10, 630, 470 };
20 path->addRoundRect(r, 15, 15);
21
22 SkPaint paint;
23 paint.setStyle(SkPaint::kStroke_Style);
24 paint.setStrokeWidth(5);
25 paint.getFillPath(*path, path);
26 return 15;
27}
28
29static SkScalar make_triangle(SkPath* path) {
30 static const int gCoord[] = {
31 10, 20, 15, 5, 30, 30
32 };
33 path->moveTo(SkIntToScalar(gCoord[0]), SkIntToScalar(gCoord[1]));
34 path->lineTo(SkIntToScalar(gCoord[2]), SkIntToScalar(gCoord[3]));
35 path->lineTo(SkIntToScalar(gCoord[4]), SkIntToScalar(gCoord[5]));
36 path->close();
37 path->offset(10, 0);
38 return SkIntToScalar(30);
39}
40
41static SkScalar make_rect(SkPath* path) {
42 SkRect r = { 10, 10, 30, 30 };
43 path->addRect(r);
44 path->offset(10, 0);
45 return SkIntToScalar(30);
46}
47
48static SkScalar make_oval(SkPath* path) {
49 SkRect r = { 10, 10, 30, 30 };
50 path->addOval(r);
51 path->offset(10, 0);
52 return SkIntToScalar(30);
53}
54
55static SkScalar make_sawtooth(SkPath* path) {
56 SkScalar x = SkIntToScalar(20);
57 SkScalar y = SkIntToScalar(20);
58 const SkScalar x0 = x;
59 const SkScalar dx = SK_Scalar1 * 5;
60 const SkScalar dy = SK_Scalar1 * 10;
rmistry@google.comae933ce2012-08-23 18:19:56 +000061
reed@google.comad89fbd2011-04-11 13:57:34 +000062 path->moveTo(x, y);
63 for (int i = 0; i < 32; i++) {
64 x += dx;
65 path->lineTo(x, y - dy);
66 x += dx;
67 path->lineTo(x, y + dy);
68 }
69 path->lineTo(x, y + 2 * dy);
70 path->lineTo(x0, y + 2 * dy);
71 path->close();
72 return SkIntToScalar(30);
73}
74
75static SkScalar make_star(SkPath* path, int n) {
76 const SkScalar c = SkIntToScalar(45);
77 const SkScalar r = SkIntToScalar(20);
78
79 SkScalar rad = -SK_ScalarPI / 2;
80 const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
81
82 path->moveTo(c, c - r);
83 for (int i = 1; i < n; i++) {
84 rad += drad;
85 SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV);
86 path->lineTo(c + SkScalarMul(cosV, r), c + SkScalarMul(sinV, r));
87 }
88 path->close();
89 return r * 2 * 6 / 5;
90}
91
92static SkScalar make_star_5(SkPath* path) { return make_star(path, 5); }
93static SkScalar make_star_13(SkPath* path) { return make_star(path, 13); }
94
95static const MakePathProc gProcs[] = {
96 make_frame,
97 make_triangle,
98 make_rect,
99 make_oval,
100 make_sawtooth,
101 make_star_5,
102 make_star_13
103};
104
105#define N SK_ARRAY_COUNT(gProcs)
106
reed@google.com03c2ef52011-05-25 20:13:06 +0000107class PathFillView : public SampleView {
reed@google.comad89fbd2011-04-11 13:57:34 +0000108 SkPath fPath[N];
109 SkScalar fDY[N];
110
111public:
112 PathFillView() {
113 for (size_t i = 0; i < N; i++) {
114 fDY[i] = gProcs[i](&fPath[i]);
115 }
reed@google.com03c2ef52011-05-25 20:13:06 +0000116 this->setBGColor(0xFFDDDDDD);
reed@google.comad89fbd2011-04-11 13:57:34 +0000117 }
118
119protected:
120 // overrides from SkEventSink
121 virtual bool onQuery(SkEvent* evt) {
122 if (SampleCode::TitleQ(*evt)) {
123 SampleCode::TitleR(evt, "PathFill");
124 return true;
125 }
126 return this->INHERITED::onQuery(evt);
127 }
128
reed@google.com03c2ef52011-05-25 20:13:06 +0000129 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.comad89fbd2011-04-11 13:57:34 +0000130 SkPaint paint;
131 paint.setAntiAlias(true);
132
133 for (size_t i = 0; i < N; i++) {
134 canvas->drawPath(fPath[i], paint);
135 canvas->translate(0, fDY[i]);
136 }
137 }
138
139private:
reed@google.com03c2ef52011-05-25 20:13:06 +0000140 typedef SampleView INHERITED;
reed@google.comad89fbd2011-04-11 13:57:34 +0000141};
142
143//////////////////////////////////////////////////////////////////////////////
144
145static SkView* MyFactory() { return new PathFillView; }
146static SkViewRegister reg(MyFactory);
147