blob: 988345c0fc947ed09b78a0c9d06232e4364aeadf [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;
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
rmistry@google.comae933ce2012-08-23 18:19:56 +000041 SkShader* shader = SkShader::CreateBitmapShader(bm, tm, tm);
reed@android.comf7d57262009-08-13 19:33:44 +000042 paint->setShader(shader)->unref();
43}
44
reed@google.com81e3d7f2011-06-01 12:42:36 +000045class RepeatTileView : public SampleView {
reed@android.comf7d57262009-08-13 19:33:44 +000046public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000047 RepeatTileView() {
reed@google.com81e3d7f2011-06-01 12:42:36 +000048 this->setBGColor(SK_ColorGRAY);
49 }
reed@android.comf7d57262009-08-13 19:33:44 +000050
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 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000060
reed@google.com81e3d7f2011-06-01 12:42:36 +000061 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.comf7d57262009-08-13 19:33:44 +000062 SkPaint paint;
63 make_paint(&paint, SkShader::kRepeat_TileMode);
rmistry@google.comae933ce2012-08-23 18:19:56 +000064
reed@android.comf7d57262009-08-13 19:33:44 +000065// canvas->scale(SK_Scalar1*2, SK_Scalar1);
66 canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
67 canvas->drawPaint(paint);
68 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000069
reed@google.com4d5c26d2013-01-08 16:17:50 +000070 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE {
reed@android.comf7d57262009-08-13 19:33:44 +000071 this->inval(NULL);
rmistry@google.comae933ce2012-08-23 18:19:56 +000072
reed@google.com4d5c26d2013-01-08 16:17:50 +000073 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.comf7d57262009-08-13 19:33:44 +000074 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000075
reed@android.comf7d57262009-08-13 19:33:44 +000076 virtual bool onClick(Click* click) {
77 return this->INHERITED::onClick(click);
78 }
79
sugoi@google.com93c7ee32013-03-12 14:36:57 +000080 virtual bool handleKey(SkKey) {
reed@android.comf7d57262009-08-13 19:33:44 +000081 this->inval(NULL);
82 return true;
83 }
84
85private:
reed@google.com81e3d7f2011-06-01 12:42:36 +000086 typedef SampleView INHERITED;
reed@android.comf7d57262009-08-13 19:33:44 +000087};
88
89//////////////////////////////////////////////////////////////////////////////
90
91static SkView* MyFactory() { return new RepeatTileView; }
92static SkViewRegister reg(MyFactory);