2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
|
|
|
#include "animation.h"
|
2015-04-19 10:29:19 +00:00
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
#include "media/media_clip_reader.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-09-28 20:28:53 +00:00
|
|
|
namespace Media {
|
|
|
|
namespace Clip {
|
|
|
|
|
|
|
|
Reader *const ReaderPointer::BadPointer = SharedMemoryLocation<Reader, 0>();
|
|
|
|
|
|
|
|
ReaderPointer::~ReaderPointer() {
|
|
|
|
if (valid()) {
|
|
|
|
delete _pointer;
|
|
|
|
}
|
|
|
|
_pointer = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Clip
|
|
|
|
} // namespace Media
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
namespace {
|
2016-06-24 10:37:29 +00:00
|
|
|
|
|
|
|
AnimationManager *_manager = nullptr;
|
2017-08-03 14:10:55 +00:00
|
|
|
bool AnimationsDisabled = false;
|
2016-06-24 10:37:29 +00:00
|
|
|
|
|
|
|
} // namespace
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
namespace anim {
|
|
|
|
|
2016-11-07 11:24:19 +00:00
|
|
|
transition linear = [](const float64 &delta, const float64 &dt) {
|
2016-06-24 10:37:29 +00:00
|
|
|
return delta * dt;
|
2016-11-07 11:24:19 +00:00
|
|
|
};
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-07 11:24:19 +00:00
|
|
|
transition sineInOut = [](const float64 &delta, const float64 &dt) {
|
2016-06-24 10:37:29 +00:00
|
|
|
return -(delta / 2) * (cos(M_PI * dt) - 1);
|
2016-11-07 11:24:19 +00:00
|
|
|
};
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-07 11:24:19 +00:00
|
|
|
transition halfSine = [](const float64 &delta, const float64 &dt) {
|
2016-06-24 10:37:29 +00:00
|
|
|
return delta * sin(M_PI * dt / 2);
|
2016-11-07 11:24:19 +00:00
|
|
|
};
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-07 11:24:19 +00:00
|
|
|
transition easeOutBack = [](const float64 &delta, const float64 &dt) {
|
|
|
|
static constexpr auto s = 1.70158;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
const float64 t = dt - 1;
|
|
|
|
return delta * (t * t * ((s + 1) * t + s) + 1);
|
2016-11-07 11:24:19 +00:00
|
|
|
};
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-07 11:24:19 +00:00
|
|
|
transition easeInCirc = [](const float64 &delta, const float64 &dt) {
|
2016-06-24 10:37:29 +00:00
|
|
|
return -delta * (sqrt(1 - dt * dt) - 1);
|
2016-11-07 11:24:19 +00:00
|
|
|
};
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-07 11:24:19 +00:00
|
|
|
transition easeOutCirc = [](const float64 &delta, const float64 &dt) {
|
2016-06-24 10:37:29 +00:00
|
|
|
const float64 t = dt - 1;
|
|
|
|
return delta * sqrt(1 - t * t);
|
2016-11-07 11:24:19 +00:00
|
|
|
};
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-07 11:24:19 +00:00
|
|
|
transition easeInCubic = [](const float64 &delta, const float64 &dt) {
|
2016-06-24 10:37:29 +00:00
|
|
|
return delta * dt * dt * dt;
|
2016-11-07 11:24:19 +00:00
|
|
|
};
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-07 11:24:19 +00:00
|
|
|
transition easeOutCubic = [](const float64 &delta, const float64 &dt) {
|
2016-06-24 10:37:29 +00:00
|
|
|
const float64 t = dt - 1;
|
|
|
|
return delta * (t * t * t + 1);
|
2016-11-07 11:24:19 +00:00
|
|
|
};
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-07 11:24:19 +00:00
|
|
|
transition easeInQuint = [](const float64 &delta, const float64 &dt) {
|
2016-06-24 10:37:29 +00:00
|
|
|
const float64 t2 = dt * dt;
|
|
|
|
return delta * t2 * t2 * dt;
|
2016-11-07 11:24:19 +00:00
|
|
|
};
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-07 11:24:19 +00:00
|
|
|
transition easeOutQuint = [](const float64 &delta, const float64 &dt) {
|
2016-06-24 10:37:29 +00:00
|
|
|
const float64 t = dt - 1, t2 = t * t;
|
|
|
|
return delta * (t2 * t2 * t + 1);
|
2016-11-07 11:24:19 +00:00
|
|
|
};
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
void startManager() {
|
|
|
|
stopManager();
|
2015-12-15 14:50:51 +00:00
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
_manager = new AnimationManager();
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
void stopManager() {
|
|
|
|
delete _manager;
|
|
|
|
_manager = nullptr;
|
|
|
|
|
|
|
|
Media::Clip::Finish();
|
|
|
|
}
|
2015-04-19 19:01:45 +00:00
|
|
|
|
2018-09-20 16:39:59 +00:00
|
|
|
void registerClipManager(not_null<Media::Clip::Manager*> manager) {
|
|
|
|
Expects(_manager != nullptr);
|
|
|
|
|
|
|
|
_manager->registerClip(manager);
|
2015-12-08 12:33:37 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-08-03 14:10:55 +00:00
|
|
|
bool Disabled() {
|
|
|
|
return AnimationsDisabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetDisabled(bool disabled) {
|
|
|
|
AnimationsDisabled = disabled;
|
2017-09-20 18:40:23 +00:00
|
|
|
if (disabled && _manager) {
|
2018-09-20 16:39:59 +00:00
|
|
|
_manager->step();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawStaticLoading(
|
|
|
|
QPainter &p,
|
|
|
|
QRectF rect,
|
|
|
|
int stroke,
|
|
|
|
QPen pen,
|
|
|
|
QBrush brush) {
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
|
|
|
|
p.setBrush(brush);
|
|
|
|
pen.setWidthF(stroke);
|
|
|
|
pen.setCapStyle(Qt::RoundCap);
|
|
|
|
pen.setJoinStyle(Qt::RoundJoin);
|
|
|
|
p.setPen(pen);
|
|
|
|
p.drawEllipse(rect);
|
|
|
|
|
|
|
|
const auto center = rect.center();
|
|
|
|
const auto first = QPointF(center.x(), rect.y() + 1.5 * stroke);
|
|
|
|
const auto delta = center.y() - first.y();
|
|
|
|
const auto second = QPointF(center.x() + delta * 2 / 3., center.y());
|
|
|
|
if (delta > 0) {
|
|
|
|
QPainterPath path;
|
|
|
|
path.moveTo(first);
|
|
|
|
path.lineTo(center);
|
|
|
|
path.lineTo(second);
|
|
|
|
p.drawPath(path);
|
2017-09-20 18:40:23 +00:00
|
|
|
}
|
2017-08-03 14:10:55 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
} // anim
|
|
|
|
|
2016-12-07 13:32:25 +00:00
|
|
|
void BasicAnimation::start() {
|
2015-12-08 12:33:37 +00:00
|
|
|
if (!_manager) return;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-05-12 16:05:20 +00:00
|
|
|
_callbacks.start();
|
2015-12-08 12:33:37 +00:00
|
|
|
_manager->start(this);
|
|
|
|
_animating = true;
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-12-07 13:32:25 +00:00
|
|
|
void BasicAnimation::stop() {
|
2015-12-08 12:33:37 +00:00
|
|
|
if (!_manager) return;
|
|
|
|
|
|
|
|
_animating = false;
|
|
|
|
_manager->stop(this);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2015-04-19 10:29:19 +00:00
|
|
|
|
2018-09-20 16:39:59 +00:00
|
|
|
AnimationManager::AnimationManager() : _timer(this) {
|
2015-12-27 21:37:48 +00:00
|
|
|
_timer.setSingleShot(false);
|
2018-09-20 16:39:59 +00:00
|
|
|
connect(&_timer, &QTimer::timeout, this, &AnimationManager::step);
|
2015-12-27 21:37:48 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 13:32:25 +00:00
|
|
|
void AnimationManager::start(BasicAnimation *obj) {
|
2015-12-27 21:37:48 +00:00
|
|
|
if (_iterating) {
|
2016-09-26 12:09:59 +00:00
|
|
|
_starting.insert(obj);
|
2018-09-20 16:39:59 +00:00
|
|
|
if (!_stopping.empty()) {
|
|
|
|
_stopping.erase(obj);
|
2015-12-27 21:37:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-09-20 16:39:59 +00:00
|
|
|
if (_objects.empty()) {
|
2015-12-27 21:37:48 +00:00
|
|
|
_timer.start(AnimationTimerDelta);
|
|
|
|
}
|
2016-09-26 12:09:59 +00:00
|
|
|
_objects.insert(obj);
|
2015-12-27 21:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 13:32:25 +00:00
|
|
|
void AnimationManager::stop(BasicAnimation *obj) {
|
2015-12-27 21:37:48 +00:00
|
|
|
if (_iterating) {
|
2016-09-26 12:09:59 +00:00
|
|
|
_stopping.insert(obj);
|
2018-09-20 16:39:59 +00:00
|
|
|
if (!_starting.empty()) {
|
|
|
|
_starting.erase(obj);
|
2015-12-27 21:37:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-09-26 12:09:59 +00:00
|
|
|
auto i = _objects.find(obj);
|
2015-12-27 21:37:48 +00:00
|
|
|
if (i != _objects.cend()) {
|
|
|
|
_objects.erase(i);
|
2016-09-26 12:09:59 +00:00
|
|
|
if (_objects.empty()) {
|
2015-12-27 21:37:48 +00:00
|
|
|
_timer.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 16:39:59 +00:00
|
|
|
void AnimationManager::registerClip(not_null<Media::Clip::Manager*> clip) {
|
|
|
|
connect(
|
|
|
|
clip,
|
|
|
|
&Media::Clip::Manager::callback,
|
|
|
|
this,
|
|
|
|
&AnimationManager::clipCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationManager::step() {
|
2015-12-27 21:37:48 +00:00
|
|
|
_iterating = true;
|
2018-09-20 16:39:59 +00:00
|
|
|
const auto ms = getms();
|
|
|
|
for (const auto object : _objects) {
|
2016-09-26 12:09:59 +00:00
|
|
|
if (!_stopping.contains(object)) {
|
|
|
|
object->step(ms, true);
|
2016-02-14 15:58:39 +00:00
|
|
|
}
|
2015-12-27 21:37:48 +00:00
|
|
|
}
|
|
|
|
_iterating = false;
|
|
|
|
|
2018-09-20 16:39:59 +00:00
|
|
|
if (!_starting.empty()) {
|
|
|
|
for (const auto object : _starting) {
|
|
|
|
_objects.emplace(object);
|
2015-12-27 21:37:48 +00:00
|
|
|
}
|
|
|
|
_starting.clear();
|
|
|
|
}
|
2018-09-20 16:39:59 +00:00
|
|
|
if (!_stopping.empty()) {
|
|
|
|
for (const auto object : _stopping) {
|
|
|
|
_objects.erase(object);
|
2015-12-27 21:37:48 +00:00
|
|
|
}
|
|
|
|
_stopping.clear();
|
|
|
|
}
|
2016-09-26 12:09:59 +00:00
|
|
|
if (_objects.empty()) {
|
2015-12-27 21:37:48 +00:00
|
|
|
_timer.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 16:39:59 +00:00
|
|
|
void AnimationManager::clipCallback(
|
|
|
|
Media::Clip::Reader *reader,
|
|
|
|
qint32 threadIndex,
|
|
|
|
qint32 notification) {
|
|
|
|
Media::Clip::Reader::callback(
|
|
|
|
reader,
|
|
|
|
threadIndex,
|
|
|
|
Media::Clip::Notification(notification));
|
2015-12-28 15:34:45 +00:00
|
|
|
}
|
2015-12-15 14:50:51 +00:00
|
|
|
|