2019-04-27 13:12:53 +04:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "lottie/lottie_animation.h"
|
|
|
|
|
|
|
|
#include "lottie/lottie_frame_renderer.h"
|
|
|
|
#include "base/algorithm.h"
|
|
|
|
|
|
|
|
#include <range/v3/view/reverse.hpp>
|
|
|
|
#include <QtMath>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonValue>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QPointF>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QImage>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QMetaObject>
|
|
|
|
#include <QLoggingCategory>
|
|
|
|
#include <QThread>
|
|
|
|
#include <math.h>
|
|
|
|
|
2019-05-09 15:11:38 +03:00
|
|
|
#include <QtBodymovin/private/bmscene_p.h>
|
2019-04-27 13:12:53 +04:00
|
|
|
|
|
|
|
#include "rasterrenderer/lottierasterrenderer.h"
|
|
|
|
|
|
|
|
namespace Lottie {
|
|
|
|
|
|
|
|
bool ValidateFile(const QString &path) {
|
|
|
|
if (!path.endsWith(qstr(".json"), Qt::CaseInsensitive)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Animation> FromFile(const QString &path) {
|
|
|
|
if (!path.endsWith(qstr(".json"), Qt::CaseInsensitive)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto f = QFile(path);
|
|
|
|
if (!f.open(QIODevice::ReadOnly)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const auto content = f.readAll();
|
|
|
|
if (content.isEmpty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return std::make_unique<Lottie::Animation>(content);
|
|
|
|
}
|
|
|
|
|
|
|
|
Animation::Animation(const QByteArray &content) {
|
|
|
|
parse(content);
|
|
|
|
}
|
|
|
|
|
|
|
|
Animation::~Animation() {
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage Animation::frame(crl::time now) const {
|
2019-05-09 15:11:38 +03:00
|
|
|
if (_scene->startFrame() == _scene->endFrame()
|
|
|
|
|| _scene->width() <= 0
|
|
|
|
|| _scene->height() <= 0) {
|
2019-04-27 13:12:53 +04:00
|
|
|
return QImage();
|
|
|
|
}
|
|
|
|
auto result = QImage(
|
2019-05-09 15:11:38 +03:00
|
|
|
_scene->width(),
|
|
|
|
_scene->height(),
|
2019-04-27 13:12:53 +04:00
|
|
|
QImage::Format_ARGB32_Premultiplied);
|
|
|
|
result.fill(Qt::transparent);
|
|
|
|
|
|
|
|
{
|
|
|
|
QPainter p(&result);
|
|
|
|
p.setRenderHints(QPainter::Antialiasing);
|
|
|
|
p.setRenderHints(QPainter::SmoothPixmapTransform);
|
|
|
|
|
|
|
|
const auto position = now;
|
2019-05-09 15:11:38 +03:00
|
|
|
const auto elapsed = int((_scene->frameRate() * position + 500) / 1000);
|
|
|
|
const auto frames = (_scene->endFrame() - _scene->startFrame());
|
2019-04-27 13:12:53 +04:00
|
|
|
const auto frame = _options.loop
|
2019-05-09 15:11:38 +03:00
|
|
|
? (_scene->startFrame() + (elapsed % frames))
|
|
|
|
: std::min(_scene->startFrame() + elapsed, _scene->endFrame());
|
2019-04-27 13:12:53 +04:00
|
|
|
|
2019-05-09 15:11:38 +03:00
|
|
|
_scene->updateProperties(frame);
|
2019-04-27 13:12:53 +04:00
|
|
|
|
|
|
|
LottieRasterRenderer renderer(&p);
|
2019-05-09 15:11:38 +03:00
|
|
|
_scene->render(renderer, frame);
|
2019-04-27 13:12:53 +04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Animation::frameRate() const {
|
2019-05-09 15:11:38 +03:00
|
|
|
return _scene->frameRate();
|
2019-04-27 13:12:53 +04:00
|
|
|
}
|
|
|
|
|
2019-05-02 16:42:47 +04:00
|
|
|
crl::time Animation::duration() const {
|
2019-05-09 15:11:38 +03:00
|
|
|
return (_scene->endFrame() - _scene->startFrame()) * crl::time(1000) / _scene->frameRate();
|
2019-05-02 16:42:47 +04:00
|
|
|
}
|
|
|
|
|
2019-04-27 13:12:53 +04:00
|
|
|
void Animation::play(const PlaybackOptions &options) {
|
|
|
|
_options = options;
|
|
|
|
_started = crl::now();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Animation::parse(const QByteArray &content) {
|
|
|
|
const auto document = QJsonDocument::fromJson(content);
|
|
|
|
const auto root = document.object();
|
|
|
|
|
|
|
|
if (root.empty()) {
|
|
|
|
_failed = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-09 15:11:38 +03:00
|
|
|
_scene = std::make_unique<BMScene>();
|
|
|
|
_scene->parse(root);
|
2019-04-27 13:12:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Lottie
|