blob: d971db9282b1ff2852f9ea1c7b2cabc9ad2b4f43 [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 virtual uint32_t onGetFlags() const SK_OVERRIDE {
38 return kSkipTiled_Flag;
39 }
40
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000041 virtual SkString onShortName() {
42 return SkString("hittestpath");
43 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000044
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000045 virtual SkISize onISize() { return SkISize::Make(700, 460); }
rmistry@google.comd6176b02012-08-23 18:14:13 +000046
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000047 virtual void onDraw(SkCanvas* canvas) {
48 SkPath path;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000049 SkLCGRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +000050
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000051 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);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000059 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000060
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000061 path.setFillType(SkPath::kEvenOdd_FillType);
reed@google.comdbc5d282012-07-03 12:23:22 +000062 path.offset(SkIntToScalar(20), SkIntToScalar(20));
rmistry@google.comd6176b02012-08-23 18:14:13 +000063
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000064 test_hittest(canvas, path);
65
66 canvas->translate(SkIntToScalar(scale), 0);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000067 path.setFillType(SkPath::kWinding_FillType);
rmistry@google.comd6176b02012-08-23 18:14:13 +000068
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000069 test_hittest(canvas, path);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000070 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000071
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000072private:
73 typedef GM INHERITED;
74};
75
76//////////////////////////////////////////////////////////////////////////////
77
78static skiagm::GM* MyFactory(void*) { return new HitTestPathGM; }
79static skiagm::GMRegistry reg(MyFactory);