2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2018-06-13 13:59:02 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkColorPriv.h"
|
|
|
|
#include "include/core/SkGraphics.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkRegion.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkTime.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/private/SkTo.h"
|
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "src/utils/SkUTF.h"
|
2009-02-06 13:30:58 +00:00
|
|
|
|
2018-06-18 19:11:00 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class PathClipView : public Sample {
|
2009-02-06 13:30:58 +00:00
|
|
|
public:
|
|
|
|
SkRect fOval;
|
|
|
|
SkPoint fCenter;
|
|
|
|
|
2015-09-28 13:16:07 +00:00
|
|
|
PathClipView() : fOval(SkRect::MakeWH(200, 50)), fCenter(SkPoint::Make(250, 250)) {}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-02-06 13:30:58 +00:00
|
|
|
protected:
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString("PathClip"); }
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-07-13 13:18:39 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2015-09-28 13:16:07 +00:00
|
|
|
const SkRect oval = fOval.makeOffset(fCenter.fX - fOval.centerX(),
|
|
|
|
fCenter.fY - fOval.centerY());
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-02-06 13:30:58 +00:00
|
|
|
SkPaint p;
|
|
|
|
p.setAntiAlias(true);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-02-06 13:30:58 +00:00
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawOval(oval, p);
|
|
|
|
|
2015-09-28 13:16:07 +00:00
|
|
|
const SkRect r = SkRect::MakeLTRB(200, 200, 300, 300);
|
2009-02-06 13:30:58 +00:00
|
|
|
canvas->clipRect(r);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-02-06 13:30:58 +00:00
|
|
|
p.setStyle(SkPaint::kFill_Style);
|
|
|
|
p.setColor(SK_ColorRED);
|
|
|
|
canvas->drawRect(r, p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-02-06 13:30:58 +00:00
|
|
|
p.setColor(0x800000FF);
|
|
|
|
canvas->drawOval(oval, p);
|
|
|
|
}
|
2009-11-16 14:52:01 +00:00
|
|
|
|
2019-07-08 20:07:57 +00:00
|
|
|
Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, ModifierKey) override {
|
2009-11-16 14:52:01 +00:00
|
|
|
return new Click(this);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-07-13 13:18:39 +00:00
|
|
|
bool onClick(Click* click) override {
|
2009-11-16 14:52:01 +00:00
|
|
|
fCenter.set(click->fCurr.fX, click->fCurr.fY);
|
2012-06-06 12:09:51 +00:00
|
|
|
return false;
|
2009-02-06 13:30:58 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-02-06 13:30:58 +00:00
|
|
|
private:
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2009-02-06 13:30:58 +00:00
|
|
|
};
|
2015-09-28 13:16:07 +00:00
|
|
|
DEF_SAMPLE( return new PathClipView; )
|
2009-02-06 13:30:58 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-09-28 13:16:07 +00:00
|
|
|
static int clip_line(const SkRect& bounds, SkPoint p0, SkPoint p1, SkPoint edges[]) {
|
|
|
|
SkPoint* edgesStart = edges;
|
|
|
|
|
|
|
|
if (p0.fY == p1.fY) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-28 13:16:07 +00:00
|
|
|
if (p0.fY > p1.fY) {
|
2018-06-18 19:11:00 +00:00
|
|
|
using std::swap;
|
|
|
|
swap(p0, p1);
|
2015-09-28 13:16:07 +00:00
|
|
|
}
|
|
|
|
// now we're monotonic in Y: p0 <= p1
|
|
|
|
if (p1.fY <= bounds.top() || p0.fY >= bounds.bottom()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-28 13:16:07 +00:00
|
|
|
double dxdy = (double)(p1.fX - p0.fX) / (p1.fY - p0.fY);
|
|
|
|
if (p0.fY < bounds.top()) {
|
|
|
|
p0.fX = SkDoubleToScalar(p0.fX + dxdy * (bounds.top() - p0.fY));
|
|
|
|
p0.fY = bounds.top();
|
|
|
|
}
|
|
|
|
if (p1.fY > bounds.bottom()) {
|
|
|
|
p1.fX = SkDoubleToScalar(p1.fX + dxdy * (bounds.bottom() - p1.fY));
|
|
|
|
p1.fY = bounds.bottom();
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-28 13:16:07 +00:00
|
|
|
// Now p0...p1 is strictly inside bounds vertically, so we just need to clip horizontally
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-28 13:16:07 +00:00
|
|
|
if (p0.fX > p1.fX) {
|
2018-06-18 19:11:00 +00:00
|
|
|
using std::swap;
|
|
|
|
swap(p0, p1);
|
2015-09-28 13:16:07 +00:00
|
|
|
}
|
|
|
|
// now we're left-to-right: p0 .. p1
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-28 13:16:07 +00:00
|
|
|
if (p1.fX <= bounds.left()) { // entirely to the left
|
|
|
|
p0.fX = p1.fX = bounds.left();
|
|
|
|
*edges++ = p0;
|
|
|
|
*edges++ = p1;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (p0.fX >= bounds.right()) { // entirely to the right
|
|
|
|
p0.fX = p1.fX = bounds.right();
|
|
|
|
*edges++ = p0;
|
|
|
|
*edges++ = p1;
|
|
|
|
return 2;
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-28 13:16:07 +00:00
|
|
|
if (p0.fX < bounds.left()) {
|
|
|
|
float y = SkDoubleToScalar(p0.fY + (bounds.left() - p0.fX) / dxdy);
|
|
|
|
*edges++ = SkPoint::Make(bounds.left(), p0.fY);
|
|
|
|
*edges++ = SkPoint::Make(bounds.left(), y);
|
|
|
|
p0.set(bounds.left(), y);
|
|
|
|
}
|
|
|
|
if (p1.fX > bounds.right()) {
|
|
|
|
float y = SkDoubleToScalar(p0.fY + (bounds.right() - p0.fX) / dxdy);
|
|
|
|
*edges++ = p0;
|
|
|
|
*edges++ = SkPoint::Make(bounds.right(), y);
|
|
|
|
*edges++ = SkPoint::Make(bounds.right(), p1.fY);
|
|
|
|
} else {
|
|
|
|
*edges++ = p0;
|
|
|
|
*edges++ = p1;
|
|
|
|
}
|
|
|
|
return SkToInt(edges - edgesStart);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_clipped_line(SkCanvas* canvas, const SkRect& bounds,
|
|
|
|
SkPoint p0, SkPoint p1, const SkPaint& paint) {
|
|
|
|
SkPoint verts[6];
|
|
|
|
int count = clip_line(bounds, p0, p1, verts);
|
|
|
|
|
|
|
|
SkPath path;
|
|
|
|
path.addPoly(verts, count, false);
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Demonstrate edge-clipping that is used in the scan converter
|
|
|
|
//
|
2018-08-08 15:36:17 +00:00
|
|
|
class EdgeClipView : public Sample {
|
2015-09-28 13:16:07 +00:00
|
|
|
enum {
|
|
|
|
N = 3
|
|
|
|
};
|
|
|
|
public:
|
|
|
|
SkPoint fPoly[N];
|
|
|
|
SkRect fClip;
|
|
|
|
SkColor fEdgeColor[N];
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-28 13:16:07 +00:00
|
|
|
EdgeClipView() : fClip(SkRect::MakeLTRB(150, 150, 550, 450)) {
|
|
|
|
fPoly[0].set(300, 40);
|
|
|
|
fPoly[1].set(550, 250);
|
|
|
|
fPoly[2].set(40, 450);
|
|
|
|
|
|
|
|
fEdgeColor[0] = 0xFFFF0000;
|
|
|
|
fEdgeColor[1] = 0xFF00FF00;
|
|
|
|
fEdgeColor[2] = 0xFF0000FF;
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-28 13:16:07 +00:00
|
|
|
protected:
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString("EdgeClip"); }
|
2015-09-28 13:16:07 +00:00
|
|
|
|
|
|
|
static SkScalar snap(SkScalar x) {
|
|
|
|
return SkScalarRoundToScalar(x * 0.5f) * 2;
|
|
|
|
}
|
|
|
|
static SkPoint snap(const SkPoint& pt) {
|
|
|
|
return SkPoint::Make(snap(pt.x()), snap(pt.y()));
|
|
|
|
}
|
|
|
|
static void snap(SkPoint dst[], const SkPoint src[], int count) {
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
dst[i] = snap(src[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
|
|
|
SkPath path;
|
|
|
|
path.addPoly(fPoly, N, true);
|
|
|
|
|
|
|
|
// Draw the full triangle, stroked and filled
|
|
|
|
SkPaint p;
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
p.setColor(0xFFE0E0E0);
|
|
|
|
canvas->drawPath(path, p);
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setStrokeWidth(2);
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
const int j = (i + 1) % N;
|
|
|
|
p.setColor(fEdgeColor[i]);
|
|
|
|
p.setAlpha(0x88);
|
2017-05-15 17:35:35 +00:00
|
|
|
canvas->drawLine(fPoly[i], fPoly[j], p);
|
2015-09-28 13:16:07 +00:00
|
|
|
}
|
|
|
|
p.setStyle(SkPaint::kFill_Style);
|
|
|
|
|
|
|
|
// Draw the clip itself
|
|
|
|
p.setColor(0xFF8888CC);
|
|
|
|
canvas->drawRect(fClip, p);
|
|
|
|
|
|
|
|
// Draw the filled triangle through the clip
|
|
|
|
p.setColor(0xFF88CC88);
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(fClip);
|
|
|
|
canvas->drawPath(path, p);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setStrokeWidth(6);
|
|
|
|
|
|
|
|
// Draw each of the "Edges" that survived the clipping
|
|
|
|
// We use a layer, so we can PLUS the different edge-colors, showing where two edges
|
|
|
|
// canceled each other out.
|
|
|
|
canvas->saveLayer(nullptr, nullptr);
|
2016-10-06 00:33:02 +00:00
|
|
|
p.setBlendMode(SkBlendMode::kPlus);
|
2015-09-28 13:16:07 +00:00
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
const int j = (i + 1) % N;
|
|
|
|
p.setColor(fEdgeColor[i]);
|
|
|
|
draw_clipped_line(canvas, fClip, fPoly[i], fPoly[j], p);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyClick : public Click {
|
|
|
|
public:
|
2018-08-08 15:36:17 +00:00
|
|
|
MyClick(Sample* view) : Click(view) {}
|
2015-09-28 13:16:07 +00:00
|
|
|
virtual void handleMove() = 0;
|
|
|
|
};
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-28 13:16:07 +00:00
|
|
|
class VertClick : public MyClick {
|
|
|
|
SkPoint* fPt;
|
|
|
|
public:
|
2018-08-08 15:36:17 +00:00
|
|
|
VertClick(Sample* view, SkPoint* pt) : MyClick(view), fPt(pt) {}
|
2015-09-28 13:16:07 +00:00
|
|
|
void handleMove() override { *fPt = snap(fCurr); }
|
|
|
|
};
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-28 13:16:07 +00:00
|
|
|
class DragRectClick : public MyClick {
|
|
|
|
SkRect* fRect;
|
|
|
|
public:
|
2018-08-08 15:36:17 +00:00
|
|
|
DragRectClick(Sample* view, SkRect* rect) : MyClick(view), fRect(rect) {}
|
2015-09-28 13:16:07 +00:00
|
|
|
void handleMove() override { fRect->offset(fCurr.x() - fPrev.x(), fCurr.y() - fPrev.y()); }
|
|
|
|
};
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-28 13:16:07 +00:00
|
|
|
class DragPolyClick : public MyClick {
|
|
|
|
SkPoint fSrc[100];
|
|
|
|
SkPoint* fPoly;
|
|
|
|
int fCount;
|
|
|
|
public:
|
2018-08-08 15:36:17 +00:00
|
|
|
DragPolyClick(Sample* view, SkPoint poly[], int count)
|
2015-09-28 13:16:07 +00:00
|
|
|
: MyClick(view), fPoly(poly), fCount(count)
|
|
|
|
{
|
|
|
|
SkASSERT((size_t)count <= SK_ARRAY_COUNT(fSrc));
|
|
|
|
memcpy(fSrc, poly, count * sizeof(SkPoint));
|
|
|
|
}
|
|
|
|
void handleMove() override {
|
|
|
|
const SkScalar dx = fCurr.x() - fOrig.x();
|
|
|
|
const SkScalar dy = fCurr.y() - fOrig.y();
|
|
|
|
for (int i = 0; i < fCount; ++i) {
|
|
|
|
fPoly[i].set(snap(fSrc[i].x() + dx), snap(fSrc[i].y() + dy));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class DoNothingClick : public MyClick {
|
|
|
|
public:
|
2018-08-08 15:36:17 +00:00
|
|
|
DoNothingClick(Sample* view) : MyClick(view) {}
|
2015-09-28 13:16:07 +00:00
|
|
|
void handleMove() override {}
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool hit_test(const SkPoint& pt, SkScalar x, SkScalar y) {
|
|
|
|
const SkScalar rad = 8;
|
|
|
|
const SkScalar dx = pt.x() - x;
|
|
|
|
const SkScalar dy = pt.y() - y;
|
|
|
|
return dx*dx + dy*dy <= rad*rad;
|
|
|
|
}
|
|
|
|
|
2019-07-08 20:07:57 +00:00
|
|
|
Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, ModifierKey) override {
|
2015-09-28 13:16:07 +00:00
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
if (hit_test(fPoly[i], x, y)) {
|
|
|
|
return new VertClick(this, &fPoly[i]);
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-28 13:16:07 +00:00
|
|
|
SkPath path;
|
|
|
|
path.addPoly(fPoly, N, true);
|
|
|
|
if (path.contains(x, y)) {
|
|
|
|
return new DragPolyClick(this, fPoly, N);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fClip.intersects(SkRect::MakeLTRB(x - 1, y - 1, x + 1, y + 1))) {
|
|
|
|
return new DragRectClick(this, &fClip);
|
|
|
|
}
|
|
|
|
return new DoNothingClick(this);
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-28 13:16:07 +00:00
|
|
|
bool onClick(Click* click) override {
|
|
|
|
((MyClick*)click)->handleMove();
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-28 13:16:07 +00:00
|
|
|
private:
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2015-09-28 13:16:07 +00:00
|
|
|
};
|
2018-08-08 15:36:17 +00:00
|
|
|
|
2015-09-28 13:16:07 +00:00
|
|
|
DEF_SAMPLE( return new EdgeClipView; )
|