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