SampleRepeatTile.cpp revision e16efc1882ab34a0bb3ae361a2d37f840044cf87
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
14static 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
37static void make_paint(SkPaint* paint, SkShader::TileMode tm) {
38    SkBitmap bm;
39    make_bitmap(&bm);
40
41    SkShader* shader = SkShader::CreateBitmapShader(bm, tm, tm);
42    paint->setShader(shader)->unref();
43}
44
45class RepeatTileView : public SampleView {
46public:
47    RepeatTileView() {
48        this->setBGColor(SK_ColorGRAY);
49    }
50
51protected:
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    }
60
61    virtual void onDrawContent(SkCanvas* canvas) {
62        SkPaint paint;
63        make_paint(&paint, SkShader::kRepeat_TileMode);
64
65//        canvas->scale(SK_Scalar1*2, SK_Scalar1);
66        canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
67        canvas->drawPaint(paint);
68    }
69
70    virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE {
71        this->inval(NULL);
72
73        return this->INHERITED::onFindClickHandler(x, y, modi);
74    }
75
76    virtual bool onClick(Click* click) {
77        return this->INHERITED::onClick(click);
78    }
79
80    virtual bool handleKey(SkKey key) {
81        this->inval(NULL);
82        return true;
83    }
84
85private:
86    typedef SampleView INHERITED;
87};
88
89//////////////////////////////////////////////////////////////////////////////
90
91static SkView* MyFactory() { return new RepeatTileView; }
92static SkViewRegister reg(MyFactory);
93