blob: 62d4a3d956c249f66cdf420b850c92d4c17e2fdf [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
halcanaryb0cce2c2015-01-26 12:49:00 -08008#include "sk_tool_utils.h"
mike@reedtribe.org50e4c722012-10-22 03:59:34 +00009#include "SampleCode.h"
10#include "SkView.h"
11#include "SkCanvas.h"
12#include "SkPath.h"
13#include "SkRegion.h"
14#include "SkShader.h"
15#include "SkUtils.h"
16#include "SkImage.h"
17#include "SkSurface.h"
18
reed@google.comc83e3522012-10-22 22:00:08 +000019#define FAT_PIXEL_COLOR SK_ColorBLACK
20#define PIXEL_CENTER_SIZE 3
21#define WIRE_FRAME_COLOR 0xFFFF0000 /*0xFF00FFFF*/
22#define WIRE_FRAME_SIZE 1.5f
23
reed@google.combbe66f02013-10-30 13:41:19 +000024static SkScalar apply_grid(SkScalar x) {
25 const SkScalar grid = 2;
26 return SkScalarRoundToScalar(x * grid) / grid;
27}
28
29static void apply_grid(SkPoint pts[], int count) {
30 for (int i = 0; i < count; ++i) {
31 pts[i].set(apply_grid(pts[i].fX), apply_grid(pts[i].fY));
32 }
33}
34
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000035static void erase(SkSurface* surface) {
junov@google.comdbfac8a2012-12-06 21:47:40 +000036 surface->getCanvas()->clear(SK_ColorTRANSPARENT);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000037}
38
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000039class FatBits {
40public:
commit-bot@chromium.org5970f622014-05-12 20:42:21 +000041 FatBits() {
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000042 fAA = false;
43 fStyle = kHair_Style;
44 fGrid = true;
reed@google.comc83e3522012-10-22 22:00:08 +000045 fShowSkeleton = true;
46 fUseGPU = false;
reed@google.com9c0bef12012-11-02 19:59:18 +000047 fUseClip = false;
mike@reedtribe.org64494e92013-03-06 02:18:33 +000048 fRectAsOval = false;
reed@google.combbe66f02013-10-30 13:41:19 +000049 fUseTriangle = false;
skia.committer@gmail.com1aa90cf2012-11-06 13:18:25 +000050
reed@google.com9c0bef12012-11-02 19:59:18 +000051 fClipRect.set(2, 2, 11, 8 );
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000052 }
53
robertphillips@google.comca47aae2012-12-12 15:58:25 +000054 int getZoom() const { return fZoom; }
reed@google.comc83e3522012-10-22 22:00:08 +000055
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000056 bool getAA() const { return fAA; }
57 void setAA(bool aa) { fAA = aa; }
58
59 bool getGrid() const { return fGrid; }
60 void setGrid(bool g) { fGrid = g; }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +000061
reed@google.comc83e3522012-10-22 22:00:08 +000062 bool getShowSkeleton() const { return fShowSkeleton; }
63 void setShowSkeleton(bool ss) { fShowSkeleton = ss; }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +000064
reed@google.comc83e3522012-10-22 22:00:08 +000065 bool getUseGPU() const { return fUseGPU; }
66 void setUseGPU(bool ug) { fUseGPU = ug; }
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +000067
reed@google.combbe66f02013-10-30 13:41:19 +000068 bool getTriangle() const { return fUseTriangle; }
69 void setTriangle(bool ut) { fUseTriangle = ut; }
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +000070
mike@reedtribe.org64494e92013-03-06 02:18:33 +000071 void toggleRectAsOval() { fRectAsOval = !fRectAsOval; }
72
reed@google.com9c0bef12012-11-02 19:59:18 +000073 bool getUseClip() const { return fUseClip; }
74 void setUseClip(bool uc) { fUseClip = uc; }
75
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000076 enum Style {
77 kHair_Style,
reed@google.comc83e3522012-10-22 22:00:08 +000078 kStroke_Style,
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000079 };
80 Style getStyle() const { return fStyle; }
81 void setStyle(Style s) { fStyle = s; }
82
83 void setWHZ(int width, int height, int zoom) {
84 fW = width;
85 fH = height;
robertphillips@google.comca47aae2012-12-12 15:58:25 +000086 fZoom = zoom;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000087 fBounds.set(0, 0, SkIntToScalar(width * zoom), SkIntToScalar(height * zoom));
88 fMatrix.setScale(SkIntToScalar(zoom), SkIntToScalar(zoom));
89 fInverse.setScale(SK_Scalar1 / zoom, SK_Scalar1 / zoom);
halcanaryb0cce2c2015-01-26 12:49:00 -080090 fShader.reset(sk_tool_utils::create_checkerboard_shader(
halcanary878fa022015-01-26 11:24:32 -080091 0xFFCCCCCC, 0xFFFFFFFF, zoom));
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +000092
commit-bot@chromium.org32678d92014-01-15 02:38:22 +000093 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
reed@google.combbe66f02013-10-30 13:41:19 +000094 fMinSurface.reset(SkSurface::NewRaster(info));
reede5ea5002014-09-03 11:54:58 -070095 info = info.makeWH(width * zoom, height * zoom);
reed@google.combbe66f02013-10-30 13:41:19 +000096 fMaxSurface.reset(SkSurface::NewRaster(info));
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000097 }
98
99 void drawBG(SkCanvas*);
100 void drawFG(SkCanvas*);
101 void drawLine(SkCanvas*, SkPoint pts[2]);
102 void drawRect(SkCanvas* canvas, SkPoint pts[2]);
reed@google.combbe66f02013-10-30 13:41:19 +0000103 void drawTriangle(SkCanvas* canvas, SkPoint pts[3]);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000104
105private:
reed@google.combbe66f02013-10-30 13:41:19 +0000106 bool fAA, fGrid, fShowSkeleton, fUseGPU, fUseClip, fRectAsOval, fUseTriangle;
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
reed@google.combbe66f02013-10-30 13:41:19 +0000135 void drawTriangleSkeleton(SkCanvas* max, const SkPoint pts[]);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000136 void drawLineSkeleton(SkCanvas* max, const SkPoint pts[]);
137 void drawRectSkeleton(SkCanvas* max, const SkRect& r) {
138 SkPaint paint;
reed@google.comc83e3522012-10-22 22:00:08 +0000139 this->setupSkeletonPaint(&paint);
140 SkPath path;
141
142 if (fUseGPU && fAA) {
143 SkRect rr = r;
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000144 rr.inset(SkIntToScalar(fZoom)/2, SkIntToScalar(fZoom)/2);
reed@google.comc83e3522012-10-22 22:00:08 +0000145 path.addRect(rr);
146 path.moveTo(rr.fLeft, rr.fTop);
147 path.lineTo(rr.fRight, rr.fBottom);
148 rr = r;
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000149 rr.inset(-SkIntToScalar(fZoom)/2, -SkIntToScalar(fZoom)/2);
reed@google.comc83e3522012-10-22 22:00:08 +0000150 path.addRect(rr);
151 } else {
mike@reedtribe.org64494e92013-03-06 02:18:33 +0000152 fRectAsOval ? path.addOval(r) : path.addRect(r);
reed@google.comc83e3522012-10-22 22:00:08 +0000153 if (fUseGPU) {
154 path.moveTo(r.fLeft, r.fTop);
155 path.lineTo(r.fRight, r.fBottom);
156 }
157 }
158 max->drawPath(path, paint);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000159 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000160
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000161 void copyMinToMax() {
162 erase(fMaxSurface);
163 SkCanvas* canvas = fMaxSurface->getCanvas();
164 canvas->save();
165 canvas->concat(fMatrix);
166 fMinSurface->draw(canvas, 0, 0, NULL);
167 canvas->restore();
168
169 SkPaint paint;
170 paint.setXfermodeMode(SkXfermode::kClear_Mode);
171 for (int iy = 1; iy < fH; ++iy) {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000172 SkScalar y = SkIntToScalar(iy * fZoom);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000173 canvas->drawLine(0, y - SK_ScalarHalf, 999, y - SK_ScalarHalf, paint);
174 }
175 for (int ix = 1; ix < fW; ++ix) {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000176 SkScalar x = SkIntToScalar(ix * fZoom);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000177 canvas->drawLine(x - SK_ScalarHalf, 0, x - SK_ScalarHalf, 999, paint);
178 }
179 }
180};
181
182void FatBits::drawBG(SkCanvas* canvas) {
183 SkPaint paint;
184
185 paint.setShader(fShader);
186 canvas->drawRect(fBounds, paint);
187 paint.setShader(NULL);
188}
189
190void FatBits::drawFG(SkCanvas* canvas) {
reed@google.comc83e3522012-10-22 22:00:08 +0000191 SkPaint inner, outer;
192
193 inner.setAntiAlias(true);
194 inner.setColor(SK_ColorBLACK);
195 inner.setStrokeWidth(PIXEL_CENTER_SIZE);
196
197 outer.setAntiAlias(true);
198 outer.setColor(SK_ColorWHITE);
199 outer.setStrokeWidth(PIXEL_CENTER_SIZE + 2);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000200
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000201 SkScalar half = SkIntToScalar(fZoom) / 2;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000202 for (int iy = 0; iy < fH; ++iy) {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000203 SkScalar y = SkIntToScalar(iy * fZoom) + half;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000204 for (int ix = 0; ix < fW; ++ix) {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000205 SkScalar x = SkIntToScalar(ix * fZoom) + half;
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000206
reed@google.comc83e3522012-10-22 22:00:08 +0000207 canvas->drawPoint(x, y, outer);
208 canvas->drawPoint(x, y, inner);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000209 }
210 }
reed@google.com9c0bef12012-11-02 19:59:18 +0000211
212 if (fUseClip) {
213 SkPaint p;
214 p.setStyle(SkPaint::kStroke_Style);
215 p.setColor(SK_ColorLTGRAY);
216 SkRect r = {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000217 fClipRect.fLeft * fZoom,
218 fClipRect.fTop * fZoom,
219 fClipRect.fRight * fZoom,
220 fClipRect.fBottom * fZoom
reed@google.com9c0bef12012-11-02 19:59:18 +0000221 };
222 canvas->drawRect(r, p);
223 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000224}
225
226void FatBits::drawLineSkeleton(SkCanvas* max, const SkPoint pts[]) {
227 SkPaint paint;
reed@google.comc83e3522012-10-22 22:00:08 +0000228 this->setupSkeletonPaint(&paint);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000229
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000230 SkPath path;
231 path.moveTo(pts[0]);
232 path.lineTo(pts[1]);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000233
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000234 switch (fStyle) {
235 case kHair_Style:
reed@google.comc83e3522012-10-22 22:00:08 +0000236 if (fUseGPU) {
237 SkPaint p;
238 p.setStyle(SkPaint::kStroke_Style);
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000239 p.setStrokeWidth(SK_Scalar1 * fZoom);
reed@google.comc83e3522012-10-22 22:00:08 +0000240 SkPath dst;
241 p.getFillPath(path, &dst);
242 path.addPath(dst);
243 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000244 break;
245 case kStroke_Style: {
246 SkPaint p;
247 p.setStyle(SkPaint::kStroke_Style);
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000248 p.setStrokeWidth(SK_Scalar1 * fZoom);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000249 SkPath dst;
250 p.getFillPath(path, &dst);
251 path = dst;
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000252
reed@google.comc83e3522012-10-22 22:00:08 +0000253 if (fUseGPU) {
254 path.moveTo(dst.getPoint(0));
255 path.lineTo(dst.getPoint(2));
256 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000257 } break;
258 }
259 max->drawPath(path, paint);
260}
261
262void FatBits::drawLine(SkCanvas* canvas, SkPoint pts[]) {
263 SkPaint paint;
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000264
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000265 fInverse.mapPoints(pts, 2);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000266
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000267 if (fGrid) {
reed@google.combbe66f02013-10-30 13:41:19 +0000268 apply_grid(pts, 2);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000269 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000270
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000271 erase(fMinSurface);
272 this->setupPaint(&paint);
reed@google.comc83e3522012-10-22 22:00:08 +0000273 paint.setColor(FAT_PIXEL_COLOR);
reed@google.com9c0bef12012-11-02 19:59:18 +0000274 if (fUseClip) {
275 fMinSurface->getCanvas()->save();
276 SkRect r = fClipRect;
277 r.inset(SK_Scalar1/3, SK_Scalar1/3);
278 fMinSurface->getCanvas()->clipRect(r, SkRegion::kIntersect_Op, true);
279 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000280 fMinSurface->getCanvas()->drawLine(pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, paint);
reed@google.com9c0bef12012-11-02 19:59:18 +0000281 if (fUseClip) {
282 fMinSurface->getCanvas()->restore();
283 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000284 this->copyMinToMax();
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000285
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000286 SkCanvas* max = fMaxSurface->getCanvas();
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000287
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000288 fMatrix.mapPoints(pts, 2);
289 this->drawLineSkeleton(max, pts);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000290
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000291 fMaxSurface->draw(canvas, 0, 0, NULL);
292}
293
294void FatBits::drawRect(SkCanvas* canvas, SkPoint pts[2]) {
295 SkPaint paint;
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000296
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000297 fInverse.mapPoints(pts, 2);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000298
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000299 if (fGrid) {
reed@google.combbe66f02013-10-30 13:41:19 +0000300 apply_grid(pts, 2);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000301 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000302
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000303 SkRect r;
304 r.set(pts, 2);
305
306 erase(fMinSurface);
307 this->setupPaint(&paint);
reed@google.comc83e3522012-10-22 22:00:08 +0000308 paint.setColor(FAT_PIXEL_COLOR);
mike@reedtribe.org64494e92013-03-06 02:18:33 +0000309 {
310 SkCanvas* c = fMinSurface->getCanvas();
311 fRectAsOval ? c->drawOval(r, paint) : c->drawRect(r, paint);
312 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000313 this->copyMinToMax();
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000314
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000315 SkCanvas* max = fMaxSurface->getCanvas();
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000316
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000317 fMatrix.mapPoints(pts, 2);
318 r.set(pts, 2);
319 this->drawRectSkeleton(max, r);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000320
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000321 fMaxSurface->draw(canvas, 0, 0, NULL);
322}
323
reed@google.combbe66f02013-10-30 13:41:19 +0000324void FatBits::drawTriangleSkeleton(SkCanvas* max, const SkPoint pts[]) {
325 SkPaint paint;
326 this->setupSkeletonPaint(&paint);
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000327
reed@google.combbe66f02013-10-30 13:41:19 +0000328 SkPath path;
329 path.moveTo(pts[0]);
330 path.lineTo(pts[1]);
331 path.lineTo(pts[2]);
332 path.close();
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000333
reed@google.combbe66f02013-10-30 13:41:19 +0000334 max->drawPath(path, paint);
335}
336
337void FatBits::drawTriangle(SkCanvas* canvas, SkPoint pts[3]) {
338 SkPaint paint;
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000339
reed@google.combbe66f02013-10-30 13:41:19 +0000340 fInverse.mapPoints(pts, 3);
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000341
reed@google.combbe66f02013-10-30 13:41:19 +0000342 if (fGrid) {
343 apply_grid(pts, 3);
344 }
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000345
reed@google.combbe66f02013-10-30 13:41:19 +0000346 SkPath path;
347 path.moveTo(pts[0]);
348 path.lineTo(pts[1]);
349 path.lineTo(pts[2]);
350 path.close();
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000351
reed@google.combbe66f02013-10-30 13:41:19 +0000352 erase(fMinSurface);
353 this->setupPaint(&paint);
354 paint.setColor(FAT_PIXEL_COLOR);
355 fMinSurface->getCanvas()->drawPath(path, paint);
356 this->copyMinToMax();
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000357
reed@google.combbe66f02013-10-30 13:41:19 +0000358 SkCanvas* max = fMaxSurface->getCanvas();
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000359
reed@google.combbe66f02013-10-30 13:41:19 +0000360 fMatrix.mapPoints(pts, 3);
361 this->drawTriangleSkeleton(max, pts);
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000362
reed@google.combbe66f02013-10-30 13:41:19 +0000363 fMaxSurface->draw(canvas, 0, 0, NULL);
364}
365
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000366///////////////////////////////////////////////////////////////////////////////////////////////////
367
368class IndexClick : public SkView::Click {
369 int fIndex;
370public:
371 IndexClick(SkView* v, int index) : SkView::Click(v), fIndex(index) {}
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000372
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000373 static int GetIndex(SkView::Click* click) {
374 return ((IndexClick*)click)->fIndex;
375 }
376};
377
378class DrawLineView : public SampleView {
379 FatBits fFB;
reed@google.combbe66f02013-10-30 13:41:19 +0000380 SkPoint fPts[3];
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000381 bool fIsRect;
382public:
383 DrawLineView() {
384 fFB.setWHZ(24, 16, 48);
reed@google.comcdbcb3e2012-10-22 22:10:20 +0000385 fPts[0].set(48, 48);
386 fPts[1].set(48 * 5, 48 * 4);
reed@google.combbe66f02013-10-30 13:41:19 +0000387 fPts[2].set(48 * 2, 48 * 6);
reed@google.comcdbcb3e2012-10-22 22:10:20 +0000388 fIsRect = false;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000389 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000390
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000391 void setStyle(FatBits::Style s) {
392 fFB.setStyle(s);
393 this->inval(NULL);
394 }
395
396protected:
mtklein36352bf2015-03-25 18:17:31 -0700397 bool onQuery(SkEvent* evt) override {
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000398 if (SampleCode::TitleQ(*evt)) {
399 SampleCode::TitleR(evt, "FatBits");
400 return true;
401 }
402 SkUnichar uni;
403 if (SampleCode::CharQ(*evt, &uni)) {
404 switch (uni) {
reed@google.com9c0bef12012-11-02 19:59:18 +0000405 case 'c':
406 fFB.setUseClip(!fFB.getUseClip());
407 this->inval(NULL);
408 return true;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000409 case 'r':
410 fIsRect = !fIsRect;
411 this->inval(NULL);
412 return true;
mike@reedtribe.org64494e92013-03-06 02:18:33 +0000413 case 'o':
414 fFB.toggleRectAsOval();
415 this->inval(NULL);
416 return true;
reed@google.comc83e3522012-10-22 22:00:08 +0000417 case 'x':
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000418 fFB.setGrid(!fFB.getGrid());
419 this->inval(NULL);
420 return true;
421 case 's':
422 if (FatBits::kStroke_Style == fFB.getStyle()) {
423 this->setStyle(FatBits::kHair_Style);
424 } else {
425 this->setStyle(FatBits::kStroke_Style);
426 }
427 return true;
428 case 'a':
429 fFB.setAA(!fFB.getAA());
430 this->inval(NULL);
431 return true;
reed@google.comc83e3522012-10-22 22:00:08 +0000432 case 'w':
433 fFB.setShowSkeleton(!fFB.getShowSkeleton());
434 this->inval(NULL);
435 return true;
436 case 'g':
437 fFB.setUseGPU(!fFB.getUseGPU());
438 this->inval(NULL);
439 return true;
reed@google.combbe66f02013-10-30 13:41:19 +0000440 case 't':
441 fFB.setTriangle(!fFB.getTriangle());
442 this->inval(NULL);
443 return true;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000444 }
445 }
446 return this->INHERITED::onQuery(evt);
447 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000448
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000449 virtual void onDrawContent(SkCanvas* canvas) {
450 fFB.drawBG(canvas);
reed@google.combbe66f02013-10-30 13:41:19 +0000451 if (fFB.getTriangle()) {
452 fFB.drawTriangle(canvas, fPts);
453 }
454 else if (fIsRect) {
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000455 fFB.drawRect(canvas, fPts);
456 } else {
457 fFB.drawLine(canvas, fPts);
458 }
459 fFB.drawFG(canvas);
skia.committer@gmail.com1e34ff72012-10-24 02:01:24 +0000460
reed@google.comffe9d012012-10-23 15:33:41 +0000461 {
462 SkString str;
reed@google.com9c0bef12012-11-02 19:59:18 +0000463 str.printf("%s %s %s %s",
reed@google.comffe9d012012-10-23 15:33:41 +0000464 fFB.getAA() ? "AA" : "BW",
465 FatBits::kHair_Style == fFB.getStyle() ? "Hair" : "Stroke",
reed@google.com9c0bef12012-11-02 19:59:18 +0000466 fFB.getUseGPU() ? "GPU" : "CPU",
467 fFB.getUseClip() ? "clip" : "noclip");
reed@google.comffe9d012012-10-23 15:33:41 +0000468 SkPaint paint;
469 paint.setAntiAlias(true);
470 paint.setTextSize(16);
471 paint.setColor(SK_ColorBLUE);
472 canvas->drawText(str.c_str(), str.size(), 10, 16, paint);
473 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000474 }
475
mike@reedtribe.org093455c2013-01-28 02:21:27 +0000476 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700477 unsigned modi) override {
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000478 SkPoint pt = { x, y };
479 int index = -1;
reed@google.combbe66f02013-10-30 13:41:19 +0000480 int count = fFB.getTriangle() ? 3 : 2;
reed@google.comcdbcb3e2012-10-22 22:10:20 +0000481 SkScalar tol = 12;
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000482
reed@google.combbe66f02013-10-30 13:41:19 +0000483 for (int i = 0; i < count; ++i) {
484 if (fPts[i].equalsWithinTolerance(pt, tol)) {
485 index = i;
486 break;
487 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000488 }
mike@reedtribe.orgc892a152012-10-23 03:10:46 +0000489 return new IndexClick(this, index);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000490 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000491
mtklein36352bf2015-03-25 18:17:31 -0700492 bool onClick(Click* click) override {
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000493 int index = IndexClick::GetIndex(click);
reed@google.combbe66f02013-10-30 13:41:19 +0000494 if (index >= 0 && index <= 2) {
mike@reedtribe.orgc892a152012-10-23 03:10:46 +0000495 fPts[index] = click->fCurr;
496 } else {
497 SkScalar dx = click->fCurr.fX - click->fPrev.fX;
498 SkScalar dy = click->fCurr.fY - click->fPrev.fY;
499 fPts[0].offset(dx, dy);
500 fPts[1].offset(dx, dy);
reed@google.combbe66f02013-10-30 13:41:19 +0000501 fPts[2].offset(dx, dy);
mike@reedtribe.orgc892a152012-10-23 03:10:46 +0000502 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000503 this->inval(NULL);
504 return true;
505 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000506
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000507private:
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000508
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000509 typedef SampleView INHERITED;
510};
511
512//////////////////////////////////////////////////////////////////////////////
513
514static SkView* MyFactory() { return new DrawLineView; }
515static SkViewRegister reg(MyFactory);