reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | #include "SampleCode.h" |
| 2 | #include "SkView.h" |
| 3 | #include "SkCanvas.h" |
| 4 | #include "SkGraphics.h" |
| 5 | #include "SkRandom.h" |
| 6 | #include "SkLayerDrawLooper.h" |
| 7 | #include "SkBlurMaskFilter.h" |
| 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #define WIDTH 200 |
| 10 | #define HEIGHT 200 |
| 11 | |
| 12 | class LooperView : public SkView { |
| 13 | public: |
| 14 | |
| 15 | SkLayerDrawLooper* fLooper; |
| 16 | |
| 17 | LooperView() { |
| 18 | static const struct { |
| 19 | SkColor fColor; |
| 20 | SkPaint::Style fStyle; |
| 21 | SkScalar fWidth; |
| 22 | SkScalar fOffset; |
| 23 | int fBlur; |
| 24 | } gParams[] = { |
| 25 | { SK_ColorWHITE, SkPaint::kStroke_Style, SkIntToScalar(1)*3/4, 0, 0 }, |
| 26 | { SK_ColorRED, SkPaint::kStroke_Style, SkIntToScalar(4), 0, 0 }, |
| 27 | { SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0 }, |
| 28 | { 0x88000000, SkPaint::kFill_Style, 0, SkIntToScalar(10), 3 } |
| 29 | }; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 30 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | fLooper = new SkLayerDrawLooper; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 32 | |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame^] | 33 | SkLayerDrawLooper::LayerInfo info; |
| 34 | info.fFlagsMask = SkPaint::kAntiAlias_Flag; |
| 35 | info.fPaintBits = SkLayerDrawLooper::kStyle_Bit | SkLayerDrawLooper::kMaskFilter_Bit; |
| 36 | info.fColorMode = SkXfermode::kSrc_Mode; |
| 37 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | for (int i = 0; i < SK_ARRAY_COUNT(gParams); i++) { |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame^] | 39 | info.fOffset.set(gParams[i].fOffset, gParams[i].fOffset); |
| 40 | SkPaint* paint = fLooper->addLayer(info); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | paint->setAntiAlias(true); |
| 42 | paint->setColor(gParams[i].fColor); |
| 43 | paint->setStyle(gParams[i].fStyle); |
| 44 | paint->setStrokeWidth(gParams[i].fWidth); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | if (gParams[i].fBlur > 0) { |
| 46 | SkMaskFilter* mf = SkBlurMaskFilter::Create(SkIntToScalar(gParams[i].fBlur), |
| 47 | SkBlurMaskFilter::kNormal_BlurStyle); |
| 48 | paint->setMaskFilter(mf)->unref(); |
| 49 | } |
| 50 | } |
| 51 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 52 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | virtual ~LooperView() { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 54 | SkSafeUnref(fLooper); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 56 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 57 | protected: |
| 58 | // overrides from SkEventSink |
| 59 | virtual bool onQuery(SkEvent* evt) { |
| 60 | if (SampleCode::TitleQ(*evt)) { |
| 61 | SampleCode::TitleR(evt, "DrawLooper"); |
| 62 | return true; |
| 63 | } |
| 64 | return this->INHERITED::onQuery(evt); |
| 65 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 66 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 67 | void drawBG(SkCanvas* canvas) { |
| 68 | canvas->drawColor(0xFFDDDDDD); |
| 69 | // canvas->drawColor(SK_ColorWHITE); |
| 70 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 71 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | virtual void onDraw(SkCanvas* canvas) { |
| 73 | this->drawBG(canvas); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 74 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | SkPaint paint; |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame^] | 76 | paint.setTextSize(SkIntToScalar(72)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 77 | paint.setLooper(fLooper); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 78 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50), |
| 80 | SkIntToScalar(30), paint); |
| 81 | |
| 82 | canvas->drawRectCoords(SkIntToScalar(150), SkIntToScalar(50), |
| 83 | SkIntToScalar(200), SkIntToScalar(100), paint); |
| 84 | |
| 85 | canvas->drawText("Looper", 6, SkIntToScalar(230), SkIntToScalar(100), |
| 86 | paint); |
| 87 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 88 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
| 90 | this->inval(NULL); |
| 91 | return this->INHERITED::onFindClickHandler(x, y); |
| 92 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 93 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | virtual bool onClick(Click* click) { |
| 95 | return this->INHERITED::onClick(click); |
| 96 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 97 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | private: |
| 99 | typedef SkView INHERITED; |
| 100 | }; |
| 101 | |
| 102 | ////////////////////////////////////////////////////////////////////////////// |
| 103 | |
| 104 | static SkView* MyFactory() { return new LooperView; } |
| 105 | static SkViewRegister reg(MyFactory); |
| 106 | |