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