epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkPaint.h" |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 11 | #include "SkView.h" |
| 12 | |
| 13 | #include "SkBlurMaskFilter.h" |
| 14 | #include "SkColorMatrixFilter.h" |
| 15 | #include "SkDiscretePathEffect.h" |
| 16 | #include "SkGradientShader.h" |
| 17 | |
| 18 | //#define COLOR 0xFFFF8844 |
| 19 | #define COLOR 0xFF888888 |
| 20 | |
| 21 | static void paint_proc0(SkPaint* paint) { |
| 22 | } |
| 23 | |
| 24 | static void paint_proc1(SkPaint* paint) { |
| 25 | paint->setMaskFilter(SkBlurMaskFilter::Create(2, |
| 26 | SkBlurMaskFilter::kNormal_BlurStyle))->unref(); |
| 27 | } |
| 28 | |
| 29 | static void paint_proc2(SkPaint* paint) { |
| 30 | SkScalar dir[3] = { 1, 1, 1}; |
| 31 | paint->setMaskFilter( |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 32 | SkBlurMaskFilter::CreateEmboss(dir, 0.1f, 0.05f, 1))->unref(); |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | static void paint_proc3(SkPaint* paint) { |
| 36 | SkColor colors[] = { SK_ColorRED, COLOR, SK_ColorBLUE }; |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 37 | SkPoint pts[] = { { 3, 0 }, { 7, 5 } }; |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 38 | paint->setShader(SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), |
| 39 | SkShader::kMirror_TileMode))->unref(); |
| 40 | } |
| 41 | |
| 42 | static void paint_proc5(SkPaint* paint) { |
| 43 | paint_proc3(paint); |
| 44 | paint_proc2(paint); |
| 45 | } |
| 46 | |
| 47 | typedef void (*PaintProc)(SkPaint*); |
| 48 | const PaintProc gPaintProcs[] = { |
| 49 | paint_proc0, |
| 50 | paint_proc1, |
| 51 | paint_proc2, |
| 52 | paint_proc3, |
| 53 | paint_proc5, |
| 54 | }; |
| 55 | |
| 56 | /////////////////////////////////////////////////////////////////////////////// |
| 57 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 58 | class EffectsView : public SampleView { |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 59 | public: |
| 60 | SkPath fPath; |
| 61 | SkPaint fPaint[SK_ARRAY_COUNT(gPaintProcs)]; |
| 62 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 63 | EffectsView() { |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 64 | size_t i; |
| 65 | const float pts[] = { |
| 66 | 0, 0, |
| 67 | 10, 0, |
| 68 | 10, 5, |
| 69 | 20, -5, |
| 70 | 10, -15, |
| 71 | 10, -10, |
| 72 | 0, -10 |
| 73 | }; |
| 74 | fPath.moveTo(pts[0], pts[1]); |
| 75 | for (i = 2; i < SK_ARRAY_COUNT(pts); i += 2) { |
| 76 | fPath.lineTo(pts[i], pts[i+1]); |
| 77 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 78 | |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 79 | for (i = 0; i < SK_ARRAY_COUNT(gPaintProcs); i++) { |
| 80 | fPaint[i].setAntiAlias(true); |
| 81 | fPaint[i].setColor(COLOR); |
| 82 | gPaintProcs[i](&fPaint[i]); |
| 83 | } |
reed@android.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 84 | |
reed@android.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 85 | SkColorMatrix cm; |
| 86 | cm.setRotate(SkColorMatrix::kG_Axis, 180); |
| 87 | cm.setIdentity(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 88 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 89 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 90 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 91 | |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 92 | protected: |
| 93 | // overrides from SkEventSink |
| 94 | virtual bool onQuery(SkEvent* evt) { |
| 95 | if (SampleCode::TitleQ(*evt)) { |
| 96 | SampleCode::TitleR(evt, "Effects"); |
| 97 | return true; |
| 98 | } |
| 99 | return this->INHERITED::onQuery(evt); |
| 100 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 101 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 102 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 103 | canvas->scale(3, 3); |
| 104 | canvas->translate(10, 30); |
| 105 | for (size_t i = 0; i < SK_ARRAY_COUNT(fPaint); i++) { |
| 106 | canvas->drawPath(fPath, fPaint[i]); |
| 107 | canvas->translate(32, 0); |
| 108 | } |
| 109 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 110 | |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 111 | private: |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 112 | typedef SampleView INHERITED; |
reed@android.com | ba974cc | 2009-05-22 13:48:35 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | /////////////////////////////////////////////////////////////////////////////// |
| 116 | |
| 117 | static SkView* MyFactory() { return new EffectsView; } |
| 118 | static SkViewRegister reg(MyFactory); |