reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
| 7 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 8 | #include "Sample.h" |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 9 | #include "SkAnimTimer.h" |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 10 | #include "SkCanvas.h" |
reed | 3cb3840 | 2015-02-06 08:36:15 -0800 | [diff] [blame] | 11 | #include "SkDrawable.h" |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 12 | #include "SkInterpolator.h" |
| 13 | #include "SkPictureRecorder.h" |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 14 | #include "SkPointPriv.h" |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 15 | #include "SkRandom.h" |
| 16 | |
| 17 | const SkRect gUnitSquare = { -1, -1, 1, 1 }; |
| 18 | |
| 19 | static void color_to_floats(SkColor c, SkScalar f[4]) { |
| 20 | f[0] = SkIntToScalar(SkColorGetA(c)); |
| 21 | f[1] = SkIntToScalar(SkColorGetR(c)); |
| 22 | f[2] = SkIntToScalar(SkColorGetG(c)); |
| 23 | f[3] = SkIntToScalar(SkColorGetB(c)); |
| 24 | } |
| 25 | |
| 26 | static SkColor floats_to_color(const SkScalar f[4]) { |
| 27 | return SkColorSetARGB(SkScalarRoundToInt(f[0]), |
| 28 | SkScalarRoundToInt(f[1]), |
| 29 | SkScalarRoundToInt(f[2]), |
| 30 | SkScalarRoundToInt(f[3])); |
| 31 | } |
| 32 | |
| 33 | static bool oval_contains(const SkRect& r, SkScalar x, SkScalar y) { |
| 34 | SkMatrix m; |
| 35 | m.setRectToRect(r, gUnitSquare, SkMatrix::kFill_ScaleToFit); |
| 36 | SkPoint pt; |
| 37 | m.mapXY(x, y, &pt); |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 38 | return SkPointPriv::LengthSqd(pt) <= 1; |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static SkColor rand_opaque_color(uint32_t seed) { |
| 42 | SkRandom rand(seed); |
| 43 | return rand.nextU() | (0xFF << 24); |
| 44 | } |
| 45 | |
reed | 3cb3840 | 2015-02-06 08:36:15 -0800 | [diff] [blame] | 46 | class HTDrawable : public SkDrawable { |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 47 | SkRect fR; |
| 48 | SkColor fColor; |
| 49 | SkInterpolator* fInterp; |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 50 | SkMSec fTime; |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 51 | |
| 52 | public: |
| 53 | HTDrawable(SkRandom& rand) { |
| 54 | fR = SkRect::MakeXYWH(rand.nextRangeF(0, 640), rand.nextRangeF(0, 480), |
| 55 | rand.nextRangeF(20, 200), rand.nextRangeF(20, 200)); |
| 56 | fColor = rand_opaque_color(rand.nextU()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 57 | fInterp = nullptr; |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 58 | fTime = 0; |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 59 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 60 | |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 61 | void spawnAnimation(SkMSec now) { |
| 62 | this->setTime(now); |
| 63 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 64 | delete fInterp; |
| 65 | fInterp = new SkInterpolator(5, 3); |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 66 | SkScalar values[5]; |
| 67 | color_to_floats(fColor, values); values[4] = 0; |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 68 | fInterp->setKeyFrame(0, now, values); |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 69 | values[0] = 0; values[4] = 180; |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 70 | fInterp->setKeyFrame(1, now + 1000, values); |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 71 | color_to_floats(rand_opaque_color(fColor), values); values[4] = 360; |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 72 | fInterp->setKeyFrame(2, now + 2000, values); |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 73 | |
| 74 | fInterp->setMirror(true); |
| 75 | fInterp->setRepeatCount(3); |
| 76 | |
| 77 | this->notifyDrawingChanged(); |
| 78 | } |
| 79 | |
| 80 | bool hitTest(SkScalar x, SkScalar y) { |
| 81 | return oval_contains(fR, x, y); |
| 82 | } |
| 83 | |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 84 | void setTime(SkMSec time) { fTime = time; } |
| 85 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 86 | void onDraw(SkCanvas* canvas) override { |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 87 | SkAutoCanvasRestore acr(canvas, false); |
| 88 | |
| 89 | SkPaint paint; |
| 90 | paint.setAntiAlias(true); |
| 91 | |
| 92 | if (fInterp) { |
| 93 | SkScalar values[5]; |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 94 | SkInterpolator::Result res = fInterp->timeToValues(fTime, values); |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 95 | fColor = floats_to_color(values); |
| 96 | |
| 97 | canvas->save(); |
bungeman | 7438bfc | 2016-07-12 15:01:19 -0700 | [diff] [blame] | 98 | canvas->rotate(values[4], fR.centerX(), fR.centerY()); |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 99 | |
| 100 | switch (res) { |
| 101 | case SkInterpolator::kFreezeEnd_Result: |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 102 | delete fInterp; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 103 | fInterp = nullptr; |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 104 | break; |
| 105 | default: |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | paint.setColor(fColor); |
| 110 | canvas->drawRect(fR, paint); |
| 111 | } |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 112 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 113 | SkRect onGetBounds() override { return fR; } |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 114 | }; |
| 115 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 116 | class HTView : public Sample { |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 117 | public: |
| 118 | enum { |
| 119 | N = 50, |
| 120 | W = 640, |
| 121 | H = 480, |
| 122 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 123 | |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 124 | struct Rec { |
| 125 | HTDrawable* fDrawable; |
| 126 | }; |
| 127 | Rec fArray[N]; |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 128 | sk_sp<SkDrawable> fRoot; |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 129 | SkMSec fTime; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 130 | |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 131 | HTView() { |
| 132 | SkRandom rand; |
| 133 | |
| 134 | SkPictureRecorder recorder; |
| 135 | SkCanvas* canvas = recorder.beginRecording(SkRect::MakeWH(W, H)); |
| 136 | for (int i = 0; i < N; ++i) { |
| 137 | fArray[i].fDrawable = new HTDrawable(rand); |
reed | 3cb3840 | 2015-02-06 08:36:15 -0800 | [diff] [blame] | 138 | canvas->drawDrawable(fArray[i].fDrawable); |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 139 | fArray[i].fDrawable->unref(); |
| 140 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 141 | fRoot = recorder.finishRecordingAsDrawable(); |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 145 | bool onQuery(Sample::Event* evt) override { |
| 146 | if (Sample::TitleQ(*evt)) { |
| 147 | Sample::TitleR(evt, "HT"); |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 148 | return true; |
| 149 | } |
| 150 | return this->INHERITED::onQuery(evt); |
| 151 | } |
| 152 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 153 | void onDrawContent(SkCanvas* canvas) override { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 154 | canvas->drawDrawable(fRoot.get()); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 155 | } |
| 156 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 157 | bool onAnimate(const SkAnimTimer& timer) override { |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 158 | fTime = timer.msec(); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 159 | for (int i = 0; i < N; ++i) { |
| 160 | fArray[i].fDrawable->setTime(fTime); |
| 161 | } |
| 162 | return true; |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 165 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override { |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 166 | // search backwards to find the top-most |
| 167 | for (int i = N - 1; i >= 0; --i) { |
| 168 | if (fArray[i].fDrawable->hitTest(x, y)) { |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 169 | fArray[i].fDrawable->spawnAnimation(fTime); |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 170 | break; |
| 171 | } |
| 172 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 173 | return nullptr; |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 177 | typedef Sample INHERITED; |
reed | adc8d98 | 2014-11-26 08:45:36 -0800 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | ////////////////////////////////////////////////////////////////////////////// |
| 181 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 182 | DEF_SAMPLE( return new HTView(); ) |