Allow querying slide animation state.

This commit is contained in:
John Preston 2019-09-24 12:31:31 +03:00
parent f7c3e22cc9
commit 7ed6610511
2 changed files with 33 additions and 16 deletions

View file

@ -23,29 +23,23 @@ void SlideAnimation::setSnapshots(
} }
void SlideAnimation::paintFrame(QPainter &p, int x, int y, int outerWidth) { void SlideAnimation::paintFrame(QPainter &p, int x, int y, int outerWidth) {
const auto dt = _animation.value(1.);
if (!animating()) { if (!animating()) {
return; return;
} }
const auto pixelRatio = style::DevicePixelRatio(); const auto pixelRatio = style::DevicePixelRatio();
const auto easeOut = anim::easeOutCirc(1., dt); const auto state = this->state();
const auto easeIn = anim::easeInCirc(1., dt);
const auto arrivingAlpha = easeIn;
const auto departingAlpha = 1. - easeOut;
const auto leftCoord = _slideLeft const auto leftCoord = _slideLeft
? anim::interpolate(-_leftSnapshotWidth, 0, easeOut) ? anim::interpolate(-_leftSnapshotWidth, 0, state.leftProgress)
: anim::interpolate(0, -_leftSnapshotWidth, easeIn); : anim::interpolate(0, -_leftSnapshotWidth, state.leftProgress);
const auto leftAlpha = (_slideLeft ? arrivingAlpha : departingAlpha);
const auto rightCoord = _slideLeft const auto rightCoord = _slideLeft
? anim::interpolate(0, _rightSnapshotWidth, easeIn) ? anim::interpolate(0, _rightSnapshotWidth, state.rightProgress)
: anim::interpolate(_rightSnapshotWidth, 0, easeOut); : anim::interpolate(_rightSnapshotWidth, 0, state.rightProgress);
const auto rightAlpha = (_slideLeft ? departingAlpha : arrivingAlpha);
if (_overflowHidden) { if (_overflowHidden) {
const auto leftWidth = (_leftSnapshotWidth + leftCoord); const auto leftWidth = (_leftSnapshotWidth + leftCoord);
if (leftWidth > 0) { if (leftWidth > 0) {
p.setOpacity(leftAlpha); p.setOpacity(state.leftAlpha);
p.drawPixmap( p.drawPixmap(
x, x,
y, y,
@ -59,7 +53,7 @@ void SlideAnimation::paintFrame(QPainter &p, int x, int y, int outerWidth) {
} }
const auto rightWidth = _rightSnapshotWidth - rightCoord; const auto rightWidth = _rightSnapshotWidth - rightCoord;
if (rightWidth > 0) { if (rightWidth > 0) {
p.setOpacity(rightAlpha); p.setOpacity(state.rightAlpha);
p.drawPixmap( p.drawPixmap(
x + rightCoord, x + rightCoord,
y, y,
@ -70,11 +64,26 @@ void SlideAnimation::paintFrame(QPainter &p, int x, int y, int outerWidth) {
_rightSnapshot.height()); _rightSnapshot.height());
} }
} else { } else {
p.setOpacity(leftAlpha); p.setOpacity(state.leftAlpha);
p.drawPixmap(x + leftCoord, y, _leftSnapshot); p.drawPixmap(x + leftCoord, y, _leftSnapshot);
p.setOpacity(rightAlpha); p.setOpacity(state.rightAlpha);
p.drawPixmap(x + rightCoord, y, _rightSnapshot); p.drawPixmap(x + rightCoord, y, _rightSnapshot);
} }
} }
SlideAnimation::State SlideAnimation::state() const {
const auto dt = _animation.value(1.);
const auto easeOut = anim::easeOutCirc(1., dt);
const auto easeIn = anim::easeInCirc(1., dt);
const auto arrivingAlpha = easeIn;
const auto departingAlpha = 1. - easeOut;
auto result = State();
result.leftProgress = _slideLeft ? easeOut : easeIn;
result.leftAlpha = _slideLeft ? arrivingAlpha : departingAlpha;
result.rightProgress = _slideLeft ? easeIn : easeOut;
result.rightAlpha = _slideLeft ? departingAlpha : arrivingAlpha;
return result;
}
} // namespace Ui } // namespace Ui

View file

@ -12,6 +12,13 @@ namespace Ui {
class SlideAnimation { class SlideAnimation {
public: public:
struct State {
float64 leftProgress = 0.;
float64 leftAlpha = 0.;
float64 rightProgress = 0.;
float64 rightAlpha = 0.;
};
void setSnapshots(QPixmap leftSnapshot, QPixmap rightSnapshot); void setSnapshots(QPixmap leftSnapshot, QPixmap rightSnapshot);
void setOverflowHidden(bool hidden) { void setOverflowHidden(bool hidden) {
@ -23,7 +30,8 @@ public:
void paintFrame(QPainter &p, int x, int y, int outerWidth); void paintFrame(QPainter &p, int x, int y, int outerWidth);
bool animating() const { [[nodiscard]] State state() const;
[[nodiscard]] bool animating() const {
return _animation.animating(); return _animation.animating();
} }