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 */ 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->allocN32Pixels(W, H); 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 40 SkShader* shader = SkShader::CreateBitmapShader(bm, tm, tm); 41 paint->setShader(shader)->unref(); 42 } 43 44 class RepeatTileView : public SampleView { 45 public: 46 RepeatTileView() { 47 this->setBGColor(SK_ColorGRAY); 48 } 49 50 protected: 51 // overrides from SkEventSink 52 bool onQuery(SkEvent* evt) override { 53 if (SampleCode::TitleQ(*evt)) { 54 SampleCode::TitleR(evt, "RepeatTile"); 55 return true; 56 } 57 return this->INHERITED::onQuery(evt); 58 } 59 60 void onDrawContent(SkCanvas* canvas) override { 61 SkPaint paint; 62 make_paint(&paint, SkShader::kRepeat_TileMode); 63 64 // canvas->scale(SK_Scalar1*2, SK_Scalar1); 65 canvas->translate(SkIntToScalar(100), SkIntToScalar(100)); 66 canvas->drawPaint(paint); 67 } 68 69 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override { 70 this->inval(nullptr); 71 72 return this->INHERITED::onFindClickHandler(x, y, modi); 73 } 74 75 bool onClick(Click* click) override { 76 return this->INHERITED::onClick(click); 77 } 78 79 virtual bool handleKey(SkKey) { 80 this->inval(nullptr); 81 return true; 82 } 83 84 private: 85 typedef SampleView INHERITED; 86 }; 87 88 ////////////////////////////////////////////////////////////////////////////// 89 90 static SkView* MyFactory() { return new RepeatTileView; } 91 static SkViewRegister reg(MyFactory); 92