diff options
-rw-r--r-- | include/core/SkRect.h | 5 | ||||
-rw-r--r-- | samplecode/SampleCode.h | 5 | ||||
-rw-r--r-- | samplecode/SamplePath.cpp | 103 |
3 files changed, 111 insertions, 2 deletions
diff --git a/include/core/SkRect.h b/include/core/SkRect.h index 8e4d669c48..cafc59afa7 100644 --- a/include/core/SkRect.h +++ b/include/core/SkRect.h @@ -692,6 +692,11 @@ public: return Intersects(fLeft, fTop, fRight, fBottom, left, top, right, bottom); } + bool intersects(const SkRect& r) const { + return Intersects(fLeft, fTop, fRight, fBottom, + r.fLeft, r.fTop, r.fRight, r.fBottom); + } + /** * Return true if rectangles a and b are not empty and intersect. */ diff --git a/samplecode/SampleCode.h b/samplecode/SampleCode.h index 26a8207ddc..93127ebb64 100644 --- a/samplecode/SampleCode.h +++ b/samplecode/SampleCode.h @@ -17,6 +17,11 @@ #include "SkOSMenu.h" class GrContext; +#define DEF_SAMPLE(code) \ + static SkView* SK_MACRO_APPEND_LINE(F_)() { code } \ + static SkViewRegister SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_)); + + class SampleCode { public: static bool KeyQ(const SkEvent&, SkKey* outKey); diff --git a/samplecode/SamplePath.cpp b/samplecode/SamplePath.cpp index e35eeb6717..49c51d3ad0 100644 --- a/samplecode/SamplePath.cpp +++ b/samplecode/SamplePath.cpp @@ -210,8 +210,107 @@ protected: private: typedef SampleView INHERITED; }; +DEF_SAMPLE( return new PathView; ) ////////////////////////////////////////////////////////////////////////////// -static SkView* MyFactory() { return new PathView; } -static SkViewRegister reg(MyFactory); +#include "SkCornerPathEffect.h" +#include "SkRandom.h" + +class ArcToView : public SampleView { + SkPaint fPtsPaint, fArcPaint, fSkeletonPaint, fCornerPaint; +public: + enum { + N = 4 + }; + SkPoint fPts[N]; + SkScalar fRadius; + + ArcToView() : fRadius(50) { + SkRandom rand; + for (int i = 0; i < N; ++i) { + fPts[i].fX = 20 + rand.nextUScalar1() * 640; + fPts[i].fY = 20 + rand.nextUScalar1() * 480; + } + + fPtsPaint.setAntiAlias(true); + fPtsPaint.setStrokeWidth(15); + fPtsPaint.setStrokeCap(SkPaint::kRound_Cap); + + fArcPaint.setAntiAlias(true); + fArcPaint.setStyle(SkPaint::kStroke_Style); + fArcPaint.setStrokeWidth(9); + fArcPaint.setColor(0x800000FF); + + fCornerPaint.setAntiAlias(true); + fCornerPaint.setStyle(SkPaint::kStroke_Style); + fCornerPaint.setStrokeWidth(13); + fCornerPaint.setColor(SK_ColorGREEN); + fCornerPaint.setPathEffect(SkCornerPathEffect::Create(fRadius*2))->unref(); + + fSkeletonPaint.setAntiAlias(true); + fSkeletonPaint.setStyle(SkPaint::kStroke_Style); + fSkeletonPaint.setColor(SK_ColorRED); + } + +protected: + // overrides from SkEventSink + bool onQuery(SkEvent* evt) SK_OVERRIDE { + if (SampleCode::TitleQ(*evt)) { + SampleCode::TitleR(evt, "ArcTo"); + return true; + } + return this->INHERITED::onQuery(evt); + } + + void onDrawContent(SkCanvas* canvas) SK_OVERRIDE { + canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint); + + SkPath path; + + path.moveTo(fPts[0]); + for (int i = 1; i < N; ++i) { + path.lineTo(fPts[i].fX, fPts[i].fY); + } + canvas->drawPath(path, fCornerPaint); + + path.reset(); + path.moveTo(fPts[0]); + for (int i = 1; i < N - 1; ++i) { + path.arcTo(fPts[i].fX, fPts[i].fY, fPts[i+1].fX, fPts[i+1].fY, fRadius); + } + path.lineTo(fPts[N - 1]); + canvas->drawPath(path, fArcPaint); + + canvas->drawPoints(SkCanvas::kPolygon_PointMode, N, fPts, fSkeletonPaint); + } + + bool onClick(Click* click) SK_OVERRIDE { + int32_t index; + if (click->fMeta.findS32("index", &index)) { + SkASSERT((unsigned)index < N); + fPts[index] = click->fCurr; + this->inval(NULL); + return true; + } + return false; + } + + SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE { + const SkScalar tol = 4; + const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2); + for (int i = 0; i < N; ++i) { + if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) { + Click* click = new Click(this); + click->fMeta.setS32("index", i); + return click; + } + } + return this->INHERITED::onFindClickHandler(x, y, modi); + } + +private: + typedef SampleView INHERITED; +}; +DEF_SAMPLE( return new ArcToView; ) + |