blob: 42e367155150a1fb89c09a6301dee310113e95dc [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
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000013static void test_hittest(SkCanvas* canvas, const SkPath& path) {
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000014 SkPaint paint;
15 SkRect r = path.getBounds();
rmistry@google.comd6176b02012-08-23 18:14:13 +000016
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000017 paint.setColor(SK_ColorRED);
18 canvas->drawPath(path, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +000019
reed@google.comdbc5d282012-07-03 12:23:22 +000020 const SkScalar MARGIN = SkIntToScalar(4);
rmistry@google.comd6176b02012-08-23 18:14:13 +000021
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.orgbad1b2f2012-07-11 01:51:33 +000025 if (path.contains(x, y)) {
26 canvas->drawPoint(x, y, paint);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000027 }
28 }
29 }
30}
31
32class HitTestPathGM : public skiagm::GM {
33public:
34 HitTestPathGM () {}
rmistry@google.comd6176b02012-08-23 18:14:13 +000035
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000036protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000037
mtklein36352bf2015-03-25 18:17:31 -070038 SkString onShortName() override {
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000039 return SkString("hittestpath");
40 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000041
mtklein36352bf2015-03-25 18:17:31 -070042 SkISize onISize() override { return SkISize::Make(700, 460); }
rmistry@google.comd6176b02012-08-23 18:14:13 +000043
mtklein36352bf2015-03-25 18:17:31 -070044 void onDraw(SkCanvas* canvas) override {
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000045 SkPath path;
scroggof9d61012014-12-15 12:54:51 -080046 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +000047
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000048 int scale = 300;
49 for (int i = 0; i < 4; ++i) {
50 path.lineTo(rand.nextUScalar1() * scale, rand.nextUScalar1() * scale);
51 path.quadTo(rand.nextUScalar1() * scale, rand.nextUScalar1() * scale,
52 rand.nextUScalar1() * scale, rand.nextUScalar1() * scale);
53 path.cubicTo(rand.nextUScalar1() * scale, rand.nextUScalar1() * scale,
54 rand.nextUScalar1() * scale, rand.nextUScalar1() * scale,
55 rand.nextUScalar1() * scale, rand.nextUScalar1() * scale);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000056 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000057
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000058 path.setFillType(SkPath::kEvenOdd_FillType);
reed@google.comdbc5d282012-07-03 12:23:22 +000059 path.offset(SkIntToScalar(20), SkIntToScalar(20));
rmistry@google.comd6176b02012-08-23 18:14:13 +000060
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000061 test_hittest(canvas, path);
62
63 canvas->translate(SkIntToScalar(scale), 0);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000064 path.setFillType(SkPath::kWinding_FillType);
rmistry@google.comd6176b02012-08-23 18:14:13 +000065
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000066 test_hittest(canvas, path);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000067 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000068
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000069private:
70 typedef GM INHERITED;
71};
72
73//////////////////////////////////////////////////////////////////////////////
74
75static skiagm::GM* MyFactory(void*) { return new HitTestPathGM; }
76static skiagm::GMRegistry reg(MyFactory);