blob: faeee20696d18775980f77a43807d35bee77f1af [file] [log] [blame]
reed@android.com0db5a7f2009-11-09 16:01:36 +00001#include "SampleCode.h"
2#include "SkView.h"
3#include "SkCanvas.h"
4#include "SkGradientShader.h"
5#include "SkAvoidXfermode.h"
6
7///////////////////////////////////////////////////////////////////////////////
8
9class AvoidView : public SkView {
10 SkShader* fShader;
11
12 enum {
13 W = 480,
14 H = 320
15 };
16public:
17 AvoidView() {
18 SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW, SK_ColorGREEN, SK_ColorCYAN, SK_ColorBLUE };
19
20#if 0
21 SkPoint pts[] = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
22 fShader = SkGradientShader::CreateLinear(pts, colors, NULL,
23 SK_ARRAY_COUNT(colors),
24 SkShader::kMirror_TileMode);
25#else
26 SkPoint pts[] = { SkIntToScalar(W)/2, SkIntToScalar(H)/2 };
27 fShader = SkGradientShader::CreateRadial(pts[0], SkIntToScalar(H)/5,
28 colors, NULL,
29 SK_ARRAY_COUNT(colors),
30 SkShader::kMirror_TileMode);
31#endif
32 }
33
34 virtual ~AvoidView() {
35 fShader->unref();
36 }
37
38protected:
39 virtual bool onQuery(SkEvent* evt) {
40 if (SampleCode::TitleQ(*evt)) {
41 SampleCode::TitleR(evt, "AvoidXfermode");
42 return true;
43 }
44 return this->INHERITED::onQuery(evt);
45 }
46
47 virtual void onDraw(SkCanvas* canvas) {
48 canvas->drawColor(SK_ColorWHITE);
49
50 SkPaint paint;
51 SkRect r = { 0, 0, SkIntToScalar(W), SkIntToScalar(H) };
52
53 canvas->translate(r.width() / 6, r.height() / 6);
54
55 paint.setShader(fShader);
56 canvas->drawRect(r, paint);
57
58 static const struct {
59 int fTolerance;
60 SkAvoidXfermode::Mode fMode;
61 float fDX, fDY;
62 } gData[] = {
63 { 16, SkAvoidXfermode::kAvoidColor_Mode, 0, 0 },
64 { 255-16, SkAvoidXfermode::kAvoidColor_Mode, 1, 0 },
65 { 16, SkAvoidXfermode::kTargetColor_Mode, 0, 1 },
66 { 255-16, SkAvoidXfermode::kTargetColor_Mode, 1, 1 },
67 };
68
69 paint.setShader(NULL);
70 paint.setColor(SK_ColorMAGENTA);
71
72 SkPaint frameP;
73 frameP.setStyle(SkPaint::kStroke_Style);
74
75 for (size_t i = 0; i < SK_ARRAY_COUNT(gData); i++) {
76 SkAvoidXfermode mode(SK_ColorGREEN, gData[i].fTolerance,
77 gData[i].fMode);
78 paint.setXfermode(&mode);
79 int div = 3;
80 SkRect rr = { 0, 0, r.width()/div, r.height()/div };
81 rr.offset(r.width()/4 - rr.width()/2, r.height()/4 - rr.height()/2);
82 rr.offset(r.width() * gData[i].fDX/2, r.height() * gData[i].fDY/2);
83 canvas->drawRect(rr, paint);
84 paint.setXfermode(NULL);
85
86 canvas->drawRect(rr, frameP);
87 }
88 }
89
90private:
91 typedef SkView INHERITED;
92};
93
94//////////////////////////////////////////////////////////////////////////////
95
96static SkView* MyFactory() {
97 return new AvoidView;
98}
99
100static SkViewRegister reg(MyFactory);
101