aboutsummaryrefslogtreecommitdiffhomepage
path: root/samplecode/SamplePath.cpp
diff options
context:
space:
mode:
authorGravatar reed <reed@google.com>2014-12-16 08:07:43 -0800
committerGravatar Commit bot <commit-bot@chromium.org>2014-12-16 08:07:43 -0800
commita7a8b10134f78d4ed55ff0370a54d8141daa7cac (patch)
tree493ea15b6646dbdd576b908b2ce6b976c59b7d0c /samplecode/SamplePath.cpp
parent86f032832fc3527ab561bcbd9d2a546f7f676f73 (diff)
add sample for arcTo
BUG=skia: TBR= Review URL: https://codereview.chromium.org/808793002
Diffstat (limited to 'samplecode/SamplePath.cpp')
-rw-r--r--samplecode/SamplePath.cpp103
1 files changed, 101 insertions, 2 deletions
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; )
+