blob: 8159cc5db80d014b3baab632f469a520d69a8db0 [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
reed@google.comd28ba802013-09-20 19:33:52 +000094 fMinSurface.reset(SkSurface::NewRasterPMColor(width, height));
95 width *= zoom;
96 height *= zoom;
97 fMaxSurface.reset(SkSurface::NewRasterPMColor(width, height));
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000098 }
99
100 void drawBG(SkCanvas*);
101 void drawFG(SkCanvas*);
102 void drawLine(SkCanvas*, SkPoint pts[2]);
103 void drawRect(SkCanvas* canvas, SkPoint pts[2]);
104
105private:
mike@reedtribe.org64494e92013-03-06 02:18:33 +0000106 bool fAA, fGrid, fShowSkeleton, fUseGPU, fUseClip, fRectAsOval;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000107 Style fStyle;
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000108 int fW, fH, fZoom;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000109 SkMatrix fMatrix, fInverse;
reed@google.com9c0bef12012-11-02 19:59:18 +0000110 SkRect fBounds, fClipRect;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000111 SkAutoTUnref<SkShader> fShader;
112 SkAutoTUnref<SkSurface> fMinSurface;
113 SkAutoTUnref<SkSurface> fMaxSurface;
114
115 void setupPaint(SkPaint* paint) {
116 bool aa = this->getAA();
117 switch (fStyle) {
118 case kHair_Style:
119 paint->setStrokeWidth(0);
120 break;
121 case kStroke_Style:
reed@google.comcdbcb3e2012-10-22 22:10:20 +0000122 paint->setStrokeWidth(SK_Scalar1);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000123 break;
124 }
125 paint->setAntiAlias(aa);
126 }
127
reed@google.comc83e3522012-10-22 22:00:08 +0000128 void setupSkeletonPaint(SkPaint* paint) {
129 paint->setStyle(SkPaint::kStroke_Style);
130 paint->setStrokeWidth(WIRE_FRAME_SIZE);
131 paint->setColor(fShowSkeleton ? WIRE_FRAME_COLOR : 0);
132 paint->setAntiAlias(true);
133 }
134
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000135 void drawLineSkeleton(SkCanvas* max, const SkPoint pts[]);
136 void drawRectSkeleton(SkCanvas* max, const SkRect& r) {
137 SkPaint paint;
reed@google.comc83e3522012-10-22 22:00:08 +0000138 this->setupSkeletonPaint(&paint);
139 SkPath path;
140
141 if (fUseGPU && fAA) {
142 SkRect rr = r;
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000143 rr.inset(SkIntToScalar(fZoom)/2, SkIntToScalar(fZoom)/2);
reed@google.comc83e3522012-10-22 22:00:08 +0000144 path.addRect(rr);
145 path.moveTo(rr.fLeft, rr.fTop);
146 path.lineTo(rr.fRight, rr.fBottom);
147 rr = r;
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000148 rr.inset(-SkIntToScalar(fZoom)/2, -SkIntToScalar(fZoom)/2);
reed@google.comc83e3522012-10-22 22:00:08 +0000149 path.addRect(rr);
150 } else {
mike@reedtribe.org64494e92013-03-06 02:18:33 +0000151 fRectAsOval ? path.addOval(r) : path.addRect(r);
reed@google.comc83e3522012-10-22 22:00:08 +0000152 if (fUseGPU) {
153 path.moveTo(r.fLeft, r.fTop);
154 path.lineTo(r.fRight, r.fBottom);
155 }
156 }
157 max->drawPath(path, paint);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000158 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000159
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000160 void copyMinToMax() {
161 erase(fMaxSurface);
162 SkCanvas* canvas = fMaxSurface->getCanvas();
163 canvas->save();
164 canvas->concat(fMatrix);
165 fMinSurface->draw(canvas, 0, 0, NULL);
166 canvas->restore();
167
168 SkPaint paint;
169 paint.setXfermodeMode(SkXfermode::kClear_Mode);
170 for (int iy = 1; iy < fH; ++iy) {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000171 SkScalar y = SkIntToScalar(iy * fZoom);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000172 canvas->drawLine(0, y - SK_ScalarHalf, 999, y - SK_ScalarHalf, paint);
173 }
174 for (int ix = 1; ix < fW; ++ix) {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000175 SkScalar x = SkIntToScalar(ix * fZoom);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000176 canvas->drawLine(x - SK_ScalarHalf, 0, x - SK_ScalarHalf, 999, paint);
177 }
178 }
179};
180
181void FatBits::drawBG(SkCanvas* canvas) {
182 SkPaint paint;
183
184 paint.setShader(fShader);
185 canvas->drawRect(fBounds, paint);
186 paint.setShader(NULL);
187}
188
189void FatBits::drawFG(SkCanvas* canvas) {
reed@google.comc83e3522012-10-22 22:00:08 +0000190 SkPaint inner, outer;
191
192 inner.setAntiAlias(true);
193 inner.setColor(SK_ColorBLACK);
194 inner.setStrokeWidth(PIXEL_CENTER_SIZE);
195
196 outer.setAntiAlias(true);
197 outer.setColor(SK_ColorWHITE);
198 outer.setStrokeWidth(PIXEL_CENTER_SIZE + 2);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000199
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000200 SkScalar half = SkIntToScalar(fZoom) / 2;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000201 for (int iy = 0; iy < fH; ++iy) {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000202 SkScalar y = SkIntToScalar(iy * fZoom) + half;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000203 for (int ix = 0; ix < fW; ++ix) {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000204 SkScalar x = SkIntToScalar(ix * fZoom) + half;
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000205
reed@google.comc83e3522012-10-22 22:00:08 +0000206 canvas->drawPoint(x, y, outer);
207 canvas->drawPoint(x, y, inner);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000208 }
209 }
reed@google.com9c0bef12012-11-02 19:59:18 +0000210
211 if (fUseClip) {
212 SkPaint p;
213 p.setStyle(SkPaint::kStroke_Style);
214 p.setColor(SK_ColorLTGRAY);
215 SkRect r = {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000216 fClipRect.fLeft * fZoom,
217 fClipRect.fTop * fZoom,
218 fClipRect.fRight * fZoom,
219 fClipRect.fBottom * fZoom
reed@google.com9c0bef12012-11-02 19:59:18 +0000220 };
221 canvas->drawRect(r, p);
222 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000223}
224
225void FatBits::drawLineSkeleton(SkCanvas* max, const SkPoint pts[]) {
226 SkPaint paint;
reed@google.comc83e3522012-10-22 22:00:08 +0000227 this->setupSkeletonPaint(&paint);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000228
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000229 SkPath path;
230 path.moveTo(pts[0]);
231 path.lineTo(pts[1]);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000232
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000233 switch (fStyle) {
234 case kHair_Style:
reed@google.comc83e3522012-10-22 22:00:08 +0000235 if (fUseGPU) {
236 SkPaint p;
237 p.setStyle(SkPaint::kStroke_Style);
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000238 p.setStrokeWidth(SK_Scalar1 * fZoom);
reed@google.comc83e3522012-10-22 22:00:08 +0000239 SkPath dst;
240 p.getFillPath(path, &dst);
241 path.addPath(dst);
242 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000243 break;
244 case kStroke_Style: {
245 SkPaint p;
246 p.setStyle(SkPaint::kStroke_Style);
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000247 p.setStrokeWidth(SK_Scalar1 * fZoom);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000248 SkPath dst;
249 p.getFillPath(path, &dst);
250 path = dst;
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000251
reed@google.comc83e3522012-10-22 22:00:08 +0000252 if (fUseGPU) {
253 path.moveTo(dst.getPoint(0));
254 path.lineTo(dst.getPoint(2));
255 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000256 } break;
257 }
258 max->drawPath(path, paint);
259}
260
261void FatBits::drawLine(SkCanvas* canvas, SkPoint pts[]) {
262 SkPaint paint;
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000263
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000264 fInverse.mapPoints(pts, 2);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000265
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000266 if (fGrid) {
reed@google.comcdbcb3e2012-10-22 22:10:20 +0000267 SkScalar dd = 0;//SK_Scalar1 / 50;
268 pts[0].set(SkScalarRoundToScalar(pts[0].fX) + dd,
269 SkScalarRoundToScalar(pts[0].fY) + dd);
270 pts[1].set(SkScalarRoundToScalar(pts[1].fX) + dd,
271 SkScalarRoundToScalar(pts[1].fY) + dd);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000272 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000273
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000274 erase(fMinSurface);
275 this->setupPaint(&paint);
reed@google.comc83e3522012-10-22 22:00:08 +0000276 paint.setColor(FAT_PIXEL_COLOR);
reed@google.com9c0bef12012-11-02 19:59:18 +0000277 if (fUseClip) {
278 fMinSurface->getCanvas()->save();
279 SkRect r = fClipRect;
280 r.inset(SK_Scalar1/3, SK_Scalar1/3);
281 fMinSurface->getCanvas()->clipRect(r, SkRegion::kIntersect_Op, true);
282 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000283 fMinSurface->getCanvas()->drawLine(pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, paint);
reed@google.com9c0bef12012-11-02 19:59:18 +0000284 if (fUseClip) {
285 fMinSurface->getCanvas()->restore();
286 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000287 this->copyMinToMax();
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000288
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000289 SkCanvas* max = fMaxSurface->getCanvas();
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000290
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000291 fMatrix.mapPoints(pts, 2);
292 this->drawLineSkeleton(max, pts);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000293
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000294 fMaxSurface->draw(canvas, 0, 0, NULL);
295}
296
297void FatBits::drawRect(SkCanvas* canvas, SkPoint pts[2]) {
298 SkPaint paint;
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000299
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000300 fInverse.mapPoints(pts, 2);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000301
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000302 if (fGrid) {
303 pts[0].set(SkScalarRoundToScalar(pts[0].fX), SkScalarRoundToScalar(pts[0].fY));
304 pts[1].set(SkScalarRoundToScalar(pts[1].fX), SkScalarRoundToScalar(pts[1].fY));
305 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000306
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000307 SkRect r;
308 r.set(pts, 2);
309
310 erase(fMinSurface);
311 this->setupPaint(&paint);
reed@google.comc83e3522012-10-22 22:00:08 +0000312 paint.setColor(FAT_PIXEL_COLOR);
mike@reedtribe.org64494e92013-03-06 02:18:33 +0000313 {
314 SkCanvas* c = fMinSurface->getCanvas();
315 fRectAsOval ? c->drawOval(r, paint) : c->drawRect(r, paint);
316 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000317 this->copyMinToMax();
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000318
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000319 SkCanvas* max = fMaxSurface->getCanvas();
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000320
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000321 fMatrix.mapPoints(pts, 2);
322 r.set(pts, 2);
323 this->drawRectSkeleton(max, r);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000324
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000325 fMaxSurface->draw(canvas, 0, 0, NULL);
326}
327
328///////////////////////////////////////////////////////////////////////////////////////////////////
329
330class IndexClick : public SkView::Click {
331 int fIndex;
332public:
333 IndexClick(SkView* v, int index) : SkView::Click(v), fIndex(index) {}
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000334
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000335 static int GetIndex(SkView::Click* click) {
336 return ((IndexClick*)click)->fIndex;
337 }
338};
339
340class DrawLineView : public SampleView {
341 FatBits fFB;
342 SkPoint fPts[2];
343 bool fIsRect;
344public:
345 DrawLineView() {
346 fFB.setWHZ(24, 16, 48);
reed@google.comcdbcb3e2012-10-22 22:10:20 +0000347 fPts[0].set(48, 48);
348 fPts[1].set(48 * 5, 48 * 4);
349 fIsRect = false;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000350 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000351
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000352 void setStyle(FatBits::Style s) {
353 fFB.setStyle(s);
354 this->inval(NULL);
355 }
356
357protected:
358 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE {
359 if (SampleCode::TitleQ(*evt)) {
360 SampleCode::TitleR(evt, "FatBits");
361 return true;
362 }
363 SkUnichar uni;
364 if (SampleCode::CharQ(*evt, &uni)) {
365 switch (uni) {
reed@google.com9c0bef12012-11-02 19:59:18 +0000366 case 'c':
367 fFB.setUseClip(!fFB.getUseClip());
368 this->inval(NULL);
369 return true;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000370 case 'r':
371 fIsRect = !fIsRect;
372 this->inval(NULL);
373 return true;
mike@reedtribe.org64494e92013-03-06 02:18:33 +0000374 case 'o':
375 fFB.toggleRectAsOval();
376 this->inval(NULL);
377 return true;
reed@google.comc83e3522012-10-22 22:00:08 +0000378 case 'x':
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000379 fFB.setGrid(!fFB.getGrid());
380 this->inval(NULL);
381 return true;
382 case 's':
383 if (FatBits::kStroke_Style == fFB.getStyle()) {
384 this->setStyle(FatBits::kHair_Style);
385 } else {
386 this->setStyle(FatBits::kStroke_Style);
387 }
388 return true;
389 case 'a':
390 fFB.setAA(!fFB.getAA());
391 this->inval(NULL);
392 return true;
reed@google.comc83e3522012-10-22 22:00:08 +0000393 case 'w':
394 fFB.setShowSkeleton(!fFB.getShowSkeleton());
395 this->inval(NULL);
396 return true;
397 case 'g':
398 fFB.setUseGPU(!fFB.getUseGPU());
399 this->inval(NULL);
400 return true;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000401 }
402 }
403 return this->INHERITED::onQuery(evt);
404 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000405
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000406 virtual void onDrawContent(SkCanvas* canvas) {
407 fFB.drawBG(canvas);
408 if (fIsRect) {
409 fFB.drawRect(canvas, fPts);
410 } else {
411 fFB.drawLine(canvas, fPts);
412 }
413 fFB.drawFG(canvas);
skia.committer@gmail.com1e34ff72012-10-24 02:01:24 +0000414
reed@google.comffe9d012012-10-23 15:33:41 +0000415 {
416 SkString str;
reed@google.com9c0bef12012-11-02 19:59:18 +0000417 str.printf("%s %s %s %s",
reed@google.comffe9d012012-10-23 15:33:41 +0000418 fFB.getAA() ? "AA" : "BW",
419 FatBits::kHair_Style == fFB.getStyle() ? "Hair" : "Stroke",
reed@google.com9c0bef12012-11-02 19:59:18 +0000420 fFB.getUseGPU() ? "GPU" : "CPU",
421 fFB.getUseClip() ? "clip" : "noclip");
reed@google.comffe9d012012-10-23 15:33:41 +0000422 SkPaint paint;
423 paint.setAntiAlias(true);
424 paint.setTextSize(16);
425 paint.setColor(SK_ColorBLUE);
426 canvas->drawText(str.c_str(), str.size(), 10, 16, paint);
427 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000428 }
429
mike@reedtribe.org093455c2013-01-28 02:21:27 +0000430 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
431 unsigned modi) SK_OVERRIDE {
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000432 SkPoint pt = { x, y };
433 int index = -1;
reed@google.comcdbcb3e2012-10-22 22:10:20 +0000434 SkScalar tol = 12;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000435 if (fPts[0].equalsWithinTolerance(pt, tol)) {
436 index = 0;
437 } else if (fPts[1].equalsWithinTolerance(pt, tol)) {
438 index = 1;
439 }
mike@reedtribe.orgc892a152012-10-23 03:10:46 +0000440 return new IndexClick(this, index);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000441 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000442
mike@reedtribe.org093455c2013-01-28 02:21:27 +0000443 virtual bool onClick(Click* click) SK_OVERRIDE {
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000444 int index = IndexClick::GetIndex(click);
mike@reedtribe.orgc892a152012-10-23 03:10:46 +0000445 if (index >= 0 && index <= 1) {
446 fPts[index] = click->fCurr;
447 } else {
448 SkScalar dx = click->fCurr.fX - click->fPrev.fX;
449 SkScalar dy = click->fCurr.fY - click->fPrev.fY;
450 fPts[0].offset(dx, dy);
451 fPts[1].offset(dx, dy);
452 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000453 this->inval(NULL);
454 return true;
455 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000456
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000457private:
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000458
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000459 typedef SampleView INHERITED;
460};
461
462//////////////////////////////////////////////////////////////////////////////
463
464static SkView* MyFactory() { return new DrawLineView; }
465static SkViewRegister reg(MyFactory);