1/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#include "SampleCode.h"
8#include "SkView.h"
9#include "SkBitmap.h"
10#include "SkCanvas.h"
11#include "SkShader.h"
12
13static void make_bitmap(SkBitmap* bm) {
14    const int W = 100;
15    const int H = 100;
16    bm->allocN32Pixels(W, H);
17
18    SkPaint paint;
19    SkCanvas canvas(*bm);
20    canvas.drawColor(SK_ColorWHITE);
21
22    const SkColor colors[] = {
23        SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE
24    };
25
26    for (int ix = 0; ix < W; ix += 1) {
27        SkScalar x = SkIntToScalar(ix) + SK_ScalarHalf;
28        paint.setColor(colors[ix & 3]);
29        canvas.drawLine(x, 0, x, SkIntToScalar(H - 1), paint);
30    }
31    paint.setColor(SK_ColorGRAY);
32    canvas.drawLine(0, 0, SkIntToScalar(W), 0, paint);
33}
34
35static void make_paint(SkPaint* paint, SkShader::TileMode tm) {
36    SkBitmap bm;
37    make_bitmap(&bm);
38
39    paint->setShader(SkShader::MakeBitmapShader(bm, tm, tm));
40}
41
42class RepeatTileView : public SampleView {
43public:
44    RepeatTileView() {
45        this->setBGColor(SK_ColorGRAY);
46    }
47
48protected:
49    // overrides from SkEventSink
50    bool onQuery(SkEvent* evt) override {
51        if (SampleCode::TitleQ(*evt)) {
52            SampleCode::TitleR(evt, "RepeatTile");
53            return true;
54        }
55        return this->INHERITED::onQuery(evt);
56    }
57
58    void onDrawContent(SkCanvas* canvas) override {
59        SkPaint paint;
60        make_paint(&paint, SkShader::kRepeat_TileMode);
61
62//        canvas->scale(SK_Scalar1*2, SK_Scalar1);
63        canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
64        canvas->drawPaint(paint);
65    }
66
67private:
68    typedef SampleView INHERITED;
69};
70
71//////////////////////////////////////////////////////////////////////////////
72
73static SkView* MyFactory() { return new RepeatTileView; }
74static SkViewRegister reg(MyFactory);
75