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