SampleConcavePaths.cpp revision 9d524f22bfde5dc3dc8f48e1be39bdebd3bb0304
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
8#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkGradientShader.h"
12#include "SkGraphics.h"
13#include "SkPath.h"
14#include "SkRegion.h"
15#include "SkShader.h"
16#include "SkUtils.h"
17#include "SkXfermode.h"
18#include "SkColorPriv.h"
19#include "SkColorFilter.h"
20#include "SkParsePath.h"
21#include "SkTime.h"
22#include "SkTypeface.h"
23
24#include "SkGeometry.h"
25
26class ConcavePathView : public SampleView {
27public:
28    ConcavePathView() {}
29
30protected:
31    // overrides from SkEventSink
32    virtual bool onQuery(SkEvent* evt) {
33        if (SampleCode::TitleQ(*evt)) {
34            SampleCode::TitleR(evt, "ConcavePaths");
35            return true;
36        }
37        return this->INHERITED::onQuery(evt);
38    }
39
40    virtual void onDrawContent(SkCanvas* canvas) {
41        SkPaint paint;
42
43        paint.setAntiAlias(true);
44        paint.setStyle(SkPaint::kFill_Style);
45
46        // Concave test
47        if (1) {
48            SkPath path;
49            canvas->translate(0, 0);
50            path.moveTo(SkIntToScalar(20), SkIntToScalar(20));
51            path.lineTo(SkIntToScalar(80), SkIntToScalar(20));
52            path.lineTo(SkIntToScalar(30), SkIntToScalar(30));
53            path.lineTo(SkIntToScalar(20), SkIntToScalar(80));
54            canvas->drawPath(path, paint);
55        }
56        // Reverse concave test
57        if (1) {
58            SkPath path;
59            canvas->save();
60            canvas->translate(100, 0);
61            path.moveTo(SkIntToScalar(20), SkIntToScalar(20));
62            path.lineTo(SkIntToScalar(20), SkIntToScalar(80));
63            path.lineTo(SkIntToScalar(30), SkIntToScalar(30));
64            path.lineTo(SkIntToScalar(80), SkIntToScalar(20));
65            canvas->drawPath(path, paint);
66            canvas->restore();
67        }
68        // Bowtie (intersection)
69        if (1) {
70            SkPath path;
71            canvas->save();
72            canvas->translate(200, 0);
73            path.moveTo(SkIntToScalar(20), SkIntToScalar(20));
74            path.lineTo(SkIntToScalar(80), SkIntToScalar(80));
75            path.lineTo(SkIntToScalar(80), SkIntToScalar(20));
76            path.lineTo(SkIntToScalar(20), SkIntToScalar(80));
77            canvas->drawPath(path, paint);
78            canvas->restore();
79        }
80        // "fake" bowtie (concave, but no intersection)
81        if (1) {
82            SkPath path;
83            canvas->save();
84            canvas->translate(300, 0);
85            path.moveTo(SkIntToScalar(20), SkIntToScalar(20));
86            path.lineTo(SkIntToScalar(50), SkIntToScalar(40));
87            path.lineTo(SkIntToScalar(80), SkIntToScalar(20));
88            path.lineTo(SkIntToScalar(80), SkIntToScalar(80));
89            path.lineTo(SkIntToScalar(50), SkIntToScalar(60));
90            path.lineTo(SkIntToScalar(20), SkIntToScalar(80));
91            canvas->drawPath(path, paint);
92            canvas->restore();
93        }
94        // Fish test (intersection/concave)
95        if (1) {
96            SkPath path;
97            canvas->save();
98            canvas->translate(0, 100);
99            path.moveTo(SkIntToScalar(20), SkIntToScalar(20));
100            path.lineTo(SkIntToScalar(80), SkIntToScalar(80));
101            path.lineTo(SkIntToScalar(70), SkIntToScalar(50));
102            path.lineTo(SkIntToScalar(80), SkIntToScalar(20));
103            path.lineTo(SkIntToScalar(20), SkIntToScalar(80));
104            path.lineTo(SkIntToScalar(0), SkIntToScalar(50));
105            canvas->drawPath(path, paint);
106            canvas->restore();
107        }
108        // Collinear test
109        if (1) {
110            SkPath path;
111            canvas->save();
112            canvas->translate(100, 100);
113            path.moveTo(SkIntToScalar(20), SkIntToScalar(20));
114            path.lineTo(SkIntToScalar(50), SkIntToScalar(20));
115            path.lineTo(SkIntToScalar(80), SkIntToScalar(20));
116            path.lineTo(SkIntToScalar(50), SkIntToScalar(80));
117            canvas->drawPath(path, paint);
118            canvas->restore();
119        }
120        // Hole test
121        if (1) {
122            SkPath path;
123            canvas->save();
124            canvas->translate(200, 100);
125            path.moveTo(SkIntToScalar(20), SkIntToScalar(20));
126            path.lineTo(SkIntToScalar(80), SkIntToScalar(20));
127            path.lineTo(SkIntToScalar(80), SkIntToScalar(80));
128            path.lineTo(SkIntToScalar(20), SkIntToScalar(80));
129            path.moveTo(SkIntToScalar(30), SkIntToScalar(30));
130            path.lineTo(SkIntToScalar(30), SkIntToScalar(70));
131            path.lineTo(SkIntToScalar(70), SkIntToScalar(70));
132            path.lineTo(SkIntToScalar(70), SkIntToScalar(30));
133            canvas->drawPath(path, paint);
134            canvas->restore();
135        }
136    }
137
138    virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
139                                              unsigned modi) {
140        this->inval(nullptr);
141        return this->INHERITED::onFindClickHandler(x, y, modi);
142    }
143
144private:
145    typedef SampleView INHERITED;
146};
147
148//////////////////////////////////////////////////////////////////////////////
149
150static SkView* MyFactory() { return new ConcavePathView; }
151static SkViewRegister reg(MyFactory);
152