2012-10-22 03:59:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBlendMode.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "src/core/SkClipOpPriv.h"
|
|
|
|
#include "src/core/SkPointPriv.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2018-03-14 19:07:43 +00:00
|
|
|
|
|
|
|
class SkEvent;
|
2012-10-22 03:59:34 +00:00
|
|
|
|
2012-10-22 22:00:08 +00:00
|
|
|
#define FAT_PIXEL_COLOR SK_ColorBLACK
|
|
|
|
#define PIXEL_CENTER_SIZE 3
|
|
|
|
#define WIRE_FRAME_COLOR 0xFFFF0000 /*0xFF00FFFF*/
|
|
|
|
#define WIRE_FRAME_SIZE 1.5f
|
|
|
|
|
2013-10-30 13:41:19 +00:00
|
|
|
static SkScalar apply_grid(SkScalar x) {
|
|
|
|
const SkScalar grid = 2;
|
|
|
|
return SkScalarRoundToScalar(x * grid) / grid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void apply_grid(SkPoint pts[], int count) {
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
pts[i].set(apply_grid(pts[i].fX), apply_grid(pts[i].fY));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
static void erase(SkSurface* surface) {
|
2012-12-06 21:47:40 +00:00
|
|
|
surface->getCanvas()->clear(SK_ColorTRANSPARENT);
|
2012-10-22 03:59:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class FatBits {
|
|
|
|
public:
|
2014-05-12 20:42:21 +00:00
|
|
|
FatBits() {
|
2012-10-22 03:59:34 +00:00
|
|
|
fAA = false;
|
|
|
|
fStyle = kHair_Style;
|
2015-08-15 14:42:31 +00:00
|
|
|
fGrid = false;
|
2012-10-22 22:00:08 +00:00
|
|
|
fShowSkeleton = true;
|
2012-11-02 19:59:18 +00:00
|
|
|
fUseClip = false;
|
2013-03-06 02:18:33 +00:00
|
|
|
fRectAsOval = false;
|
2013-10-30 13:41:19 +00:00
|
|
|
fUseTriangle = false;
|
2015-11-17 02:57:18 +00:00
|
|
|
fStrokeCap = SkPaint::kButt_Cap;
|
2012-11-06 13:18:25 +00:00
|
|
|
|
2012-11-02 19:59:18 +00:00
|
|
|
fClipRect.set(2, 2, 11, 8 );
|
2012-10-22 03:59:34 +00:00
|
|
|
}
|
|
|
|
|
2012-12-12 15:58:25 +00:00
|
|
|
int getZoom() const { return fZoom; }
|
2012-10-22 22:00:08 +00:00
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
bool getAA() const { return fAA; }
|
|
|
|
void setAA(bool aa) { fAA = aa; }
|
|
|
|
|
|
|
|
bool getGrid() const { return fGrid; }
|
|
|
|
void setGrid(bool g) { fGrid = g; }
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2012-10-22 22:00:08 +00:00
|
|
|
bool getShowSkeleton() const { return fShowSkeleton; }
|
|
|
|
void setShowSkeleton(bool ss) { fShowSkeleton = ss; }
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2013-10-30 13:41:19 +00:00
|
|
|
bool getTriangle() const { return fUseTriangle; }
|
|
|
|
void setTriangle(bool ut) { fUseTriangle = ut; }
|
2013-10-31 07:01:53 +00:00
|
|
|
|
2013-03-06 02:18:33 +00:00
|
|
|
void toggleRectAsOval() { fRectAsOval = !fRectAsOval; }
|
|
|
|
|
2016-09-19 14:10:36 +00:00
|
|
|
void togglePixelColors() {
|
|
|
|
if (fShader == fShader0) {
|
|
|
|
fShader = fShader1;
|
|
|
|
} else {
|
|
|
|
fShader = fShader0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 01:17:11 +00:00
|
|
|
float fStrokeWidth = 1;
|
|
|
|
|
2012-11-02 19:59:18 +00:00
|
|
|
bool getUseClip() const { return fUseClip; }
|
|
|
|
void setUseClip(bool uc) { fUseClip = uc; }
|
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
enum Style {
|
|
|
|
kHair_Style,
|
2012-10-22 22:00:08 +00:00
|
|
|
kStroke_Style,
|
2012-10-22 03:59:34 +00:00
|
|
|
};
|
|
|
|
Style getStyle() const { return fStyle; }
|
|
|
|
void setStyle(Style s) { fStyle = s; }
|
|
|
|
|
|
|
|
void setWHZ(int width, int height, int zoom) {
|
|
|
|
fW = width;
|
|
|
|
fH = height;
|
2012-12-12 15:58:25 +00:00
|
|
|
fZoom = zoom;
|
2012-10-22 03:59:34 +00:00
|
|
|
fBounds.set(0, 0, SkIntToScalar(width * zoom), SkIntToScalar(height * zoom));
|
|
|
|
fMatrix.setScale(SkIntToScalar(zoom), SkIntToScalar(zoom));
|
|
|
|
fInverse.setScale(SK_Scalar1 / zoom, SK_Scalar1 / zoom);
|
2019-03-20 16:12:10 +00:00
|
|
|
fShader0 = ToolUtils::create_checkerboard_shader(0xFFDDDDDD, 0xFFFFFFFF, zoom);
|
2019-04-09 17:55:36 +00:00
|
|
|
fShader1 = SkShaders::Color(SK_ColorWHITE);
|
2016-09-19 14:10:36 +00:00
|
|
|
fShader = fShader0;
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2014-01-15 02:38:22 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
|
2016-03-24 01:59:25 +00:00
|
|
|
fMinSurface = SkSurface::MakeRaster(info);
|
2014-09-03 18:54:58 +00:00
|
|
|
info = info.makeWH(width * zoom, height * zoom);
|
2016-03-24 01:59:25 +00:00
|
|
|
fMaxSurface = SkSurface::MakeRaster(info);
|
2012-10-22 03:59:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void drawBG(SkCanvas*);
|
|
|
|
void drawFG(SkCanvas*);
|
|
|
|
void drawLine(SkCanvas*, SkPoint pts[2]);
|
|
|
|
void drawRect(SkCanvas* canvas, SkPoint pts[2]);
|
2013-10-30 13:41:19 +00:00
|
|
|
void drawTriangle(SkCanvas* canvas, SkPoint pts[3]);
|
2012-10-22 03:59:34 +00:00
|
|
|
|
2015-11-17 02:57:18 +00:00
|
|
|
SkPaint::Cap fStrokeCap;
|
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
private:
|
2016-09-19 14:10:36 +00:00
|
|
|
bool fAA, fGrid, fShowSkeleton, fUseClip, fRectAsOval, fUseTriangle;
|
2012-10-22 03:59:34 +00:00
|
|
|
Style fStyle;
|
2012-12-12 15:58:25 +00:00
|
|
|
int fW, fH, fZoom;
|
2016-09-19 14:10:36 +00:00
|
|
|
SkMatrix fMatrix, fInverse;
|
|
|
|
SkRect fBounds, fClipRect;
|
|
|
|
sk_sp<SkShader> fShader0;
|
|
|
|
sk_sp<SkShader> fShader1;
|
|
|
|
sk_sp<SkShader> fShader;
|
|
|
|
sk_sp<SkSurface> fMinSurface;
|
|
|
|
sk_sp<SkSurface> fMaxSurface;
|
2012-10-22 03:59:34 +00:00
|
|
|
|
|
|
|
void setupPaint(SkPaint* paint) {
|
|
|
|
bool aa = this->getAA();
|
2015-11-17 02:57:18 +00:00
|
|
|
paint->setStrokeCap(fStrokeCap);
|
2012-10-22 03:59:34 +00:00
|
|
|
switch (fStyle) {
|
|
|
|
case kHair_Style:
|
|
|
|
paint->setStrokeWidth(0);
|
|
|
|
break;
|
|
|
|
case kStroke_Style:
|
2017-11-02 01:17:11 +00:00
|
|
|
paint->setStrokeWidth(fStrokeWidth);
|
2012-10-22 03:59:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
paint->setAntiAlias(aa);
|
|
|
|
}
|
|
|
|
|
2012-10-22 22:00:08 +00:00
|
|
|
void setupSkeletonPaint(SkPaint* paint) {
|
|
|
|
paint->setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint->setStrokeWidth(WIRE_FRAME_SIZE);
|
|
|
|
paint->setColor(fShowSkeleton ? WIRE_FRAME_COLOR : 0);
|
|
|
|
paint->setAntiAlias(true);
|
|
|
|
}
|
|
|
|
|
2013-10-30 13:41:19 +00:00
|
|
|
void drawTriangleSkeleton(SkCanvas* max, const SkPoint pts[]);
|
2012-10-22 03:59:34 +00:00
|
|
|
void drawLineSkeleton(SkCanvas* max, const SkPoint pts[]);
|
|
|
|
void drawRectSkeleton(SkCanvas* max, const SkRect& r) {
|
|
|
|
SkPaint paint;
|
2012-10-22 22:00:08 +00:00
|
|
|
this->setupSkeletonPaint(&paint);
|
|
|
|
SkPath path;
|
|
|
|
|
2016-09-19 14:10:36 +00:00
|
|
|
fRectAsOval ? path.addOval(r) : path.addRect(r);
|
2012-10-22 22:00:08 +00:00
|
|
|
max->drawPath(path, paint);
|
2012-10-22 03:59:34 +00:00
|
|
|
}
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
void copyMinToMax() {
|
2016-03-24 01:59:25 +00:00
|
|
|
erase(fMaxSurface.get());
|
2012-10-22 03:59:34 +00:00
|
|
|
SkCanvas* canvas = fMaxSurface->getCanvas();
|
|
|
|
canvas->save();
|
|
|
|
canvas->concat(fMatrix);
|
2015-08-27 14:41:13 +00:00
|
|
|
fMinSurface->draw(canvas, 0, 0, nullptr);
|
2012-10-22 03:59:34 +00:00
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
SkPaint paint;
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setBlendMode(SkBlendMode::kClear);
|
2012-10-22 03:59:34 +00:00
|
|
|
for (int iy = 1; iy < fH; ++iy) {
|
2012-12-12 15:58:25 +00:00
|
|
|
SkScalar y = SkIntToScalar(iy * fZoom);
|
2012-10-22 03:59:34 +00:00
|
|
|
canvas->drawLine(0, y - SK_ScalarHalf, 999, y - SK_ScalarHalf, paint);
|
|
|
|
}
|
|
|
|
for (int ix = 1; ix < fW; ++ix) {
|
2012-12-12 15:58:25 +00:00
|
|
|
SkScalar x = SkIntToScalar(ix * fZoom);
|
2012-10-22 03:59:34 +00:00
|
|
|
canvas->drawLine(x - SK_ScalarHalf, 0, x - SK_ScalarHalf, 999, paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void FatBits::drawBG(SkCanvas* canvas) {
|
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
paint.setShader(fShader);
|
|
|
|
canvas->drawRect(fBounds, paint);
|
2015-08-27 14:41:13 +00:00
|
|
|
paint.setShader(nullptr);
|
2012-10-22 03:59:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FatBits::drawFG(SkCanvas* canvas) {
|
2012-10-22 22:00:08 +00:00
|
|
|
SkPaint inner, outer;
|
|
|
|
|
|
|
|
inner.setAntiAlias(true);
|
|
|
|
inner.setColor(SK_ColorBLACK);
|
|
|
|
inner.setStrokeWidth(PIXEL_CENTER_SIZE);
|
|
|
|
|
|
|
|
outer.setAntiAlias(true);
|
|
|
|
outer.setColor(SK_ColorWHITE);
|
|
|
|
outer.setStrokeWidth(PIXEL_CENTER_SIZE + 2);
|
2012-10-22 03:59:34 +00:00
|
|
|
|
2012-12-12 15:58:25 +00:00
|
|
|
SkScalar half = SkIntToScalar(fZoom) / 2;
|
2012-10-22 03:59:34 +00:00
|
|
|
for (int iy = 0; iy < fH; ++iy) {
|
2012-12-12 15:58:25 +00:00
|
|
|
SkScalar y = SkIntToScalar(iy * fZoom) + half;
|
2012-10-22 03:59:34 +00:00
|
|
|
for (int ix = 0; ix < fW; ++ix) {
|
2012-12-12 15:58:25 +00:00
|
|
|
SkScalar x = SkIntToScalar(ix * fZoom) + half;
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2012-10-22 22:00:08 +00:00
|
|
|
canvas->drawPoint(x, y, outer);
|
|
|
|
canvas->drawPoint(x, y, inner);
|
2012-10-22 03:59:34 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-02 19:59:18 +00:00
|
|
|
|
|
|
|
if (fUseClip) {
|
|
|
|
SkPaint p;
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setColor(SK_ColorLTGRAY);
|
|
|
|
SkRect r = {
|
2012-12-12 15:58:25 +00:00
|
|
|
fClipRect.fLeft * fZoom,
|
|
|
|
fClipRect.fTop * fZoom,
|
|
|
|
fClipRect.fRight * fZoom,
|
|
|
|
fClipRect.fBottom * fZoom
|
2012-11-02 19:59:18 +00:00
|
|
|
};
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
}
|
2012-10-22 03:59:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FatBits::drawLineSkeleton(SkCanvas* max, const SkPoint pts[]) {
|
|
|
|
SkPaint paint;
|
2012-10-22 22:00:08 +00:00
|
|
|
this->setupSkeletonPaint(&paint);
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
SkPath path;
|
|
|
|
path.moveTo(pts[0]);
|
|
|
|
path.lineTo(pts[1]);
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2016-09-19 14:10:36 +00:00
|
|
|
if (fStyle == kStroke_Style) {
|
|
|
|
SkPaint p;
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
2017-11-02 01:17:11 +00:00
|
|
|
p.setStrokeWidth(fStrokeWidth * fZoom);
|
2016-09-19 14:10:36 +00:00
|
|
|
p.setStrokeCap(fStrokeCap);
|
|
|
|
SkPath dst;
|
|
|
|
p.getFillPath(path, &dst);
|
|
|
|
path = dst;
|
|
|
|
|
|
|
|
path.moveTo(pts[0]);
|
|
|
|
path.lineTo(pts[1]);
|
2012-10-22 03:59:34 +00:00
|
|
|
}
|
|
|
|
max->drawPath(path, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FatBits::drawLine(SkCanvas* canvas, SkPoint pts[]) {
|
|
|
|
SkPaint paint;
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
fInverse.mapPoints(pts, 2);
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
if (fGrid) {
|
2013-10-30 13:41:19 +00:00
|
|
|
apply_grid(pts, 2);
|
2012-10-22 03:59:34 +00:00
|
|
|
}
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2016-03-24 01:59:25 +00:00
|
|
|
erase(fMinSurface.get());
|
2012-10-22 03:59:34 +00:00
|
|
|
this->setupPaint(&paint);
|
2012-10-22 22:00:08 +00:00
|
|
|
paint.setColor(FAT_PIXEL_COLOR);
|
2012-11-02 19:59:18 +00:00
|
|
|
if (fUseClip) {
|
|
|
|
fMinSurface->getCanvas()->save();
|
|
|
|
SkRect r = fClipRect;
|
|
|
|
r.inset(SK_Scalar1/3, SK_Scalar1/3);
|
2016-12-09 14:00:50 +00:00
|
|
|
fMinSurface->getCanvas()->clipRect(r, kIntersect_SkClipOp, true);
|
2012-11-02 19:59:18 +00:00
|
|
|
}
|
2017-05-15 17:35:35 +00:00
|
|
|
fMinSurface->getCanvas()->drawLine(pts[0], pts[1], paint);
|
2012-11-02 19:59:18 +00:00
|
|
|
if (fUseClip) {
|
|
|
|
fMinSurface->getCanvas()->restore();
|
|
|
|
}
|
2012-10-22 03:59:34 +00:00
|
|
|
this->copyMinToMax();
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
SkCanvas* max = fMaxSurface->getCanvas();
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
fMatrix.mapPoints(pts, 2);
|
|
|
|
this->drawLineSkeleton(max, pts);
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
fMaxSurface->draw(canvas, 0, 0, nullptr);
|
2012-10-22 03:59:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FatBits::drawRect(SkCanvas* canvas, SkPoint pts[2]) {
|
|
|
|
SkPaint paint;
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
fInverse.mapPoints(pts, 2);
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
if (fGrid) {
|
2013-10-30 13:41:19 +00:00
|
|
|
apply_grid(pts, 2);
|
2012-10-22 03:59:34 +00:00
|
|
|
}
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
SkRect r;
|
|
|
|
r.set(pts, 2);
|
|
|
|
|
2016-03-24 01:59:25 +00:00
|
|
|
erase(fMinSurface.get());
|
2012-10-22 03:59:34 +00:00
|
|
|
this->setupPaint(&paint);
|
2012-10-22 22:00:08 +00:00
|
|
|
paint.setColor(FAT_PIXEL_COLOR);
|
2013-03-06 02:18:33 +00:00
|
|
|
{
|
|
|
|
SkCanvas* c = fMinSurface->getCanvas();
|
|
|
|
fRectAsOval ? c->drawOval(r, paint) : c->drawRect(r, paint);
|
|
|
|
}
|
2012-10-22 03:59:34 +00:00
|
|
|
this->copyMinToMax();
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
SkCanvas* max = fMaxSurface->getCanvas();
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
fMatrix.mapPoints(pts, 2);
|
|
|
|
r.set(pts, 2);
|
|
|
|
this->drawRectSkeleton(max, r);
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
fMaxSurface->draw(canvas, 0, 0, nullptr);
|
2012-10-22 03:59:34 +00:00
|
|
|
}
|
|
|
|
|
2013-10-30 13:41:19 +00:00
|
|
|
void FatBits::drawTriangleSkeleton(SkCanvas* max, const SkPoint pts[]) {
|
|
|
|
SkPaint paint;
|
|
|
|
this->setupSkeletonPaint(&paint);
|
2013-10-31 07:01:53 +00:00
|
|
|
|
2013-10-30 13:41:19 +00:00
|
|
|
SkPath path;
|
|
|
|
path.moveTo(pts[0]);
|
|
|
|
path.lineTo(pts[1]);
|
|
|
|
path.lineTo(pts[2]);
|
|
|
|
path.close();
|
2013-10-31 07:01:53 +00:00
|
|
|
|
2013-10-30 13:41:19 +00:00
|
|
|
max->drawPath(path, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FatBits::drawTriangle(SkCanvas* canvas, SkPoint pts[3]) {
|
|
|
|
SkPaint paint;
|
2013-10-31 07:01:53 +00:00
|
|
|
|
2013-10-30 13:41:19 +00:00
|
|
|
fInverse.mapPoints(pts, 3);
|
2013-10-31 07:01:53 +00:00
|
|
|
|
2013-10-30 13:41:19 +00:00
|
|
|
if (fGrid) {
|
|
|
|
apply_grid(pts, 3);
|
|
|
|
}
|
2013-10-31 07:01:53 +00:00
|
|
|
|
2013-10-30 13:41:19 +00:00
|
|
|
SkPath path;
|
|
|
|
path.moveTo(pts[0]);
|
|
|
|
path.lineTo(pts[1]);
|
|
|
|
path.lineTo(pts[2]);
|
|
|
|
path.close();
|
2013-10-31 07:01:53 +00:00
|
|
|
|
2016-03-24 01:59:25 +00:00
|
|
|
erase(fMinSurface.get());
|
2013-10-30 13:41:19 +00:00
|
|
|
this->setupPaint(&paint);
|
|
|
|
paint.setColor(FAT_PIXEL_COLOR);
|
|
|
|
fMinSurface->getCanvas()->drawPath(path, paint);
|
|
|
|
this->copyMinToMax();
|
2013-10-31 07:01:53 +00:00
|
|
|
|
2013-10-30 13:41:19 +00:00
|
|
|
SkCanvas* max = fMaxSurface->getCanvas();
|
2013-10-31 07:01:53 +00:00
|
|
|
|
2013-10-30 13:41:19 +00:00
|
|
|
fMatrix.mapPoints(pts, 3);
|
|
|
|
this->drawTriangleSkeleton(max, pts);
|
2013-10-31 07:01:53 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
fMaxSurface->draw(canvas, 0, 0, nullptr);
|
2013-10-30 13:41:19 +00:00
|
|
|
}
|
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class IndexClick : public Sample::Click {
|
2012-10-22 03:59:34 +00:00
|
|
|
int fIndex;
|
|
|
|
public:
|
2019-07-12 15:32:43 +00:00
|
|
|
IndexClick(int index) : fIndex(index) {}
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
static int GetIndex(Sample::Click* click) {
|
2012-10-22 03:59:34 +00:00
|
|
|
return ((IndexClick*)click)->fIndex;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class DrawLineView : public Sample {
|
2012-10-22 03:59:34 +00:00
|
|
|
FatBits fFB;
|
2013-10-30 13:41:19 +00:00
|
|
|
SkPoint fPts[3];
|
2012-10-22 03:59:34 +00:00
|
|
|
bool fIsRect;
|
2016-09-19 14:10:36 +00:00
|
|
|
int fZoom = 64;
|
2012-10-22 03:59:34 +00:00
|
|
|
public:
|
|
|
|
DrawLineView() {
|
2016-09-19 14:10:36 +00:00
|
|
|
fFB.setWHZ(24*2, 16*2, fZoom);
|
2015-08-15 14:42:31 +00:00
|
|
|
fPts[0].set(1, 1);
|
|
|
|
fPts[1].set(5, 4);
|
|
|
|
fPts[2].set(2, 6);
|
2016-09-19 14:10:36 +00:00
|
|
|
SkMatrix::MakeScale(SkIntToScalar(fZoom)).mapPoints(fPts, 3);
|
2012-10-22 22:10:20 +00:00
|
|
|
fIsRect = false;
|
2012-10-22 03:59:34 +00:00
|
|
|
}
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
void setStyle(FatBits::Style s) {
|
|
|
|
fFB.setStyle(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString("FatBits"); }
|
|
|
|
|
2019-07-03 19:53:04 +00:00
|
|
|
bool onChar(SkUnichar uni) override {
|
2012-10-22 03:59:34 +00:00
|
|
|
switch (uni) {
|
2012-11-02 19:59:18 +00:00
|
|
|
case 'c':
|
|
|
|
fFB.setUseClip(!fFB.getUseClip());
|
|
|
|
return true;
|
2012-10-22 03:59:34 +00:00
|
|
|
case 'r':
|
|
|
|
fIsRect = !fIsRect;
|
|
|
|
return true;
|
2013-03-06 02:18:33 +00:00
|
|
|
case 'o':
|
|
|
|
fFB.toggleRectAsOval();
|
|
|
|
return true;
|
2012-10-22 22:00:08 +00:00
|
|
|
case 'x':
|
2012-10-22 03:59:34 +00:00
|
|
|
fFB.setGrid(!fFB.getGrid());
|
|
|
|
return true;
|
|
|
|
case 's':
|
|
|
|
if (FatBits::kStroke_Style == fFB.getStyle()) {
|
|
|
|
this->setStyle(FatBits::kHair_Style);
|
|
|
|
} else {
|
|
|
|
this->setStyle(FatBits::kStroke_Style);
|
|
|
|
}
|
|
|
|
return true;
|
2015-11-17 02:57:18 +00:00
|
|
|
case 'k': {
|
|
|
|
const SkPaint::Cap caps[] = {
|
|
|
|
SkPaint::kButt_Cap, SkPaint::kRound_Cap, SkPaint::kSquare_Cap,
|
|
|
|
};
|
|
|
|
fFB.fStrokeCap = caps[(fFB.fStrokeCap + 1) % 3];
|
|
|
|
return true;
|
|
|
|
} break;
|
2012-10-22 03:59:34 +00:00
|
|
|
case 'a':
|
|
|
|
fFB.setAA(!fFB.getAA());
|
|
|
|
return true;
|
2012-10-22 22:00:08 +00:00
|
|
|
case 'w':
|
|
|
|
fFB.setShowSkeleton(!fFB.getShowSkeleton());
|
|
|
|
return true;
|
|
|
|
case 'g':
|
2016-09-19 14:10:36 +00:00
|
|
|
fFB.togglePixelColors();
|
2012-10-22 22:00:08 +00:00
|
|
|
return true;
|
2013-10-30 13:41:19 +00:00
|
|
|
case 't':
|
|
|
|
fFB.setTriangle(!fFB.getTriangle());
|
|
|
|
return true;
|
2017-11-02 01:17:11 +00:00
|
|
|
case '-':
|
|
|
|
fFB.fStrokeWidth -= 0.125f;
|
|
|
|
return true;
|
|
|
|
case '=':
|
|
|
|
fFB.fStrokeWidth += 0.125f;
|
|
|
|
return true;
|
2012-10-22 03:59:34 +00:00
|
|
|
}
|
2019-07-03 19:53:04 +00:00
|
|
|
return false;
|
2012-10-22 03:59:34 +00:00
|
|
|
}
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2015-07-13 13:18:39 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2012-10-22 03:59:34 +00:00
|
|
|
fFB.drawBG(canvas);
|
2013-10-30 13:41:19 +00:00
|
|
|
if (fFB.getTriangle()) {
|
|
|
|
fFB.drawTriangle(canvas, fPts);
|
|
|
|
}
|
|
|
|
else if (fIsRect) {
|
2012-10-22 03:59:34 +00:00
|
|
|
fFB.drawRect(canvas, fPts);
|
|
|
|
} else {
|
|
|
|
fFB.drawLine(canvas, fPts);
|
|
|
|
}
|
|
|
|
fFB.drawFG(canvas);
|
2012-10-24 02:01:24 +00:00
|
|
|
|
2012-10-23 15:33:41 +00:00
|
|
|
{
|
|
|
|
SkString str;
|
2016-09-19 14:10:36 +00:00
|
|
|
str.printf("%s %s %s",
|
2012-10-23 15:33:41 +00:00
|
|
|
fFB.getAA() ? "AA" : "BW",
|
|
|
|
FatBits::kHair_Style == fFB.getStyle() ? "Hair" : "Stroke",
|
2012-11-02 19:59:18 +00:00
|
|
|
fFB.getUseClip() ? "clip" : "noclip");
|
2012-10-23 15:33:41 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
2019-01-08 19:00:08 +00:00
|
|
|
SkFont font(nullptr, 16);
|
|
|
|
canvas->drawString(str, 10, 16, font, paint);
|
2012-10-23 15:33:41 +00:00
|
|
|
}
|
2012-10-22 03:59:34 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 20:07:57 +00:00
|
|
|
Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, ModifierKey modi) override {
|
2012-10-22 03:59:34 +00:00
|
|
|
SkPoint pt = { x, y };
|
|
|
|
int index = -1;
|
2013-10-30 13:41:19 +00:00
|
|
|
int count = fFB.getTriangle() ? 3 : 2;
|
2012-10-22 22:10:20 +00:00
|
|
|
SkScalar tol = 12;
|
2013-10-31 07:01:53 +00:00
|
|
|
|
2013-10-30 13:41:19 +00:00
|
|
|
for (int i = 0; i < count; ++i) {
|
2017-11-08 16:44:31 +00:00
|
|
|
if (SkPointPriv::EqualsWithinTolerance(fPts[i], pt, tol)) {
|
2013-10-30 13:41:19 +00:00
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
2012-10-22 03:59:34 +00:00
|
|
|
}
|
2019-07-12 15:32:43 +00:00
|
|
|
return new IndexClick(index);
|
2012-10-22 03:59:34 +00:00
|
|
|
}
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool onClick(Click* click) override {
|
2012-10-22 03:59:34 +00:00
|
|
|
int index = IndexClick::GetIndex(click);
|
2013-10-30 13:41:19 +00:00
|
|
|
if (index >= 0 && index <= 2) {
|
2012-10-23 03:10:46 +00:00
|
|
|
fPts[index] = click->fCurr;
|
|
|
|
} else {
|
|
|
|
SkScalar dx = click->fCurr.fX - click->fPrev.fX;
|
|
|
|
SkScalar dy = click->fCurr.fY - click->fPrev.fY;
|
|
|
|
fPts[0].offset(dx, dy);
|
|
|
|
fPts[1].offset(dx, dy);
|
2013-10-30 13:41:19 +00:00
|
|
|
fPts[2].offset(dx, dy);
|
2012-10-23 03:10:46 +00:00
|
|
|
}
|
2012-10-22 03:59:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
private:
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2012-10-22 03:59:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new DrawLineView(); )
|