• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
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  */
8 #include "SampleCode.h"
9 #include "SkView.h"
10 #include "SkCanvas.h"
11 #include "SkGradientShader.h"
12 #include "SkAvoidXfermode.h"
13 
14 ///////////////////////////////////////////////////////////////////////////////
15 
16 class AvoidView : public SampleView {
17     SkShader* fShader;
18 
19     enum {
20         W = 480,
21         H = 320
22     };
23 public:
AvoidView()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
33         SkPoint pts[] = { { SkIntToScalar(W)/2, SkIntToScalar(H)/2 } };
34         fShader = SkGradientShader::CreateRadial(pts[0], SkIntToScalar(H)/5,
35                                                  colors, NULL,
36                                                  SK_ARRAY_COUNT(colors),
37                                                  SkShader::kMirror_TileMode);
38 #endif
39     }
40 
~AvoidView()41     virtual ~AvoidView() {
42         fShader->unref();
43     }
44 
45 protected:
onQuery(SkEvent * evt)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 
onDrawContent(SkCanvas * canvas)54     virtual void onDrawContent(SkCanvas* canvas) {
55         SkPaint paint;
56         SkRect r = { 0, 0, SkIntToScalar(W), SkIntToScalar(H) };
57 
58         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);
76 
77         SkPaint frameP;
78         frameP.setStyle(SkPaint::kStroke_Style);
79 
80         for (size_t i = 0; i < SK_ARRAY_COUNT(gData); i++) {
81             SkAutoTUnref<SkAvoidXfermode> mode(SkAvoidXfermode::Create(
82                 SK_ColorGREEN, gData[i].fTolerance, 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     }
94 
95 private:
96     typedef SampleView INHERITED;
97 };
98 
99 //////////////////////////////////////////////////////////////////////////////
100 
MyFactory()101 static SkView* MyFactory() {
102     return new AvoidView;
103 }
104 
105 static SkViewRegister reg(MyFactory);
106