blob: 6ffc1f9f869d9d00d2c6d1f014157e7a1a5ba9e7 [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:
37 virtual SkString onShortName() {
38 return SkString("hittestpath");
39 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000040
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000041 virtual SkISize onISize() { return SkISize::Make(700, 460); }
rmistry@google.comd6176b02012-08-23 18:14:13 +000042
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000043 virtual void onDraw(SkCanvas* canvas) {
44 SkPath path;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000045 SkLCGRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +000046
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000047 int scale = 300;
48 for (int i = 0; i < 4; ++i) {
49 path.lineTo(rand.nextUScalar1() * scale, rand.nextUScalar1() * scale);
50 path.quadTo(rand.nextUScalar1() * scale, rand.nextUScalar1() * scale,
51 rand.nextUScalar1() * scale, rand.nextUScalar1() * scale);
52 path.cubicTo(rand.nextUScalar1() * scale, rand.nextUScalar1() * scale,
53 rand.nextUScalar1() * scale, rand.nextUScalar1() * scale,
54 rand.nextUScalar1() * scale, rand.nextUScalar1() * scale);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000055 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000056
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000057 path.setFillType(SkPath::kEvenOdd_FillType);
reed@google.comdbc5d282012-07-03 12:23:22 +000058 path.offset(SkIntToScalar(20), SkIntToScalar(20));
rmistry@google.comd6176b02012-08-23 18:14:13 +000059
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000060 test_hittest(canvas, path);
61
62 canvas->translate(SkIntToScalar(scale), 0);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000063 path.setFillType(SkPath::kWinding_FillType);
rmistry@google.comd6176b02012-08-23 18:14:13 +000064
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000065 test_hittest(canvas, path);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000066 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000067
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000068private:
69 typedef GM INHERITED;
70};
71
72//////////////////////////////////////////////////////////////////////////////
73
74static skiagm::GM* MyFactory(void*) { return new HitTestPathGM; }
75static skiagm::GMRegistry reg(MyFactory);