blob: 20648ccdac5ee205b80576d03caeae81ee325196 [file] [log] [blame]
reedadc8d982014-11-26 08:45:36 -08001/*
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
8#include "SampleCode.h"
reed76113a92015-02-02 12:55:02 -08009#include "SkAnimTimer.h"
reedadc8d982014-11-26 08:45:36 -080010#include "SkView.h"
11#include "SkCanvas.h"
reed3cb38402015-02-06 08:36:15 -080012#include "SkDrawable.h"
reedadc8d982014-11-26 08:45:36 -080013#include "SkInterpolator.h"
14#include "SkPictureRecorder.h"
15#include "SkRandom.h"
16
17const SkRect gUnitSquare = { -1, -1, 1, 1 };
18
19static 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
26static 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
33static 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);
38 return pt.lengthSqd() <= 1;
39}
40
41static SkColor rand_opaque_color(uint32_t seed) {
42 SkRandom rand(seed);
43 return rand.nextU() | (0xFF << 24);
44}
45
reed3cb38402015-02-06 08:36:15 -080046class HTDrawable : public SkDrawable {
reedadc8d982014-11-26 08:45:36 -080047 SkRect fR;
48 SkColor fColor;
49 SkInterpolator* fInterp;
reedd9adfe62015-02-01 19:01:04 -080050 SkMSec fTime;
reedadc8d982014-11-26 08:45:36 -080051
52public:
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());
57 fInterp = NULL;
reedd9adfe62015-02-01 19:01:04 -080058 fTime = 0;
reedadc8d982014-11-26 08:45:36 -080059 }
60
reedd9adfe62015-02-01 19:01:04 -080061 void spawnAnimation(SkMSec now) {
62 this->setTime(now);
63
reedadc8d982014-11-26 08:45:36 -080064 SkDELETE(fInterp);
65 fInterp = SkNEW_ARGS(SkInterpolator, (5, 3));
66 SkScalar values[5];
67 color_to_floats(fColor, values); values[4] = 0;
reedd9adfe62015-02-01 19:01:04 -080068 fInterp->setKeyFrame(0, now, values);
reedadc8d982014-11-26 08:45:36 -080069 values[0] = 0; values[4] = 180;
reedd9adfe62015-02-01 19:01:04 -080070 fInterp->setKeyFrame(1, now + 1000, values);
reedadc8d982014-11-26 08:45:36 -080071 color_to_floats(rand_opaque_color(fColor), values); values[4] = 360;
reedd9adfe62015-02-01 19:01:04 -080072 fInterp->setKeyFrame(2, now + 2000, values);
reedadc8d982014-11-26 08:45:36 -080073
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
reedd9adfe62015-02-01 19:01:04 -080084 void setTime(SkMSec time) { fTime = time; }
85
mtklein36352bf2015-03-25 18:17:31 -070086 void onDraw(SkCanvas* canvas) override {
reedadc8d982014-11-26 08:45:36 -080087 SkAutoCanvasRestore acr(canvas, false);
88
89 SkPaint paint;
90 paint.setAntiAlias(true);
91
92 if (fInterp) {
93 SkScalar values[5];
reedd9adfe62015-02-01 19:01:04 -080094 SkInterpolator::Result res = fInterp->timeToValues(fTime, values);
reedadc8d982014-11-26 08:45:36 -080095 fColor = floats_to_color(values);
96
97 canvas->save();
98 canvas->translate(fR.centerX(), fR.centerY());
99 canvas->rotate(values[4]);
100 canvas->translate(-fR.centerX(), -fR.centerY());
101
102 switch (res) {
103 case SkInterpolator::kFreezeEnd_Result:
104 SkDELETE(fInterp);
105 fInterp = NULL;
106 break;
107 default:
108 break;
109 }
110 }
111 paint.setColor(fColor);
112 canvas->drawRect(fR, paint);
113 }
reedd9adfe62015-02-01 19:01:04 -0800114
mtklein36352bf2015-03-25 18:17:31 -0700115 SkRect onGetBounds() override { return fR; }
reedadc8d982014-11-26 08:45:36 -0800116};
117
118class HTView : public SampleView {
119public:
120 enum {
121 N = 50,
122 W = 640,
123 H = 480,
124 };
125
126 struct Rec {
127 HTDrawable* fDrawable;
128 };
129 Rec fArray[N];
reed3cb38402015-02-06 08:36:15 -0800130 SkAutoTUnref<SkDrawable> fRoot;
reedd9adfe62015-02-01 19:01:04 -0800131 SkMSec fTime;
reedadc8d982014-11-26 08:45:36 -0800132
133 HTView() {
134 SkRandom rand;
135
136 SkPictureRecorder recorder;
137 SkCanvas* canvas = recorder.beginRecording(SkRect::MakeWH(W, H));
138 for (int i = 0; i < N; ++i) {
139 fArray[i].fDrawable = new HTDrawable(rand);
reed3cb38402015-02-06 08:36:15 -0800140 canvas->drawDrawable(fArray[i].fDrawable);
reedadc8d982014-11-26 08:45:36 -0800141 fArray[i].fDrawable->unref();
142 }
reed3cb38402015-02-06 08:36:15 -0800143 fRoot.reset(recorder.endRecordingAsDrawable());
reedadc8d982014-11-26 08:45:36 -0800144 }
145
146protected:
mtklein36352bf2015-03-25 18:17:31 -0700147 bool onQuery(SkEvent* evt) override {
reedadc8d982014-11-26 08:45:36 -0800148 if (SampleCode::TitleQ(*evt)) {
149 SampleCode::TitleR(evt, "HT");
150 return true;
151 }
152 return this->INHERITED::onQuery(evt);
153 }
154
mtklein36352bf2015-03-25 18:17:31 -0700155 void onDrawContent(SkCanvas* canvas) override {
reed3cb38402015-02-06 08:36:15 -0800156 canvas->drawDrawable(fRoot);
reedd9adfe62015-02-01 19:01:04 -0800157 }
158
mtklein36352bf2015-03-25 18:17:31 -0700159 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -0800160 fTime = timer.msec();
reedd9adfe62015-02-01 19:01:04 -0800161 for (int i = 0; i < N; ++i) {
162 fArray[i].fDrawable->setTime(fTime);
163 }
164 return true;
reedadc8d982014-11-26 08:45:36 -0800165 }
166
mtklein36352bf2015-03-25 18:17:31 -0700167 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reedadc8d982014-11-26 08:45:36 -0800168 // search backwards to find the top-most
169 for (int i = N - 1; i >= 0; --i) {
170 if (fArray[i].fDrawable->hitTest(x, y)) {
reedd9adfe62015-02-01 19:01:04 -0800171 fArray[i].fDrawable->spawnAnimation(fTime);
reedadc8d982014-11-26 08:45:36 -0800172 break;
173 }
174 }
175 this->inval(NULL);
176 return NULL;
177 }
178
179private:
180 typedef SampleView INHERITED;
181};
182
183//////////////////////////////////////////////////////////////////////////////
184
185static SkView* MyFactory() { return new HTView; }
186static SkViewRegister reg(MyFactory);