epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
reed@google.com | bf0001d | 2014-01-13 14:53:55 +0000 | [diff] [blame] | 7 | |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 9 | #include "SkAnimTimer.h" |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 10 | #include "SkView.h" |
| 11 | #include "SkCanvas.h" |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 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; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 30 | SkPaint paint; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 31 | paint.setShader(SkGradientShader::MakeTwoPointConical(c0, r0, c1, r1, colors, |
| 32 | pos, SK_ARRAY_COUNT(pos), |
| 33 | SkShader::kClamp_TileMode)); |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 34 | canvas->drawRect(rect, paint); |
| 35 | } |
| 36 | |
| 37 | |
| 38 | class DegenerateTwoPtRadialsView : public SampleView { |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 39 | public: |
| 40 | DegenerateTwoPtRadialsView() { |
| 41 | fTime = 0; |
| 42 | this->setBGColor(0xFFDDDDDD); |
| 43 | } |
| 44 | |
| 45 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 46 | bool onQuery(SkEvent* evt) override { |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 47 | if (SampleCode::TitleQ(*evt)) { |
| 48 | SampleCode::TitleR(evt, "DegenerateTwoPtRadials"); |
| 49 | return true; |
| 50 | } |
| 51 | return this->INHERITED::onQuery(evt); |
| 52 | } |
| 53 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 54 | void onDrawContent(SkCanvas* canvas) override { |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 55 | SkScalar delta = fTime / 15.f; |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 56 | int intPart = SkScalarFloorToInt(delta); |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 57 | delta = delta - SK_Scalar1 * intPart; |
| 58 | if (intPart % 2) { |
| 59 | delta = SK_Scalar1 - delta; |
| 60 | } |
| 61 | delta -= SK_ScalarHalf; |
| 62 | static const int DELTA_SCALE = 500; |
| 63 | delta /= DELTA_SCALE; |
| 64 | |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 65 | SkScalar w = SK_Scalar1 * 500; |
| 66 | SkScalar h = SK_Scalar1 * 500; |
| 67 | SkScalar l = SK_Scalar1 * 100; |
| 68 | SkScalar t = SK_Scalar1 * 100; |
| 69 | draw_gradient2(canvas, SkRect::MakeXYWH(l, t, w, h), delta); |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 70 | SkString txt; |
| 71 | txt.appendf("gap at \"tangent\" pt = %f", SkScalarToFloat(delta)); |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 72 | SkPaint paint; |
| 73 | paint.setAntiAlias(true); |
| 74 | paint.setColor(SK_ColorBLACK); |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 75 | canvas->drawText(txt.c_str(), txt.size(), l + w/2 + w*DELTA_SCALE*delta, t + h + SK_Scalar1 * 10, paint); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 76 | } |
| 77 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 78 | bool onAnimate(const SkAnimTimer& timer) override { |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 79 | fTime = SkDoubleToScalar(timer.secs() / 15); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 80 | return true; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | private: |
| 84 | SkScalar fTime; |
| 85 | typedef SampleView INHERITED; |
| 86 | }; |
| 87 | |
| 88 | ////////////////////////////////////////////////////////////////////////////// |
| 89 | |
| 90 | static SkView* MyFactory() { return new DegenerateTwoPtRadialsView; } |
| 91 | static SkViewRegister reg(MyFactory); |