2018-02-15 23:40:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SlideDir.h"
|
|
|
|
|
|
|
|
#include "SkAnimTimer.h"
|
|
|
|
#include "SkCanvas.h"
|
2018-02-20 15:31:01 +00:00
|
|
|
#include "SkCubicMap.h"
|
2018-02-15 23:40:48 +00:00
|
|
|
#include "SkMakeUnique.h"
|
|
|
|
#include "SkSGColor.h"
|
|
|
|
#include "SkSGDraw.h"
|
|
|
|
#include "SkSGGroup.h"
|
2018-02-20 16:26:15 +00:00
|
|
|
#include "SkSGPlane.h"
|
2018-02-19 18:25:18 +00:00
|
|
|
#include "SkSGRect.h"
|
2018-02-15 23:40:48 +00:00
|
|
|
#include "SkSGRenderNode.h"
|
|
|
|
#include "SkSGScene.h"
|
|
|
|
#include "SkSGText.h"
|
|
|
|
#include "SkSGTransform.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
|
2018-02-19 18:25:18 +00:00
|
|
|
#include <cmath>
|
2018-06-18 19:11:00 +00:00
|
|
|
#include <utility>
|
2018-02-19 18:25:18 +00:00
|
|
|
|
2018-02-15 23:40:48 +00:00
|
|
|
namespace {
|
|
|
|
|
2018-02-19 18:25:18 +00:00
|
|
|
static constexpr float kAspectRatio = 1.5f;
|
|
|
|
static constexpr float kLabelSize = 12.0f;
|
|
|
|
static constexpr SkSize kPadding = { 12.0f , 24.0f };
|
2018-02-20 15:31:01 +00:00
|
|
|
|
|
|
|
static constexpr float kFocusDuration = 500;
|
|
|
|
static constexpr SkSize kFocusInset = { 100.0f, 100.0f };
|
|
|
|
static constexpr SkPoint kFocusCtrl0 = { 0.3f, 1.0f };
|
|
|
|
static constexpr SkPoint kFocusCtrl1 = { 0.0f, 1.0f };
|
|
|
|
static constexpr SkColor kFocusShade = 0xa0000000;
|
2018-02-19 18:25:18 +00:00
|
|
|
|
|
|
|
// TODO: better unfocus binding?
|
|
|
|
static constexpr SkUnichar kUnfocusKey = ' ';
|
2018-02-15 23:40:48 +00:00
|
|
|
|
|
|
|
class SlideAdapter final : public sksg::RenderNode {
|
|
|
|
public:
|
|
|
|
explicit SlideAdapter(sk_sp<Slide> slide)
|
|
|
|
: fSlide(std::move(slide)) {
|
|
|
|
SkASSERT(fSlide);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<sksg::Animator> makeForwardingAnimator() {
|
|
|
|
// Trivial sksg::Animator -> skottie::Animation tick adapter
|
|
|
|
class ForwardingAnimator final : public sksg::Animator {
|
|
|
|
public:
|
|
|
|
explicit ForwardingAnimator(sk_sp<SlideAdapter> adapter)
|
|
|
|
: fAdapter(std::move(adapter)) {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void onTick(float t) override {
|
|
|
|
fAdapter->tick(SkScalarRoundToInt(t));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
sk_sp<SlideAdapter> fAdapter;
|
|
|
|
};
|
|
|
|
|
|
|
|
return skstd::make_unique<ForwardingAnimator>(sk_ref_sp(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkRect onRevalidate(sksg::InvalidationController* ic, const SkMatrix& ctm) override {
|
|
|
|
const auto isize = fSlide->getDimensions();
|
|
|
|
return SkRect::MakeIWH(isize.width(), isize.height());
|
|
|
|
}
|
|
|
|
|
2018-08-09 11:40:01 +00:00
|
|
|
void onRender(SkCanvas* canvas, const RenderContext* ctx) const override {
|
2018-04-05 19:15:59 +00:00
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->clipRect(SkRect::Make(fSlide->getDimensions()), true);
|
2018-08-09 11:40:01 +00:00
|
|
|
|
|
|
|
// TODO: commit the context?
|
2018-02-15 23:40:48 +00:00
|
|
|
fSlide->draw(canvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void tick(SkMSec t) {
|
|
|
|
fSlide->animate(SkAnimTimer(0, t * 1e6, SkAnimTimer::kRunning_State));
|
|
|
|
this->invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
const sk_sp<Slide> fSlide;
|
|
|
|
|
|
|
|
using INHERITED = sksg::RenderNode;
|
|
|
|
};
|
|
|
|
|
2018-02-19 18:25:18 +00:00
|
|
|
SkMatrix SlideMatrix(const sk_sp<Slide>& slide, const SkRect& dst) {
|
|
|
|
const auto slideSize = slide->getDimensions();
|
|
|
|
return SkMatrix::MakeRectToRect(SkRect::MakeIWH(slideSize.width(), slideSize.height()),
|
|
|
|
dst,
|
|
|
|
SkMatrix::kCenter_ScaleToFit);
|
|
|
|
}
|
|
|
|
|
2018-02-15 23:40:48 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
struct SlideDir::Rec {
|
2018-02-19 18:25:18 +00:00
|
|
|
sk_sp<Slide> fSlide;
|
|
|
|
sk_sp<sksg::Transform> fTransform;
|
|
|
|
SkRect fRect;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SlideDir::FocusController final : public sksg::Animator {
|
|
|
|
public:
|
|
|
|
FocusController(const SlideDir* dir, const SkRect& focusRect)
|
|
|
|
: fDir(dir)
|
|
|
|
, fRect(focusRect)
|
|
|
|
, fTarget(nullptr)
|
2018-02-20 15:31:01 +00:00
|
|
|
, fState(State::kIdle) {
|
|
|
|
fMap.setPts(kFocusCtrl1, kFocusCtrl0);
|
|
|
|
|
|
|
|
fShadePaint = sksg::Color::Make(kFocusShade);
|
2018-02-20 16:26:15 +00:00
|
|
|
fShade = sksg::Draw::Make(sksg::Plane::Make(), fShadePaint);
|
2018-02-20 15:31:01 +00:00
|
|
|
}
|
2018-02-19 18:25:18 +00:00
|
|
|
|
|
|
|
bool hasFocus() const { return fState == State::kFocused; }
|
|
|
|
|
|
|
|
void startFocus(const Rec* target) {
|
2018-02-20 15:31:01 +00:00
|
|
|
if (fState != State::kIdle)
|
|
|
|
return;
|
|
|
|
|
2018-02-19 18:25:18 +00:00
|
|
|
fTarget = target;
|
2018-02-20 15:31:01 +00:00
|
|
|
|
|
|
|
// Move the shade & slide to front.
|
|
|
|
fDir->fRoot->removeChild(fTarget->fTransform);
|
|
|
|
fDir->fRoot->addChild(fShade);
|
|
|
|
fDir->fRoot->addChild(fTarget->fTransform);
|
|
|
|
|
2018-02-19 18:25:18 +00:00
|
|
|
fM0 = SlideMatrix(fTarget->fSlide, fTarget->fRect);
|
|
|
|
fM1 = SlideMatrix(fTarget->fSlide, fRect);
|
|
|
|
|
2018-02-20 15:31:01 +00:00
|
|
|
fOpacity0 = 0;
|
|
|
|
fOpacity1 = 1;
|
|
|
|
|
2018-02-19 18:25:18 +00:00
|
|
|
fTimeBase = 0;
|
|
|
|
fState = State::kFocusing;
|
2018-02-20 15:31:01 +00:00
|
|
|
|
|
|
|
// Push initial state to the scene graph.
|
|
|
|
this->onTick(fTimeBase);
|
2018-02-19 18:25:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void startUnfocus() {
|
|
|
|
SkASSERT(fTarget);
|
|
|
|
|
2018-06-18 19:11:00 +00:00
|
|
|
using std::swap;
|
|
|
|
swap(fM0, fM1);
|
|
|
|
swap(fOpacity0, fOpacity1);
|
2018-02-19 18:25:18 +00:00
|
|
|
|
|
|
|
fTimeBase = 0;
|
|
|
|
fState = State::kUnfocusing;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onMouse(SkScalar x, SkScalar y, sk_app::Window::InputState state, uint32_t modifiers) {
|
|
|
|
SkASSERT(fTarget);
|
|
|
|
|
2018-02-20 16:44:43 +00:00
|
|
|
if (!fRect.contains(x, y)) {
|
2018-02-20 15:31:01 +00:00
|
|
|
this->startUnfocus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-19 18:25:18 +00:00
|
|
|
// Map coords to slide space.
|
|
|
|
const auto xform = SkMatrix::MakeRectToRect(fRect,
|
|
|
|
SkRect::MakeSize(fDir->fWinSize),
|
|
|
|
SkMatrix::kCenter_ScaleToFit);
|
|
|
|
const auto pt = xform.mapXY(x, y);
|
|
|
|
|
|
|
|
return fTarget->fSlide->onMouse(pt.x(), pt.y(), state, modifiers);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onChar(SkUnichar c) {
|
|
|
|
SkASSERT(fTarget);
|
|
|
|
|
|
|
|
return fTarget->fSlide->onChar(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void onTick(float t) {
|
|
|
|
if (!this->isAnimating())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!fTimeBase) {
|
|
|
|
fTimeBase = t;
|
|
|
|
}
|
|
|
|
|
2018-02-20 15:31:01 +00:00
|
|
|
const auto rel_t = (t - fTimeBase) / kFocusDuration,
|
|
|
|
map_t = SkTPin(fMap.computeYFromX(rel_t), 0.0f, 1.0f);
|
2018-02-19 18:25:18 +00:00
|
|
|
|
|
|
|
SkMatrix m;
|
|
|
|
for (int i = 0; i < 9; ++i) {
|
2018-02-20 15:31:01 +00:00
|
|
|
m[i] = fM0[i] + map_t * (fM1[i] - fM0[i]);
|
2018-02-19 18:25:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkASSERT(fTarget);
|
|
|
|
fTarget->fTransform->getMatrix()->setMatrix(m);
|
|
|
|
|
2018-02-20 15:31:01 +00:00
|
|
|
const auto shadeOpacity = fOpacity0 + map_t * (fOpacity1 - fOpacity0);
|
|
|
|
fShadePaint->setOpacity(shadeOpacity);
|
|
|
|
|
2018-02-19 18:25:18 +00:00
|
|
|
if (rel_t < 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (fState) {
|
|
|
|
case State::kFocusing:
|
|
|
|
fState = State::kFocused;
|
|
|
|
break;
|
|
|
|
case State::kUnfocusing:
|
|
|
|
fState = State::kIdle;
|
2018-02-20 15:31:01 +00:00
|
|
|
fDir->fRoot->removeChild(fShade);
|
2018-02-19 18:25:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case State::kIdle:
|
|
|
|
case State::kFocused:
|
|
|
|
SkASSERT(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum class State {
|
|
|
|
kIdle,
|
|
|
|
kFocusing,
|
|
|
|
kUnfocusing,
|
|
|
|
kFocused,
|
|
|
|
};
|
|
|
|
|
|
|
|
bool isAnimating() const { return fState == State::kFocusing || fState == State::kUnfocusing; }
|
|
|
|
|
2018-02-20 15:31:01 +00:00
|
|
|
const SlideDir* fDir;
|
|
|
|
const SkRect fRect;
|
|
|
|
const Rec* fTarget;
|
|
|
|
|
|
|
|
SkCubicMap fMap;
|
|
|
|
sk_sp<sksg::RenderNode> fShade;
|
|
|
|
sk_sp<sksg::PaintNode> fShadePaint;
|
2018-02-19 18:25:18 +00:00
|
|
|
|
|
|
|
SkMatrix fM0 = SkMatrix::I(),
|
|
|
|
fM1 = SkMatrix::I();
|
2018-02-20 15:31:01 +00:00
|
|
|
float fOpacity0 = 0,
|
|
|
|
fOpacity1 = 1,
|
|
|
|
fTimeBase = 0;
|
2018-02-19 18:25:18 +00:00
|
|
|
State fState = State::kIdle;
|
|
|
|
|
|
|
|
using INHERITED = sksg::Animator;
|
2018-02-15 23:40:48 +00:00
|
|
|
};
|
|
|
|
|
2018-09-05 19:41:23 +00:00
|
|
|
SlideDir::SlideDir(const SkString& name, SkTArray<sk_sp<Slide>>&& slides, int columns)
|
2018-02-15 23:40:48 +00:00
|
|
|
: fSlides(std::move(slides))
|
|
|
|
, fColumns(columns) {
|
|
|
|
fName = name;
|
|
|
|
}
|
|
|
|
|
2018-02-19 18:25:18 +00:00
|
|
|
static sk_sp<sksg::RenderNode> MakeLabel(const SkString& txt,
|
|
|
|
const SkPoint& pos,
|
|
|
|
const SkMatrix& dstXform) {
|
|
|
|
const auto size = kLabelSize / std::sqrt(dstXform.getScaleX() * dstXform.getScaleY());
|
2018-02-15 23:40:48 +00:00
|
|
|
auto text = sksg::Text::Make(nullptr, txt);
|
|
|
|
text->setFlags(SkPaint::kAntiAlias_Flag);
|
2018-02-19 18:25:18 +00:00
|
|
|
text->setSize(size);
|
2018-02-15 23:40:48 +00:00
|
|
|
text->setAlign(SkPaint::kCenter_Align);
|
2018-02-19 18:25:18 +00:00
|
|
|
text->setPosition(pos + SkPoint::Make(0, size));
|
2018-02-15 23:40:48 +00:00
|
|
|
|
|
|
|
return sksg::Draw::Make(std::move(text), sksg::Color::Make(SK_ColorBLACK));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SlideDir::load(SkScalar winWidth, SkScalar winHeight) {
|
|
|
|
// Build a global scene using transformed animation fragments:
|
|
|
|
//
|
|
|
|
// [Group(root)]
|
|
|
|
// [Transform]
|
|
|
|
// [Group]
|
|
|
|
// [AnimationWrapper]
|
|
|
|
// [Draw]
|
|
|
|
// [Text]
|
|
|
|
// [Color]
|
|
|
|
// [Transform]
|
|
|
|
// [Group]
|
|
|
|
// [AnimationWrapper]
|
|
|
|
// [Draw]
|
|
|
|
// [Text]
|
|
|
|
// [Color]
|
|
|
|
// ...
|
|
|
|
//
|
|
|
|
|
2018-02-19 18:25:18 +00:00
|
|
|
fWinSize = SkSize::Make(winWidth, winHeight);
|
|
|
|
const auto cellWidth = winWidth / fColumns;
|
|
|
|
fCellSize = SkSize::Make(cellWidth, cellWidth / kAspectRatio);
|
2018-02-15 23:40:48 +00:00
|
|
|
|
|
|
|
sksg::AnimatorList sceneAnimators;
|
2018-02-19 18:25:18 +00:00
|
|
|
fRoot = sksg::Group::Make();
|
2018-02-15 23:40:48 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < fSlides.count(); ++i) {
|
|
|
|
const auto& slide = fSlides[i];
|
|
|
|
slide->load(winWidth, winHeight);
|
|
|
|
|
|
|
|
const auto slideSize = slide->getDimensions();
|
2018-02-19 18:25:18 +00:00
|
|
|
const auto cell = SkRect::MakeXYWH(fCellSize.width() * (i % fColumns),
|
|
|
|
fCellSize.height() * (i / fColumns),
|
|
|
|
fCellSize.width(),
|
|
|
|
fCellSize.height()),
|
2018-02-15 23:40:48 +00:00
|
|
|
slideRect = cell.makeInset(kPadding.width(), kPadding.height());
|
|
|
|
|
2018-02-19 18:25:18 +00:00
|
|
|
auto slideMatrix = SlideMatrix(slide, slideRect);
|
|
|
|
auto adapter = sk_make_sp<SlideAdapter>(slide);
|
|
|
|
auto slideGrp = sksg::Group::Make();
|
|
|
|
slideGrp->addChild(sksg::Draw::Make(sksg::Rect::Make(SkRect::MakeIWH(slideSize.width(),
|
|
|
|
slideSize.height())),
|
|
|
|
sksg::Color::Make(0xfff0f0f0)));
|
|
|
|
slideGrp->addChild(adapter);
|
|
|
|
slideGrp->addChild(MakeLabel(slide->getName(),
|
|
|
|
SkPoint::Make(slideSize.width() / 2, slideSize.height()),
|
|
|
|
slideMatrix));
|
|
|
|
auto slideTransform = sksg::Transform::Make(std::move(slideGrp), slideMatrix);
|
2018-02-15 23:40:48 +00:00
|
|
|
|
|
|
|
sceneAnimators.push_back(adapter->makeForwardingAnimator());
|
|
|
|
|
2018-02-19 18:25:18 +00:00
|
|
|
fRoot->addChild(slideTransform);
|
|
|
|
fRecs.push_back({ slide, slideTransform, slideRect });
|
2018-02-15 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 18:25:18 +00:00
|
|
|
fScene = sksg::Scene::Make(fRoot, std::move(sceneAnimators));
|
|
|
|
|
|
|
|
const auto focusRect = SkRect::MakeSize(fWinSize).makeInset(kFocusInset.width(),
|
|
|
|
kFocusInset.height());
|
|
|
|
fFocusController = skstd::make_unique<FocusController>(this, focusRect);
|
2018-02-15 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SlideDir::unload() {
|
|
|
|
for (const auto& slide : fSlides) {
|
|
|
|
slide->unload();
|
|
|
|
}
|
|
|
|
|
|
|
|
fRecs.reset();
|
|
|
|
fScene.reset();
|
2018-02-19 18:25:18 +00:00
|
|
|
fFocusController.reset();
|
|
|
|
fRoot.reset();
|
2018-02-15 23:40:48 +00:00
|
|
|
fTimeBase = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize SlideDir::getDimensions() const {
|
2018-02-20 21:49:20 +00:00
|
|
|
return SkSize::Make(fWinSize.width(),
|
|
|
|
fCellSize.height() * (1 + (fSlides.count() - 1) / fColumns)).toCeil();
|
2018-02-15 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SlideDir::draw(SkCanvas* canvas) {
|
|
|
|
fScene->render(canvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SlideDir::animate(const SkAnimTimer& timer) {
|
|
|
|
if (fTimeBase == 0) {
|
|
|
|
// Reset the animation time.
|
|
|
|
fTimeBase = timer.msec();
|
|
|
|
}
|
2018-02-19 18:25:18 +00:00
|
|
|
|
|
|
|
const auto t = timer.msec() - fTimeBase;
|
|
|
|
fScene->animate(t);
|
|
|
|
fFocusController->tick(t);
|
2018-02-15 23:40:48 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SlideDir::onChar(SkUnichar c) {
|
2018-02-19 18:25:18 +00:00
|
|
|
if (fFocusController->hasFocus()) {
|
|
|
|
if (c == kUnfocusKey) {
|
|
|
|
fFocusController->startUnfocus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return fFocusController->onChar(c);
|
|
|
|
}
|
|
|
|
|
2018-02-15 23:40:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-19 18:25:18 +00:00
|
|
|
bool SlideDir::onMouse(SkScalar x, SkScalar y, sk_app::Window::InputState state,
|
|
|
|
uint32_t modifiers) {
|
|
|
|
if (state == sk_app::Window::kMove_InputState || modifiers)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (fFocusController->hasFocus()) {
|
|
|
|
return fFocusController->onMouse(x, y, state, modifiers);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto* cell = this->findCell(x, y);
|
|
|
|
if (!cell)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
static constexpr SkScalar kClickMoveTolerance = 4;
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case sk_app::Window::kDown_InputState:
|
|
|
|
fTrackingCell = cell;
|
|
|
|
fTrackingPos = SkPoint::Make(x, y);
|
|
|
|
break;
|
|
|
|
case sk_app::Window::kUp_InputState:
|
|
|
|
if (cell == fTrackingCell &&
|
|
|
|
SkPoint::Distance(fTrackingPos, SkPoint::Make(x, y)) < kClickMoveTolerance) {
|
|
|
|
fFocusController->startFocus(cell);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-02-15 23:40:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-02-19 18:25:18 +00:00
|
|
|
|
|
|
|
const SlideDir::Rec* SlideDir::findCell(float x, float y) const {
|
|
|
|
// TODO: use SG hit testing instead of layout info?
|
|
|
|
const auto size = this->getDimensions();
|
|
|
|
if (x < 0 || y < 0 || x >= size.width() || y >= size.height()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int col = static_cast<int>(x / fCellSize.width()),
|
|
|
|
row = static_cast<int>(y / fCellSize.height()),
|
|
|
|
idx = row * fColumns + col;
|
|
|
|
|
2018-02-20 21:49:20 +00:00
|
|
|
return idx < fRecs.count() ? &fRecs[idx] : nullptr;
|
2018-02-19 18:25:18 +00:00
|
|
|
}
|