blob: 04fa1a39cbb7459873962cb5fc36ccf9371e3006 [file] [log] [blame]
mike@reedtribe.org50e4c722012-10-22 03:59:34 +00001/*
2 * Copyright 2012 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
8#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkPath.h"
12#include "SkRegion.h"
13#include "SkShader.h"
14#include "SkUtils.h"
15#include "SkImage.h"
16#include "SkSurface.h"
17
reed@google.comc83e3522012-10-22 22:00:08 +000018#define FAT_PIXEL_COLOR SK_ColorBLACK
19#define PIXEL_CENTER_SIZE 3
20#define WIRE_FRAME_COLOR 0xFFFF0000 /*0xFF00FFFF*/
21#define WIRE_FRAME_SIZE 1.5f
22
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000023static void erase(SkSurface* surface) {
junov@google.comdbfac8a2012-12-06 21:47:40 +000024 surface->getCanvas()->clear(SK_ColorTRANSPARENT);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000025}
26
27static SkShader* createChecker() {
reed@google.com9c0bef12012-11-02 19:59:18 +000028// SkColor colors[] = { 0xFFFDFDFD, 0xFFF4F4F4 };
29 SkColor colors[] = { 0xFFFFFFFF, 0xFFFFFFFF };
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000030 SkBitmap bm;
31 bm.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
32 bm.allocPixels();
33 bm.lockPixels();
reed@google.com9c0bef12012-11-02 19:59:18 +000034 *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = SkPreMultiplyColor(colors[0]);
35 *bm.getAddr32(0, 1) = *bm.getAddr32(1, 0) = SkPreMultiplyColor(colors[1]);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000036 SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
37 SkShader::kRepeat_TileMode);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +000038
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000039 SkMatrix m;
40 m.setScale(12, 12);
41 s->setLocalMatrix(m);
42 return s;
43}
44
45class FatBits {
46public:
47 FatBits() : fShader(createChecker()) {
48 fAA = false;
49 fStyle = kHair_Style;
50 fGrid = true;
reed@google.comc83e3522012-10-22 22:00:08 +000051 fShowSkeleton = true;
52 fUseGPU = false;
reed@google.com9c0bef12012-11-02 19:59:18 +000053 fUseClip = false;
mike@reedtribe.org64494e92013-03-06 02:18:33 +000054 fRectAsOval = false;
skia.committer@gmail.com1aa90cf2012-11-06 13:18:25 +000055
reed@google.com9c0bef12012-11-02 19:59:18 +000056 fClipRect.set(2, 2, 11, 8 );
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000057 }
58
robertphillips@google.comca47aae2012-12-12 15:58:25 +000059 int getZoom() const { return fZoom; }
reed@google.comc83e3522012-10-22 22:00:08 +000060
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000061 bool getAA() const { return fAA; }
62 void setAA(bool aa) { fAA = aa; }
63
64 bool getGrid() const { return fGrid; }
65 void setGrid(bool g) { fGrid = g; }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +000066
reed@google.comc83e3522012-10-22 22:00:08 +000067 bool getShowSkeleton() const { return fShowSkeleton; }
68 void setShowSkeleton(bool ss) { fShowSkeleton = ss; }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +000069
reed@google.comc83e3522012-10-22 22:00:08 +000070 bool getUseGPU() const { return fUseGPU; }
71 void setUseGPU(bool ug) { fUseGPU = ug; }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000072
mike@reedtribe.org64494e92013-03-06 02:18:33 +000073 void toggleRectAsOval() { fRectAsOval = !fRectAsOval; }
74
reed@google.com9c0bef12012-11-02 19:59:18 +000075 bool getUseClip() const { return fUseClip; }
76 void setUseClip(bool uc) { fUseClip = uc; }
77
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000078 enum Style {
79 kHair_Style,
reed@google.comc83e3522012-10-22 22:00:08 +000080 kStroke_Style,
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000081 };
82 Style getStyle() const { return fStyle; }
83 void setStyle(Style s) { fStyle = s; }
84
85 void setWHZ(int width, int height, int zoom) {
86 fW = width;
87 fH = height;
robertphillips@google.comca47aae2012-12-12 15:58:25 +000088 fZoom = zoom;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000089 fBounds.set(0, 0, SkIntToScalar(width * zoom), SkIntToScalar(height * zoom));
90 fMatrix.setScale(SkIntToScalar(zoom), SkIntToScalar(zoom));
91 fInverse.setScale(SK_Scalar1 / zoom, SK_Scalar1 / zoom);
92 fShader->setLocalMatrix(fMatrix);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +000093
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000094 SkImage::Info info = {
95 width, height, SkImage::kPMColor_ColorType, SkImage::kPremul_AlphaType
96 };
mike@reedtribe.orgb9476252012-11-15 02:37:45 +000097 fMinSurface.reset(SkSurface::NewRaster(info));
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000098 info.fWidth *= zoom;
99 info.fHeight *= zoom;
mike@reedtribe.orgb9476252012-11-15 02:37:45 +0000100 fMaxSurface.reset(SkSurface::NewRaster(info));
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000101 }
102
103 void drawBG(SkCanvas*);
104 void drawFG(SkCanvas*);
105 void drawLine(SkCanvas*, SkPoint pts[2]);
106 void drawRect(SkCanvas* canvas, SkPoint pts[2]);
107
108private:
mike@reedtribe.org64494e92013-03-06 02:18:33 +0000109 bool fAA, fGrid, fShowSkeleton, fUseGPU, fUseClip, fRectAsOval;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000110 Style fStyle;
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000111 int fW, fH, fZoom;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000112 SkMatrix fMatrix, fInverse;
reed@google.com9c0bef12012-11-02 19:59:18 +0000113 SkRect fBounds, fClipRect;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000114 SkAutoTUnref<SkShader> fShader;
115 SkAutoTUnref<SkSurface> fMinSurface;
116 SkAutoTUnref<SkSurface> fMaxSurface;
117
118 void setupPaint(SkPaint* paint) {
119 bool aa = this->getAA();
120 switch (fStyle) {
121 case kHair_Style:
122 paint->setStrokeWidth(0);
123 break;
124 case kStroke_Style:
reed@google.comcdbcb3e2012-10-22 22:10:20 +0000125 paint->setStrokeWidth(SK_Scalar1);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000126 break;
127 }
128 paint->setAntiAlias(aa);
129 }
130
reed@google.comc83e3522012-10-22 22:00:08 +0000131 void setupSkeletonPaint(SkPaint* paint) {
132 paint->setStyle(SkPaint::kStroke_Style);
133 paint->setStrokeWidth(WIRE_FRAME_SIZE);
134 paint->setColor(fShowSkeleton ? WIRE_FRAME_COLOR : 0);
135 paint->setAntiAlias(true);
136 }
137
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000138 void drawLineSkeleton(SkCanvas* max, const SkPoint pts[]);
139 void drawRectSkeleton(SkCanvas* max, const SkRect& r) {
140 SkPaint paint;
reed@google.comc83e3522012-10-22 22:00:08 +0000141 this->setupSkeletonPaint(&paint);
142 SkPath path;
143
144 if (fUseGPU && fAA) {
145 SkRect rr = r;
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000146 rr.inset(SkIntToScalar(fZoom)/2, SkIntToScalar(fZoom)/2);
reed@google.comc83e3522012-10-22 22:00:08 +0000147 path.addRect(rr);
148 path.moveTo(rr.fLeft, rr.fTop);
149 path.lineTo(rr.fRight, rr.fBottom);
150 rr = r;
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000151 rr.inset(-SkIntToScalar(fZoom)/2, -SkIntToScalar(fZoom)/2);
reed@google.comc83e3522012-10-22 22:00:08 +0000152 path.addRect(rr);
153 } else {
mike@reedtribe.org64494e92013-03-06 02:18:33 +0000154 fRectAsOval ? path.addOval(r) : path.addRect(r);
reed@google.comc83e3522012-10-22 22:00:08 +0000155 if (fUseGPU) {
156 path.moveTo(r.fLeft, r.fTop);
157 path.lineTo(r.fRight, r.fBottom);
158 }
159 }
160 max->drawPath(path, paint);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000161 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000162
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000163 void copyMinToMax() {
164 erase(fMaxSurface);
165 SkCanvas* canvas = fMaxSurface->getCanvas();
166 canvas->save();
167 canvas->concat(fMatrix);
168 fMinSurface->draw(canvas, 0, 0, NULL);
169 canvas->restore();
170
171 SkPaint paint;
172 paint.setXfermodeMode(SkXfermode::kClear_Mode);
173 for (int iy = 1; iy < fH; ++iy) {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000174 SkScalar y = SkIntToScalar(iy * fZoom);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000175 canvas->drawLine(0, y - SK_ScalarHalf, 999, y - SK_ScalarHalf, paint);
176 }
177 for (int ix = 1; ix < fW; ++ix) {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000178 SkScalar x = SkIntToScalar(ix * fZoom);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000179 canvas->drawLine(x - SK_ScalarHalf, 0, x - SK_ScalarHalf, 999, paint);
180 }
181 }
182};
183
184void FatBits::drawBG(SkCanvas* canvas) {
185 SkPaint paint;
186
187 paint.setShader(fShader);
188 canvas->drawRect(fBounds, paint);
189 paint.setShader(NULL);
190}
191
192void FatBits::drawFG(SkCanvas* canvas) {
reed@google.comc83e3522012-10-22 22:00:08 +0000193 SkPaint inner, outer;
194
195 inner.setAntiAlias(true);
196 inner.setColor(SK_ColorBLACK);
197 inner.setStrokeWidth(PIXEL_CENTER_SIZE);
198
199 outer.setAntiAlias(true);
200 outer.setColor(SK_ColorWHITE);
201 outer.setStrokeWidth(PIXEL_CENTER_SIZE + 2);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000202
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000203 SkScalar half = SkIntToScalar(fZoom) / 2;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000204 for (int iy = 0; iy < fH; ++iy) {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000205 SkScalar y = SkIntToScalar(iy * fZoom) + half;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000206 for (int ix = 0; ix < fW; ++ix) {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000207 SkScalar x = SkIntToScalar(ix * fZoom) + half;
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000208
reed@google.comc83e3522012-10-22 22:00:08 +0000209 canvas->drawPoint(x, y, outer);
210 canvas->drawPoint(x, y, inner);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000211 }
212 }
reed@google.com9c0bef12012-11-02 19:59:18 +0000213
214 if (fUseClip) {
215 SkPaint p;
216 p.setStyle(SkPaint::kStroke_Style);
217 p.setColor(SK_ColorLTGRAY);
218 SkRect r = {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000219 fClipRect.fLeft * fZoom,
220 fClipRect.fTop * fZoom,
221 fClipRect.fRight * fZoom,
222 fClipRect.fBottom * fZoom
reed@google.com9c0bef12012-11-02 19:59:18 +0000223 };
224 canvas->drawRect(r, p);
225 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000226}
227
228void FatBits::drawLineSkeleton(SkCanvas* max, const SkPoint pts[]) {
229 SkPaint paint;
reed@google.comc83e3522012-10-22 22:00:08 +0000230 this->setupSkeletonPaint(&paint);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000231
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000232 SkPath path;
233 path.moveTo(pts[0]);
234 path.lineTo(pts[1]);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000235
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000236 switch (fStyle) {
237 case kHair_Style:
reed@google.comc83e3522012-10-22 22:00:08 +0000238 if (fUseGPU) {
239 SkPaint p;
240 p.setStyle(SkPaint::kStroke_Style);
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000241 p.setStrokeWidth(SK_Scalar1 * fZoom);
reed@google.comc83e3522012-10-22 22:00:08 +0000242 SkPath dst;
243 p.getFillPath(path, &dst);
244 path.addPath(dst);
245 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000246 break;
247 case kStroke_Style: {
248 SkPaint p;
249 p.setStyle(SkPaint::kStroke_Style);
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000250 p.setStrokeWidth(SK_Scalar1 * fZoom);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000251 SkPath dst;
252 p.getFillPath(path, &dst);
253 path = dst;
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000254
reed@google.comc83e3522012-10-22 22:00:08 +0000255 if (fUseGPU) {
256 path.moveTo(dst.getPoint(0));
257 path.lineTo(dst.getPoint(2));
258 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000259 } break;
260 }
261 max->drawPath(path, paint);
262}
263
264void FatBits::drawLine(SkCanvas* canvas, SkPoint pts[]) {
265 SkPaint paint;
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000266
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000267 fInverse.mapPoints(pts, 2);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000268
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000269 if (fGrid) {
reed@google.comcdbcb3e2012-10-22 22:10:20 +0000270 SkScalar dd = 0;//SK_Scalar1 / 50;
271 pts[0].set(SkScalarRoundToScalar(pts[0].fX) + dd,
272 SkScalarRoundToScalar(pts[0].fY) + dd);
273 pts[1].set(SkScalarRoundToScalar(pts[1].fX) + dd,
274 SkScalarRoundToScalar(pts[1].fY) + dd);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000275 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000276
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000277 erase(fMinSurface);
278 this->setupPaint(&paint);
reed@google.comc83e3522012-10-22 22:00:08 +0000279 paint.setColor(FAT_PIXEL_COLOR);
reed@google.com9c0bef12012-11-02 19:59:18 +0000280 if (fUseClip) {
281 fMinSurface->getCanvas()->save();
282 SkRect r = fClipRect;
283 r.inset(SK_Scalar1/3, SK_Scalar1/3);
284 fMinSurface->getCanvas()->clipRect(r, SkRegion::kIntersect_Op, true);
285 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000286 fMinSurface->getCanvas()->drawLine(pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, paint);
reed@google.com9c0bef12012-11-02 19:59:18 +0000287 if (fUseClip) {
288 fMinSurface->getCanvas()->restore();
289 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000290 this->copyMinToMax();
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000291
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000292 SkCanvas* max = fMaxSurface->getCanvas();
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000293
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000294 fMatrix.mapPoints(pts, 2);
295 this->drawLineSkeleton(max, pts);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000296
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000297 fMaxSurface->draw(canvas, 0, 0, NULL);
298}
299
300void FatBits::drawRect(SkCanvas* canvas, SkPoint pts[2]) {
301 SkPaint paint;
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000302
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000303 fInverse.mapPoints(pts, 2);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000304
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000305 if (fGrid) {
306 pts[0].set(SkScalarRoundToScalar(pts[0].fX), SkScalarRoundToScalar(pts[0].fY));
307 pts[1].set(SkScalarRoundToScalar(pts[1].fX), SkScalarRoundToScalar(pts[1].fY));
308 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000309
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000310 SkRect r;
311 r.set(pts, 2);
312
313 erase(fMinSurface);
314 this->setupPaint(&paint);
reed@google.comc83e3522012-10-22 22:00:08 +0000315 paint.setColor(FAT_PIXEL_COLOR);
mike@reedtribe.org64494e92013-03-06 02:18:33 +0000316 {
317 SkCanvas* c = fMinSurface->getCanvas();
318 fRectAsOval ? c->drawOval(r, paint) : c->drawRect(r, paint);
319 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000320 this->copyMinToMax();
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000321
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000322 SkCanvas* max = fMaxSurface->getCanvas();
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000323
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000324 fMatrix.mapPoints(pts, 2);
325 r.set(pts, 2);
326 this->drawRectSkeleton(max, r);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000327
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000328 fMaxSurface->draw(canvas, 0, 0, NULL);
329}
330
331///////////////////////////////////////////////////////////////////////////////////////////////////
332
333class IndexClick : public SkView::Click {
334 int fIndex;
335public:
336 IndexClick(SkView* v, int index) : SkView::Click(v), fIndex(index) {}
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000337
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000338 static int GetIndex(SkView::Click* click) {
339 return ((IndexClick*)click)->fIndex;
340 }
341};
342
343class DrawLineView : public SampleView {
344 FatBits fFB;
345 SkPoint fPts[2];
346 bool fIsRect;
347public:
348 DrawLineView() {
349 fFB.setWHZ(24, 16, 48);
reed@google.comcdbcb3e2012-10-22 22:10:20 +0000350 fPts[0].set(48, 48);
351 fPts[1].set(48 * 5, 48 * 4);
352 fIsRect = false;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000353 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000354
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000355 void setStyle(FatBits::Style s) {
356 fFB.setStyle(s);
357 this->inval(NULL);
358 }
359
360protected:
361 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE {
362 if (SampleCode::TitleQ(*evt)) {
363 SampleCode::TitleR(evt, "FatBits");
364 return true;
365 }
366 SkUnichar uni;
367 if (SampleCode::CharQ(*evt, &uni)) {
368 switch (uni) {
reed@google.com9c0bef12012-11-02 19:59:18 +0000369 case 'c':
370 fFB.setUseClip(!fFB.getUseClip());
371 this->inval(NULL);
372 return true;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000373 case 'r':
374 fIsRect = !fIsRect;
375 this->inval(NULL);
376 return true;
mike@reedtribe.org64494e92013-03-06 02:18:33 +0000377 case 'o':
378 fFB.toggleRectAsOval();
379 this->inval(NULL);
380 return true;
reed@google.comc83e3522012-10-22 22:00:08 +0000381 case 'x':
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000382 fFB.setGrid(!fFB.getGrid());
383 this->inval(NULL);
384 return true;
385 case 's':
386 if (FatBits::kStroke_Style == fFB.getStyle()) {
387 this->setStyle(FatBits::kHair_Style);
388 } else {
389 this->setStyle(FatBits::kStroke_Style);
390 }
391 return true;
392 case 'a':
393 fFB.setAA(!fFB.getAA());
394 this->inval(NULL);
395 return true;
reed@google.comc83e3522012-10-22 22:00:08 +0000396 case 'w':
397 fFB.setShowSkeleton(!fFB.getShowSkeleton());
398 this->inval(NULL);
399 return true;
400 case 'g':
401 fFB.setUseGPU(!fFB.getUseGPU());
402 this->inval(NULL);
403 return true;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000404 }
405 }
406 return this->INHERITED::onQuery(evt);
407 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000408
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000409 virtual void onDrawContent(SkCanvas* canvas) {
410 fFB.drawBG(canvas);
411 if (fIsRect) {
412 fFB.drawRect(canvas, fPts);
413 } else {
414 fFB.drawLine(canvas, fPts);
415 }
416 fFB.drawFG(canvas);
skia.committer@gmail.com1e34ff72012-10-24 02:01:24 +0000417
reed@google.comffe9d012012-10-23 15:33:41 +0000418 {
419 SkString str;
reed@google.com9c0bef12012-11-02 19:59:18 +0000420 str.printf("%s %s %s %s",
reed@google.comffe9d012012-10-23 15:33:41 +0000421 fFB.getAA() ? "AA" : "BW",
422 FatBits::kHair_Style == fFB.getStyle() ? "Hair" : "Stroke",
reed@google.com9c0bef12012-11-02 19:59:18 +0000423 fFB.getUseGPU() ? "GPU" : "CPU",
424 fFB.getUseClip() ? "clip" : "noclip");
reed@google.comffe9d012012-10-23 15:33:41 +0000425 SkPaint paint;
426 paint.setAntiAlias(true);
427 paint.setTextSize(16);
428 paint.setColor(SK_ColorBLUE);
429 canvas->drawText(str.c_str(), str.size(), 10, 16, paint);
430 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000431 }
432
mike@reedtribe.org093455c2013-01-28 02:21:27 +0000433 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
434 unsigned modi) SK_OVERRIDE {
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000435 SkPoint pt = { x, y };
436 int index = -1;
reed@google.comcdbcb3e2012-10-22 22:10:20 +0000437 SkScalar tol = 12;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000438 if (fPts[0].equalsWithinTolerance(pt, tol)) {
439 index = 0;
440 } else if (fPts[1].equalsWithinTolerance(pt, tol)) {
441 index = 1;
442 }
mike@reedtribe.orgc892a152012-10-23 03:10:46 +0000443 return new IndexClick(this, index);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000444 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000445
mike@reedtribe.org093455c2013-01-28 02:21:27 +0000446 virtual bool onClick(Click* click) SK_OVERRIDE {
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000447 int index = IndexClick::GetIndex(click);
mike@reedtribe.orgc892a152012-10-23 03:10:46 +0000448 if (index >= 0 && index <= 1) {
449 fPts[index] = click->fCurr;
450 } else {
451 SkScalar dx = click->fCurr.fX - click->fPrev.fX;
452 SkScalar dy = click->fCurr.fY - click->fPrev.fY;
453 fPts[0].offset(dx, dy);
454 fPts[1].offset(dx, dy);
455 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000456 this->inval(NULL);
457 return true;
458 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000459
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000460private:
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000461
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000462 typedef SampleView INHERITED;
463};
464
465//////////////////////////////////////////////////////////////////////////////
466
467static SkView* MyFactory() { return new DrawLineView; }
468static SkViewRegister reg(MyFactory);