SampleComplexClip.cpp revision 807cec4397cb50a64de7355ae129cc7c8f2c772b
1#include "SampleCode.h" 2#include "SkCanvas.h" 3#include "SkPath.h" 4#include "SkView.h" 5 6class ComplexClipView : public SkView { 7public: 8 ComplexClipView() { 9 } 10 11protected: 12 // overrides from SkEventSink 13 virtual bool onQuery(SkEvent* evt) { 14 if (SampleCode::TitleQ(*evt)) { 15 SampleCode::TitleR(evt, "ComplexClip"); 16 return true; 17 } 18 return this->INHERITED::onQuery(evt); 19 } 20 21 void drawBG(SkCanvas* canvas) { 22 canvas->drawColor(SkColorSetRGB(0xA0,0xDD,0xA0)); 23 } 24 25 virtual void onDraw(SkCanvas* canvas) { 26 SkPath path; 27 path.moveTo(SkIntToScalar(0), SkIntToScalar(50)); 28 path.quadTo(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(50), SkIntToScalar(0)); 29 path.lineTo(SkIntToScalar(175), SkIntToScalar(0)); 30 path.quadTo(SkIntToScalar(200), SkIntToScalar(0), SkIntToScalar(200), SkIntToScalar(25)); 31 path.lineTo(SkIntToScalar(200), SkIntToScalar(150)); 32 path.quadTo(SkIntToScalar(200), SkIntToScalar(200), SkIntToScalar(150), SkIntToScalar(200)); 33 path.lineTo(SkIntToScalar(0), SkIntToScalar(200)); 34 path.close(); 35 path.moveTo(SkIntToScalar(50), SkIntToScalar(50)); 36 path.lineTo(SkIntToScalar(150), SkIntToScalar(50)); 37 path.lineTo(SkIntToScalar(150), SkIntToScalar(125)); 38 path.quadTo(SkIntToScalar(150), SkIntToScalar(150), SkIntToScalar(125), SkIntToScalar(150)); 39 path.lineTo(SkIntToScalar(50), SkIntToScalar(150)); 40 path.close(); 41 path.setFillType(SkPath::kEvenOdd_FillType); 42 SkColor pathColor = SK_ColorBLACK; 43 SkPaint pathPaint; 44 pathPaint.setAntiAlias(true); 45 pathPaint.setColor(pathColor); 46 47 SkPath clipA; 48 clipA.moveTo(SkIntToScalar(10), SkIntToScalar(20)); 49 clipA.lineTo(SkIntToScalar(165), SkIntToScalar(22)); 50 clipA.lineTo(SkIntToScalar(70), SkIntToScalar(105)); 51 clipA.lineTo(SkIntToScalar(165), SkIntToScalar(177)); 52 clipA.lineTo(SkIntToScalar(-5), SkIntToScalar(180)); 53 clipA.close(); 54 SkColor colorA = SK_ColorCYAN; 55 56 SkPath clipB; 57 clipB.moveTo(SkIntToScalar(40), SkIntToScalar(10)); 58 clipB.lineTo(SkIntToScalar(190), SkIntToScalar(15)); 59 clipB.lineTo(SkIntToScalar(195), SkIntToScalar(190)); 60 clipB.lineTo(SkIntToScalar(40), SkIntToScalar(185)); 61 clipB.lineTo(SkIntToScalar(155), SkIntToScalar(100)); 62 clipB.close(); 63 SkColor colorB = SK_ColorRED; 64 65 drawBG(canvas); 66 SkPaint paint; 67 paint.setAntiAlias(true); 68 69 paint.setStyle(SkPaint::kStroke_Style); 70 paint.setStrokeWidth(0); 71 72 canvas->translate(SkIntToScalar(10),SkIntToScalar(10)); 73 canvas->drawPath(path, pathPaint); 74 paint.setColor(colorA); 75 canvas->drawPath(clipA, paint); 76 paint.setColor(colorB); 77 canvas->drawPath(clipB, paint); 78 79 static const struct { 80 SkRegion::Op fOp; 81 const char* fName; 82 } gOps[] = { //extra spaces in names for measureText 83 {SkRegion::kIntersect_Op, "Isect "}, 84 {SkRegion::kDifference_Op, "Diff " }, 85 {SkRegion::kUnion_Op, "Union "}, 86 {SkRegion::kXOR_Op, "Xor " }, 87 {SkRegion::kReverseDifference_Op, "RDiff "} 88 }; 89 90 canvas->translate(0, SkIntToScalar(40)); 91 canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4); 92 canvas->save(); 93 94 for (int invA = 0; invA < 2; ++invA) { 95 for (int op = 0; op < SK_ARRAY_COUNT(gOps); ++op) { 96 int idx = invA * SK_ARRAY_COUNT(gOps) + op; 97 if (!(idx % 3)) { 98 canvas->restore(); 99 canvas->translate(0, SkIntToScalar(250)); 100 canvas->save(); 101 } 102 canvas->save(); 103 // set clip 104 clipA.setFillType(invA ? SkPath::kInverseEvenOdd_FillType : 105 SkPath::kEvenOdd_FillType); 106 canvas->clipPath(clipA); 107 canvas->clipPath(clipB, gOps[op].fOp); 108 109 // draw path clipped 110 canvas->drawPath(path, pathPaint); 111 canvas->restore(); 112 113 // draw path in hairline 114 paint.setColor(pathColor); 115 canvas->drawPath(path, paint); 116 117 // draw clips in hair line 118 paint.setColor(colorA); 119 canvas->drawPath(clipA, paint); 120 paint.setColor(colorB); 121 canvas->drawPath(clipB, paint); 122 123 paint.setTextSize(SkIntToScalar(20)); 124 125 SkScalar txtX = SkIntToScalar(55); 126 paint.setColor(colorA); 127 const char* aTxt = invA ? "InverseA " : "A "; 128 canvas->drawText(aTxt, strlen(aTxt), txtX, SkIntToScalar(220), paint); 129 txtX += paint.measureText(aTxt, strlen(aTxt)); 130 paint.setColor(SK_ColorBLACK); 131 canvas->drawText(gOps[op].fName, strlen(gOps[op].fName), 132 txtX, SkIntToScalar(220), paint); 133 txtX += paint.measureText(gOps[op].fName, strlen(gOps[op].fName)); 134 paint.setColor(colorB); 135 canvas->drawText("B", 1, txtX, SkIntToScalar(220), paint); 136 137 canvas->translate(SkIntToScalar(250),0); 138 } 139 } 140 canvas->restore(); 141 } 142 143 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { 144 this->inval(NULL); 145 return this->INHERITED::onFindClickHandler(x, y); 146 } 147private: 148 typedef SkView INHERITED; 149}; 150 151////////////////////////////////////////////////////////////////////////////// 152 153static SkView* MyFactory() { return new ComplexClipView; } 154static SkViewRegister reg(MyFactory); 155 156