Home | History | Annotate | Download | only in samplecode
      1 
      2 /*
      3  * Copyright 2011 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 #include "SampleCode.h"
      9 #include "SkView.h"
     10 #include "SkCanvas.h"
     11 #include "SkConcaveToTriangles.h"
     12 
     13 #define SIZE    SkIntToScalar(150)
     14 
     15 typedef void (*PathProc)(SkPath*);
     16 
     17 static void make_path0(SkPath* path) {
     18     SkRect r;
     19     r.set(0, 0, SIZE, SIZE);
     20     path->addRect(r);
     21 }
     22 
     23 static void make_path1(SkPath* path) {
     24     SkRect r;
     25     r.set(0, 0, SIZE, SIZE);
     26     path->addRoundRect(r, SIZE/4, SIZE/4);
     27 }
     28 
     29 static void make_path2(SkPath* path) {
     30     SkRect r;
     31     r.set(0, 0, SIZE, SIZE);
     32     path->addOval(r);
     33 }
     34 
     35 static const PathProc gProcs[] = {
     36     make_path0,
     37     make_path1,
     38     make_path2,
     39 };
     40 
     41 #define COUNT_PROCS SK_ARRAY_COUNT(gProcs)
     42 
     43 class TriangleView : public SkView {
     44 public:
     45     SkPath fPaths[COUNT_PROCS];
     46 
     47 	TriangleView() {
     48         for (size_t i = 0; i < COUNT_PROCS; i++) {
     49             gProcs[i](&fPaths[i]);
     50         }
     51     }
     52 
     53 protected:
     54     // overrides from SkEventSink
     55     virtual bool onQuery(SkEvent* evt) {
     56         if (SampleCode::TitleQ(*evt)) {
     57             SampleCode::TitleR(evt, "Triangles");
     58             return true;
     59         }
     60         return this->INHERITED::onQuery(evt);
     61     }
     62 
     63     void drawBG(SkCanvas* canvas) {
     64         canvas->drawColor(SK_ColorGRAY);
     65     }
     66 
     67     static void draw_path(SkCanvas* canvas, const SkPaint& pathPaint,
     68                           const SkPath& path, const SkPaint& triPaint) {
     69         canvas->drawPath(path, pathPaint);
     70 
     71         int n = path.getPoints(NULL, 0);
     72         SkPoint* pts = new SkPoint[n];
     73         path.getPoints(pts, n);
     74 
     75         SkTDArray<SkPoint> triangles;
     76         if (SkConcaveToTriangles(n, pts, &triangles)) {
     77             canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
     78                                  triangles.count(), triangles.begin(), NULL,
     79                                  NULL, NULL, NULL, 0, triPaint);
     80         }
     81 
     82         SkPaint paint;
     83         paint.setColor(SK_ColorGREEN);
     84         paint.setStrokeWidth(SkIntToScalar(4));
     85         canvas->drawPoints(SkCanvas::kPoints_PointMode, n, pts, paint);
     86         delete[] pts;
     87     }
     88 
     89     virtual void onDraw(SkCanvas* canvas) {
     90         this->drawBG(canvas);
     91 
     92         canvas->translate(SIZE/2, SIZE/2);
     93 
     94         SkPaint pathPaint, triPaint;
     95 
     96         pathPaint.setColor(SK_ColorBLUE);
     97         pathPaint.setStrokeWidth(SIZE / 12);
     98 
     99         triPaint.setColor(SK_ColorRED);
    100         triPaint.setStyle(SkPaint::kStroke_Style);
    101 
    102         for (size_t i = 0; i < COUNT_PROCS; i++) {
    103             pathPaint.setStyle(SkPaint::kFill_Style);
    104             draw_path(canvas, pathPaint, fPaths[i], triPaint);
    105 
    106             canvas->save();
    107             canvas->translate(0, SIZE * 6 / 5);
    108 
    109             pathPaint.setStyle(SkPaint::kStroke_Style);
    110             draw_path(canvas, pathPaint, fPaths[i], triPaint);
    111 
    112             canvas->restore();
    113             canvas->translate(SIZE * 6 / 5, 0);
    114         }
    115     }
    116 
    117 private:
    118     typedef SkView INHERITED;
    119 };
    120 
    121 //////////////////////////////////////////////////////////////////////////////
    122 
    123 static SkView* MyFactory() { return new TriangleView; }
    124 static SkViewRegister reg(MyFactory);
    125 
    126