blob: e1b63a1839bffe1e1a5bc034897d26b592db7d20 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001#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.com8a1c16f2008-12-17 15:59:43 +00009#define WIDTH 200
10#define HEIGHT 200
11
12class LooperView : public SkView {
13public:
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.com82065d62011-02-07 15:30:46 +000030
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 fLooper = new SkLayerDrawLooper;
reed@google.com82065d62011-02-07 15:30:46 +000032
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000033 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.com8a1c16f2008-12-17 15:59:43 +000038 for (int i = 0; i < SK_ARRAY_COUNT(gParams); i++) {
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000039 info.fOffset.set(gParams[i].fOffset, gParams[i].fOffset);
40 SkPaint* paint = fLooper->addLayer(info);
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 paint->setAntiAlias(true);
42 paint->setColor(gParams[i].fColor);
43 paint->setStyle(gParams[i].fStyle);
44 paint->setStrokeWidth(gParams[i].fWidth);
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 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.com82065d62011-02-07 15:30:46 +000052
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 virtual ~LooperView() {
reed@google.com82065d62011-02-07 15:30:46 +000054 SkSafeUnref(fLooper);
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 }
reed@google.com82065d62011-02-07 15:30:46 +000056
reed@android.com8a1c16f2008-12-17 15:59:43 +000057protected:
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.com82065d62011-02-07 15:30:46 +000066
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 void drawBG(SkCanvas* canvas) {
68 canvas->drawColor(0xFFDDDDDD);
69// canvas->drawColor(SK_ColorWHITE);
70 }
reed@google.com82065d62011-02-07 15:30:46 +000071
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 virtual void onDraw(SkCanvas* canvas) {
73 this->drawBG(canvas);
reed@google.com82065d62011-02-07 15:30:46 +000074
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 SkPaint paint;
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000076 paint.setTextSize(SkIntToScalar(72));
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 paint.setLooper(fLooper);
reed@google.com82065d62011-02-07 15:30:46 +000078
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 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.com82065d62011-02-07 15:30:46 +000088
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
90 this->inval(NULL);
91 return this->INHERITED::onFindClickHandler(x, y);
92 }
reed@google.com82065d62011-02-07 15:30:46 +000093
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 virtual bool onClick(Click* click) {
95 return this->INHERITED::onClick(click);
96 }
reed@google.com82065d62011-02-07 15:30:46 +000097
reed@android.com8a1c16f2008-12-17 15:59:43 +000098private:
99 typedef SkView INHERITED;
100};
101
102//////////////////////////////////////////////////////////////////////////////
103
104static SkView* MyFactory() { return new LooperView; }
105static SkViewRegister reg(MyFactory);
106