epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +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 | */ |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 7 | #include "SampleCode.h" |
| 8 | #include "SkView.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkShader.h" |
| 11 | #include "SkKey.h" |
| 12 | |
| 13 | static void make_bitmap(SkBitmap* bm) { |
| 14 | const int W = 100; |
| 15 | const int H = 100; |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 16 | bm->allocN32Pixels(W, H); |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 17 | |
| 18 | SkPaint paint; |
| 19 | SkCanvas canvas(*bm); |
| 20 | canvas.drawColor(SK_ColorWHITE); |
| 21 | |
| 22 | const SkColor colors[] = { |
| 23 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE |
| 24 | }; |
| 25 | |
| 26 | for (int ix = 0; ix < W; ix += 1) { |
| 27 | SkScalar x = SkIntToScalar(ix) + SK_ScalarHalf; |
| 28 | paint.setColor(colors[ix & 3]); |
| 29 | canvas.drawLine(x, 0, x, SkIntToScalar(H - 1), paint); |
| 30 | } |
| 31 | paint.setColor(SK_ColorGRAY); |
| 32 | canvas.drawLine(0, 0, SkIntToScalar(W), 0, paint); |
| 33 | } |
| 34 | |
| 35 | static void make_paint(SkPaint* paint, SkShader::TileMode tm) { |
| 36 | SkBitmap bm; |
| 37 | make_bitmap(&bm); |
| 38 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 39 | paint->setShader(SkShader::MakeBitmapShader(bm, tm, tm)); |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 40 | } |
| 41 | |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 42 | class RepeatTileView : public SampleView { |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 43 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 44 | RepeatTileView() { |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 45 | this->setBGColor(SK_ColorGRAY); |
| 46 | } |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 47 | |
| 48 | protected: |
| 49 | // overrides from SkEventSink |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 50 | bool onQuery(SkEvent* evt) override { |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 51 | if (SampleCode::TitleQ(*evt)) { |
| 52 | SampleCode::TitleR(evt, "RepeatTile"); |
| 53 | return true; |
| 54 | } |
| 55 | return this->INHERITED::onQuery(evt); |
| 56 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 57 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 58 | void onDrawContent(SkCanvas* canvas) override { |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 59 | SkPaint paint; |
| 60 | make_paint(&paint, SkShader::kRepeat_TileMode); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 61 | |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 62 | // canvas->scale(SK_Scalar1*2, SK_Scalar1); |
| 63 | canvas->translate(SkIntToScalar(100), SkIntToScalar(100)); |
| 64 | canvas->drawPaint(paint); |
| 65 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 66 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 67 | SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 68 | this->inval(nullptr); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 69 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 70 | return this->INHERITED::onFindClickHandler(x, y, modi); |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 71 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 72 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 73 | bool onClick(Click* click) override { |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 74 | return this->INHERITED::onClick(click); |
| 75 | } |
| 76 | |
sugoi@google.com | 93c7ee3 | 2013-03-12 14:36:57 +0000 | [diff] [blame] | 77 | virtual bool handleKey(SkKey) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 78 | this->inval(nullptr); |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 79 | return true; |
| 80 | } |
| 81 | |
| 82 | private: |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 83 | typedef SampleView INHERITED; |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | ////////////////////////////////////////////////////////////////////////////// |
| 87 | |
| 88 | static SkView* MyFactory() { return new RepeatTileView; } |
| 89 | static SkViewRegister reg(MyFactory); |