blob: 9287439613cbadeebdb3fa83f36f9c93acd1f41a [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@android.comf7d57262009-08-13 19:33:44 +00008#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;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000017 bm->allocN32Pixels(W, H);
reed@android.comf7d57262009-08-13 19:33:44 +000018
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
36static void make_paint(SkPaint* paint, SkShader::TileMode tm) {
37 SkBitmap bm;
38 make_bitmap(&bm);
39
rmistry@google.comae933ce2012-08-23 18:19:56 +000040 SkShader* shader = SkShader::CreateBitmapShader(bm, tm, tm);
reed@android.comf7d57262009-08-13 19:33:44 +000041 paint->setShader(shader)->unref();
42}
43
reed@google.com81e3d7f2011-06-01 12:42:36 +000044class RepeatTileView : public SampleView {
reed@android.comf7d57262009-08-13 19:33:44 +000045public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000046 RepeatTileView() {
reed@google.com81e3d7f2011-06-01 12:42:36 +000047 this->setBGColor(SK_ColorGRAY);
48 }
reed@android.comf7d57262009-08-13 19:33:44 +000049
50protected:
51 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -070052 bool onQuery(SkEvent* evt) override {
reed@android.comf7d57262009-08-13 19:33:44 +000053 if (SampleCode::TitleQ(*evt)) {
54 SampleCode::TitleR(evt, "RepeatTile");
55 return true;
56 }
57 return this->INHERITED::onQuery(evt);
58 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000059
mtklein36352bf2015-03-25 18:17:31 -070060 void onDrawContent(SkCanvas* canvas) override {
reed@android.comf7d57262009-08-13 19:33:44 +000061 SkPaint paint;
62 make_paint(&paint, SkShader::kRepeat_TileMode);
rmistry@google.comae933ce2012-08-23 18:19:56 +000063
reed@android.comf7d57262009-08-13 19:33:44 +000064// canvas->scale(SK_Scalar1*2, SK_Scalar1);
65 canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
66 canvas->drawPaint(paint);
67 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000068
mtklein36352bf2015-03-25 18:17:31 -070069 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
halcanary96fcdcc2015-08-27 07:41:13 -070070 this->inval(nullptr);
rmistry@google.comae933ce2012-08-23 18:19:56 +000071
reed@google.com4d5c26d2013-01-08 16:17:50 +000072 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.comf7d57262009-08-13 19:33:44 +000073 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000074
mtklein36352bf2015-03-25 18:17:31 -070075 bool onClick(Click* click) override {
reed@android.comf7d57262009-08-13 19:33:44 +000076 return this->INHERITED::onClick(click);
77 }
78
sugoi@google.com93c7ee32013-03-12 14:36:57 +000079 virtual bool handleKey(SkKey) {
halcanary96fcdcc2015-08-27 07:41:13 -070080 this->inval(nullptr);
reed@android.comf7d57262009-08-13 19:33:44 +000081 return true;
82 }
83
84private:
reed@google.com81e3d7f2011-06-01 12:42:36 +000085 typedef SampleView INHERITED;
reed@android.comf7d57262009-08-13 19:33:44 +000086};
87
88//////////////////////////////////////////////////////////////////////////////
89
90static SkView* MyFactory() { return new RepeatTileView; }
91static SkViewRegister reg(MyFactory);