tdesktop/Telegram/SourceFiles/lottie/lottie_animation.cpp

129 lines
2.9 KiB
C++
Raw Normal View History

2019-04-27 09:12:53 +00: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 12:11:38 +00:00
#include <QtBodymovin/private/bmscene_p.h>
2019-04-27 09:12:53 +00: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 FromData(content);
}
std::unique_ptr<Animation> FromData(const QByteArray &data) {
return std::make_unique<Lottie::Animation>(data);
2019-04-27 09:12:53 +00:00
}
Animation::Animation(const QByteArray &content) {
parse(content);
}
Animation::~Animation() {
}
QImage Animation::frame(crl::time now) const {
2019-05-09 12:11:38 +00:00
if (_scene->startFrame() == _scene->endFrame()
|| _scene->width() <= 0
|| _scene->height() <= 0) {
2019-04-27 09:12:53 +00:00
return QImage();
}
auto result = QImage(
2019-05-09 12:11:38 +00:00
_scene->width(),
_scene->height(),
2019-04-27 09:12:53 +00: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 12:11:38 +00:00
const auto elapsed = int((_scene->frameRate() * position + 500) / 1000);
const auto from = _scene->startFrame();
const auto till = _scene->endFrame();
const auto frames = (till - from);
2019-04-27 09:12:53 +00:00
const auto frame = _options.loop
? (from + (elapsed % frames))
: std::min(from + elapsed, till);
2019-04-27 09:12:53 +00:00
2019-05-09 12:11:38 +00:00
_scene->updateProperties(frame);
2019-04-27 09:12:53 +00:00
LottieRasterRenderer renderer(&p);
2019-05-09 12:11:38 +00:00
_scene->render(renderer, frame);
2019-04-27 09:12:53 +00:00
}
return result;
}
int Animation::frameRate() const {
2019-05-09 12:11:38 +00:00
return _scene->frameRate();
2019-04-27 09:12:53 +00:00
}
2019-05-02 12:42:47 +00:00
crl::time Animation::duration() const {
2019-05-09 12:11:38 +00:00
return (_scene->endFrame() - _scene->startFrame()) * crl::time(1000) / _scene->frameRate();
2019-05-02 12:42:47 +00:00
}
2019-04-27 09:12:53 +00: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 12:11:38 +00:00
_scene = std::make_unique<BMScene>();
_scene->parse(root);
2019-04-27 09:12:53 +00:00
}
} // namespace Lottie