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" 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 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 23 static SkPathEffect* make_pe(int flags) { 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 47 static SkPathEffect* make_warp_pe() { 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" 67 #include "SkLayerRasterizer.h" 68 69 class testrast : public SkLayerRasterizer { 70 public: 71 testrast() { 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); 81 paint.setXfermode(SkXfermode::kClear_Mode); 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 93 class PathEffectView : public SampleView { 94 SkPath fPath; 95 SkPoint fClickPt; 96 public: 97 PathEffectView() { 98 SkRandom rand; 99 int steps = 20; 100 SkScalar dist = SkIntToScalar(400); 101 SkScalar x = SkIntToScalar(20); 102 SkScalar y = SkIntToScalar(50); 103 104 fPath.moveTo(x, y); 105 for (int i = 0; i < steps; i++) { 106 x += dist/steps; 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 } 113 } 114 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 123 fClickPt.set(SkIntToScalar(200), SkIntToScalar(200)); 124 125 this->setBGColor(0xFFDDDDDD); 126 } 127 128 protected: 129 // overrides from SkEventSink 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); 136 } 137 138 virtual void onDrawContent(SkCanvas* canvas) { 139 gPhase -= SampleCode::GetAnimSecondsDelta() * 40; 140 this->inval(NULL); 141 142 SkPaint paint; 143 144 #if 0 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 151 paint.setColor(SK_ColorWHITE); 152 paint.setPathEffect(make_pe(1))->unref(); 153 canvas->drawPath(fPath, paint); 154 #endif 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); 174 } 175 176 private: 177 typedef SampleView INHERITED; 178 }; 179 180 ////////////////////////////////////////////////////////////////////////////// 181 182 static SkView* MyFactory() { return new PathEffectView; } 183 static SkViewRegister reg(MyFactory); 184 185