blob: 06f89e4dd241ff38bc5a6a49cf7fa1567aeb8966 [file] [log] [blame]
mike@reedtribe.org43c62b12012-07-03 02:44:02 +00001/*
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
reed@google.comdbc5d282012-07-03 12:23:22 +000020 const SkScalar MARGIN = SkIntToScalar(4);
21
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000022 paint.setColor(0x800000FF);
reed@google.comdbc5d282012-07-03 12:23:22 +000023 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) {
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000025 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);
reed@google.comdbc5d282012-07-03 12:23:22 +000060 path.offset(SkIntToScalar(20), SkIntToScalar(20));
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000061
62 test_hittest(canvas, path, false);
reed@google.comdbc5d282012-07-03 12:23:22 +000063 canvas->translate(SkIntToScalar(200), 0);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000064 test_hittest(canvas, path, true);
65
reed@google.comdbc5d282012-07-03 12:23:22 +000066 canvas->translate(-SkIntToScalar(200), SkIntToScalar(200));
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000067 path.setFillType(SkPath::kWinding_FillType);
68
69 test_hittest(canvas, path, false);
reed@google.comdbc5d282012-07-03 12:23:22 +000070 canvas->translate(SkIntToScalar(200), 0);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000071 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