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