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