bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 7 | |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +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 | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 10 | #include "SkBlurMaskFilter.h" |
| 11 | #include "SkColorPriv.h" |
| 12 | #include "SkCanvas.h" |
| 13 | #include "SkRandom.h" |
| 14 | |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 15 | SkScalar get_anim_sin(double secs, SkScalar amplitude, SkScalar periodInSec, SkScalar phaseInSec) { |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 16 | if (!periodInSec) { |
| 17 | return 0; |
| 18 | } |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 19 | double t = secs + phaseInSec; |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 20 | t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec; |
| 21 | amplitude = SK_ScalarHalf * amplitude; |
| 22 | return amplitude * SkDoubleToScalar(sin(t)) + amplitude; |
| 23 | } |
| 24 | |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 25 | class AnimBlurView : public SampleView { |
| 26 | public: |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 27 | AnimBlurView() : fBlurSigma(0), fCircleRadius(100) {} |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 28 | |
| 29 | protected: |
| 30 | // overrides from SkEventSink |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 31 | bool onQuery(SkEvent* evt) override { |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 32 | if (SampleCode::TitleQ(*evt)) { |
| 33 | SampleCode::TitleR(evt, "AnimBlur"); |
| 34 | return true; |
| 35 | } |
| 36 | return this->INHERITED::onQuery(evt); |
| 37 | } |
| 38 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 39 | void onDrawContent(SkCanvas* canvas) override { |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 40 | static const SkBlurStyle gStyles[] = { |
| 41 | kNormal_SkBlurStyle, |
| 42 | kInner_SkBlurStyle, |
| 43 | kSolid_SkBlurStyle, |
| 44 | kOuter_SkBlurStyle, |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 45 | }; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 46 | SkRandom random; |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 47 | |
robertphillips@google.com | 7460b37 | 2012-04-25 16:54:51 +0000 | [diff] [blame] | 48 | for (size_t i = 0; i < SK_ARRAY_COUNT(gStyles); ++i) { |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 49 | SkMaskFilter* mf = SkBlurMaskFilter::Create( |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 50 | gStyles[i], |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 51 | fBlurSigma, |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 52 | SkBlurMaskFilter::kHighQuality_BlurFlag); |
| 53 | SkPaint paint; |
reed@google.com | 59ed2c2 | 2012-04-09 17:12:30 +0000 | [diff] [blame] | 54 | SkSafeUnref(paint.setMaskFilter(mf)); |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 55 | paint.setColor(random.nextU() | 0xff000000); |
| 56 | canvas->drawCircle(200 * SK_Scalar1 + 400 * (i % 2) * SK_Scalar1, |
| 57 | 200 * SK_Scalar1 + i / 2 * 400 * SK_Scalar1, |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 58 | fCircleRadius, paint); |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 59 | } |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 60 | } |
| 61 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 62 | bool onAnimate(const SkAnimTimer& timer) override { |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 63 | fBlurSigma = get_anim_sin(timer.secs(), 100, 4, 5); |
| 64 | fCircleRadius = 3 + get_anim_sin(timer.secs(), 150, 25, 3); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 65 | return true; |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | private: |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 69 | SkScalar fBlurSigma, fCircleRadius; |
| 70 | |
| 71 | typedef SampleView INHERITED; |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | ////////////////////////////////////////////////////////////////////////////// |
| 75 | |
| 76 | static SkView* MyFactory() { return new AnimBlurView; } |
| 77 | static SkViewRegister reg(MyFactory); |