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" |
| 11 | #include "SkRandom.h" |
| 12 | |
| 13 | static void test_hittest(SkCanvas* canvas, const SkPath& path, bool hires) { |
| 14 | SkPaint paint; |
| 15 | SkRect r = path.getBounds(); |
| 16 | |
| 17 | paint.setColor(SK_ColorRED); |
| 18 | canvas->drawPath(path, paint); |
| 19 | |
reed@google.com | dbc5d28 | 2012-07-03 12:23:22 +0000 | [diff] [blame^] | 20 | const SkScalar MARGIN = SkIntToScalar(4); |
| 21 | |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 22 | paint.setColor(0x800000FF); |
reed@google.com | dbc5d28 | 2012-07-03 12:23:22 +0000 | [diff] [blame^] | 23 | 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.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 25 | if (hires) { |
| 26 | if (SkHitTestPathEx(path, x, y)) { |
| 27 | canvas->drawPoint(x, y, paint); |
| 28 | } |
| 29 | } else { |
| 30 | if (SkHitTestPath(path, x, y, false)) { |
| 31 | canvas->drawPoint(x, y, paint); |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | class HitTestPathGM : public skiagm::GM { |
| 39 | public: |
| 40 | HitTestPathGM () {} |
| 41 | |
| 42 | protected: |
| 43 | virtual SkString onShortName() { |
| 44 | return SkString("hittestpath"); |
| 45 | } |
| 46 | |
| 47 | virtual SkISize onISize() { return SkISize::Make(700, 460); } |
| 48 | |
| 49 | virtual void onDraw(SkCanvas* canvas) { |
| 50 | SkPath path; |
| 51 | SkRandom rand; |
| 52 | |
| 53 | for (int i = 0; i < 5; ++i) { |
| 54 | path.lineTo(rand.nextUScalar1() * 150, rand.nextUScalar1() * 150); |
| 55 | path.quadTo(rand.nextUScalar1() * 150, rand.nextUScalar1() * 150, |
| 56 | rand.nextUScalar1() * 150, rand.nextUScalar1() * 150); |
| 57 | } |
| 58 | |
| 59 | path.setFillType(SkPath::kEvenOdd_FillType); |
reed@google.com | dbc5d28 | 2012-07-03 12:23:22 +0000 | [diff] [blame^] | 60 | path.offset(SkIntToScalar(20), SkIntToScalar(20)); |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 61 | |
| 62 | test_hittest(canvas, path, false); |
reed@google.com | dbc5d28 | 2012-07-03 12:23:22 +0000 | [diff] [blame^] | 63 | canvas->translate(SkIntToScalar(200), 0); |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 64 | test_hittest(canvas, path, true); |
| 65 | |
reed@google.com | dbc5d28 | 2012-07-03 12:23:22 +0000 | [diff] [blame^] | 66 | canvas->translate(-SkIntToScalar(200), SkIntToScalar(200)); |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 67 | path.setFillType(SkPath::kWinding_FillType); |
| 68 | |
| 69 | test_hittest(canvas, path, false); |
reed@google.com | dbc5d28 | 2012-07-03 12:23:22 +0000 | [diff] [blame^] | 70 | canvas->translate(SkIntToScalar(200), 0); |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 71 | test_hittest(canvas, path, true); |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | typedef GM INHERITED; |
| 76 | }; |
| 77 | |
| 78 | ////////////////////////////////////////////////////////////////////////////// |
| 79 | |
| 80 | static skiagm::GM* MyFactory(void*) { return new HitTestPathGM; } |
| 81 | static skiagm::GMRegistry reg(MyFactory); |
| 82 | |
| 83 | |