add sample for arcTo
authorreed <reed@google.com>
Tue, 16 Dec 2014 16:07:43 +0000 (08:07 -0800)
committerCommit bot <commit-bot@chromium.org>
Tue, 16 Dec 2014 16:07:43 +0000 (08:07 -0800)
BUG=skia:
TBR=

Review URL: https://codereview.chromium.org/808793002

include/core/SkRect.h
samplecode/SampleCode.h
samplecode/SamplePath.cpp

index 8e4d669c48279c02c08ede805d43f44c935b9dcd..cafc59afa73684f407057dd749fd42da768039bd 100644 (file)
@@ -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.
      */
index 26a8207ddcb6d29c9c9ba96550f7af806dcc130b..93127ebb644d40a4d54afe8c4f83d24499c200e6 100644 (file)
 #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);
index e35eeb6717575ffe2e91c1ca349cc2e1b5c449f5..49c51d3ad055e855424914c56d123fea6fb09939 100644 (file)
@@ -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; )
+