2014-11-26 16:45:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
#include "Sample.h"
|
2015-02-02 20:55:02 +00:00
|
|
|
#include "SkAnimTimer.h"
|
2014-11-26 16:45:36 +00:00
|
|
|
#include "SkCanvas.h"
|
2015-02-06 16:36:15 +00:00
|
|
|
#include "SkDrawable.h"
|
2014-11-26 16:45:36 +00:00
|
|
|
#include "SkInterpolator.h"
|
|
|
|
#include "SkPictureRecorder.h"
|
2017-11-08 16:44:31 +00:00
|
|
|
#include "SkPointPriv.h"
|
2014-11-26 16:45:36 +00:00
|
|
|
#include "SkRandom.h"
|
|
|
|
|
|
|
|
const SkRect gUnitSquare = { -1, -1, 1, 1 };
|
|
|
|
|
|
|
|
static void color_to_floats(SkColor c, SkScalar f[4]) {
|
|
|
|
f[0] = SkIntToScalar(SkColorGetA(c));
|
|
|
|
f[1] = SkIntToScalar(SkColorGetR(c));
|
|
|
|
f[2] = SkIntToScalar(SkColorGetG(c));
|
|
|
|
f[3] = SkIntToScalar(SkColorGetB(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkColor floats_to_color(const SkScalar f[4]) {
|
|
|
|
return SkColorSetARGB(SkScalarRoundToInt(f[0]),
|
|
|
|
SkScalarRoundToInt(f[1]),
|
|
|
|
SkScalarRoundToInt(f[2]),
|
|
|
|
SkScalarRoundToInt(f[3]));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool oval_contains(const SkRect& r, SkScalar x, SkScalar y) {
|
|
|
|
SkMatrix m;
|
|
|
|
m.setRectToRect(r, gUnitSquare, SkMatrix::kFill_ScaleToFit);
|
|
|
|
SkPoint pt;
|
|
|
|
m.mapXY(x, y, &pt);
|
2017-11-08 16:44:31 +00:00
|
|
|
return SkPointPriv::LengthSqd(pt) <= 1;
|
2014-11-26 16:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static SkColor rand_opaque_color(uint32_t seed) {
|
|
|
|
SkRandom rand(seed);
|
|
|
|
return rand.nextU() | (0xFF << 24);
|
|
|
|
}
|
|
|
|
|
2015-02-06 16:36:15 +00:00
|
|
|
class HTDrawable : public SkDrawable {
|
2014-11-26 16:45:36 +00:00
|
|
|
SkRect fR;
|
|
|
|
SkColor fColor;
|
|
|
|
SkInterpolator* fInterp;
|
2015-02-02 03:01:04 +00:00
|
|
|
SkMSec fTime;
|
2014-11-26 16:45:36 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
HTDrawable(SkRandom& rand) {
|
|
|
|
fR = SkRect::MakeXYWH(rand.nextRangeF(0, 640), rand.nextRangeF(0, 480),
|
|
|
|
rand.nextRangeF(20, 200), rand.nextRangeF(20, 200));
|
|
|
|
fColor = rand_opaque_color(rand.nextU());
|
2015-08-27 14:41:13 +00:00
|
|
|
fInterp = nullptr;
|
2015-02-02 03:01:04 +00:00
|
|
|
fTime = 0;
|
2014-11-26 16:45:36 +00:00
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-02-02 03:01:04 +00:00
|
|
|
void spawnAnimation(SkMSec now) {
|
|
|
|
this->setTime(now);
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
delete fInterp;
|
|
|
|
fInterp = new SkInterpolator(5, 3);
|
2014-11-26 16:45:36 +00:00
|
|
|
SkScalar values[5];
|
|
|
|
color_to_floats(fColor, values); values[4] = 0;
|
2015-02-02 03:01:04 +00:00
|
|
|
fInterp->setKeyFrame(0, now, values);
|
2014-11-26 16:45:36 +00:00
|
|
|
values[0] = 0; values[4] = 180;
|
2015-02-02 03:01:04 +00:00
|
|
|
fInterp->setKeyFrame(1, now + 1000, values);
|
2014-11-26 16:45:36 +00:00
|
|
|
color_to_floats(rand_opaque_color(fColor), values); values[4] = 360;
|
2015-02-02 03:01:04 +00:00
|
|
|
fInterp->setKeyFrame(2, now + 2000, values);
|
2014-11-26 16:45:36 +00:00
|
|
|
|
|
|
|
fInterp->setMirror(true);
|
|
|
|
fInterp->setRepeatCount(3);
|
|
|
|
|
|
|
|
this->notifyDrawingChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hitTest(SkScalar x, SkScalar y) {
|
|
|
|
return oval_contains(fR, x, y);
|
|
|
|
}
|
|
|
|
|
2015-02-02 03:01:04 +00:00
|
|
|
void setTime(SkMSec time) { fTime = time; }
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-11-26 16:45:36 +00:00
|
|
|
SkAutoCanvasRestore acr(canvas, false);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
|
|
|
if (fInterp) {
|
|
|
|
SkScalar values[5];
|
2015-02-02 03:01:04 +00:00
|
|
|
SkInterpolator::Result res = fInterp->timeToValues(fTime, values);
|
2014-11-26 16:45:36 +00:00
|
|
|
fColor = floats_to_color(values);
|
|
|
|
|
|
|
|
canvas->save();
|
2016-07-12 22:01:19 +00:00
|
|
|
canvas->rotate(values[4], fR.centerX(), fR.centerY());
|
2014-11-26 16:45:36 +00:00
|
|
|
|
|
|
|
switch (res) {
|
|
|
|
case SkInterpolator::kFreezeEnd_Result:
|
2015-08-26 20:07:48 +00:00
|
|
|
delete fInterp;
|
2015-08-27 14:41:13 +00:00
|
|
|
fInterp = nullptr;
|
2014-11-26 16:45:36 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
paint.setColor(fColor);
|
|
|
|
canvas->drawRect(fR, paint);
|
|
|
|
}
|
2015-02-02 03:01:04 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkRect onGetBounds() override { return fR; }
|
2014-11-26 16:45:36 +00:00
|
|
|
};
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class HTView : public Sample {
|
2014-11-26 16:45:36 +00:00
|
|
|
public:
|
|
|
|
enum {
|
|
|
|
N = 50,
|
|
|
|
W = 640,
|
|
|
|
H = 480,
|
|
|
|
};
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-11-26 16:45:36 +00:00
|
|
|
struct Rec {
|
|
|
|
HTDrawable* fDrawable;
|
|
|
|
};
|
|
|
|
Rec fArray[N];
|
2016-03-18 14:25:55 +00:00
|
|
|
sk_sp<SkDrawable> fRoot;
|
2015-02-02 03:01:04 +00:00
|
|
|
SkMSec fTime;
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-11-26 16:45:36 +00:00
|
|
|
HTView() {
|
|
|
|
SkRandom rand;
|
|
|
|
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
SkCanvas* canvas = recorder.beginRecording(SkRect::MakeWH(W, H));
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
fArray[i].fDrawable = new HTDrawable(rand);
|
2015-02-06 16:36:15 +00:00
|
|
|
canvas->drawDrawable(fArray[i].fDrawable);
|
2014-11-26 16:45:36 +00:00
|
|
|
fArray[i].fDrawable->unref();
|
|
|
|
}
|
2016-03-18 14:25:55 +00:00
|
|
|
fRoot = recorder.finishRecordingAsDrawable();
|
2014-11-26 16:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-08-08 15:36:17 +00:00
|
|
|
bool onQuery(Sample::Event* evt) override {
|
|
|
|
if (Sample::TitleQ(*evt)) {
|
|
|
|
Sample::TitleR(evt, "HT");
|
2014-11-26 16:45:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2016-03-18 14:25:55 +00:00
|
|
|
canvas->drawDrawable(fRoot.get());
|
2015-02-02 03:01:04 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool onAnimate(const SkAnimTimer& timer) override {
|
2015-02-02 20:55:02 +00:00
|
|
|
fTime = timer.msec();
|
2015-02-02 03:01:04 +00:00
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
fArray[i].fDrawable->setTime(fTime);
|
|
|
|
}
|
|
|
|
return true;
|
2014-11-26 16:45:36 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
|
2014-11-26 16:45:36 +00:00
|
|
|
// search backwards to find the top-most
|
|
|
|
for (int i = N - 1; i >= 0; --i) {
|
|
|
|
if (fArray[i].fDrawable->hitTest(x, y)) {
|
2015-02-02 03:01:04 +00:00
|
|
|
fArray[i].fDrawable->spawnAnimation(fTime);
|
2014-11-26 16:45:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2014-11-26 16:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2014-11-26 16:45:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new HTView(); )
|