tdesktop/Telegram/SourceFiles/gui/animation.cpp

290 lines
6.9 KiB
C++
Raw Normal View History

/*
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.
2015-10-03 16:16:42 +03:00
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
2015-10-03 16:16:42 +03:00
Copyright (c) 2014-2015 John Preston, https://desktop.telegram.org
*/
#include "stdafx.h"
#include "animation.h"
2015-04-19 13:29:19 +03:00
#include "mainwidget.h"
#include "window.h"
namespace {
2015-12-08 15:33:37 +03:00
AnimationManager *_manager = 0;
};
namespace anim {
float64 linear(const float64 &delta, const float64 &dt) {
return delta * dt;
}
float64 sineInOut(const float64 &delta, const float64 &dt) {
return -(delta / 2) * (cos(M_PI * dt) - 1);
}
float64 halfSine(const float64 &delta, const float64 &dt) {
return delta * sin(M_PI * dt / 2);
}
float64 easeOutBack(const float64 &delta, const float64 &dt) {
static const float64 s = 1.70158;
const float64 t = dt - 1;
return delta * (t * t * ((s + 1) * t + s) + 1);
}
float64 easeInCirc(const float64 &delta, const float64 &dt) {
return -delta * (sqrt(1 - dt * dt) - 1);
}
float64 easeOutCirc(const float64 &delta, const float64 &dt) {
const float64 t = dt - 1;
return delta * sqrt(1 - t * t);
}
float64 easeInCubic(const float64 &delta, const float64 &dt) {
return delta * dt * dt * dt;
}
float64 easeOutCubic(const float64 &delta, const float64 &dt) {
const float64 t = dt - 1;
return delta * (t * t * t + 1);
}
float64 easeInQuint(const float64 &delta, const float64 &dt) {
const float64 t2 = dt * dt;
return delta * t2 * t2 * dt;
}
float64 easeOutQuint(const float64 &delta, const float64 &dt) {
const float64 t = dt - 1, t2 = t * t;
return delta * (t2 * t2 * t + 1);
}
2015-12-08 15:33:37 +03:00
void startManager() {
delete _manager;
_manager = new AnimationManager();
}
2015-12-08 15:33:37 +03:00
void stopManager() {
delete _manager;
_manager = 0;
}
2015-12-08 15:33:37 +03:00
}
2015-12-08 15:33:37 +03:00
void Animation::start() {
if (!_manager) return;
2015-12-08 15:33:37 +03:00
_cb->start();
_manager->start(this);
_animating = true;
}
2015-12-08 15:33:37 +03:00
void Animation::stop() {
if (!_manager) return;
_animating = false;
_manager->stop(this);
}
2015-04-19 13:29:19 +03:00
2015-12-08 15:33:37 +03:00
void AnimatedGif::step_frame(float64 ms, bool timer) {
2015-04-19 13:29:19 +03:00
int32 f = frame;
2015-05-21 15:08:05 +03:00
while (f < images.size() && ms > delays[f]) {
2015-04-19 13:29:19 +03:00
++f;
2015-05-21 15:08:05 +03:00
if (f == images.size() && images.size() < framesCount) {
2015-04-19 13:29:19 +03:00
if (reader->read(&img)) {
int64 d = reader->nextImageDelay(), delay = delays[f - 1];
if (!d) d = 1;
delay += d;
2015-05-21 15:08:05 +03:00
if (img.size() != QSize(w, h)) img = img.scaled(w, h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
images.push_back(img);
frames.push_back(QPixmap());
2015-04-19 13:29:19 +03:00
delays.push_back(delay);
2015-05-21 15:08:05 +03:00
for (int32 i = 0; i < images.size(); ++i) {
if (!images[i].isNull() || !frames[i].isNull()) {
images[i] = QImage();
2015-04-19 13:29:19 +03:00
frames[i] = QPixmap();
break;
}
}
} else {
2015-05-21 15:08:05 +03:00
framesCount = images.size();
2015-04-19 13:29:19 +03:00
}
}
2015-05-21 15:08:05 +03:00
if (f == images.size()) {
2015-04-19 13:29:19 +03:00
if (!duration) {
duration = delays.isEmpty() ? 1 : delays.back();
}
f = 0;
for (int32 i = 0, s = delays.size() - 1; i <= s; ++i) {
delays[i] += duration;
}
2015-05-21 15:08:05 +03:00
if (images[f].isNull()) {
2015-04-19 13:29:19 +03:00
QString fname = reader->fileName();
delete reader;
reader = new QImageReader(fname);
}
}
2015-05-21 15:08:05 +03:00
if (images[f].isNull() && reader->read(&img)) {
if (img.size() != QSize(w, h)) img = img.scaled(w, h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
images[f] = img;
frames[f] = QPixmap();
for (int32 i = 0; i < f; ++i) {
if (!images[i].isNull() || !frames[i].isNull()) {
images[i] = QImage();
frames[i] = QPixmap();
break;
}
}
2015-04-19 13:29:19 +03:00
}
}
if (frame != f) {
frame = f;
2015-12-08 15:33:37 +03:00
if (timer) {
if (msg) {
Ui::redrawHistoryItem(msg);
2015-12-08 15:33:37 +03:00
} else {
emit updated();
}
2015-04-19 13:29:19 +03:00
}
}
}
void AnimatedGif::start(HistoryItem *row, const FileLocation &f) {
2015-04-19 13:29:19 +03:00
stop();
file = new FileLocation(f);
if (!file->accessEnable()) {
stop();
return;
}
access = true;
reader = new QImageReader(file->name());
2015-04-19 13:29:19 +03:00
if (!reader->canRead() || !reader->supportsAnimation()) {
stop();
return;
}
QSize s = reader->size();
w = s.width();
h = s.height();
framesCount = reader->imageCount();
if (!w || !h || !framesCount) {
stop();
return;
}
frames.reserve(framesCount);
2015-05-21 15:08:05 +03:00
images.reserve(framesCount);
2015-04-19 13:29:19 +03:00
delays.reserve(framesCount);
int32 sizeLeft = MediaViewImageSizeLimit, delay = 0;
for (bool read = reader->read(&img); read; read = reader->read(&img)) {
sizeLeft -= w * h * 4;
2015-05-21 15:08:05 +03:00
if (img.size() != s) img = img.scaled(w, h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
images.push_back(img);
frames.push_back(QPixmap());
2015-04-19 13:29:19 +03:00
int32 d = reader->nextImageDelay();
if (!d) d = 1;
delay += d;
delays.push_back(delay);
if (sizeLeft < 0) break;
}
msg = row;
2015-12-08 15:33:37 +03:00
_a_frames.start();
2015-04-19 13:29:19 +03:00
if (msg) {
msg->initDimensions();
if (App::main()) App::main()->itemResized(msg, true);
}
}
void AnimatedGif::stop(bool onItemRemoved) {
if (file) {
if (access) {
file->accessDisable();
}
delete file;
file = 0;
}
access = false;
2015-04-19 13:29:19 +03:00
if (isNull()) return;
delete reader;
reader = 0;
HistoryItem *row = msg;
msg = 0;
frames.clear();
2015-05-21 15:08:05 +03:00
images.clear();
2015-04-19 13:29:19 +03:00
delays.clear();
w = h = frame = framesCount = duration = 0;
2015-12-08 15:33:37 +03:00
_a_frames.stop();
2015-04-19 13:29:19 +03:00
if (row && !onItemRemoved) {
row->initDimensions();
if (App::main()) App::main()->itemResized(row, true);
}
}
2015-05-21 15:08:05 +03:00
const QPixmap &AnimatedGif::current(int32 width, int32 height, bool rounded) {
if (!width) width = w;
if (!height) height = h;
if ((frames[frame].isNull() || frames[frame].width() != width || frames[frame].height() != height) && !images[frame].isNull()) {
QImage img = images[frame];
if (img.width() != width || img.height() != height) img = img.scaled(width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
if (rounded) imageRound(img);
frames[frame] = QPixmap::fromImage(img, Qt::ColorOnly);
frames[frame].setDevicePixelRatio(cRetinaFactor());
}
return frames[frame];
}
ClipReader::ClipReader(const FileLocation &location, const QByteArray &data) : _state(ClipStopped)
, _location(location.isEmpty() ? 0 : new FileLocation(location))
, _data(data)
, _width(0)
, _height(0)
, _rounded(false)
, _currentDisplayed(true) {
}
void ClipReader::start(int32 framew, int32 frameh, bool rounded) {
_rounded = rounded;
}
const QPixmap &ClipReader::current(int32 framew, int32 frameh) {
_currentDisplayed = true;
return _current;
}
ClipState ClipReader::state() const {
return _state;
}
ClipReader::~ClipReader() {
delete _location;
setBadPointer(_location);
}