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