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.
|
|
|
|
*/
|
|
|
|
|
2015-01-26 20:49:00 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2012-10-22 03:59:34 +00:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "SkImage.h"
|
|
|
|
#include "SkSurface.h"
|
|
|
|
|
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;
|
|
|
|
fGrid = true;
|
2012-10-22 22:00:08 +00:00
|
|
|
fShowSkeleton = true;
|
|
|
|
fUseGPU = false;
|
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;
|
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
|
|
|
|
2012-10-22 22:00:08 +00:00
|
|
|
bool getUseGPU() const { return fUseGPU; }
|
|
|
|
void setUseGPU(bool ug) { fUseGPU = ug; }
|
2013-10-31 07:01:53 +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; }
|
|
|
|
|
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);
|
2015-01-26 20:49:00 +00:00
|
|
|
fShader.reset(sk_tool_utils::create_checkerboard_shader(
|
2015-01-26 19:24:32 +00:00
|
|
|
0xFFCCCCCC, 0xFFFFFFFF, zoom));
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2014-01-15 02:38:22 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
|
2013-10-30 13:41:19 +00:00
|
|
|
fMinSurface.reset(SkSurface::NewRaster(info));
|
2014-09-03 18:54:58 +00:00
|
|
|
info = info.makeWH(width * zoom, height * zoom);
|
2013-10-30 13:41:19 +00:00
|
|
|
fMaxSurface.reset(SkSurface::NewRaster(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
|
|
|
|
|
|
|
private:
|
2013-10-30 13:41:19 +00:00
|
|
|
bool fAA, fGrid, fShowSkeleton, fUseGPU, 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;
|
2012-10-22 03:59:34 +00:00
|
|
|
SkMatrix fMatrix, fInverse;
|
2012-11-02 19:59:18 +00:00
|
|
|
SkRect fBounds, fClipRect;
|
2012-10-22 03:59:34 +00:00
|
|
|
SkAutoTUnref<SkShader> fShader;
|
|
|
|
SkAutoTUnref<SkSurface> fMinSurface;
|
|
|
|
SkAutoTUnref<SkSurface> fMaxSurface;
|
|
|
|
|
|
|
|
void setupPaint(SkPaint* paint) {
|
|
|
|
bool aa = this->getAA();
|
|
|
|
switch (fStyle) {
|
|
|
|
case kHair_Style:
|
|
|
|
paint->setStrokeWidth(0);
|
|
|
|
break;
|
|
|
|
case kStroke_Style:
|
2012-10-22 22:10:20 +00:00
|
|
|
paint->setStrokeWidth(SK_Scalar1);
|
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;
|
|
|
|
|
|
|
|
if (fUseGPU && fAA) {
|
|
|
|
SkRect rr = r;
|
2012-12-12 15:58:25 +00:00
|
|
|
rr.inset(SkIntToScalar(fZoom)/2, SkIntToScalar(fZoom)/2);
|
2012-10-22 22:00:08 +00:00
|
|
|
path.addRect(rr);
|
|
|
|
path.moveTo(rr.fLeft, rr.fTop);
|
|
|
|
path.lineTo(rr.fRight, rr.fBottom);
|
|
|
|
rr = r;
|
2012-12-12 15:58:25 +00:00
|
|
|
rr.inset(-SkIntToScalar(fZoom)/2, -SkIntToScalar(fZoom)/2);
|
2012-10-22 22:00:08 +00:00
|
|
|
path.addRect(rr);
|
|
|
|
} else {
|
2013-03-06 02:18:33 +00:00
|
|
|
fRectAsOval ? path.addOval(r) : path.addRect(r);
|
2012-10-22 22:00:08 +00:00
|
|
|
if (fUseGPU) {
|
|
|
|
path.moveTo(r.fLeft, r.fTop);
|
|
|
|
path.lineTo(r.fRight, r.fBottom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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() {
|
|
|
|
erase(fMaxSurface);
|
|
|
|
SkCanvas* canvas = fMaxSurface->getCanvas();
|
|
|
|
canvas->save();
|
|
|
|
canvas->concat(fMatrix);
|
|
|
|
fMinSurface->draw(canvas, 0, 0, NULL);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setXfermodeMode(SkXfermode::kClear_Mode);
|
|
|
|
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);
|
|
|
|
paint.setShader(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
switch (fStyle) {
|
|
|
|
case kHair_Style:
|
2012-10-22 22:00:08 +00:00
|
|
|
if (fUseGPU) {
|
|
|
|
SkPaint p;
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
2012-12-12 15:58:25 +00:00
|
|
|
p.setStrokeWidth(SK_Scalar1 * fZoom);
|
2012-10-22 22:00:08 +00:00
|
|
|
SkPath dst;
|
|
|
|
p.getFillPath(path, &dst);
|
|
|
|
path.addPath(dst);
|
|
|
|
}
|
2012-10-22 03:59:34 +00:00
|
|
|
break;
|
|
|
|
case kStroke_Style: {
|
|
|
|
SkPaint p;
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
2012-12-12 15:58:25 +00:00
|
|
|
p.setStrokeWidth(SK_Scalar1 * fZoom);
|
2012-10-22 03:59:34 +00:00
|
|
|
SkPath dst;
|
|
|
|
p.getFillPath(path, &dst);
|
|
|
|
path = dst;
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2012-10-22 22:00:08 +00:00
|
|
|
if (fUseGPU) {
|
|
|
|
path.moveTo(dst.getPoint(0));
|
|
|
|
path.lineTo(dst.getPoint(2));
|
|
|
|
}
|
2012-10-22 03:59:34 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
erase(fMinSurface);
|
|
|
|
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);
|
|
|
|
fMinSurface->getCanvas()->clipRect(r, SkRegion::kIntersect_Op, true);
|
|
|
|
}
|
2012-10-22 03:59:34 +00:00
|
|
|
fMinSurface->getCanvas()->drawLine(pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, 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
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
fMaxSurface->draw(canvas, 0, 0, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
erase(fMinSurface);
|
|
|
|
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
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
fMaxSurface->draw(canvas, 0, 0, NULL);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-10-30 13:41:19 +00:00
|
|
|
erase(fMinSurface);
|
|
|
|
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
|
|
|
|
2013-10-30 13:41:19 +00:00
|
|
|
fMaxSurface->draw(canvas, 0, 0, NULL);
|
|
|
|
}
|
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class IndexClick : public SkView::Click {
|
|
|
|
int fIndex;
|
|
|
|
public:
|
|
|
|
IndexClick(SkView* v, int index) : SkView::Click(v), fIndex(index) {}
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
static int GetIndex(SkView::Click* click) {
|
|
|
|
return ((IndexClick*)click)->fIndex;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class DrawLineView : public SampleView {
|
|
|
|
FatBits fFB;
|
2013-10-30 13:41:19 +00:00
|
|
|
SkPoint fPts[3];
|
2012-10-22 03:59:34 +00:00
|
|
|
bool fIsRect;
|
|
|
|
public:
|
|
|
|
DrawLineView() {
|
|
|
|
fFB.setWHZ(24, 16, 48);
|
2012-10-22 22:10:20 +00:00
|
|
|
fPts[0].set(48, 48);
|
|
|
|
fPts[1].set(48 * 5, 48 * 4);
|
2013-10-30 13:41:19 +00:00
|
|
|
fPts[2].set(48 * 2, 48 * 6);
|
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);
|
|
|
|
this->inval(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
bool onQuery(SkEvent* evt) override {
|
2012-10-22 03:59:34 +00:00
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "FatBits");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
SkUnichar uni;
|
|
|
|
if (SampleCode::CharQ(*evt, &uni)) {
|
|
|
|
switch (uni) {
|
2012-11-02 19:59:18 +00:00
|
|
|
case 'c':
|
|
|
|
fFB.setUseClip(!fFB.getUseClip());
|
|
|
|
this->inval(NULL);
|
|
|
|
return true;
|
2012-10-22 03:59:34 +00:00
|
|
|
case 'r':
|
|
|
|
fIsRect = !fIsRect;
|
|
|
|
this->inval(NULL);
|
|
|
|
return true;
|
2013-03-06 02:18:33 +00:00
|
|
|
case 'o':
|
|
|
|
fFB.toggleRectAsOval();
|
|
|
|
this->inval(NULL);
|
|
|
|
return true;
|
2012-10-22 22:00:08 +00:00
|
|
|
case 'x':
|
2012-10-22 03:59:34 +00:00
|
|
|
fFB.setGrid(!fFB.getGrid());
|
|
|
|
this->inval(NULL);
|
|
|
|
return true;
|
|
|
|
case 's':
|
|
|
|
if (FatBits::kStroke_Style == fFB.getStyle()) {
|
|
|
|
this->setStyle(FatBits::kHair_Style);
|
|
|
|
} else {
|
|
|
|
this->setStyle(FatBits::kStroke_Style);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case 'a':
|
|
|
|
fFB.setAA(!fFB.getAA());
|
|
|
|
this->inval(NULL);
|
|
|
|
return true;
|
2012-10-22 22:00:08 +00:00
|
|
|
case 'w':
|
|
|
|
fFB.setShowSkeleton(!fFB.getShowSkeleton());
|
|
|
|
this->inval(NULL);
|
|
|
|
return true;
|
|
|
|
case 'g':
|
|
|
|
fFB.setUseGPU(!fFB.getUseGPU());
|
|
|
|
this->inval(NULL);
|
|
|
|
return true;
|
2013-10-30 13:41:19 +00:00
|
|
|
case 't':
|
|
|
|
fFB.setTriangle(!fFB.getTriangle());
|
|
|
|
this->inval(NULL);
|
|
|
|
return true;
|
2012-10-22 03:59:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2012-10-23 02:01:22 +00:00
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
|
|
|
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;
|
2012-11-02 19:59:18 +00:00
|
|
|
str.printf("%s %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.getUseGPU() ? "GPU" : "CPU",
|
|
|
|
fFB.getUseClip() ? "clip" : "noclip");
|
2012-10-23 15:33:41 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setTextSize(16);
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
canvas->drawText(str.c_str(), str.size(), 10, 16, paint);
|
|
|
|
}
|
2012-10-22 03:59:34 +00:00
|
|
|
}
|
|
|
|
|
2013-01-28 02:21:27 +00:00
|
|
|
virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
|
2015-03-26 01:17:31 +00:00
|
|
|
unsigned 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) {
|
|
|
|
if (fPts[i].equalsWithinTolerance(pt, tol)) {
|
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
2012-10-22 03:59:34 +00:00
|
|
|
}
|
2012-10-23 03:10:46 +00:00
|
|
|
return new IndexClick(this, 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
|
|
|
this->inval(NULL);
|
|
|
|
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
|
|
|
|
2012-10-22 03:59:34 +00:00
|
|
|
typedef SampleView INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new DrawLineView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|