reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame^] | 1 | #include "SampleCode.h" |
| 2 | #include "SkView.h" |
| 3 | #include "SkCanvas.h" |
| 4 | #include "SkGradientShader.h" |
| 5 | |
| 6 | struct GradData { |
| 7 | int fCount; |
| 8 | const SkColor* fColors; |
| 9 | const SkScalar* fPos; |
| 10 | }; |
| 11 | |
| 12 | static const SkColor gColors[] = { |
| 13 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK |
| 14 | }; |
| 15 | static const SkScalar gPos0[] = { 0, SK_Scalar1 }; |
| 16 | static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 }; |
| 17 | static const SkScalar gPos2[] = { |
| 18 | 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1 |
| 19 | }; |
| 20 | |
| 21 | static const GradData gGradData[] = { |
| 22 | { 2, gColors, NULL }, |
| 23 | { 2, gColors, gPos0 }, |
| 24 | { 2, gColors, gPos1 }, |
| 25 | { 5, gColors, NULL }, |
| 26 | { 5, gColors, gPos2 } |
| 27 | }; |
| 28 | |
| 29 | static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, |
| 30 | SkShader::TileMode tm, SkUnitMapper* mapper) { |
| 31 | return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, |
| 32 | data.fCount, tm, mapper); |
| 33 | } |
| 34 | |
| 35 | static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, |
| 36 | SkShader::TileMode tm, SkUnitMapper* mapper) { |
| 37 | SkPoint center; |
| 38 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 39 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 40 | return SkGradientShader::CreateRadial(center, center.fX, data.fColors, |
| 41 | data.fPos, data.fCount, tm, mapper); |
| 42 | } |
| 43 | |
| 44 | static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, |
| 45 | SkShader::TileMode tm, SkUnitMapper* mapper) { |
| 46 | SkPoint center; |
| 47 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 48 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 49 | return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, |
| 50 | data.fPos, data.fCount, mapper); |
| 51 | } |
| 52 | |
| 53 | typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, |
| 54 | SkShader::TileMode tm, SkUnitMapper* mapper); |
| 55 | static const GradMaker gGradMakers[] = { |
| 56 | MakeLinear, MakeRadial, MakeSweep |
| 57 | }; |
| 58 | |
| 59 | /////////////////////////////////////////////////////////////////////////////// |
| 60 | |
| 61 | class GradientsView : public SkView { |
| 62 | public: |
| 63 | GradientsView() {} |
| 64 | |
| 65 | protected: |
| 66 | // overrides from SkEventSink |
| 67 | virtual bool onQuery(SkEvent* evt) { |
| 68 | if (SampleCode::TitleQ(*evt)) { |
| 69 | SampleCode::TitleR(evt, "Gradients"); |
| 70 | return true; |
| 71 | } |
| 72 | return this->INHERITED::onQuery(evt); |
| 73 | } |
| 74 | |
| 75 | void drawBG(SkCanvas* canvas) { |
| 76 | canvas->drawColor(0xFFDDDDDD); |
| 77 | } |
| 78 | |
| 79 | virtual void onDraw(SkCanvas* canvas) { |
| 80 | this->drawBG(canvas); |
| 81 | |
| 82 | SkPoint pts[2] = { |
| 83 | { 0, 0 }, |
| 84 | { SkIntToScalar(100), SkIntToScalar(100) } |
| 85 | }; |
| 86 | SkShader::TileMode tm = SkShader::kClamp_TileMode; |
| 87 | SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; |
| 88 | SkPaint paint; |
| 89 | paint.setAntiAlias(true); |
| 90 | |
| 91 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 92 | for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { |
| 93 | canvas->save(); |
| 94 | for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { |
| 95 | SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL); |
| 96 | paint.setShader(shader); |
| 97 | canvas->drawRect(r, paint); |
| 98 | shader->unref(); |
| 99 | canvas->translate(0, SkIntToScalar(120)); |
| 100 | } |
| 101 | canvas->restore(); |
| 102 | canvas->translate(SkIntToScalar(120), 0); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | typedef SkView INHERITED; |
| 108 | }; |
| 109 | |
| 110 | /////////////////////////////////////////////////////////////////////////////// |
| 111 | |
| 112 | static SkView* MyFactory() { return new GradientsView; } |
| 113 | static SkViewRegister reg(MyFactory); |
| 114 | |