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.
|
|
|
|
*/
|
|
|
|
|
2018-03-22 19:21:12 +00:00
|
|
|
#include "SkTypes.h"
|
|
|
|
|
|
|
|
#ifdef SK_XML
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
#include "Sample.h"
|
2017-09-25 20:53:49 +00:00
|
|
|
#include "Resources.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkDOM.h"
|
|
|
|
#include "SkOSFile.h"
|
|
|
|
#include "SkOSPath.h"
|
|
|
|
#include "SkRect.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkSVGDOM.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class CowboyView : public Sample {
|
2017-09-25 20:53:49 +00:00
|
|
|
public:
|
|
|
|
CowboyView()
|
|
|
|
: fLabel("SampleCowboy")
|
|
|
|
, fState(kZoomIn)
|
|
|
|
, fAnimationLoop(kAnimationIterations)
|
|
|
|
, fDelta(1) {}
|
|
|
|
~CowboyView() override = default;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static constexpr auto kAnimationIterations = 5;
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2017-09-25 20:53:49 +00:00
|
|
|
enum State {
|
|
|
|
kZoomIn,
|
|
|
|
kScroll,
|
|
|
|
kZoomOut
|
|
|
|
};
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2017-09-25 20:53:49 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2017-12-09 01:27:41 +00:00
|
|
|
constexpr char path[] = "Cowboy.svg";
|
|
|
|
auto data = GetResourceAsData(path);
|
|
|
|
if (!data) {
|
|
|
|
SkDebugf("file not found: \"%s\"\n", path);
|
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)) {
|
|
|
|
SkDebugf("XML parsing failed: \"path\"\n", fPath.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fDom = SkSVGDOM::MakeFromDOM(xmlDom);
|
|
|
|
if (fDom) {
|
|
|
|
fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
|
|
|
if (fDom) {
|
|
|
|
canvas->setMatrix(SkMatrix::MakeScale(3));
|
|
|
|
canvas->clipRect(SkRect::MakeLTRB(0, 0, 400, 400));
|
|
|
|
switch (fState) {
|
|
|
|
case kZoomIn:
|
|
|
|
fDelta += 0.2f;
|
|
|
|
canvas->concat(SkMatrix::MakeScale(fDelta));
|
|
|
|
break;
|
|
|
|
case kScroll:
|
|
|
|
if (fAnimationLoop > kAnimationIterations/2) {
|
|
|
|
fDelta += 80.f;
|
|
|
|
} else {
|
|
|
|
fDelta -= 80.f;
|
|
|
|
}
|
|
|
|
canvas->concat(SkMatrix::MakeScale(fDelta));
|
|
|
|
canvas->translate(fDelta, 0);
|
|
|
|
break;
|
|
|
|
case kZoomOut:
|
|
|
|
fDelta += 0.2f;
|
|
|
|
canvas->concat(SkMatrix::MakeScale(fDelta));
|
|
|
|
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()));
|
|
|
|
}
|
|
|
|
|
|
|
|
this->INHERITED::onSizeChange();
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
bool onQuery(Sample::Event* evt) override {
|
|
|
|
if (Sample::TitleQ(*evt)) {
|
|
|
|
Sample::TitleR(evt, fLabel.c_str());
|
2017-09-25 20:53:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2017-09-25 20:53:49 +00:00
|
|
|
bool onAnimate(const SkAnimTimer& timer) override {
|
|
|
|
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;
|
|
|
|
}
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2017-09-25 20:53:49 +00:00
|
|
|
private:
|
|
|
|
sk_sp<SkSVGDOM> fDom;
|
|
|
|
SkString fPath;
|
|
|
|
SkString fLabel;
|
|
|
|
State fState;
|
|
|
|
int fAnimationLoop;
|
|
|
|
SkScalar fDelta;
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2017-09-25 20:53:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
DEF_SAMPLE( return new CowboyView(); )
|
|
|
|
|
2018-03-22 19:21:12 +00:00
|
|
|
#endif // SK_XML
|