SampleRepeatTile.cpp revision 81e3d7f7943d5c257a07580b75218a5e256b0aad
1#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
38class RepeatTileView : public SampleView {
39public:
40	RepeatTileView() {
41        this->setBGColor(SK_ColorGRAY);
42    }
43
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
54    virtual void onDrawContent(SkCanvas* canvas) {
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
78private:
79    typedef SampleView INHERITED;
80};
81
82//////////////////////////////////////////////////////////////////////////////
83
84static SkView* MyFactory() { return new RepeatTileView; }
85static SkViewRegister reg(MyFactory);
86
87