blob: f56492dc0f384982bcb1693c0c0059b56dd1bbd2 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reed@android.comf7d57262009-08-13 19:33:44 +00007#include "SampleCode.h"
8#include "SkView.h"
9#include "SkCanvas.h"
10#include "SkShader.h"
11#include "SkKey.h"
12
13static void make_bitmap(SkBitmap* bm) {
14 const int W = 100;
15 const int H = 100;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000016 bm->allocN32Pixels(W, H);
reed@android.comf7d57262009-08-13 19:33:44 +000017
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
reed8a21c9f2016-03-08 18:50:00 -080039 paint->setShader(SkShader::MakeBitmapShader(bm, tm, tm));
reed@android.comf7d57262009-08-13 19:33:44 +000040}
41
reed@google.com81e3d7f2011-06-01 12:42:36 +000042class RepeatTileView : public SampleView {
reed@android.comf7d57262009-08-13 19:33:44 +000043public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000044 RepeatTileView() {
reed@google.com81e3d7f2011-06-01 12:42:36 +000045 this->setBGColor(SK_ColorGRAY);
46 }
reed@android.comf7d57262009-08-13 19:33:44 +000047
48protected:
49 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -070050 bool onQuery(SkEvent* evt) override {
reed@android.comf7d57262009-08-13 19:33:44 +000051 if (SampleCode::TitleQ(*evt)) {
52 SampleCode::TitleR(evt, "RepeatTile");
53 return true;
54 }
55 return this->INHERITED::onQuery(evt);
56 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000057
mtklein36352bf2015-03-25 18:17:31 -070058 void onDrawContent(SkCanvas* canvas) override {
reed@android.comf7d57262009-08-13 19:33:44 +000059 SkPaint paint;
60 make_paint(&paint, SkShader::kRepeat_TileMode);
rmistry@google.comae933ce2012-08-23 18:19:56 +000061
reed@android.comf7d57262009-08-13 19:33:44 +000062// canvas->scale(SK_Scalar1*2, SK_Scalar1);
63 canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
64 canvas->drawPaint(paint);
65 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000066
mtklein36352bf2015-03-25 18:17:31 -070067 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
halcanary96fcdcc2015-08-27 07:41:13 -070068 this->inval(nullptr);
rmistry@google.comae933ce2012-08-23 18:19:56 +000069
reed@google.com4d5c26d2013-01-08 16:17:50 +000070 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.comf7d57262009-08-13 19:33:44 +000071 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000072
mtklein36352bf2015-03-25 18:17:31 -070073 bool onClick(Click* click) override {
reed@android.comf7d57262009-08-13 19:33:44 +000074 return this->INHERITED::onClick(click);
75 }
76
sugoi@google.com93c7ee32013-03-12 14:36:57 +000077 virtual bool handleKey(SkKey) {
halcanary96fcdcc2015-08-27 07:41:13 -070078 this->inval(nullptr);
reed@android.comf7d57262009-08-13 19:33:44 +000079 return true;
80 }
81
82private:
reed@google.com81e3d7f2011-06-01 12:42:36 +000083 typedef SampleView INHERITED;
reed@android.comf7d57262009-08-13 19:33:44 +000084};
85
86//////////////////////////////////////////////////////////////////////////////
87
88static SkView* MyFactory() { return new RepeatTileView; }
89static SkViewRegister reg(MyFactory);