| 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 |  */ | 
| bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" | 
 | 9 | #include "SkView.h" | 
 | 10 | #include "SkCanvas.h" | 
 | 11 | #include "Sk64.h" | 
 | 12 | #include "SkGradientShader.h" | 
| bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 13 | #include "SkString.h" | 
| bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 14 |  | 
 | 15 | static void draw_gradient2(SkCanvas* canvas, const SkRect& rect, SkScalar delta) { | 
 | 16 |     SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorMAGENTA }; | 
| commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 17 |     SkScalar pos[] = { 0, 0.25f, 0.75f, SK_Scalar1 }; | 
| bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 18 |  | 
 | 19 |     SkScalar l = rect.fLeft; | 
 | 20 |     SkScalar t = rect.fTop; | 
 | 21 |     SkScalar w = rect.width(); | 
 | 22 |     SkScalar h = rect.height(); | 
 | 23 |  | 
 | 24 |     SkASSERT(0 == SkScalarMod(w, SK_Scalar1 * 5)); | 
 | 25 |  | 
 | 26 |     SkPoint c0 = { l + 2 * w / 5 + delta, t + h / 2 }; | 
 | 27 |     SkPoint c1 = { l + 3 * w / 5, t + h / 2 }; | 
 | 28 |     SkScalar r0 = w / 5; | 
 | 29 |     SkScalar r1 = 2 * w / 5; | 
| rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 30 |     SkShader* s = SkGradientShader::CreateTwoPointConical(c0, r0, c1, r1, colors, | 
| bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 31 |                                                          pos, SK_ARRAY_COUNT(pos), | 
 | 32 |                                                          SkShader::kClamp_TileMode); | 
 | 33 |     SkPaint paint; | 
 | 34 |     paint.setShader(s)->unref(); | 
 | 35 |  | 
 | 36 |     canvas->drawRect(rect, paint); | 
 | 37 | } | 
 | 38 |  | 
 | 39 |  | 
 | 40 | class DegenerateTwoPtRadialsView : public SampleView { | 
 | 41 |  | 
 | 42 | public: | 
 | 43 |     DegenerateTwoPtRadialsView() { | 
 | 44 |         fTime = 0; | 
 | 45 |         this->setBGColor(0xFFDDDDDD); | 
 | 46 |     } | 
 | 47 |  | 
 | 48 | protected: | 
 | 49 |     // overrides from SkEventSink | 
 | 50 |     virtual bool onQuery(SkEvent* evt) { | 
 | 51 |         if (SampleCode::TitleQ(*evt)) { | 
 | 52 |             SampleCode::TitleR(evt, "DegenerateTwoPtRadials"); | 
 | 53 |             return true; | 
 | 54 |         } | 
 | 55 |         return this->INHERITED::onQuery(evt); | 
 | 56 |     } | 
 | 57 |  | 
 | 58 |     virtual void onDrawContent(SkCanvas* canvas) { | 
 | 59 |         fTime += SampleCode::GetAnimSecondsDelta(); | 
 | 60 |         SkScalar delta = fTime / 15.f; | 
| reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame^] | 61 |         int intPart = SkScalarFloorToInt(delta); | 
| bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 62 |         delta = delta - SK_Scalar1 * intPart; | 
 | 63 |         if (intPart % 2) { | 
 | 64 |             delta = SK_Scalar1 - delta; | 
 | 65 |         } | 
 | 66 |         delta -= SK_ScalarHalf; | 
 | 67 |         static const int DELTA_SCALE = 500; | 
 | 68 |         delta /= DELTA_SCALE; | 
 | 69 |  | 
| bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 70 |         SkScalar w = SK_Scalar1 * 500; | 
 | 71 |         SkScalar h = SK_Scalar1 * 500; | 
 | 72 |         SkScalar l = SK_Scalar1 * 100; | 
 | 73 |         SkScalar t = SK_Scalar1 * 100; | 
 | 74 |         draw_gradient2(canvas, SkRect::MakeXYWH(l, t, w, h), delta); | 
| bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 75 |         SkString txt; | 
 | 76 |         txt.appendf("gap at \"tangent\" pt = %f", SkScalarToFloat(delta)); | 
| bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 77 |         SkPaint paint; | 
 | 78 |         paint.setAntiAlias(true); | 
 | 79 |         paint.setColor(SK_ColorBLACK); | 
| bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 80 |         canvas->drawText(txt.c_str(), txt.size(), l + w/2 + w*DELTA_SCALE*delta, t + h + SK_Scalar1 * 10, paint); | 
| bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 81 |         this->inval(NULL); | 
 | 82 |     } | 
 | 83 |  | 
 | 84 | private: | 
 | 85 |     SkScalar           fTime; | 
 | 86 |     typedef SampleView INHERITED; | 
 | 87 | }; | 
 | 88 |  | 
 | 89 | ////////////////////////////////////////////////////////////////////////////// | 
 | 90 |  | 
 | 91 | static SkView* MyFactory() { return new DegenerateTwoPtRadialsView; } | 
 | 92 | static SkViewRegister reg(MyFactory); |