reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 1 | #include "SampleCode.h" |
| 2 | #include "SkCanvas.h" |
| 3 | #include "SkParsePath.h" |
| 4 | #include "SkPath.h" |
| 5 | #include "SkRandom.h" |
| 6 | #include "SkView.h" |
| 7 | |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame^] | 8 | #include "SkBlurMaskFilter.h" |
| 9 | |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 10 | static void scale_to_width(SkPath* path, SkScalar dstWidth) { |
| 11 | const SkRect& bounds = path->getBounds(); |
| 12 | SkScalar scale = dstWidth / bounds.width(); |
| 13 | SkMatrix matrix; |
| 14 | |
| 15 | matrix.setScale(scale, scale); |
| 16 | path->transform(matrix); |
| 17 | } |
| 18 | |
| 19 | static const struct { |
| 20 | SkPaint::Style fStyle; |
| 21 | SkPaint::Join fJoin; |
| 22 | int fStrokeWidth; |
| 23 | } gRec[] = { |
| 24 | { SkPaint::kFill_Style, SkPaint::kMiter_Join, 0 }, |
| 25 | { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 0 }, |
| 26 | { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 10 }, |
| 27 | { SkPaint::kStrokeAndFill_Style, SkPaint::kMiter_Join, 10 }, |
| 28 | }; |
| 29 | |
| 30 | class StrokePathView : public SkView { |
| 31 | SkScalar fWidth; |
| 32 | SkPath fPath; |
| 33 | public: |
| 34 | StrokePathView() { |
| 35 | fWidth = SkIntToScalar(120); |
| 36 | |
| 37 | #if 0 |
| 38 | const char str[] = |
| 39 | "M 0, 3" |
| 40 | "C 10, -10, 30, -10, 0, 28" |
| 41 | "C -30, -10, -10, -10, 0, 3" |
| 42 | "Z"; |
| 43 | SkParsePath::FromSVGString(str, &fPath); |
| 44 | #else |
| 45 | fPath.addCircle(0, 0, SkIntToScalar(50), SkPath::kCW_Direction); |
| 46 | fPath.addCircle(0, SkIntToScalar(-50), SkIntToScalar(30), SkPath::kCW_Direction); |
| 47 | #endif |
| 48 | |
| 49 | scale_to_width(&fPath, fWidth); |
| 50 | const SkRect& bounds = fPath.getBounds(); |
| 51 | fPath.offset(-bounds.fLeft, -bounds.fTop); |
| 52 | } |
| 53 | |
| 54 | protected: |
| 55 | // overrides from SkEventSink |
| 56 | virtual bool onQuery(SkEvent* evt) { |
| 57 | if (SampleCode::TitleQ(*evt)) { |
| 58 | SampleCode::TitleR(evt, "StrokePath"); |
| 59 | return true; |
| 60 | } |
| 61 | return this->INHERITED::onQuery(evt); |
| 62 | } |
| 63 | |
| 64 | void drawBG(SkCanvas* canvas) { |
| 65 | canvas->drawColor(0xFFDDDDDD); |
| 66 | } |
| 67 | |
| 68 | SkRandom rand; |
| 69 | |
| 70 | void drawSet(SkCanvas* canvas, SkPaint* paint) { |
| 71 | SkAutoCanvasRestore acr(canvas, true); |
| 72 | |
| 73 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) { |
| 74 | paint->setStyle(gRec[i].fStyle); |
| 75 | paint->setStrokeJoin(gRec[i].fJoin); |
| 76 | paint->setStrokeWidth(SkIntToScalar(gRec[i].fStrokeWidth)); |
| 77 | canvas->drawPath(fPath, *paint); |
| 78 | canvas->translate(fWidth * 5 / 4, 0); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | virtual void onDraw(SkCanvas* canvas) { |
| 83 | drawBG(canvas); |
| 84 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
| 85 | |
| 86 | SkPaint paint; |
| 87 | paint.setAntiAlias(true); |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame^] | 88 | |
| 89 | if (true) { |
| 90 | canvas->drawColor(SK_ColorBLACK); |
| 91 | |
| 92 | paint.setTextSize(24); |
| 93 | paint.setColor(SK_ColorWHITE); |
| 94 | canvas->translate(10, 30); |
| 95 | |
| 96 | static const SkBlurMaskFilter::BlurStyle gStyle[] = { |
| 97 | SkBlurMaskFilter::kNormal_BlurStyle, |
| 98 | SkBlurMaskFilter::kInner_BlurStyle, |
| 99 | SkBlurMaskFilter::kOuter_BlurStyle, |
| 100 | SkBlurMaskFilter::kSolid_BlurStyle, |
| 101 | }; |
| 102 | for (int x = 0; x < 5; x++) { |
| 103 | SkMaskFilter* mf; |
| 104 | SkScalar radius = 4; |
| 105 | for (int y = 0; y < 10; y++) { |
| 106 | if (x) { |
| 107 | mf = SkBlurMaskFilter::Create(radius, gStyle[x - 1]); |
| 108 | paint.setMaskFilter(mf)->unref(); |
| 109 | } |
| 110 | canvas->drawText("Title Bar", 9, x*100, y*30, paint); |
| 111 | radius *= 0.75f; |
| 112 | } |
| 113 | |
| 114 | } |
| 115 | return; |
| 116 | } |
| 117 | |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 118 | paint.setColor(SK_ColorBLUE); |
| 119 | |
| 120 | #if 1 |
| 121 | SkPath p; |
| 122 | float r = rand.nextUScalar1() + 0.5f; |
| 123 | SkScalar x = 0, y = 0; |
| 124 | p.moveTo(x, y); |
| 125 | #if 0 |
| 126 | p.cubicTo(x-75*r, y+75*r, x-40*r, y+125*r, x, y+85*r); |
| 127 | p.cubicTo(x+40*r, y+125*r, x+75*r, y+75*r, x, y); |
| 128 | #else |
| 129 | p.cubicTo(x+75*r, y+75*r, x+40*r, y+125*r, x, y+85*r); |
| 130 | p.cubicTo(x-40*r, y+125*r, x-75*r, y+75*r, x, y); |
| 131 | #endif |
| 132 | p.close(); |
| 133 | fPath = p; |
| 134 | fPath.offset(100, 0); |
| 135 | #endif |
| 136 | |
| 137 | fPath.setFillType(SkPath::kWinding_FillType); |
| 138 | drawSet(canvas, &paint); |
| 139 | |
| 140 | canvas->translate(0, fPath.getBounds().height() * 5 / 4); |
| 141 | fPath.setFillType(SkPath::kEvenOdd_FillType); |
| 142 | drawSet(canvas, &paint); |
| 143 | } |
| 144 | |
| 145 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
| 146 | this->inval(NULL); |
| 147 | return this->INHERITED::onFindClickHandler(x, y); |
| 148 | } |
| 149 | private: |
| 150 | typedef SkView INHERITED; |
| 151 | }; |
| 152 | |
| 153 | ////////////////////////////////////////////////////////////////////////////// |
| 154 | |
| 155 | static SkView* MyFactory() { return new StrokePathView; } |
| 156 | static SkViewRegister reg(MyFactory); |
| 157 | |