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) {
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 (path.contains(x, y)) {
26                canvas->drawPoint(x, y, paint);
27            }
28        }
29    }
30}
31
32class HitTestPathGM : public skiagm::GM {
33public:
34    HitTestPathGM () {}
35
36protected:
37    virtual uint32_t onGetFlags() const SK_OVERRIDE {
38        return kSkipTiled_Flag;
39    }
40
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        SkLCGRandom rand;
50
51        int scale = 300;
52        for (int i = 0; i < 4; ++i) {
53            path.lineTo(rand.nextUScalar1() * scale, rand.nextUScalar1() * scale);
54            path.quadTo(rand.nextUScalar1() * scale, rand.nextUScalar1() * scale,
55                        rand.nextUScalar1() * scale, rand.nextUScalar1() * scale);
56            path.cubicTo(rand.nextUScalar1() * scale, rand.nextUScalar1() * scale,
57                         rand.nextUScalar1() * scale, rand.nextUScalar1() * scale,
58                         rand.nextUScalar1() * scale, rand.nextUScalar1() * scale);
59        }
60
61        path.setFillType(SkPath::kEvenOdd_FillType);
62        path.offset(SkIntToScalar(20), SkIntToScalar(20));
63
64        test_hittest(canvas, path);
65
66        canvas->translate(SkIntToScalar(scale), 0);
67        path.setFillType(SkPath::kWinding_FillType);
68
69        test_hittest(canvas, path);
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