hittestpath.cpp revision dbc5d28979bef1c92aa79e0dc1b5d27a41de5c9a
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 "gm.h"
9#include "SkCanvas.h"
10#include "SkCullPoints.h"
11#include "SkRandom.h"
12
13static void test_hittest(SkCanvas* canvas, const SkPath& path, bool hires) {
14    SkPaint paint;
15    SkRect r = path.getBounds();
16
17    paint.setColor(SK_ColorRED);
18    canvas->drawPath(path, paint);
19
20    const SkScalar MARGIN = SkIntToScalar(4);
21
22    paint.setColor(0x800000FF);
23    for (SkScalar y = r.fTop + SK_ScalarHalf - MARGIN; y < r.fBottom + MARGIN; y += SK_Scalar1) {
24        for (SkScalar x = r.fLeft + SK_ScalarHalf - MARGIN; x < r.fRight + MARGIN; x += SK_Scalar1) {
25            if (hires) {
26                if (SkHitTestPathEx(path, x, y)) {
27                    canvas->drawPoint(x, y, paint);
28                }
29            } else {
30                if (SkHitTestPath(path, x, y, false)) {
31                    canvas->drawPoint(x, y, paint);
32                }
33            }
34        }
35    }
36}
37
38class HitTestPathGM : public skiagm::GM {
39public:
40    HitTestPathGM () {}
41
42protected:
43    virtual SkString onShortName() {
44        return SkString("hittestpath");
45    }
46
47    virtual SkISize onISize() { return SkISize::Make(700, 460); }
48
49    virtual void onDraw(SkCanvas* canvas) {
50        SkPath path;
51        SkRandom rand;
52
53        for (int i = 0; i < 5; ++i) {
54            path.lineTo(rand.nextUScalar1() * 150, rand.nextUScalar1() * 150);
55            path.quadTo(rand.nextUScalar1() * 150, rand.nextUScalar1() * 150,
56                        rand.nextUScalar1() * 150, rand.nextUScalar1() * 150);
57        }
58
59        path.setFillType(SkPath::kEvenOdd_FillType);
60        path.offset(SkIntToScalar(20), SkIntToScalar(20));
61
62        test_hittest(canvas, path, false);
63        canvas->translate(SkIntToScalar(200), 0);
64        test_hittest(canvas, path, true);
65
66        canvas->translate(-SkIntToScalar(200), SkIntToScalar(200));
67        path.setFillType(SkPath::kWinding_FillType);
68
69        test_hittest(canvas, path, false);
70        canvas->translate(SkIntToScalar(200), 0);
71        test_hittest(canvas, path, true);
72    }
73
74private:
75    typedef GM INHERITED;
76};
77
78//////////////////////////////////////////////////////////////////////////////
79
80static skiagm::GM* MyFactory(void*) { return new HitTestPathGM; }
81static skiagm::GMRegistry reg(MyFactory);
82
83
84