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