blob: f415ba3785dbaee1a0128c5e067e04c366cd6577 [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) {
caryclark99a69eb2015-06-04 09:27:43 -070050 // get the random values deterministically
51 SkScalar randoms[12];
52 for (int index = 0; index < (int) SK_ARRAY_COUNT(randoms); ++index) {
53 randoms[index] = rand.nextUScalar1();
54 }
55 path.lineTo(randoms[0] * scale, randoms[1] * scale);
56 path.quadTo(randoms[2] * scale, randoms[3] * scale,
57 randoms[4] * scale, randoms[5] * scale);
58 path.cubicTo(randoms[6] * scale, randoms[7] * scale,
59 randoms[8] * scale, randoms[9] * scale,
60 randoms[10] * scale, randoms[11] * scale);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000061 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000062
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000063 path.setFillType(SkPath::kEvenOdd_FillType);
reed@google.comdbc5d282012-07-03 12:23:22 +000064 path.offset(SkIntToScalar(20), SkIntToScalar(20));
rmistry@google.comd6176b02012-08-23 18:14:13 +000065
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000066 test_hittest(canvas, path);
67
68 canvas->translate(SkIntToScalar(scale), 0);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000069 path.setFillType(SkPath::kWinding_FillType);
rmistry@google.comd6176b02012-08-23 18:14:13 +000070
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000071 test_hittest(canvas, path);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000072 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000073
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000074private:
75 typedef GM INHERITED;
76};
77
78//////////////////////////////////////////////////////////////////////////////
79
80static skiagm::GM* MyFactory(void*) { return new HitTestPathGM; }
81static skiagm::GMRegistry reg(MyFactory);