2016-09-15 16:32:49 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2017-01-11 18:31:31 +00:00
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
2016-09-15 16:32:49 +00:00
|
|
|
*/
|
|
|
|
#include "media/player/media_player_button.h"
|
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
#include "styles/style_widgets.h"
|
|
|
|
|
2016-09-15 16:32:49 +00:00
|
|
|
namespace Media {
|
|
|
|
namespace Player {
|
2016-10-08 18:28:43 +00:00
|
|
|
namespace {
|
2016-09-15 16:32:49 +00:00
|
|
|
|
2016-10-08 18:28:43 +00:00
|
|
|
template <int N>
|
|
|
|
QPainterPath interpolatePaths(QPointF (&from)[N], QPointF (&to)[N], float64 k) {
|
|
|
|
static_assert(N > 1, "Wrong points count in path!");
|
2016-09-16 12:49:36 +00:00
|
|
|
|
2016-10-09 17:08:16 +00:00
|
|
|
auto from_coef = 1. - k, to_coef = k;
|
2016-10-08 18:28:43 +00:00
|
|
|
QPainterPath result;
|
|
|
|
auto x = from[0].x() * from_coef + to[0].x() * to_coef;
|
|
|
|
auto y = from[0].y() * from_coef + to[0].y() * to_coef;
|
|
|
|
result.moveTo(x, y);
|
|
|
|
for (int i = 1; i != N; ++i) {
|
|
|
|
result.lineTo(from[i].x() * from_coef + to[i].x() * to_coef, from[i].y() * from_coef + to[i].y() * to_coef);
|
2016-09-23 16:04:26 +00:00
|
|
|
}
|
2016-10-08 18:28:43 +00:00
|
|
|
result.lineTo(x, y);
|
|
|
|
return result;
|
2016-09-23 16:04:26 +00:00
|
|
|
}
|
|
|
|
|
2016-10-08 18:28:43 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-02-26 11:32:13 +00:00
|
|
|
PlayButtonLayout::PlayButtonLayout(const style::MediaPlayerButton &st, base::lambda<void()> callback)
|
2016-10-08 18:28:43 +00:00
|
|
|
: _st(st)
|
2017-02-21 13:45:56 +00:00
|
|
|
, _callback(std::move(callback)) {
|
2016-09-16 12:49:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-08 18:28:43 +00:00
|
|
|
void PlayButtonLayout::setState(State state) {
|
|
|
|
if (_nextState == state) return;
|
|
|
|
|
|
|
|
_nextState = state;
|
|
|
|
if (!_transformProgress.animating(getms())) {
|
|
|
|
_oldState = _state;
|
|
|
|
_state = _nextState;
|
|
|
|
_transformBackward = false;
|
2016-10-09 17:08:16 +00:00
|
|
|
if (_state != _oldState) {
|
|
|
|
startTransform(0., 1.);
|
|
|
|
if (_callback) _callback();
|
|
|
|
}
|
2016-10-08 18:28:43 +00:00
|
|
|
} else if (_oldState == _nextState) {
|
|
|
|
qSwap(_oldState, _state);
|
|
|
|
startTransform(_transformBackward ? 0. : 1., _transformBackward ? 1. : 0.);
|
|
|
|
_transformBackward = !_transformBackward;
|
2016-09-16 12:49:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 17:08:16 +00:00
|
|
|
void PlayButtonLayout::finishTransform() {
|
|
|
|
_transformProgress.finish();
|
|
|
|
_transformBackward = false;
|
|
|
|
if (_callback) _callback();
|
|
|
|
}
|
|
|
|
|
2016-10-08 18:28:43 +00:00
|
|
|
void PlayButtonLayout::paint(Painter &p, const QBrush &brush) {
|
|
|
|
if (_transformProgress.animating(getms())) {
|
|
|
|
auto from = _oldState, to = _state;
|
|
|
|
auto backward = _transformBackward;
|
|
|
|
auto progress = _transformProgress.current(1.);
|
|
|
|
if (from == State::Cancel || (from == State::Pause && to == State::Play)) {
|
|
|
|
qSwap(from, to);
|
|
|
|
backward = !backward;
|
|
|
|
}
|
|
|
|
if (backward) progress = 1. - progress;
|
|
|
|
|
|
|
|
t_assert(from != to);
|
|
|
|
if (from == State::Play) {
|
|
|
|
if (to == State::Pause) {
|
|
|
|
paintPlayToPause(p, brush, progress);
|
|
|
|
} else {
|
|
|
|
t_assert(to == State::Cancel);
|
|
|
|
paintPlayToCancel(p, brush, progress);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t_assert(from == State::Pause && to == State::Cancel);
|
|
|
|
paintPauseToCancel(p, brush, progress);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (_state) {
|
|
|
|
case State::Play: paintPlay(p, brush); break;
|
|
|
|
case State::Pause: paintPlayToPause(p, brush, 1.); break;
|
|
|
|
case State::Cancel: paintPlayToCancel(p, brush, 1.); break;
|
|
|
|
}
|
2016-09-16 12:49:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 18:28:43 +00:00
|
|
|
void PlayButtonLayout::paintPlay(Painter &p, const QBrush &brush) {
|
|
|
|
auto playLeft = 0. + _st.playPosition.x();
|
|
|
|
auto playTop = 0. + _st.playPosition.y();
|
|
|
|
auto playWidth = _st.playOuter.width() - 2 * playLeft;
|
|
|
|
auto playHeight = _st.playOuter.height() - 2 * playTop;
|
2016-09-16 12:49:36 +00:00
|
|
|
|
2016-12-03 12:10:35 +00:00
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
|
2016-09-16 12:49:36 +00:00
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
|
2016-10-08 18:28:43 +00:00
|
|
|
QPainterPath pathPlay;
|
|
|
|
pathPlay.moveTo(playLeft, playTop);
|
|
|
|
pathPlay.lineTo(playLeft + playWidth, playTop + (playHeight / 2.));
|
|
|
|
pathPlay.lineTo(playLeft, playTop + playHeight);
|
|
|
|
pathPlay.lineTo(playLeft, playTop);
|
|
|
|
p.fillPath(pathPlay, brush);
|
2016-09-16 12:49:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-08 18:28:43 +00:00
|
|
|
void PlayButtonLayout::paintPlayToPause(Painter &p, const QBrush &brush, float64 progress) {
|
|
|
|
auto playLeft = 0. + _st.playPosition.x();
|
|
|
|
auto playTop = 0. + _st.playPosition.y();
|
|
|
|
auto playWidth = _st.playOuter.width() - 2 * playLeft;
|
|
|
|
auto playHeight = _st.playOuter.height() - 2 * playTop;
|
2016-09-16 12:49:36 +00:00
|
|
|
|
2016-10-08 18:28:43 +00:00
|
|
|
auto pauseLeft = 0. + _st.pausePosition.x();
|
|
|
|
auto pauseTop = 0. + _st.pausePosition.y();
|
|
|
|
auto pauseWidth = _st.pauseOuter.width() - 2 * pauseLeft;
|
|
|
|
auto pauseHeight = _st.pauseOuter.height() - 2 * pauseTop;
|
|
|
|
auto pauseStroke = 0. + _st.pauseStroke;
|
2016-09-16 12:49:36 +00:00
|
|
|
|
2016-10-08 18:28:43 +00:00
|
|
|
p.setPen(Qt::NoPen);
|
2016-12-03 12:10:35 +00:00
|
|
|
PainterHighQualityEnabler hq(p);
|
2016-09-16 12:49:36 +00:00
|
|
|
|
2016-10-08 18:28:43 +00:00
|
|
|
QPointF pathLeftPause[] = {
|
|
|
|
{ pauseLeft, pauseTop },
|
|
|
|
{ pauseLeft + pauseStroke, pauseTop },
|
|
|
|
{ pauseLeft + pauseStroke, pauseTop + pauseHeight },
|
|
|
|
{ pauseLeft, pauseTop + pauseHeight },
|
|
|
|
};
|
|
|
|
QPointF pathLeftPlay[] = {
|
|
|
|
{ playLeft, playTop },
|
|
|
|
{ playLeft + (playWidth / 2.), playTop + (playHeight / 4.) },
|
|
|
|
{ playLeft + (playWidth / 2.), playTop + (3 * playHeight / 4.) },
|
|
|
|
{ playLeft, playTop + playHeight },
|
|
|
|
};
|
2016-10-09 17:08:16 +00:00
|
|
|
p.fillPath(interpolatePaths(pathLeftPlay, pathLeftPause, progress), brush);
|
2016-10-08 18:28:43 +00:00
|
|
|
|
|
|
|
QPointF pathRightPause[] = {
|
|
|
|
{ pauseLeft + pauseWidth - pauseStroke, pauseTop },
|
|
|
|
{ pauseLeft + pauseWidth, pauseTop },
|
|
|
|
{ pauseLeft + pauseWidth, pauseTop + pauseHeight },
|
|
|
|
{ pauseLeft + pauseWidth - pauseStroke, pauseTop + pauseHeight },
|
|
|
|
};
|
|
|
|
QPointF pathRightPlay[] = {
|
|
|
|
{ playLeft + (playWidth / 2.), playTop + (playHeight / 4.) },
|
|
|
|
{ playLeft + playWidth, playTop + (playHeight / 2.) },
|
|
|
|
{ playLeft + playWidth, playTop + (playHeight / 2.) },
|
|
|
|
{ playLeft + (playWidth / 2.), playTop + (3 * playHeight / 4.) },
|
|
|
|
};
|
2016-10-09 17:08:16 +00:00
|
|
|
p.fillPath(interpolatePaths(pathRightPlay, pathRightPause, progress), brush);
|
2016-10-08 18:28:43 +00:00
|
|
|
}
|
2016-09-16 12:49:36 +00:00
|
|
|
|
2016-10-08 18:28:43 +00:00
|
|
|
void PlayButtonLayout::paintPlayToCancel(Painter &p, const QBrush &brush, float64 progress) {
|
2016-10-09 17:08:16 +00:00
|
|
|
static const auto sqrt2 = sqrt(2.);
|
|
|
|
|
|
|
|
auto playLeft = 0. + _st.playPosition.x();
|
|
|
|
auto playTop = 0. + _st.playPosition.y();
|
|
|
|
auto playWidth = _st.playOuter.width() - 2 * playLeft;
|
|
|
|
auto playHeight = _st.playOuter.height() - 2 * playTop;
|
|
|
|
|
|
|
|
auto cancelLeft = 0. + _st.cancelPosition.x();
|
|
|
|
auto cancelTop = 0. + _st.cancelPosition.y();
|
|
|
|
auto cancelWidth = _st.cancelOuter.width() - 2 * cancelLeft;
|
|
|
|
auto cancelHeight = _st.cancelOuter.height() - 2 * cancelTop;
|
|
|
|
auto cancelStroke = (0. + _st.cancelStroke) / sqrt2;
|
2016-09-16 12:49:36 +00:00
|
|
|
|
2016-10-09 17:08:16 +00:00
|
|
|
p.setPen(Qt::NoPen);
|
2016-12-03 12:10:35 +00:00
|
|
|
PainterHighQualityEnabler hq(p);
|
2016-10-09 17:08:16 +00:00
|
|
|
|
|
|
|
QPointF pathPlay[] = {
|
|
|
|
{ playLeft, playTop },
|
|
|
|
{ playLeft, playTop },
|
|
|
|
{ playLeft + (playWidth / 2.), playTop + (playHeight / 4.) },
|
|
|
|
{ playLeft + playWidth, playTop + (playHeight / 2.) },
|
|
|
|
{ playLeft + playWidth, playTop + (playHeight / 2.) },
|
|
|
|
{ playLeft + playWidth, playTop + (playHeight / 2.) },
|
|
|
|
{ playLeft + playWidth, playTop + (playHeight / 2.) },
|
|
|
|
{ playLeft + playWidth, playTop + (playHeight / 2.) },
|
|
|
|
{ playLeft + (playWidth / 2.), playTop + (3 * playHeight / 4.) },
|
|
|
|
{ playLeft, playTop + playHeight },
|
|
|
|
{ playLeft, playTop + playHeight },
|
|
|
|
{ playLeft, playTop + (playHeight / 2.) },
|
|
|
|
};
|
|
|
|
QPointF pathCancel[] = {
|
|
|
|
{ cancelLeft, cancelTop + cancelStroke },
|
|
|
|
{ cancelLeft + cancelStroke, cancelTop },
|
|
|
|
{ cancelLeft + (cancelWidth / 2.), cancelTop + (cancelHeight / 2.) - cancelStroke },
|
|
|
|
{ cancelLeft + cancelWidth - cancelStroke, cancelTop },
|
|
|
|
{ cancelLeft + cancelWidth, cancelTop + cancelStroke },
|
|
|
|
{ cancelLeft + (cancelWidth / 2.) + cancelStroke, cancelTop + (cancelHeight / 2.) },
|
|
|
|
{ cancelLeft + cancelWidth, cancelTop + cancelHeight - cancelStroke },
|
|
|
|
{ cancelLeft + cancelWidth - cancelStroke, cancelTop + cancelHeight },
|
|
|
|
{ cancelLeft + (cancelWidth / 2.), cancelTop + (cancelHeight / 2.) + cancelStroke },
|
|
|
|
{ cancelLeft + cancelStroke, cancelTop + cancelHeight },
|
|
|
|
{ cancelLeft, cancelTop + cancelHeight - cancelStroke },
|
|
|
|
{ cancelLeft + (cancelWidth / 2.) - cancelStroke, cancelTop + (cancelHeight / 2.) },
|
|
|
|
};
|
|
|
|
p.fillPath(interpolatePaths(pathPlay, pathCancel, progress), brush);
|
2016-10-08 18:28:43 +00:00
|
|
|
}
|
2016-09-16 12:49:36 +00:00
|
|
|
|
2016-10-08 18:28:43 +00:00
|
|
|
void PlayButtonLayout::paintPauseToCancel(Painter &p, const QBrush &brush, float64 progress) {
|
2016-10-09 17:08:16 +00:00
|
|
|
static const auto sqrt2 = sqrt(2.);
|
2016-09-16 12:49:36 +00:00
|
|
|
|
2016-10-09 17:08:16 +00:00
|
|
|
auto pauseLeft = 0. + _st.pausePosition.x();
|
|
|
|
auto pauseTop = 0. + _st.pausePosition.y();
|
|
|
|
auto pauseWidth = _st.pauseOuter.width() - 2 * pauseLeft;
|
|
|
|
auto pauseHeight = _st.pauseOuter.height() - 2 * pauseTop;
|
|
|
|
auto pauseStroke = 0. + _st.pauseStroke;
|
|
|
|
|
|
|
|
auto cancelLeft = 0. + _st.cancelPosition.x();
|
|
|
|
auto cancelTop = 0. + _st.cancelPosition.y();
|
|
|
|
auto cancelWidth = _st.cancelOuter.width() - 2 * cancelLeft;
|
|
|
|
auto cancelHeight = _st.cancelOuter.height() - 2 * cancelTop;
|
|
|
|
auto cancelStroke = (0. + _st.cancelStroke) / sqrt2;
|
|
|
|
|
|
|
|
p.setPen(Qt::NoPen);
|
2016-12-03 12:10:35 +00:00
|
|
|
PainterHighQualityEnabler hq(p);
|
2016-10-09 17:08:16 +00:00
|
|
|
|
|
|
|
QPointF pathLeftPause[] = {
|
|
|
|
{ pauseLeft, pauseTop },
|
|
|
|
{ pauseLeft + pauseStroke, pauseTop },
|
|
|
|
{ pauseLeft + pauseStroke, pauseTop + pauseHeight },
|
|
|
|
{ pauseLeft, pauseTop + pauseHeight },
|
|
|
|
};
|
|
|
|
QPointF pathLeftCancel[] = {
|
|
|
|
{ cancelLeft, cancelTop + cancelStroke },
|
|
|
|
{ cancelLeft + cancelStroke, cancelTop },
|
|
|
|
{ cancelLeft + cancelWidth, cancelTop + cancelHeight - cancelStroke },
|
|
|
|
{ cancelLeft + cancelWidth - cancelStroke, cancelTop + cancelHeight },
|
|
|
|
};
|
|
|
|
p.fillPath(interpolatePaths(pathLeftPause, pathLeftCancel, progress), brush);
|
|
|
|
|
|
|
|
QPointF pathRightPause[] = {
|
|
|
|
{ pauseLeft + pauseWidth - pauseStroke, pauseTop },
|
|
|
|
{ pauseLeft + pauseWidth, pauseTop },
|
|
|
|
{ pauseLeft + pauseWidth, pauseTop + pauseHeight },
|
|
|
|
{ pauseLeft + pauseWidth - pauseStroke, pauseTop + pauseHeight },
|
|
|
|
};
|
|
|
|
QPointF pathRightCancel[] = {
|
|
|
|
{ cancelLeft + cancelWidth - cancelStroke, cancelTop },
|
|
|
|
{ cancelLeft + cancelWidth, cancelTop + cancelStroke },
|
|
|
|
{ cancelLeft + cancelStroke, cancelTop + cancelHeight },
|
|
|
|
{ cancelLeft, cancelTop + cancelHeight - cancelStroke },
|
|
|
|
};
|
|
|
|
p.fillPath(interpolatePaths(pathRightPause, pathRightCancel, progress), brush);
|
2016-10-08 18:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PlayButtonLayout::animationCallback() {
|
|
|
|
if (!_transformProgress.animating()) {
|
|
|
|
auto finalState = _nextState;
|
|
|
|
_nextState = _state;
|
|
|
|
setState(finalState);
|
2016-09-16 12:49:36 +00:00
|
|
|
}
|
2016-10-08 18:28:43 +00:00
|
|
|
_callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlayButtonLayout::startTransform(float64 from, float64 to) {
|
|
|
|
_transformProgress.start([this] { animationCallback(); }, from, to, st::mediaPlayerButtonTransformDuration);
|
2016-09-16 12:49:36 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 16:32:49 +00:00
|
|
|
} // namespace Player
|
|
|
|
} // namespace Media
|