2017-09-25 20:53:49 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2018-03-22 19:21:12 +00:00
|
|
|
|
|
|
|
#ifdef SK_XML
|
|
|
|
|
2020-10-15 18:54:12 +00:00
|
|
|
#include "experimental/svg/model/SkSVGDOM.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "src/core/SkOSFile.h"
|
|
|
|
#include "src/utils/SkOSPath.h"
|
|
|
|
#include "src/xml/SkDOM.h"
|
|
|
|
#include "tools/Resources.h"
|
2017-09-25 20:53:49 +00:00
|
|
|
|
|
|
|
namespace {
|
2019-07-17 13:08:11 +00:00
|
|
|
class AnimatedSVGSample : public Sample {
|
2017-09-25 20:53:49 +00:00
|
|
|
static constexpr auto kAnimationIterations = 5;
|
|
|
|
enum State {
|
|
|
|
kZoomIn,
|
|
|
|
kScroll,
|
|
|
|
kZoomOut
|
|
|
|
};
|
2019-07-17 13:08:11 +00:00
|
|
|
sk_sp<SkSVGDOM> fDom;
|
|
|
|
const char* fResource = nullptr;
|
|
|
|
const char* fName = nullptr;
|
|
|
|
State fState = kZoomIn;
|
|
|
|
int fAnimationLoop = kAnimationIterations;
|
|
|
|
SkScalar fDelta = 1;
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2019-07-17 13:08:11 +00:00
|
|
|
public:
|
|
|
|
AnimatedSVGSample(const char* r, const char* n) : fResource(r), fName(n) {}
|
|
|
|
|
|
|
|
private:
|
2017-09-25 20:53:49 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2019-07-17 13:08:11 +00:00
|
|
|
SkASSERT(fResource);
|
|
|
|
auto data = GetResourceAsData(fResource);
|
2017-12-09 01:27:41 +00:00
|
|
|
if (!data) {
|
2019-07-17 13:08:11 +00:00
|
|
|
SkDebugf("Resource not found: \"%s\"\n", fResource);
|
2017-09-25 20:53:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-12-09 01:27:41 +00:00
|
|
|
SkMemoryStream svgStream(std::move(data));
|
2017-09-25 20:53:49 +00:00
|
|
|
|
|
|
|
SkDOM xmlDom;
|
|
|
|
if (!xmlDom.build(svgStream)) {
|
2019-07-17 13:08:11 +00:00
|
|
|
SkDebugf("XML parsing failed: \"%s\"\n", fResource);
|
2017-09-25 20:53:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fDom = SkSVGDOM::MakeFromDOM(xmlDom);
|
|
|
|
if (fDom) {
|
|
|
|
fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
|
|
|
if (fDom) {
|
2020-05-21 16:11:27 +00:00
|
|
|
canvas->setMatrix(SkMatrix::Scale(3, 3));
|
2017-09-25 20:53:49 +00:00
|
|
|
canvas->clipRect(SkRect::MakeLTRB(0, 0, 400, 400));
|
|
|
|
switch (fState) {
|
|
|
|
case kZoomIn:
|
|
|
|
fDelta += 0.2f;
|
2020-05-21 16:11:27 +00:00
|
|
|
canvas->scale(fDelta, fDelta);
|
2017-09-25 20:53:49 +00:00
|
|
|
break;
|
|
|
|
case kScroll:
|
|
|
|
if (fAnimationLoop > kAnimationIterations/2) {
|
|
|
|
fDelta += 80.f;
|
|
|
|
} else {
|
|
|
|
fDelta -= 80.f;
|
|
|
|
}
|
2020-05-21 16:11:27 +00:00
|
|
|
canvas->scale(fDelta, fDelta);
|
2017-09-25 20:53:49 +00:00
|
|
|
canvas->translate(fDelta, 0);
|
|
|
|
break;
|
|
|
|
case kZoomOut:
|
|
|
|
fDelta += 0.2f;
|
2020-05-21 16:11:27 +00:00
|
|
|
canvas->scale(fDelta, fDelta);
|
2017-09-25 20:53:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2017-09-25 20:53:49 +00:00
|
|
|
fDom->render(canvas);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onSizeChange() override {
|
|
|
|
if (fDom) {
|
|
|
|
fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-17 13:08:11 +00:00
|
|
|
SkString name() override { return SkASSERT(fName), SkString(fName); }
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
bool onAnimate(double nanos) override {
|
2017-09-25 20:53:49 +00:00
|
|
|
if (!fDom) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2017-09-25 20:53:49 +00:00
|
|
|
--fAnimationLoop;
|
|
|
|
if (fAnimationLoop == 0) {
|
|
|
|
fAnimationLoop = kAnimationIterations;
|
|
|
|
switch (fState) {
|
|
|
|
case kZoomIn:
|
|
|
|
fState = kScroll;
|
|
|
|
fDelta = 0;
|
|
|
|
break;
|
|
|
|
case kScroll:
|
|
|
|
fState = kZoomOut;
|
|
|
|
fDelta = 2;
|
|
|
|
break;
|
|
|
|
case kZoomOut:
|
|
|
|
fState = kZoomIn;
|
|
|
|
fDelta = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2019-07-17 13:08:11 +00:00
|
|
|
} // namespace
|
2017-09-25 20:53:49 +00:00
|
|
|
|
2019-07-17 13:08:11 +00:00
|
|
|
DEF_SAMPLE( return new AnimatedSVGSample("Cowboy.svg", "SampleCowboy"); )
|
2017-09-25 20:53:49 +00:00
|
|
|
|
2018-03-22 19:21:12 +00:00
|
|
|
#endif // SK_XML
|