reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | #include "SampleCode.h" |
| 2 | #include "SkView.h" |
| 3 | #include "SkCanvas.h" |
| 4 | #include "SkGradientShader.h" |
| 5 | #include "SkGraphics.h" |
| 6 | #include "SkImageDecoder.h" |
| 7 | #include "SkPath.h" |
| 8 | #include "SkPorterDuff.h" |
| 9 | #include "SkRegion.h" |
| 10 | #include "SkShader.h" |
| 11 | #include "SkUtils.h" |
| 12 | #include "SkXfermode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkColorPriv.h" |
| 14 | #include "SkColorFilter.h" |
| 15 | #include "SkTime.h" |
| 16 | #include "SkTypeface.h" |
| 17 | |
| 18 | class PathView : public SkView { |
| 19 | public: |
| 20 | int fDStroke, fStroke, fMinStroke, fMaxStroke; |
| 21 | SkPath fPath[6]; |
| 22 | bool fShowHairline; |
| 23 | |
| 24 | PathView() |
| 25 | { |
| 26 | fShowHairline = false; |
| 27 | |
| 28 | fDStroke = 1; |
| 29 | fStroke = 10; |
| 30 | fMinStroke = 10; |
| 31 | fMaxStroke = 180; |
| 32 | |
| 33 | const int V = 85; |
| 34 | |
| 35 | fPath[0].moveTo(SkIntToScalar(40), SkIntToScalar(70)); |
| 36 | fPath[0].lineTo(SkIntToScalar(70), SkIntToScalar(70) + SK_Scalar1/1); |
| 37 | fPath[0].lineTo(SkIntToScalar(110), SkIntToScalar(70)); |
| 38 | |
| 39 | fPath[1].moveTo(SkIntToScalar(40), SkIntToScalar(70)); |
| 40 | fPath[1].lineTo(SkIntToScalar(70), SkIntToScalar(70) - SK_Scalar1/1); |
| 41 | fPath[1].lineTo(SkIntToScalar(110), SkIntToScalar(70)); |
| 42 | |
| 43 | fPath[2].moveTo(SkIntToScalar(V), SkIntToScalar(V)); |
| 44 | fPath[2].lineTo(SkIntToScalar(50), SkIntToScalar(V)); |
| 45 | fPath[2].lineTo(SkIntToScalar(50), SkIntToScalar(50)); |
| 46 | |
| 47 | fPath[3].moveTo(SkIntToScalar(50), SkIntToScalar(50)); |
| 48 | fPath[3].lineTo(SkIntToScalar(50), SkIntToScalar(V)); |
| 49 | fPath[3].lineTo(SkIntToScalar(V), SkIntToScalar(V)); |
| 50 | |
| 51 | fPath[4].moveTo(SkIntToScalar(50), SkIntToScalar(50)); |
| 52 | fPath[4].lineTo(SkIntToScalar(50), SkIntToScalar(V)); |
| 53 | fPath[4].lineTo(SkIntToScalar(52), SkIntToScalar(50)); |
| 54 | |
| 55 | fPath[5].moveTo(SkIntToScalar(52), SkIntToScalar(50)); |
| 56 | fPath[5].lineTo(SkIntToScalar(50), SkIntToScalar(V)); |
| 57 | fPath[5].lineTo(SkIntToScalar(50), SkIntToScalar(50)); |
| 58 | } |
| 59 | |
| 60 | virtual ~PathView() |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | void nextStroke() |
| 65 | { |
| 66 | fStroke += fDStroke; |
| 67 | if (fStroke > fMaxStroke || fStroke < fMinStroke) |
| 68 | fDStroke = -fDStroke; |
| 69 | } |
| 70 | |
| 71 | protected: |
| 72 | // overrides from SkEventSink |
| 73 | virtual bool onQuery(SkEvent* evt) |
| 74 | { |
| 75 | if (SampleCode::TitleQ(*evt)) |
| 76 | { |
| 77 | SampleCode::TitleR(evt, "Paths"); |
| 78 | return true; |
| 79 | } |
| 80 | return this->INHERITED::onQuery(evt); |
| 81 | } |
| 82 | |
| 83 | void drawBG(SkCanvas* canvas) |
| 84 | { |
| 85 | canvas->drawColor(0xFFDDDDDD); |
| 86 | // canvas->drawColor(SK_ColorWHITE); |
| 87 | } |
| 88 | |
| 89 | void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) |
| 90 | { |
| 91 | SkPaint paint; |
| 92 | |
| 93 | paint.setAntiAlias(true); |
| 94 | paint.setStyle(SkPaint::kStroke_Style); |
| 95 | paint.setStrokeJoin(j); |
| 96 | paint.setStrokeWidth(SkIntToScalar(fStroke)); |
| 97 | |
| 98 | if (fShowHairline) |
| 99 | { |
| 100 | SkPath fill; |
| 101 | |
| 102 | paint.getFillPath(path, &fill); |
| 103 | paint.setStrokeWidth(0); |
| 104 | canvas->drawPath(fill, paint); |
| 105 | } |
| 106 | else |
| 107 | canvas->drawPath(path, paint); |
| 108 | |
| 109 | paint.setColor(SK_ColorRED); |
| 110 | paint.setStrokeWidth(0); |
| 111 | canvas->drawPath(path, paint); |
| 112 | } |
| 113 | |
| 114 | virtual void onDraw(SkCanvas* canvas) |
| 115 | { |
| 116 | this->drawBG(canvas); |
| 117 | |
| 118 | canvas->translate(SkIntToScalar(50), SkIntToScalar(50)); |
| 119 | |
| 120 | static const SkPaint::Join gJoins[] = { |
| 121 | SkPaint::kBevel_Join, |
| 122 | SkPaint::kMiter_Join, |
| 123 | SkPaint::kRound_Join |
| 124 | }; |
| 125 | |
| 126 | for (int i = 0; i < SK_ARRAY_COUNT(gJoins); i++) |
| 127 | { |
| 128 | canvas->save(); |
| 129 | for (int j = 0; j < SK_ARRAY_COUNT(fPath); j++) |
| 130 | { |
| 131 | this->drawPath(canvas, fPath[j], gJoins[i]); |
| 132 | canvas->translate(SkIntToScalar(200), 0); |
| 133 | } |
| 134 | canvas->restore(); |
| 135 | |
| 136 | canvas->translate(0, SkIntToScalar(200)); |
| 137 | } |
| 138 | |
| 139 | this->nextStroke(); |
| 140 | this->inval(NULL); |
| 141 | } |
| 142 | |
| 143 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) |
| 144 | { |
| 145 | fShowHairline = !fShowHairline; |
| 146 | this->inval(NULL); |
| 147 | return this->INHERITED::onFindClickHandler(x, y); |
| 148 | } |
| 149 | |
| 150 | virtual bool onClick(Click* click) |
| 151 | { |
| 152 | return this->INHERITED::onClick(click); |
| 153 | } |
| 154 | |
| 155 | private: |
| 156 | typedef SkView INHERITED; |
| 157 | }; |
| 158 | |
| 159 | ////////////////////////////////////////////////////////////////////////////// |
| 160 | |
| 161 | static SkView* MyFactory() { return new PathView; } |
| 162 | static SkViewRegister reg(MyFactory); |
| 163 | |