blob: 879481b41c07234d02fe085228b07d0bce2e059d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com0db5a7f2009-11-09 16:01:36 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkGradientShader.h"
12#include "SkAvoidXfermode.h"
13
14///////////////////////////////////////////////////////////////////////////////
15
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000016class AvoidView : public SampleView {
reed@android.com0db5a7f2009-11-09 16:01:36 +000017 SkShader* fShader;
18
19 enum {
20 W = 480,
21 H = 320
22 };
23public:
24 AvoidView() {
25 SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW, SK_ColorGREEN, SK_ColorCYAN, SK_ColorBLUE };
26
27#if 0
28 SkPoint pts[] = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
29 fShader = SkGradientShader::CreateLinear(pts, colors, NULL,
30 SK_ARRAY_COUNT(colors),
31 SkShader::kMirror_TileMode);
32#else
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000033 SkPoint pts[] = { { SkIntToScalar(W)/2, SkIntToScalar(H)/2 } };
reed@android.com0db5a7f2009-11-09 16:01:36 +000034 fShader = SkGradientShader::CreateRadial(pts[0], SkIntToScalar(H)/5,
35 colors, NULL,
36 SK_ARRAY_COUNT(colors),
37 SkShader::kMirror_TileMode);
38#endif
39 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000040
reed@android.com0db5a7f2009-11-09 16:01:36 +000041 virtual ~AvoidView() {
42 fShader->unref();
43 }
44
45protected:
46 virtual bool onQuery(SkEvent* evt) {
47 if (SampleCode::TitleQ(*evt)) {
48 SampleCode::TitleR(evt, "AvoidXfermode");
49 return true;
50 }
51 return this->INHERITED::onQuery(evt);
52 }
53
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000054 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com0db5a7f2009-11-09 16:01:36 +000055 SkPaint paint;
56 SkRect r = { 0, 0, SkIntToScalar(W), SkIntToScalar(H) };
rmistry@google.comae933ce2012-08-23 18:19:56 +000057
reed@android.com0db5a7f2009-11-09 16:01:36 +000058 canvas->translate(r.width() / 6, r.height() / 6);
59
60 paint.setShader(fShader);
61 canvas->drawRect(r, paint);
62
63 static const struct {
64 int fTolerance;
65 SkAvoidXfermode::Mode fMode;
66 float fDX, fDY;
67 } gData[] = {
68 { 16, SkAvoidXfermode::kAvoidColor_Mode, 0, 0 },
69 { 255-16, SkAvoidXfermode::kAvoidColor_Mode, 1, 0 },
70 { 16, SkAvoidXfermode::kTargetColor_Mode, 0, 1 },
71 { 255-16, SkAvoidXfermode::kTargetColor_Mode, 1, 1 },
72 };
73
74 paint.setShader(NULL);
75 paint.setColor(SK_ColorMAGENTA);
rmistry@google.comae933ce2012-08-23 18:19:56 +000076
reed@android.com0db5a7f2009-11-09 16:01:36 +000077 SkPaint frameP;
78 frameP.setStyle(SkPaint::kStroke_Style);
79
80 for (size_t i = 0; i < SK_ARRAY_COUNT(gData); i++) {
81 SkAvoidXfermode mode(SK_ColorGREEN, gData[i].fTolerance,
82 gData[i].fMode);
83 paint.setXfermode(&mode);
84 int div = 3;
85 SkRect rr = { 0, 0, r.width()/div, r.height()/div };
86 rr.offset(r.width()/4 - rr.width()/2, r.height()/4 - rr.height()/2);
87 rr.offset(r.width() * gData[i].fDX/2, r.height() * gData[i].fDY/2);
88 canvas->drawRect(rr, paint);
89 paint.setXfermode(NULL);
90
91 canvas->drawRect(rr, frameP);
92 }
93 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000094
reed@android.com0db5a7f2009-11-09 16:01:36 +000095private:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000096 typedef SampleView INHERITED;
reed@android.com0db5a7f2009-11-09 16:01:36 +000097};
98
99//////////////////////////////////////////////////////////////////////////////
100
101static SkView* MyFactory() {
102 return new AvoidView;
103}
104
105static SkViewRegister reg(MyFactory);