blob: 7ea7b75b079006d16d41e58c974c7dde55925b20 [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 };
30
31 fLooper = new SkLayerDrawLooper;
32
33 for (int i = 0; i < SK_ARRAY_COUNT(gParams); i++) {
34 SkPaint* paint = fLooper->addLayer(gParams[i].fOffset,
35 gParams[i].fOffset);
36 paint->setAntiAlias(true);
37 paint->setColor(gParams[i].fColor);
38 paint->setStyle(gParams[i].fStyle);
39 paint->setStrokeWidth(gParams[i].fWidth);
40 paint->setTextSize(SkIntToScalar(72));
41 if (gParams[i].fBlur > 0) {
42 SkMaskFilter* mf = SkBlurMaskFilter::Create(SkIntToScalar(gParams[i].fBlur),
43 SkBlurMaskFilter::kNormal_BlurStyle);
44 paint->setMaskFilter(mf)->unref();
45 }
46 }
47 }
48
49 virtual ~LooperView() {
50 fLooper->safeUnref();
51 }
52
53protected:
54 // overrides from SkEventSink
55 virtual bool onQuery(SkEvent* evt) {
56 if (SampleCode::TitleQ(*evt)) {
57 SampleCode::TitleR(evt, "DrawLooper");
58 return true;
59 }
60 return this->INHERITED::onQuery(evt);
61 }
62
63 void drawBG(SkCanvas* canvas) {
64 canvas->drawColor(0xFFDDDDDD);
65// canvas->drawColor(SK_ColorWHITE);
66 }
67
68 virtual void onDraw(SkCanvas* canvas) {
69 this->drawBG(canvas);
70
71 SkPaint paint;
72 paint.setLooper(fLooper);
73
74 canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
75 SkIntToScalar(30), paint);
76
77 canvas->drawRectCoords(SkIntToScalar(150), SkIntToScalar(50),
78 SkIntToScalar(200), SkIntToScalar(100), paint);
79
80 canvas->drawText("Looper", 6, SkIntToScalar(230), SkIntToScalar(100),
81 paint);
82 }
83
84 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
85 this->inval(NULL);
86 return this->INHERITED::onFindClickHandler(x, y);
87 }
88
89 virtual bool onClick(Click* click) {
90 return this->INHERITED::onClick(click);
91 }
92
93private:
94 typedef SkView INHERITED;
95};
96
97//////////////////////////////////////////////////////////////////////////////
98
99static SkView* MyFactory() { return new LooperView; }
100static SkViewRegister reg(MyFactory);
101