epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
reed@android.com | 0db5a7f | 2009-11-09 16:01:36 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkGradientShader.h" |
| 12 | #include "SkAvoidXfermode.h" |
| 13 | |
| 14 | /////////////////////////////////////////////////////////////////////////////// |
| 15 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 16 | class AvoidView : public SampleView { |
reed@android.com | 0db5a7f | 2009-11-09 16:01:36 +0000 | [diff] [blame] | 17 | SkShader* fShader; |
| 18 | |
| 19 | enum { |
| 20 | W = 480, |
| 21 | H = 320 |
| 22 | }; |
| 23 | public: |
| 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.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 33 | SkPoint pts[] = { { SkIntToScalar(W)/2, SkIntToScalar(H)/2 } }; |
reed@android.com | 0db5a7f | 2009-11-09 16:01:36 +0000 | [diff] [blame] | 34 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 40 | |
reed@android.com | 0db5a7f | 2009-11-09 16:01:36 +0000 | [diff] [blame] | 41 | virtual ~AvoidView() { |
| 42 | fShader->unref(); |
| 43 | } |
| 44 | |
| 45 | protected: |
| 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.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 54 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 0db5a7f | 2009-11-09 16:01:36 +0000 | [diff] [blame] | 55 | SkPaint paint; |
| 56 | SkRect r = { 0, 0, SkIntToScalar(W), SkIntToScalar(H) }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 57 | |
reed@android.com | 0db5a7f | 2009-11-09 16:01:36 +0000 | [diff] [blame] | 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); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 76 | |
reed@android.com | 0db5a7f | 2009-11-09 16:01:36 +0000 | [diff] [blame] | 77 | SkPaint frameP; |
| 78 | frameP.setStyle(SkPaint::kStroke_Style); |
| 79 | |
| 80 | for (size_t i = 0; i < SK_ARRAY_COUNT(gData); i++) { |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 81 | SkAutoTUnref<SkAvoidXfermode> mode(SkAvoidXfermode::Create( |
| 82 | SK_ColorGREEN, gData[i].fTolerance, gData[i].fMode)); |
| 83 | paint.setXfermode(mode); |
reed@android.com | 0db5a7f | 2009-11-09 16:01:36 +0000 | [diff] [blame] | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 94 | |
reed@android.com | 0db5a7f | 2009-11-09 16:01:36 +0000 | [diff] [blame] | 95 | private: |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 96 | typedef SampleView INHERITED; |
reed@android.com | 0db5a7f | 2009-11-09 16:01:36 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | ////////////////////////////////////////////////////////////////////////////// |
| 100 | |
| 101 | static SkView* MyFactory() { |
| 102 | return new AvoidView; |
| 103 | } |
| 104 | |
| 105 | static SkViewRegister reg(MyFactory); |