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.
|
|
|
|
*/
|
2009-11-16 20:39:43 +00:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkImageDecoder.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "SkXfermode.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkTime.h"
|
|
|
|
#include "SkRandom.h"
|
2009-11-17 18:47:52 +00:00
|
|
|
|
2009-11-16 20:39:43 +00:00
|
|
|
#include "SkLineClipper.h"
|
2009-11-18 16:09:51 +00:00
|
|
|
#include "SkEdgeClipper.h"
|
2009-11-17 18:47:52 +00:00
|
|
|
|
2009-11-19 20:46:39 +00:00
|
|
|
#define AUTO_ANIMATE true
|
|
|
|
|
|
|
|
static int test0(SkPoint pts[], SkRect* clip) {
|
|
|
|
pts[0].set(200000, 140);
|
|
|
|
pts[1].set(-740000, 483);
|
|
|
|
pts[2].set(1.00000102e-06f, 9.10000017e-05f);
|
|
|
|
clip->set(0, 0, 640, 480);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2009-11-17 18:47:52 +00:00
|
|
|
static void drawQuad(SkCanvas* canvas, const SkPoint pts[3], const SkPaint& p) {
|
|
|
|
SkPath path;
|
|
|
|
path.moveTo(pts[0]);
|
|
|
|
path.quadTo(pts[1], pts[2]);
|
|
|
|
canvas->drawPath(path, p);
|
|
|
|
}
|
|
|
|
|
2009-11-18 13:47:40 +00:00
|
|
|
static void drawCubic(SkCanvas* canvas, const SkPoint pts[4], const SkPaint& p) {
|
|
|
|
SkPath path;
|
|
|
|
path.moveTo(pts[0]);
|
|
|
|
path.cubicTo(pts[1], pts[2], pts[3]);
|
|
|
|
canvas->drawPath(path, p);
|
|
|
|
}
|
|
|
|
|
2009-11-17 18:47:52 +00:00
|
|
|
typedef void (*clipper_proc)(const SkPoint src[], const SkRect& clip,
|
|
|
|
SkCanvas*, const SkPaint&, const SkPaint&);
|
|
|
|
|
|
|
|
static void check_clipper(int count, const SkPoint pts[], const SkRect& clip) {
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
SkASSERT(pts[i].fX >= clip.fLeft);
|
|
|
|
SkASSERT(pts[i].fX <= clip.fRight);
|
|
|
|
SkASSERT(pts[i].fY >= clip.fTop);
|
|
|
|
SkASSERT(pts[i].fY <= clip.fBottom);
|
|
|
|
}
|
2009-11-17 19:39:51 +00:00
|
|
|
|
|
|
|
if (count > 1) {
|
2009-11-18 13:47:40 +00:00
|
|
|
sk_assert_monotonic_y(pts, count);
|
2009-11-17 19:39:51 +00:00
|
|
|
}
|
2009-11-17 18:47:52 +00:00
|
|
|
}
|
|
|
|
|
2009-11-19 20:46:39 +00:00
|
|
|
static void line_intersector(const SkPoint src[], const SkRect& clip,
|
|
|
|
SkCanvas* canvas, const SkPaint& p0, const SkPaint& p1) {
|
2009-11-17 18:47:52 +00:00
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 2, src, p1);
|
2009-11-19 20:46:39 +00:00
|
|
|
|
|
|
|
SkPoint dst[2];
|
|
|
|
if (SkLineClipper::IntersectLine(src, clip, dst)) {
|
|
|
|
check_clipper(2, dst, clip);
|
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 2, dst, p0);
|
|
|
|
}
|
|
|
|
}
|
2009-11-17 18:47:52 +00:00
|
|
|
|
2009-11-19 20:46:39 +00:00
|
|
|
static void line_clipper(const SkPoint src[], const SkRect& clip,
|
|
|
|
SkCanvas* canvas, const SkPaint& p0, const SkPaint& p1) {
|
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 2, src, p1);
|
|
|
|
|
2009-11-17 18:47:52 +00:00
|
|
|
SkPoint dst[SkLineClipper::kMaxPoints];
|
|
|
|
int count = SkLineClipper::ClipLine(src, clip, dst);
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
check_clipper(2, &dst[i], clip);
|
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 2, &dst[i], p0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void quad_clipper(const SkPoint src[], const SkRect& clip,
|
2009-11-18 13:47:40 +00:00
|
|
|
SkCanvas* canvas, const SkPaint& p0, const SkPaint& p1) {
|
2009-11-17 18:47:52 +00:00
|
|
|
drawQuad(canvas, src, p1);
|
2009-11-18 13:47:40 +00:00
|
|
|
|
2009-11-18 16:09:51 +00:00
|
|
|
SkEdgeClipper clipper;
|
2009-11-17 18:47:52 +00:00
|
|
|
if (clipper.clipQuad(src, clip)) {
|
2011-03-18 14:47:36 +00:00
|
|
|
SkPoint pts[4];
|
2009-11-17 18:47:52 +00:00
|
|
|
SkPath::Verb verb;
|
|
|
|
while ((verb = clipper.next(pts)) != SkPath::kDone_Verb) {
|
|
|
|
switch (verb) {
|
|
|
|
case SkPath::kLine_Verb:
|
2009-11-17 19:39:51 +00:00
|
|
|
check_clipper(2, pts, clip);
|
2009-11-17 18:47:52 +00:00
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p0);
|
|
|
|
break;
|
|
|
|
case SkPath::kQuad_Verb:
|
2009-11-17 19:39:51 +00:00
|
|
|
check_clipper(3, pts, clip);
|
2009-11-17 18:47:52 +00:00
|
|
|
drawQuad(canvas, pts, p0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SkASSERT(!"unexpected verb");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-18 13:47:40 +00:00
|
|
|
static void cubic_clipper(const SkPoint src[], const SkRect& clip,
|
|
|
|
SkCanvas* canvas, const SkPaint& p0, const SkPaint& p1) {
|
|
|
|
drawCubic(canvas, src, p1);
|
|
|
|
|
2009-11-18 16:09:51 +00:00
|
|
|
SkEdgeClipper clipper;
|
2009-11-18 13:47:40 +00:00
|
|
|
if (clipper.clipCubic(src, clip)) {
|
|
|
|
SkPoint pts[4];
|
|
|
|
SkPath::Verb verb;
|
|
|
|
while ((verb = clipper.next(pts)) != SkPath::kDone_Verb) {
|
|
|
|
switch (verb) {
|
|
|
|
case SkPath::kLine_Verb:
|
|
|
|
check_clipper(2, pts, clip);
|
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p0);
|
|
|
|
break;
|
|
|
|
case SkPath::kCubic_Verb:
|
|
|
|
// check_clipper(4, pts, clip);
|
|
|
|
drawCubic(canvas, pts, p0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SkASSERT(!"unexpected verb");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-17 18:47:52 +00:00
|
|
|
static const clipper_proc gProcs[] = {
|
2009-11-19 20:46:39 +00:00
|
|
|
line_intersector,
|
2009-11-17 18:47:52 +00:00
|
|
|
line_clipper,
|
2009-11-18 13:47:40 +00:00
|
|
|
quad_clipper,
|
|
|
|
cubic_clipper
|
2009-11-17 18:47:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2009-11-16 20:39:43 +00:00
|
|
|
|
|
|
|
enum {
|
2009-11-17 18:47:52 +00:00
|
|
|
W = 640/3,
|
|
|
|
H = 480/3
|
2009-11-16 20:39:43 +00:00
|
|
|
};
|
|
|
|
|
2011-06-18 01:35:18 +00:00
|
|
|
class LineClipperView : public SampleView {
|
2009-11-23 21:07:51 +00:00
|
|
|
SkMSec fNow;
|
2009-11-17 19:39:51 +00:00
|
|
|
int fCounter;
|
2009-11-17 18:47:52 +00:00
|
|
|
int fProcIndex;
|
2009-11-16 20:39:43 +00:00
|
|
|
SkRect fClip;
|
|
|
|
SkRandom fRand;
|
2009-11-17 18:47:52 +00:00
|
|
|
SkPoint fPts[4];
|
2009-11-16 20:39:43 +00:00
|
|
|
|
|
|
|
void randPts() {
|
2011-05-19 19:58:58 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(fPts); i++) {
|
2009-11-17 18:47:52 +00:00
|
|
|
fPts[i].set(fRand.nextUScalar1() * 640,
|
|
|
|
fRand.nextUScalar1() * 480);
|
|
|
|
}
|
2009-11-17 19:39:51 +00:00
|
|
|
fCounter += 1;
|
2009-11-16 20:39:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
LineClipperView() {
|
2009-11-19 20:46:39 +00:00
|
|
|
fProcIndex = 0;
|
2009-11-17 19:39:51 +00:00
|
|
|
fCounter = 0;
|
2011-05-24 20:25:32 +00:00
|
|
|
fNow = 0;
|
2009-11-17 19:39:51 +00:00
|
|
|
|
2009-11-16 20:39:43 +00:00
|
|
|
int x = (640 - W)/2;
|
|
|
|
int y = (480 - H)/2;
|
2011-03-18 14:47:36 +00:00
|
|
|
fClip.set(SkIntToScalar(x), SkIntToScalar(y),
|
|
|
|
SkIntToScalar(x + W), SkIntToScalar(y + H));
|
2009-11-16 20:39:43 +00:00
|
|
|
this->randPts();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "LineClipper");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void drawVLine(SkCanvas* canvas, SkScalar x, const SkPaint& paint) {
|
|
|
|
canvas->drawLine(x, -999, x, 999, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void drawHLine(SkCanvas* canvas, SkScalar y, const SkPaint& paint) {
|
|
|
|
canvas->drawLine(-999, y, 999, y, paint);
|
|
|
|
}
|
|
|
|
|
2011-06-18 01:35:18 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2009-11-23 21:07:51 +00:00
|
|
|
SkMSec now = SampleCode::GetAnimTime();
|
|
|
|
if (fNow != now) {
|
|
|
|
fNow = now;
|
|
|
|
this->randPts();
|
|
|
|
this->inval(NULL);
|
|
|
|
}
|
|
|
|
|
2009-11-19 20:46:39 +00:00
|
|
|
// fProcIndex = test0(fPts, &fClip);
|
2009-11-16 20:39:43 +00:00
|
|
|
|
2009-11-17 18:47:52 +00:00
|
|
|
SkPaint paint, paint1;
|
2009-11-16 20:39:43 +00:00
|
|
|
|
|
|
|
drawVLine(canvas, fClip.fLeft + SK_ScalarHalf, paint);
|
|
|
|
drawVLine(canvas, fClip.fRight - SK_ScalarHalf, paint);
|
|
|
|
drawHLine(canvas, fClip.fTop + SK_ScalarHalf, paint);
|
|
|
|
drawHLine(canvas, fClip.fBottom - SK_ScalarHalf, paint);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorLTGRAY);
|
|
|
|
canvas->drawRect(fClip, paint);
|
|
|
|
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
2009-11-17 18:47:52 +00:00
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
// paint.setStrokeWidth(SkIntToScalar(3));
|
2009-11-16 20:39:43 +00:00
|
|
|
paint.setStrokeCap(SkPaint::kRound_Cap);
|
2009-11-17 18:47:52 +00:00
|
|
|
|
|
|
|
paint1.setAntiAlias(true);
|
|
|
|
paint1.setColor(SK_ColorRED);
|
|
|
|
paint1.setStyle(SkPaint::kStroke_Style);
|
|
|
|
gProcs[fProcIndex](fPts, fClip, canvas, paint, paint1);
|
2011-06-18 01:35:18 +00:00
|
|
|
this->inval(NULL);
|
2009-11-16 20:39:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
|
2009-11-17 18:47:52 +00:00
|
|
|
// fProcIndex = (fProcIndex + 1) % SK_ARRAY_COUNT(gProcs);
|
|
|
|
if (x < 50 && y < 50) {
|
|
|
|
this->randPts();
|
|
|
|
}
|
2009-11-16 20:39:43 +00:00
|
|
|
this->inval(NULL);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool onClick(Click* click) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-06-18 01:35:18 +00:00
|
|
|
typedef SampleView INHERITED;
|
2009-11-16 20:39:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new LineClipperView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|
|
|
|
|