blob: 9867074a1a7bf57ad226f0041ab1736355d18341 [file] [log] [blame]
reed@android.comf7d57262009-08-13 19:33:44 +00001#include "SampleCode.h"
2#include "SkView.h"
3#include "SkCanvas.h"
4#include "SkShader.h"
5#include "SkKey.h"
6
7static 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
30static 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.com81e3d7f2011-06-01 12:42:36 +000038class RepeatTileView : public SampleView {
reed@android.comf7d57262009-08-13 19:33:44 +000039public:
reed@google.com81e3d7f2011-06-01 12:42:36 +000040 RepeatTileView() {
41 this->setBGColor(SK_ColorGRAY);
42 }
reed@android.comf7d57262009-08-13 19:33:44 +000043
44protected:
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.com81e3d7f2011-06-01 12:42:36 +000054 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.comf7d57262009-08-13 19:33:44 +000055 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
78private:
reed@google.com81e3d7f2011-06-01 12:42:36 +000079 typedef SampleView INHERITED;
reed@android.comf7d57262009-08-13 19:33:44 +000080};
81
82//////////////////////////////////////////////////////////////////////////////
83
84static SkView* MyFactory() { return new RepeatTileView; }
85static SkViewRegister reg(MyFactory);
86