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 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 13 | static void test_hittest(SkCanvas* canvas, const SkPath& path) { |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 14 | SkPaint paint; |
| 15 | SkRect r = path.getBounds(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 16 | |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 17 | paint.setColor(SK_ColorRED); |
| 18 | canvas->drawPath(path, paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 19 | |
reed@google.com | dbc5d28 | 2012-07-03 12:23:22 +0000 | [diff] [blame] | 20 | const SkScalar MARGIN = SkIntToScalar(4); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 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 | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 25 | if (path.contains(x, y)) { |
| 26 | canvas->drawPoint(x, y, paint); |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 27 | } |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | class HitTestPathGM : public skiagm::GM { |
| 33 | public: |
| 34 | HitTestPathGM () {} |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 35 | |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 36 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 37 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 38 | return kSkipTiled_Flag; |
| 39 | } |
| 40 | |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 41 | virtual SkString onShortName() { |
| 42 | return SkString("hittestpath"); |
| 43 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 44 | |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 45 | virtual SkISize onISize() { return SkISize::Make(700, 460); } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 46 | |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 47 | virtual void onDraw(SkCanvas* canvas) { |
| 48 | SkPath path; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 49 | SkLCGRandom rand; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 50 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 51 | 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.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 59 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 60 | |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 61 | path.setFillType(SkPath::kEvenOdd_FillType); |
reed@google.com | dbc5d28 | 2012-07-03 12:23:22 +0000 | [diff] [blame] | 62 | path.offset(SkIntToScalar(20), SkIntToScalar(20)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 63 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 64 | test_hittest(canvas, path); |
| 65 | |
| 66 | canvas->translate(SkIntToScalar(scale), 0); |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 67 | path.setFillType(SkPath::kWinding_FillType); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 68 | |
mike@reedtribe.org | bad1b2f | 2012-07-11 01:51:33 +0000 | [diff] [blame] | 69 | test_hittest(canvas, path); |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 70 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 71 | |
mike@reedtribe.org | 43c62b1 | 2012-07-03 02:44:02 +0000 | [diff] [blame] | 72 | private: |
| 73 | typedef GM INHERITED; |
| 74 | }; |
| 75 | |
| 76 | ////////////////////////////////////////////////////////////////////////////// |
| 77 | |
| 78 | static skiagm::GM* MyFactory(void*) { return new HitTestPathGM; } |
| 79 | static skiagm::GMRegistry reg(MyFactory); |