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 "SkPath.h" |
| 6 | #include "SkRegion.h" |
| 7 | #include "SkShader.h" |
| 8 | #include "SkUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #include "Sk1DPathEffect.h" |
| 10 | #include "SkCornerPathEffect.h" |
| 11 | #include "SkPathMeasure.h" |
| 12 | #include "SkRandom.h" |
| 13 | #include "SkColorPriv.h" |
| 14 | #include "SkPixelXorXfermode.h" |
| 15 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | #define CORNER_RADIUS 12 |
| 17 | static SkScalar gPhase; |
| 18 | |
| 19 | static const int gXY[] = { |
| 20 | 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4 |
| 21 | }; |
| 22 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 23 | static SkPathEffect* make_pe(int flags) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | if (flags == 1) |
| 25 | return new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS)); |
| 26 | |
| 27 | SkPath path; |
| 28 | path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); |
| 29 | for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) |
| 30 | path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); |
| 31 | path.close(); |
| 32 | path.offset(SkIntToScalar(-6), 0); |
| 33 | |
| 34 | SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12), gPhase, SkPath1DPathEffect::kRotate_Style); |
| 35 | |
| 36 | if (flags == 2) |
| 37 | return outer; |
| 38 | |
| 39 | SkPathEffect* inner = new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS)); |
| 40 | |
| 41 | SkPathEffect* pe = new SkComposePathEffect(outer, inner); |
| 42 | outer->unref(); |
| 43 | inner->unref(); |
| 44 | return pe; |
| 45 | } |
| 46 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 47 | static SkPathEffect* make_warp_pe() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | SkPath path; |
| 49 | path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); |
| 50 | for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) |
| 51 | path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); |
| 52 | path.close(); |
| 53 | path.offset(SkIntToScalar(-6), 0); |
| 54 | |
| 55 | SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12), gPhase, SkPath1DPathEffect::kMorph_Style); |
| 56 | SkPathEffect* inner = new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS)); |
| 57 | |
| 58 | SkPathEffect* pe = new SkComposePathEffect(outer, inner); |
| 59 | outer->unref(); |
| 60 | inner->unref(); |
| 61 | return pe; |
| 62 | } |
| 63 | |
| 64 | /////////////////////////////////////////////////////////// |
| 65 | |
| 66 | #include "SkColorFilter.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 67 | #include "SkLayerRasterizer.h" |
| 68 | |
| 69 | class testrast : public SkLayerRasterizer { |
| 70 | public: |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 71 | testrast() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | SkPaint paint; |
| 73 | paint.setAntiAlias(true); |
| 74 | |
| 75 | #if 0 |
| 76 | paint.setStyle(SkPaint::kStroke_Style); |
| 77 | paint.setStrokeWidth(SK_Scalar1*4); |
| 78 | this->addLayer(paint); |
| 79 | |
| 80 | paint.setStrokeWidth(SK_Scalar1*1); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 81 | paint.setXfermode(SkXfermode::kClear_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | this->addLayer(paint); |
| 83 | #else |
| 84 | paint.setAlpha(0x66); |
| 85 | this->addLayer(paint, SkIntToScalar(4), SkIntToScalar(4)); |
| 86 | |
| 87 | paint.setAlpha(0xFF); |
| 88 | this->addLayer(paint); |
| 89 | #endif |
| 90 | } |
| 91 | }; |
| 92 | |
reed@google.com | e5ff439 | 2011-06-01 11:59:08 +0000 | [diff] [blame] | 93 | class PathEffectView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | SkPath fPath; |
| 95 | SkPoint fClickPt; |
| 96 | public: |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 97 | PathEffectView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | SkRandom rand; |
| 99 | int steps = 20; |
reed@android.com | 92a50ea | 2009-08-14 19:27:37 +0000 | [diff] [blame] | 100 | SkScalar dist = SkIntToScalar(400); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | SkScalar x = SkIntToScalar(20); |
| 102 | SkScalar y = SkIntToScalar(50); |
| 103 | |
| 104 | fPath.moveTo(x, y); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 105 | for (int i = 0; i < steps; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | x += dist/steps; |
reed@android.com | 92a50ea | 2009-08-14 19:27:37 +0000 | [diff] [blame] | 107 | SkScalar tmpY = y + SkIntToScalar(rand.nextS() % 25); |
| 108 | if (i == steps/2) { |
| 109 | fPath.moveTo(x, tmpY); |
| 110 | } else { |
| 111 | fPath.lineTo(x, tmpY); |
| 112 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | } |
| 114 | |
reed@android.com | 92a50ea | 2009-08-14 19:27:37 +0000 | [diff] [blame] | 115 | { |
| 116 | SkRect oval; |
| 117 | oval.set(SkIntToScalar(20), SkIntToScalar(30), |
| 118 | SkIntToScalar(100), SkIntToScalar(60)); |
| 119 | oval.offset(x, 0); |
| 120 | fPath.addRoundRect(oval, SkIntToScalar(8), SkIntToScalar(8)); |
| 121 | } |
| 122 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | fClickPt.set(SkIntToScalar(200), SkIntToScalar(200)); |
reed@google.com | e5ff439 | 2011-06-01 11:59:08 +0000 | [diff] [blame] | 124 | |
| 125 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | protected: |
| 129 | // overrides from SkEventSink |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 130 | virtual bool onQuery(SkEvent* evt) { |
| 131 | if (SampleCode::TitleQ(*evt)) { |
| 132 | SampleCode::TitleR(evt, "PathEffects"); |
| 133 | return true; |
| 134 | } |
| 135 | return this->INHERITED::onQuery(evt); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 136 | } |
| 137 | |
reed@google.com | e5ff439 | 2011-06-01 11:59:08 +0000 | [diff] [blame] | 138 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 139 | gPhase -= SampleCode::GetAnimSecondsDelta() * 40; |
reed@android.com | 671cd65 | 2009-05-22 20:44:12 +0000 | [diff] [blame] | 140 | this->inval(NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | |
| 142 | SkPaint paint; |
| 143 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 144 | #if 0 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 145 | paint.setAntiAlias(true); |
| 146 | paint.setStyle(SkPaint::kStroke_Style); |
| 147 | paint.setStrokeWidth(SkIntToScalar(5)); |
| 148 | canvas->drawPath(fPath, paint); |
| 149 | paint.setStrokeWidth(0); |
| 150 | |
reed@android.com | 92a50ea | 2009-08-14 19:27:37 +0000 | [diff] [blame] | 151 | paint.setColor(SK_ColorWHITE); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | paint.setPathEffect(make_pe(1))->unref(); |
| 153 | canvas->drawPath(fPath, paint); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 154 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 155 | |
| 156 | canvas->translate(0, SkIntToScalar(50)); |
| 157 | |
| 158 | paint.setColor(SK_ColorBLUE); |
| 159 | paint.setPathEffect(make_pe(2))->unref(); |
| 160 | canvas->drawPath(fPath, paint); |
| 161 | |
| 162 | canvas->translate(0, SkIntToScalar(50)); |
| 163 | |
| 164 | paint.setARGB(0xFF, 0, 0xBB, 0); |
| 165 | paint.setPathEffect(make_pe(3))->unref(); |
| 166 | canvas->drawPath(fPath, paint); |
| 167 | |
| 168 | canvas->translate(0, SkIntToScalar(50)); |
| 169 | |
| 170 | paint.setARGB(0xFF, 0, 0, 0); |
| 171 | paint.setPathEffect(make_warp_pe())->unref(); |
| 172 | paint.setRasterizer(new testrast)->unref(); |
| 173 | canvas->drawPath(fPath, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | private: |
reed@google.com | e5ff439 | 2011-06-01 11:59:08 +0000 | [diff] [blame] | 177 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | ////////////////////////////////////////////////////////////////////////////// |
| 181 | |
| 182 | static SkView* MyFactory() { return new PathEffectView; } |
| 183 | static SkViewRegister reg(MyFactory); |
| 184 | |