blob: 868a67c62f792252bcb97614e5045d96e0ac082e [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
mike@reedtribe.org5fd92432011-05-05 01:59:48 +00009class AvoidView : public SampleView {
reed@android.com0db5a7f2009-11-09 16:01:36 +000010 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
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000026 SkPoint pts[] = { { SkIntToScalar(W)/2, SkIntToScalar(H)/2 } };
reed@android.com0db5a7f2009-11-09 16:01:36 +000027 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
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000047 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com0db5a7f2009-11-09 16:01:36 +000048 SkPaint paint;
49 SkRect r = { 0, 0, SkIntToScalar(W), SkIntToScalar(H) };
50
51 canvas->translate(r.width() / 6, r.height() / 6);
52
53 paint.setShader(fShader);
54 canvas->drawRect(r, paint);
55
56 static const struct {
57 int fTolerance;
58 SkAvoidXfermode::Mode fMode;
59 float fDX, fDY;
60 } gData[] = {
61 { 16, SkAvoidXfermode::kAvoidColor_Mode, 0, 0 },
62 { 255-16, SkAvoidXfermode::kAvoidColor_Mode, 1, 0 },
63 { 16, SkAvoidXfermode::kTargetColor_Mode, 0, 1 },
64 { 255-16, SkAvoidXfermode::kTargetColor_Mode, 1, 1 },
65 };
66
67 paint.setShader(NULL);
68 paint.setColor(SK_ColorMAGENTA);
69
70 SkPaint frameP;
71 frameP.setStyle(SkPaint::kStroke_Style);
72
73 for (size_t i = 0; i < SK_ARRAY_COUNT(gData); i++) {
74 SkAvoidXfermode mode(SK_ColorGREEN, gData[i].fTolerance,
75 gData[i].fMode);
76 paint.setXfermode(&mode);
77 int div = 3;
78 SkRect rr = { 0, 0, r.width()/div, r.height()/div };
79 rr.offset(r.width()/4 - rr.width()/2, r.height()/4 - rr.height()/2);
80 rr.offset(r.width() * gData[i].fDX/2, r.height() * gData[i].fDY/2);
81 canvas->drawRect(rr, paint);
82 paint.setXfermode(NULL);
83
84 canvas->drawRect(rr, frameP);
85 }
86 }
87
88private:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000089 typedef SampleView INHERITED;
reed@android.com0db5a7f2009-11-09 16:01:36 +000090};
91
92//////////////////////////////////////////////////////////////////////////////
93
94static SkView* MyFactory() {
95 return new AvoidView;
96}
97
98static SkViewRegister reg(MyFactory);
99