reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 1 | #include "SampleCode.h" |
| 2 | #include "SkCanvas.h" |
| 3 | #include "SkPaint.h" |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 4 | #include "SkView.h" |
| 5 | |
| 6 | #include "SkBlurMaskFilter.h" |
| 7 | #include "SkColorMatrixFilter.h" |
| 8 | #include "SkDiscretePathEffect.h" |
| 9 | #include "SkGradientShader.h" |
| 10 | |
| 11 | //#define COLOR 0xFFFF8844 |
| 12 | #define COLOR 0xFF888888 |
| 13 | |
| 14 | static void paint_proc0(SkPaint* paint) { |
| 15 | } |
| 16 | |
| 17 | static void paint_proc1(SkPaint* paint) { |
| 18 | paint->setMaskFilter(SkBlurMaskFilter::Create(2, |
| 19 | SkBlurMaskFilter::kNormal_BlurStyle))->unref(); |
| 20 | } |
| 21 | |
| 22 | static void paint_proc2(SkPaint* paint) { |
| 23 | SkScalar dir[3] = { 1, 1, 1}; |
| 24 | paint->setMaskFilter( |
| 25 | SkBlurMaskFilter::CreateEmboss(dir, 0.1, 0.05, 1))->unref(); |
| 26 | } |
| 27 | |
| 28 | static void paint_proc3(SkPaint* paint) { |
| 29 | SkColor colors[] = { SK_ColorRED, COLOR, SK_ColorBLUE }; |
| 30 | SkPoint pts[] = { 3, 0, 7, 5 }; |
| 31 | paint->setShader(SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), |
| 32 | SkShader::kMirror_TileMode))->unref(); |
| 33 | } |
| 34 | |
| 35 | static void paint_proc5(SkPaint* paint) { |
| 36 | paint_proc3(paint); |
| 37 | paint_proc2(paint); |
| 38 | } |
| 39 | |
| 40 | typedef void (*PaintProc)(SkPaint*); |
| 41 | const PaintProc gPaintProcs[] = { |
| 42 | paint_proc0, |
| 43 | paint_proc1, |
| 44 | paint_proc2, |
| 45 | paint_proc3, |
| 46 | paint_proc5, |
| 47 | }; |
| 48 | |
| 49 | /////////////////////////////////////////////////////////////////////////////// |
| 50 | |
| 51 | class EffectsView : public SkView { |
| 52 | public: |
| 53 | SkPath fPath; |
| 54 | SkPaint fPaint[SK_ARRAY_COUNT(gPaintProcs)]; |
| 55 | |
| 56 | EffectsView() { |
| 57 | size_t i; |
| 58 | const float pts[] = { |
| 59 | 0, 0, |
| 60 | 10, 0, |
| 61 | 10, 5, |
| 62 | 20, -5, |
| 63 | 10, -15, |
| 64 | 10, -10, |
| 65 | 0, -10 |
| 66 | }; |
| 67 | fPath.moveTo(pts[0], pts[1]); |
| 68 | for (i = 2; i < SK_ARRAY_COUNT(pts); i += 2) { |
| 69 | fPath.lineTo(pts[i], pts[i+1]); |
| 70 | } |
| 71 | |
| 72 | for (i = 0; i < SK_ARRAY_COUNT(gPaintProcs); i++) { |
| 73 | fPaint[i].setAntiAlias(true); |
| 74 | fPaint[i].setColor(COLOR); |
| 75 | gPaintProcs[i](&fPaint[i]); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | protected: |
| 80 | // overrides from SkEventSink |
| 81 | virtual bool onQuery(SkEvent* evt) { |
| 82 | if (SampleCode::TitleQ(*evt)) { |
| 83 | SampleCode::TitleR(evt, "Effects"); |
| 84 | return true; |
| 85 | } |
| 86 | return this->INHERITED::onQuery(evt); |
| 87 | } |
| 88 | |
| 89 | void drawBG(SkCanvas* canvas) { |
| 90 | canvas->drawColor(0xFFDDDDDD); |
| 91 | } |
| 92 | |
| 93 | virtual void onDraw(SkCanvas* canvas) { |
| 94 | this->drawBG(canvas); |
| 95 | |
| 96 | canvas->scale(3, 3); |
| 97 | canvas->translate(10, 30); |
| 98 | for (size_t i = 0; i < SK_ARRAY_COUNT(fPaint); i++) { |
| 99 | canvas->drawPath(fPath, fPaint[i]); |
| 100 | canvas->translate(32, 0); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | private: |
| 105 | typedef SkView INHERITED; |
| 106 | }; |
| 107 | |
| 108 | /////////////////////////////////////////////////////////////////////////////// |
| 109 | |
| 110 | static SkView* MyFactory() { return new EffectsView; } |
| 111 | static SkViewRegister reg(MyFactory); |
| 112 | |