blob: 990daaad0116ae3ea2eed6616e70f3f6ee9da3ea [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04007#include "Sample.h"
reed@google.comad89fbd2011-04-11 13:57:34 +00008#include "SkCanvas.h"
9#include "SkGraphics.h"
10#include "SkRandom.h"
11#include "SkBlurDrawLooper.h"
12#include "SkGradientShader.h"
13
14typedef SkScalar (*MakePathProc)(SkPath*);
15
16static SkScalar make_frame(SkPath* path) {
17 SkRect r = { 10, 10, 630, 470 };
18 path->addRoundRect(r, 15, 15);
19
20 SkPaint paint;
21 paint.setStyle(SkPaint::kStroke_Style);
22 paint.setStrokeWidth(5);
23 paint.getFillPath(*path, path);
24 return 15;
25}
26
27static SkScalar make_triangle(SkPath* path) {
28 static const int gCoord[] = {
29 10, 20, 15, 5, 30, 30
30 };
31 path->moveTo(SkIntToScalar(gCoord[0]), SkIntToScalar(gCoord[1]));
32 path->lineTo(SkIntToScalar(gCoord[2]), SkIntToScalar(gCoord[3]));
33 path->lineTo(SkIntToScalar(gCoord[4]), SkIntToScalar(gCoord[5]));
34 path->close();
35 path->offset(10, 0);
36 return SkIntToScalar(30);
37}
38
39static SkScalar make_rect(SkPath* path) {
40 SkRect r = { 10, 10, 30, 30 };
41 path->addRect(r);
42 path->offset(10, 0);
43 return SkIntToScalar(30);
44}
45
46static SkScalar make_oval(SkPath* path) {
47 SkRect r = { 10, 10, 30, 30 };
48 path->addOval(r);
49 path->offset(10, 0);
50 return SkIntToScalar(30);
51}
52
53static SkScalar make_sawtooth(SkPath* path) {
54 SkScalar x = SkIntToScalar(20);
55 SkScalar y = SkIntToScalar(20);
56 const SkScalar x0 = x;
57 const SkScalar dx = SK_Scalar1 * 5;
58 const SkScalar dy = SK_Scalar1 * 10;
rmistry@google.comae933ce2012-08-23 18:19:56 +000059
reed@google.comad89fbd2011-04-11 13:57:34 +000060 path->moveTo(x, y);
61 for (int i = 0; i < 32; i++) {
62 x += dx;
63 path->lineTo(x, y - dy);
64 x += dx;
65 path->lineTo(x, y + dy);
66 }
67 path->lineTo(x, y + 2 * dy);
68 path->lineTo(x0, y + 2 * dy);
69 path->close();
70 return SkIntToScalar(30);
71}
72
73static SkScalar make_star(SkPath* path, int n) {
74 const SkScalar c = SkIntToScalar(45);
75 const SkScalar r = SkIntToScalar(20);
76
77 SkScalar rad = -SK_ScalarPI / 2;
78 const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
79
80 path->moveTo(c, c - r);
81 for (int i = 1; i < n; i++) {
82 rad += drad;
83 SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV);
Mike Reeddf85c382017-02-14 10:59:19 -050084 path->lineTo(c + cosV * r, c + sinV * r);
reed@google.comad89fbd2011-04-11 13:57:34 +000085 }
86 path->close();
87 return r * 2 * 6 / 5;
88}
89
90static SkScalar make_star_5(SkPath* path) { return make_star(path, 5); }
91static SkScalar make_star_13(SkPath* path) { return make_star(path, 13); }
92
93static const MakePathProc gProcs[] = {
94 make_frame,
95 make_triangle,
96 make_rect,
97 make_oval,
98 make_sawtooth,
99 make_star_5,
100 make_star_13
101};
102
103#define N SK_ARRAY_COUNT(gProcs)
104
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400105class PathFillView : public Sample {
reed@google.comad89fbd2011-04-11 13:57:34 +0000106 SkPath fPath[N];
107 SkScalar fDY[N];
108
109public:
110 PathFillView() {
111 for (size_t i = 0; i < N; i++) {
112 fDY[i] = gProcs[i](&fPath[i]);
113 }
reed@google.com03c2ef52011-05-25 20:13:06 +0000114 this->setBGColor(0xFFDDDDDD);
reed@google.comad89fbd2011-04-11 13:57:34 +0000115 }
116
117protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400118 virtual bool onQuery(Sample::Event* evt) {
119 if (Sample::TitleQ(*evt)) {
120 Sample::TitleR(evt, "PathFill");
reed@google.comad89fbd2011-04-11 13:57:34 +0000121 return true;
122 }
123 return this->INHERITED::onQuery(evt);
124 }
125
reed@google.com03c2ef52011-05-25 20:13:06 +0000126 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.comad89fbd2011-04-11 13:57:34 +0000127 SkPaint paint;
128 paint.setAntiAlias(true);
129
130 for (size_t i = 0; i < N; i++) {
131 canvas->drawPath(fPath[i], paint);
132 canvas->translate(0, fDY[i]);
133 }
134 }
135
136private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400137 typedef Sample INHERITED;
reed@google.comad89fbd2011-04-11 13:57:34 +0000138};
139
140//////////////////////////////////////////////////////////////////////////////
141
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400142static Sample* MyFactory() { return new PathFillView; }
143static SampleRegister reg(MyFactory);