reed@google.com | ad89fbd | 2011-04-11 13:57:34 +0000 | [diff] [blame^] | 1 | #include "SampleCode.h" |
| 2 | #include "SkView.h" |
| 3 | #include "SkCanvas.h" |
| 4 | #include "SkGraphics.h" |
| 5 | #include "SkRandom.h" |
| 6 | #include "SkBlurDrawLooper.h" |
| 7 | #include "SkGradientShader.h" |
| 8 | |
| 9 | typedef SkScalar (*MakePathProc)(SkPath*); |
| 10 | |
| 11 | static SkScalar make_frame(SkPath* path) { |
| 12 | SkRect r = { 10, 10, 630, 470 }; |
| 13 | path->addRoundRect(r, 15, 15); |
| 14 | |
| 15 | SkPaint paint; |
| 16 | paint.setStyle(SkPaint::kStroke_Style); |
| 17 | paint.setStrokeWidth(5); |
| 18 | paint.getFillPath(*path, path); |
| 19 | return 15; |
| 20 | } |
| 21 | |
| 22 | static SkScalar make_triangle(SkPath* path) { |
| 23 | static const int gCoord[] = { |
| 24 | 10, 20, 15, 5, 30, 30 |
| 25 | }; |
| 26 | path->moveTo(SkIntToScalar(gCoord[0]), SkIntToScalar(gCoord[1])); |
| 27 | path->lineTo(SkIntToScalar(gCoord[2]), SkIntToScalar(gCoord[3])); |
| 28 | path->lineTo(SkIntToScalar(gCoord[4]), SkIntToScalar(gCoord[5])); |
| 29 | path->close(); |
| 30 | path->offset(10, 0); |
| 31 | return SkIntToScalar(30); |
| 32 | } |
| 33 | |
| 34 | static SkScalar make_rect(SkPath* path) { |
| 35 | SkRect r = { 10, 10, 30, 30 }; |
| 36 | path->addRect(r); |
| 37 | path->offset(10, 0); |
| 38 | return SkIntToScalar(30); |
| 39 | } |
| 40 | |
| 41 | static SkScalar make_oval(SkPath* path) { |
| 42 | SkRect r = { 10, 10, 30, 30 }; |
| 43 | path->addOval(r); |
| 44 | path->offset(10, 0); |
| 45 | return SkIntToScalar(30); |
| 46 | } |
| 47 | |
| 48 | static SkScalar make_sawtooth(SkPath* path) { |
| 49 | SkScalar x = SkIntToScalar(20); |
| 50 | SkScalar y = SkIntToScalar(20); |
| 51 | const SkScalar x0 = x; |
| 52 | const SkScalar dx = SK_Scalar1 * 5; |
| 53 | const SkScalar dy = SK_Scalar1 * 10; |
| 54 | |
| 55 | path->moveTo(x, y); |
| 56 | for (int i = 0; i < 32; i++) { |
| 57 | x += dx; |
| 58 | path->lineTo(x, y - dy); |
| 59 | x += dx; |
| 60 | path->lineTo(x, y + dy); |
| 61 | } |
| 62 | path->lineTo(x, y + 2 * dy); |
| 63 | path->lineTo(x0, y + 2 * dy); |
| 64 | path->close(); |
| 65 | return SkIntToScalar(30); |
| 66 | } |
| 67 | |
| 68 | static SkScalar make_star(SkPath* path, int n) { |
| 69 | const SkScalar c = SkIntToScalar(45); |
| 70 | const SkScalar r = SkIntToScalar(20); |
| 71 | |
| 72 | SkScalar rad = -SK_ScalarPI / 2; |
| 73 | const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n; |
| 74 | |
| 75 | path->moveTo(c, c - r); |
| 76 | for (int i = 1; i < n; i++) { |
| 77 | rad += drad; |
| 78 | SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV); |
| 79 | path->lineTo(c + SkScalarMul(cosV, r), c + SkScalarMul(sinV, r)); |
| 80 | } |
| 81 | path->close(); |
| 82 | return r * 2 * 6 / 5; |
| 83 | } |
| 84 | |
| 85 | static SkScalar make_star_5(SkPath* path) { return make_star(path, 5); } |
| 86 | static SkScalar make_star_13(SkPath* path) { return make_star(path, 13); } |
| 87 | |
| 88 | static const MakePathProc gProcs[] = { |
| 89 | make_frame, |
| 90 | make_triangle, |
| 91 | make_rect, |
| 92 | make_oval, |
| 93 | make_sawtooth, |
| 94 | make_star_5, |
| 95 | make_star_13 |
| 96 | }; |
| 97 | |
| 98 | #define N SK_ARRAY_COUNT(gProcs) |
| 99 | |
| 100 | class PathFillView : public SkView { |
| 101 | SkPath fPath[N]; |
| 102 | SkScalar fDY[N]; |
| 103 | |
| 104 | public: |
| 105 | PathFillView() { |
| 106 | for (size_t i = 0; i < N; i++) { |
| 107 | fDY[i] = gProcs[i](&fPath[i]); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | protected: |
| 112 | // overrides from SkEventSink |
| 113 | virtual bool onQuery(SkEvent* evt) { |
| 114 | if (SampleCode::TitleQ(*evt)) { |
| 115 | SampleCode::TitleR(evt, "PathFill"); |
| 116 | return true; |
| 117 | } |
| 118 | return this->INHERITED::onQuery(evt); |
| 119 | } |
| 120 | |
| 121 | void drawBG(SkCanvas* canvas) { |
| 122 | canvas->drawColor(0xFFDDDDDD); |
| 123 | } |
| 124 | |
| 125 | virtual void onDraw(SkCanvas* canvas) { |
| 126 | this->drawBG(canvas); |
| 127 | |
| 128 | SkPaint paint; |
| 129 | paint.setAntiAlias(true); |
| 130 | |
| 131 | for (size_t i = 0; i < N; i++) { |
| 132 | canvas->drawPath(fPath[i], paint); |
| 133 | canvas->translate(0, fDY[i]); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | private: |
| 138 | typedef SkView INHERITED; |
| 139 | }; |
| 140 | |
| 141 | ////////////////////////////////////////////////////////////////////////////// |
| 142 | |
| 143 | static SkView* MyFactory() { return new PathFillView; } |
| 144 | static SkViewRegister reg(MyFactory); |
| 145 | |