mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 1 | /* |
| 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" |
bungeman | 7403d87 | 2015-08-04 14:56:53 -0700 | [diff] [blame^] | 11 | #include "SkPath.h" |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 12 | #include "SkRandom.h" |
| 13 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 14 | static void test_hittest(SkCanvas* canvas, const SkPath& path) { |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 15 | SkPaint paint; |
| 16 | SkRect r = path.getBounds(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 17 | |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 18 | paint.setColor(SK_ColorRED); |
| 19 | canvas->drawPath(path, paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 20 | |
reed@google.com | dbc5d28 | 2012-07-03 12:23:22 +0000 | [diff] [blame] | 21 | const SkScalar MARGIN = SkIntToScalar(4); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 22 | |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 23 | paint.setColor(0x800000FF); |
reed@google.com | dbc5d28 | 2012-07-03 12:23:22 +0000 | [diff] [blame] | 24 | for (SkScalar y = r.fTop + SK_ScalarHalf - MARGIN; y < r.fBottom + MARGIN; y += SK_Scalar1) { |
| 25 | for (SkScalar x = r.fLeft + SK_ScalarHalf - MARGIN; x < r.fRight + MARGIN; x += SK_Scalar1) { |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 26 | if (path.contains(x, y)) { |
| 27 | canvas->drawPoint(x, y, paint); |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 28 | } |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | class HitTestPathGM : public skiagm::GM { |
| 34 | public: |
| 35 | HitTestPathGM () {} |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 36 | |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 37 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 38 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 39 | SkString onShortName() override { |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 40 | return SkString("hittestpath"); |
| 41 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 42 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 43 | SkISize onISize() override { return SkISize::Make(700, 460); } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 44 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 45 | void onDraw(SkCanvas* canvas) override { |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 46 | SkPath path; |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 47 | SkRandom rand; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 48 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 49 | int scale = 300; |
| 50 | for (int i = 0; i < 4; ++i) { |
caryclark | 99a69eb | 2015-06-04 09:27:43 -0700 | [diff] [blame] | 51 | // get the random values deterministically |
| 52 | SkScalar randoms[12]; |
| 53 | for (int index = 0; index < (int) SK_ARRAY_COUNT(randoms); ++index) { |
| 54 | randoms[index] = rand.nextUScalar1(); |
| 55 | } |
| 56 | path.lineTo(randoms[0] * scale, randoms[1] * scale); |
| 57 | path.quadTo(randoms[2] * scale, randoms[3] * scale, |
| 58 | randoms[4] * scale, randoms[5] * scale); |
| 59 | path.cubicTo(randoms[6] * scale, randoms[7] * scale, |
| 60 | randoms[8] * scale, randoms[9] * scale, |
| 61 | randoms[10] * scale, randoms[11] * scale); |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 62 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 63 | |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 64 | path.setFillType(SkPath::kEvenOdd_FillType); |
reed@google.com | dbc5d28 | 2012-07-03 12:23:22 +0000 | [diff] [blame] | 65 | path.offset(SkIntToScalar(20), SkIntToScalar(20)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 66 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 67 | test_hittest(canvas, path); |
| 68 | |
| 69 | canvas->translate(SkIntToScalar(scale), 0); |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 70 | path.setFillType(SkPath::kWinding_FillType); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 71 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 72 | test_hittest(canvas, path); |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 73 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 74 | |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 75 | private: |
| 76 | typedef GM INHERITED; |
| 77 | }; |
| 78 | |
| 79 | ////////////////////////////////////////////////////////////////////////////// |
| 80 | |
| 81 | static skiagm::GM* MyFactory(void*) { return new HitTestPathGM; } |
| 82 | static skiagm::GMRegistry reg(MyFactory); |