blob: 1e988e7683d6b79973a751b47d507f39c7cb57e4 [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
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04008#include "Sample.h"
Ben Wagnercb3d49c2018-03-14 15:07:43 -04009#include "SkBlendMode.h"
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000010#include "SkCanvas.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050011#include "SkClipOpPriv.h"
Ben Wagnercb3d49c2018-03-14 15:07:43 -040012#include "SkColor.h"
13#include "SkImageInfo.h"
14#include "SkMatrix.h"
15#include "SkPaint.h"
16#include "SkPath.h"
17#include "SkPoint.h"
18#include "SkPointPriv.h"
19#include "SkRect.h"
20#include "SkRefCnt.h"
21#include "SkScalar.h"
22#include "SkShader.h"
23#include "SkString.h"
24#include "SkSurface.h"
25#include "SkTypes.h"
Ben Wagnercb3d49c2018-03-14 15:07:43 -040026#include "sk_tool_utils.h"
27
28class SkEvent;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000029
reed@google.comc83e3522012-10-22 22:00:08 +000030#define FAT_PIXEL_COLOR SK_ColorBLACK
31#define PIXEL_CENTER_SIZE 3
32#define WIRE_FRAME_COLOR 0xFFFF0000 /*0xFF00FFFF*/
33#define WIRE_FRAME_SIZE 1.5f
34
reed@google.combbe66f02013-10-30 13:41:19 +000035static SkScalar apply_grid(SkScalar x) {
36 const SkScalar grid = 2;
37 return SkScalarRoundToScalar(x * grid) / grid;
38}
39
40static void apply_grid(SkPoint pts[], int count) {
41 for (int i = 0; i < count; ++i) {
42 pts[i].set(apply_grid(pts[i].fX), apply_grid(pts[i].fY));
43 }
44}
45
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000046static void erase(SkSurface* surface) {
junov@google.comdbfac8a2012-12-06 21:47:40 +000047 surface->getCanvas()->clear(SK_ColorTRANSPARENT);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000048}
49
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000050class FatBits {
51public:
commit-bot@chromium.org5970f622014-05-12 20:42:21 +000052 FatBits() {
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000053 fAA = false;
54 fStyle = kHair_Style;
reedbb886742015-08-15 07:42:31 -070055 fGrid = false;
reed@google.comc83e3522012-10-22 22:00:08 +000056 fShowSkeleton = true;
reed@google.com9c0bef12012-11-02 19:59:18 +000057 fUseClip = false;
mike@reedtribe.org64494e92013-03-06 02:18:33 +000058 fRectAsOval = false;
reed@google.combbe66f02013-10-30 13:41:19 +000059 fUseTriangle = false;
reeda5d12072015-11-16 18:57:18 -080060 fStrokeCap = SkPaint::kButt_Cap;
skia.committer@gmail.com1aa90cf2012-11-06 13:18:25 +000061
reed@google.com9c0bef12012-11-02 19:59:18 +000062 fClipRect.set(2, 2, 11, 8 );
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000063 }
64
robertphillips@google.comca47aae2012-12-12 15:58:25 +000065 int getZoom() const { return fZoom; }
reed@google.comc83e3522012-10-22 22:00:08 +000066
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000067 bool getAA() const { return fAA; }
68 void setAA(bool aa) { fAA = aa; }
69
70 bool getGrid() const { return fGrid; }
71 void setGrid(bool g) { fGrid = g; }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +000072
reed@google.comc83e3522012-10-22 22:00:08 +000073 bool getShowSkeleton() const { return fShowSkeleton; }
74 void setShowSkeleton(bool ss) { fShowSkeleton = ss; }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +000075
reed@google.combbe66f02013-10-30 13:41:19 +000076 bool getTriangle() const { return fUseTriangle; }
77 void setTriangle(bool ut) { fUseTriangle = ut; }
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +000078
mike@reedtribe.org64494e92013-03-06 02:18:33 +000079 void toggleRectAsOval() { fRectAsOval = !fRectAsOval; }
80
reed8716a8a2016-09-19 07:10:36 -070081 void togglePixelColors() {
82 if (fShader == fShader0) {
83 fShader = fShader1;
84 } else {
85 fShader = fShader0;
86 }
87 }
88
Mike Reedce54bce2017-11-01 21:17:11 -040089 float fStrokeWidth = 1;
90
reed@google.com9c0bef12012-11-02 19:59:18 +000091 bool getUseClip() const { return fUseClip; }
92 void setUseClip(bool uc) { fUseClip = uc; }
93
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000094 enum Style {
95 kHair_Style,
reed@google.comc83e3522012-10-22 22:00:08 +000096 kStroke_Style,
mike@reedtribe.org50e4c722012-10-22 03:59:34 +000097 };
98 Style getStyle() const { return fStyle; }
99 void setStyle(Style s) { fStyle = s; }
100
101 void setWHZ(int width, int height, int zoom) {
102 fW = width;
103 fH = height;
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000104 fZoom = zoom;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000105 fBounds.set(0, 0, SkIntToScalar(width * zoom), SkIntToScalar(height * zoom));
106 fMatrix.setScale(SkIntToScalar(zoom), SkIntToScalar(zoom));
107 fInverse.setScale(SK_Scalar1 / zoom, SK_Scalar1 / zoom);
reed8716a8a2016-09-19 07:10:36 -0700108 fShader0 = sk_tool_utils::create_checkerboard_shader(0xFFDDDDDD, 0xFFFFFFFF, zoom);
109 fShader1 = SkShader::MakeColorShader(SK_ColorWHITE);
110 fShader = fShader0;
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000111
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000112 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
reede8f30622016-03-23 18:59:25 -0700113 fMinSurface = SkSurface::MakeRaster(info);
reede5ea5002014-09-03 11:54:58 -0700114 info = info.makeWH(width * zoom, height * zoom);
reede8f30622016-03-23 18:59:25 -0700115 fMaxSurface = SkSurface::MakeRaster(info);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000116 }
117
118 void drawBG(SkCanvas*);
119 void drawFG(SkCanvas*);
120 void drawLine(SkCanvas*, SkPoint pts[2]);
121 void drawRect(SkCanvas* canvas, SkPoint pts[2]);
reed@google.combbe66f02013-10-30 13:41:19 +0000122 void drawTriangle(SkCanvas* canvas, SkPoint pts[3]);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000123
reeda5d12072015-11-16 18:57:18 -0800124 SkPaint::Cap fStrokeCap;
125
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000126private:
reed8716a8a2016-09-19 07:10:36 -0700127 bool fAA, fGrid, fShowSkeleton, fUseClip, fRectAsOval, fUseTriangle;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000128 Style fStyle;
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000129 int fW, fH, fZoom;
reed8716a8a2016-09-19 07:10:36 -0700130 SkMatrix fMatrix, fInverse;
131 SkRect fBounds, fClipRect;
132 sk_sp<SkShader> fShader0;
133 sk_sp<SkShader> fShader1;
134 sk_sp<SkShader> fShader;
135 sk_sp<SkSurface> fMinSurface;
136 sk_sp<SkSurface> fMaxSurface;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000137
138 void setupPaint(SkPaint* paint) {
139 bool aa = this->getAA();
reeda5d12072015-11-16 18:57:18 -0800140 paint->setStrokeCap(fStrokeCap);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000141 switch (fStyle) {
142 case kHair_Style:
143 paint->setStrokeWidth(0);
144 break;
145 case kStroke_Style:
Mike Reedce54bce2017-11-01 21:17:11 -0400146 paint->setStrokeWidth(fStrokeWidth);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000147 break;
148 }
149 paint->setAntiAlias(aa);
150 }
151
reed@google.comc83e3522012-10-22 22:00:08 +0000152 void setupSkeletonPaint(SkPaint* paint) {
153 paint->setStyle(SkPaint::kStroke_Style);
154 paint->setStrokeWidth(WIRE_FRAME_SIZE);
155 paint->setColor(fShowSkeleton ? WIRE_FRAME_COLOR : 0);
156 paint->setAntiAlias(true);
157 }
158
reed@google.combbe66f02013-10-30 13:41:19 +0000159 void drawTriangleSkeleton(SkCanvas* max, const SkPoint pts[]);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000160 void drawLineSkeleton(SkCanvas* max, const SkPoint pts[]);
161 void drawRectSkeleton(SkCanvas* max, const SkRect& r) {
162 SkPaint paint;
reed@google.comc83e3522012-10-22 22:00:08 +0000163 this->setupSkeletonPaint(&paint);
164 SkPath path;
165
reed8716a8a2016-09-19 07:10:36 -0700166 fRectAsOval ? path.addOval(r) : path.addRect(r);
reed@google.comc83e3522012-10-22 22:00:08 +0000167 max->drawPath(path, paint);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000168 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000169
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000170 void copyMinToMax() {
reede8f30622016-03-23 18:59:25 -0700171 erase(fMaxSurface.get());
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000172 SkCanvas* canvas = fMaxSurface->getCanvas();
173 canvas->save();
174 canvas->concat(fMatrix);
halcanary96fcdcc2015-08-27 07:41:13 -0700175 fMinSurface->draw(canvas, 0, 0, nullptr);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000176 canvas->restore();
177
178 SkPaint paint;
reed374772b2016-10-05 17:33:02 -0700179 paint.setBlendMode(SkBlendMode::kClear);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000180 for (int iy = 1; iy < fH; ++iy) {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000181 SkScalar y = SkIntToScalar(iy * fZoom);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000182 canvas->drawLine(0, y - SK_ScalarHalf, 999, y - SK_ScalarHalf, paint);
183 }
184 for (int ix = 1; ix < fW; ++ix) {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000185 SkScalar x = SkIntToScalar(ix * fZoom);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000186 canvas->drawLine(x - SK_ScalarHalf, 0, x - SK_ScalarHalf, 999, paint);
187 }
188 }
189};
190
191void FatBits::drawBG(SkCanvas* canvas) {
192 SkPaint paint;
193
194 paint.setShader(fShader);
195 canvas->drawRect(fBounds, paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700196 paint.setShader(nullptr);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000197}
198
199void FatBits::drawFG(SkCanvas* canvas) {
reed@google.comc83e3522012-10-22 22:00:08 +0000200 SkPaint inner, outer;
201
202 inner.setAntiAlias(true);
203 inner.setColor(SK_ColorBLACK);
204 inner.setStrokeWidth(PIXEL_CENTER_SIZE);
205
206 outer.setAntiAlias(true);
207 outer.setColor(SK_ColorWHITE);
208 outer.setStrokeWidth(PIXEL_CENTER_SIZE + 2);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000209
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000210 SkScalar half = SkIntToScalar(fZoom) / 2;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000211 for (int iy = 0; iy < fH; ++iy) {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000212 SkScalar y = SkIntToScalar(iy * fZoom) + half;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000213 for (int ix = 0; ix < fW; ++ix) {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000214 SkScalar x = SkIntToScalar(ix * fZoom) + half;
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000215
reed@google.comc83e3522012-10-22 22:00:08 +0000216 canvas->drawPoint(x, y, outer);
217 canvas->drawPoint(x, y, inner);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000218 }
219 }
reed@google.com9c0bef12012-11-02 19:59:18 +0000220
221 if (fUseClip) {
222 SkPaint p;
223 p.setStyle(SkPaint::kStroke_Style);
224 p.setColor(SK_ColorLTGRAY);
225 SkRect r = {
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000226 fClipRect.fLeft * fZoom,
227 fClipRect.fTop * fZoom,
228 fClipRect.fRight * fZoom,
229 fClipRect.fBottom * fZoom
reed@google.com9c0bef12012-11-02 19:59:18 +0000230 };
231 canvas->drawRect(r, p);
232 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000233}
234
235void FatBits::drawLineSkeleton(SkCanvas* max, const SkPoint pts[]) {
236 SkPaint paint;
reed@google.comc83e3522012-10-22 22:00:08 +0000237 this->setupSkeletonPaint(&paint);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000238
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000239 SkPath path;
240 path.moveTo(pts[0]);
241 path.lineTo(pts[1]);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000242
reed8716a8a2016-09-19 07:10:36 -0700243 if (fStyle == kStroke_Style) {
244 SkPaint p;
245 p.setStyle(SkPaint::kStroke_Style);
Mike Reedce54bce2017-11-01 21:17:11 -0400246 p.setStrokeWidth(fStrokeWidth * fZoom);
reed8716a8a2016-09-19 07:10:36 -0700247 p.setStrokeCap(fStrokeCap);
248 SkPath dst;
249 p.getFillPath(path, &dst);
250 path = dst;
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000251
reed8716a8a2016-09-19 07:10:36 -0700252 path.moveTo(pts[0]);
253 path.lineTo(pts[1]);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000254 }
255 max->drawPath(path, paint);
256}
257
258void FatBits::drawLine(SkCanvas* canvas, SkPoint pts[]) {
259 SkPaint paint;
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000260
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000261 fInverse.mapPoints(pts, 2);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000262
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000263 if (fGrid) {
reed@google.combbe66f02013-10-30 13:41:19 +0000264 apply_grid(pts, 2);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000265 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000266
reede8f30622016-03-23 18:59:25 -0700267 erase(fMinSurface.get());
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000268 this->setupPaint(&paint);
reed@google.comc83e3522012-10-22 22:00:08 +0000269 paint.setColor(FAT_PIXEL_COLOR);
reed@google.com9c0bef12012-11-02 19:59:18 +0000270 if (fUseClip) {
271 fMinSurface->getCanvas()->save();
272 SkRect r = fClipRect;
273 r.inset(SK_Scalar1/3, SK_Scalar1/3);
Mike Reedc1f77742016-12-09 09:00:50 -0500274 fMinSurface->getCanvas()->clipRect(r, kIntersect_SkClipOp, true);
reed@google.com9c0bef12012-11-02 19:59:18 +0000275 }
Hal Canary23e474c2017-05-15 13:35:35 -0400276 fMinSurface->getCanvas()->drawLine(pts[0], pts[1], paint);
reed@google.com9c0bef12012-11-02 19:59:18 +0000277 if (fUseClip) {
278 fMinSurface->getCanvas()->restore();
279 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000280 this->copyMinToMax();
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000281
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000282 SkCanvas* max = fMaxSurface->getCanvas();
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000283
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000284 fMatrix.mapPoints(pts, 2);
285 this->drawLineSkeleton(max, pts);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000286
halcanary96fcdcc2015-08-27 07:41:13 -0700287 fMaxSurface->draw(canvas, 0, 0, nullptr);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000288}
289
290void FatBits::drawRect(SkCanvas* canvas, SkPoint pts[2]) {
291 SkPaint paint;
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000292
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000293 fInverse.mapPoints(pts, 2);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000294
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000295 if (fGrid) {
reed@google.combbe66f02013-10-30 13:41:19 +0000296 apply_grid(pts, 2);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000297 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000298
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000299 SkRect r;
300 r.set(pts, 2);
301
reede8f30622016-03-23 18:59:25 -0700302 erase(fMinSurface.get());
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000303 this->setupPaint(&paint);
reed@google.comc83e3522012-10-22 22:00:08 +0000304 paint.setColor(FAT_PIXEL_COLOR);
mike@reedtribe.org64494e92013-03-06 02:18:33 +0000305 {
306 SkCanvas* c = fMinSurface->getCanvas();
307 fRectAsOval ? c->drawOval(r, paint) : c->drawRect(r, paint);
308 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000309 this->copyMinToMax();
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000310
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000311 SkCanvas* max = fMaxSurface->getCanvas();
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000312
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000313 fMatrix.mapPoints(pts, 2);
314 r.set(pts, 2);
315 this->drawRectSkeleton(max, r);
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000316
halcanary96fcdcc2015-08-27 07:41:13 -0700317 fMaxSurface->draw(canvas, 0, 0, nullptr);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000318}
319
reed@google.combbe66f02013-10-30 13:41:19 +0000320void FatBits::drawTriangleSkeleton(SkCanvas* max, const SkPoint pts[]) {
321 SkPaint paint;
322 this->setupSkeletonPaint(&paint);
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000323
reed@google.combbe66f02013-10-30 13:41:19 +0000324 SkPath path;
325 path.moveTo(pts[0]);
326 path.lineTo(pts[1]);
327 path.lineTo(pts[2]);
328 path.close();
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000329
reed@google.combbe66f02013-10-30 13:41:19 +0000330 max->drawPath(path, paint);
331}
332
333void FatBits::drawTriangle(SkCanvas* canvas, SkPoint pts[3]) {
334 SkPaint paint;
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000335
reed@google.combbe66f02013-10-30 13:41:19 +0000336 fInverse.mapPoints(pts, 3);
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000337
reed@google.combbe66f02013-10-30 13:41:19 +0000338 if (fGrid) {
339 apply_grid(pts, 3);
340 }
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000341
reed@google.combbe66f02013-10-30 13:41:19 +0000342 SkPath path;
343 path.moveTo(pts[0]);
344 path.lineTo(pts[1]);
345 path.lineTo(pts[2]);
346 path.close();
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000347
reede8f30622016-03-23 18:59:25 -0700348 erase(fMinSurface.get());
reed@google.combbe66f02013-10-30 13:41:19 +0000349 this->setupPaint(&paint);
350 paint.setColor(FAT_PIXEL_COLOR);
351 fMinSurface->getCanvas()->drawPath(path, paint);
352 this->copyMinToMax();
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000353
reed@google.combbe66f02013-10-30 13:41:19 +0000354 SkCanvas* max = fMaxSurface->getCanvas();
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000355
reed@google.combbe66f02013-10-30 13:41:19 +0000356 fMatrix.mapPoints(pts, 3);
357 this->drawTriangleSkeleton(max, pts);
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000358
halcanary96fcdcc2015-08-27 07:41:13 -0700359 fMaxSurface->draw(canvas, 0, 0, nullptr);
reed@google.combbe66f02013-10-30 13:41:19 +0000360}
361
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000362///////////////////////////////////////////////////////////////////////////////////////////////////
363
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400364class IndexClick : public Sample::Click {
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000365 int fIndex;
366public:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400367 IndexClick(Sample* v, int index) : Sample::Click(v), fIndex(index) {}
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000368
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400369 static int GetIndex(Sample::Click* click) {
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000370 return ((IndexClick*)click)->fIndex;
371 }
372};
373
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400374class DrawLineView : public Sample {
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000375 FatBits fFB;
reed@google.combbe66f02013-10-30 13:41:19 +0000376 SkPoint fPts[3];
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000377 bool fIsRect;
reed8716a8a2016-09-19 07:10:36 -0700378 int fZoom = 64;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000379public:
380 DrawLineView() {
reed8716a8a2016-09-19 07:10:36 -0700381 fFB.setWHZ(24*2, 16*2, fZoom);
reedbb886742015-08-15 07:42:31 -0700382 fPts[0].set(1, 1);
383 fPts[1].set(5, 4);
384 fPts[2].set(2, 6);
reed8716a8a2016-09-19 07:10:36 -0700385 SkMatrix::MakeScale(SkIntToScalar(fZoom)).mapPoints(fPts, 3);
reed@google.comcdbcb3e2012-10-22 22:10:20 +0000386 fIsRect = false;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000387 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000388
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000389 void setStyle(FatBits::Style s) {
390 fFB.setStyle(s);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000391 }
392
393protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400394 bool onQuery(Sample::Event* evt) override {
395 if (Sample::TitleQ(*evt)) {
396 Sample::TitleR(evt, "FatBits");
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000397 return true;
398 }
399 SkUnichar uni;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400400 if (Sample::CharQ(*evt, &uni)) {
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000401 switch (uni) {
reed@google.com9c0bef12012-11-02 19:59:18 +0000402 case 'c':
403 fFB.setUseClip(!fFB.getUseClip());
reed@google.com9c0bef12012-11-02 19:59:18 +0000404 return true;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000405 case 'r':
406 fIsRect = !fIsRect;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000407 return true;
mike@reedtribe.org64494e92013-03-06 02:18:33 +0000408 case 'o':
409 fFB.toggleRectAsOval();
mike@reedtribe.org64494e92013-03-06 02:18:33 +0000410 return true;
reed@google.comc83e3522012-10-22 22:00:08 +0000411 case 'x':
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000412 fFB.setGrid(!fFB.getGrid());
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000413 return true;
414 case 's':
415 if (FatBits::kStroke_Style == fFB.getStyle()) {
416 this->setStyle(FatBits::kHair_Style);
417 } else {
418 this->setStyle(FatBits::kStroke_Style);
419 }
420 return true;
reeda5d12072015-11-16 18:57:18 -0800421 case 'k': {
422 const SkPaint::Cap caps[] = {
423 SkPaint::kButt_Cap, SkPaint::kRound_Cap, SkPaint::kSquare_Cap,
424 };
425 fFB.fStrokeCap = caps[(fFB.fStrokeCap + 1) % 3];
reeda5d12072015-11-16 18:57:18 -0800426 return true;
427 } break;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000428 case 'a':
429 fFB.setAA(!fFB.getAA());
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000430 return true;
reed@google.comc83e3522012-10-22 22:00:08 +0000431 case 'w':
432 fFB.setShowSkeleton(!fFB.getShowSkeleton());
reed@google.comc83e3522012-10-22 22:00:08 +0000433 return true;
434 case 'g':
reed8716a8a2016-09-19 07:10:36 -0700435 fFB.togglePixelColors();
reed@google.comc83e3522012-10-22 22:00:08 +0000436 return true;
reed@google.combbe66f02013-10-30 13:41:19 +0000437 case 't':
438 fFB.setTriangle(!fFB.getTriangle());
reed@google.combbe66f02013-10-30 13:41:19 +0000439 return true;
Mike Reedce54bce2017-11-01 21:17:11 -0400440 case '-':
441 fFB.fStrokeWidth -= 0.125f;
Mike Reedce54bce2017-11-01 21:17:11 -0400442 return true;
443 case '=':
444 fFB.fStrokeWidth += 0.125f;
Mike Reedce54bce2017-11-01 21:17:11 -0400445 return true;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000446 }
447 }
448 return this->INHERITED::onQuery(evt);
449 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000450
mtkleinf0599002015-07-13 06:18:39 -0700451 void onDrawContent(SkCanvas* canvas) override {
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000452 fFB.drawBG(canvas);
reed@google.combbe66f02013-10-30 13:41:19 +0000453 if (fFB.getTriangle()) {
454 fFB.drawTriangle(canvas, fPts);
455 }
456 else if (fIsRect) {
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000457 fFB.drawRect(canvas, fPts);
458 } else {
459 fFB.drawLine(canvas, fPts);
460 }
461 fFB.drawFG(canvas);
skia.committer@gmail.com1e34ff72012-10-24 02:01:24 +0000462
reed@google.comffe9d012012-10-23 15:33:41 +0000463 {
464 SkString str;
reed8716a8a2016-09-19 07:10:36 -0700465 str.printf("%s %s %s",
reed@google.comffe9d012012-10-23 15:33:41 +0000466 fFB.getAA() ? "AA" : "BW",
467 FatBits::kHair_Style == fFB.getStyle() ? "Hair" : "Stroke",
reed@google.com9c0bef12012-11-02 19:59:18 +0000468 fFB.getUseClip() ? "clip" : "noclip");
reed@google.comffe9d012012-10-23 15:33:41 +0000469 SkPaint paint;
reed@google.comffe9d012012-10-23 15:33:41 +0000470 paint.setColor(SK_ColorBLUE);
Hal Canary4484b8f2019-01-08 14:00:08 -0500471 SkFont font(nullptr, 16);
472 canvas->drawString(str, 10, 16, font, paint);
reed@google.comffe9d012012-10-23 15:33:41 +0000473 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000474 }
475
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400476 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000477 SkPoint pt = { x, y };
478 int index = -1;
reed@google.combbe66f02013-10-30 13:41:19 +0000479 int count = fFB.getTriangle() ? 3 : 2;
reed@google.comcdbcb3e2012-10-22 22:10:20 +0000480 SkScalar tol = 12;
skia.committer@gmail.com7ed98df2013-10-31 07:01:53 +0000481
reed@google.combbe66f02013-10-30 13:41:19 +0000482 for (int i = 0; i < count; ++i) {
Cary Clarkdf429f32017-11-08 11:44:31 -0500483 if (SkPointPriv::EqualsWithinTolerance(fPts[i], pt, tol)) {
reed@google.combbe66f02013-10-30 13:41:19 +0000484 index = i;
485 break;
486 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000487 }
mike@reedtribe.orgc892a152012-10-23 03:10:46 +0000488 return new IndexClick(this, index);
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000489 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000490
mtklein36352bf2015-03-25 18:17:31 -0700491 bool onClick(Click* click) override {
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000492 int index = IndexClick::GetIndex(click);
reed@google.combbe66f02013-10-30 13:41:19 +0000493 if (index >= 0 && index <= 2) {
mike@reedtribe.orgc892a152012-10-23 03:10:46 +0000494 fPts[index] = click->fCurr;
495 } else {
496 SkScalar dx = click->fCurr.fX - click->fPrev.fX;
497 SkScalar dy = click->fCurr.fY - click->fPrev.fY;
498 fPts[0].offset(dx, dy);
499 fPts[1].offset(dx, dy);
reed@google.combbe66f02013-10-30 13:41:19 +0000500 fPts[2].offset(dx, dy);
mike@reedtribe.orgc892a152012-10-23 03:10:46 +0000501 }
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000502 return true;
503 }
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000504
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000505private:
skia.committer@gmail.com45ba2f72012-10-23 02:01:22 +0000506
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400507 typedef Sample INHERITED;
mike@reedtribe.org50e4c722012-10-22 03:59:34 +0000508};
509
510//////////////////////////////////////////////////////////////////////////////
511
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400512DEF_SAMPLE( return new DrawLineView(); )