added gif frame updating

This commit is contained in:
fourtf 2017-02-06 17:42:28 +01:00
parent bdd03ba3fa
commit 33ba35471f
4 changed files with 85 additions and 17 deletions

View file

@ -27,6 +27,9 @@ ConcurrentMap<int, messages::LazyLoadedImage *>
ConcurrentMap<long, messages::LazyLoadedImage *> Emotes::twitchEmoteFromCache;
ConcurrentMap<QString, messages::LazyLoadedImage *> Emotes::miscImageFromCache;
QTimer Emotes::gifUpdateTimer;
bool Emotes::gifUpdateTimerInitiated(false);
int Emotes::generation = 0;
Emotes::Emotes()

View file

@ -7,6 +7,7 @@
#include <QMap>
#include <QMutex>
#include <QTimer>
namespace chatterino {
@ -82,6 +83,19 @@ public:
generation++;
}
static QTimer &
getGifUpdateTimer()
{
if (!gifUpdateTimerInitiated) {
gifUpdateTimerInitiated = true;
gifUpdateTimer.setInterval(33);
gifUpdateTimer.start();
}
return gifUpdateTimer;
}
private:
Emotes();
@ -102,6 +116,9 @@ private:
static QString getTwitchEmoteLink(long id, qreal &scale);
static QTimer gifUpdateTimer;
static bool gifUpdateTimerInitiated;
static void loadFfzEmotes();
static void loadBttvEmotes();

View file

@ -5,9 +5,12 @@
#include "ircmanager.h"
#include "windows.h"
#include <QBuffer>
#include <QImageReader>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QTimer>
#include <functional>
namespace chatterino {
@ -16,7 +19,9 @@ namespace messages {
LazyLoadedImage::LazyLoadedImage(const QString &url, qreal scale,
const QString &name, const QString &tooltip,
const QMargins &margin, bool isHat)
: pixmap(NULL)
: currentPixmap(NULL)
, allFrames()
, currentFrame(0)
, url(url)
, name(name)
, tooltip(tooltip)
@ -31,7 +36,9 @@ LazyLoadedImage::LazyLoadedImage(const QString &url, qreal scale,
LazyLoadedImage::LazyLoadedImage(QPixmap *image, qreal scale,
const QString &name, const QString &tooltip,
const QMargins &margin, bool isHat)
: pixmap(image)
: currentPixmap(image)
, allFrames()
, currentFrame(0)
, url()
, name(name)
, tooltip(tooltip)
@ -46,7 +53,6 @@ LazyLoadedImage::LazyLoadedImage(QPixmap *image, qreal scale,
void
LazyLoadedImage::loadImage()
{
// QThreadPool::globalInstance()->start(new LambdaQRunnable([=] {
QNetworkAccessManager *manager = new QNetworkAccessManager();
QUrl url(this->url);
@ -55,21 +61,54 @@ LazyLoadedImage::loadImage()
QNetworkReply *reply = manager->get(request);
QObject::connect(reply, &QNetworkReply::finished, [=] {
QPixmap *pixmap = new QPixmap();
pixmap->loadFromData(reply->readAll());
QByteArray array = reply->readAll();
QBuffer buffer(&array);
buffer.open(QIODevice::ReadOnly);
if (pixmap->isNull()) {
return;
QImage image;
QImageReader reader(&buffer);
bool first = true;
for (int index = 0; index < reader.imageCount(); ++index) {
if (reader.read(&image)) {
auto pixmap = new QPixmap(QPixmap::fromImage(image));
if (first) {
first = false;
this->currentPixmap = pixmap;
}
FrameData data;
data.duration = std::max(20, reader.nextImageDelay());
data.image = pixmap;
allFrames.push_back(data);
}
}
if (allFrames.size() > 1) {
QObject::connect(&Emotes::getGifUpdateTimer(), &QTimer::timeout,
[this] { gifUpdateTimout(); });
}
this->pixmap = pixmap;
Emotes::incGeneration();
Windows::layoutVisibleChatWidgets();
reply->deleteLater();
manager->deleteLater();
});
// }));
}
void
LazyLoadedImage::gifUpdateTimout()
{
if (this->currentFrame >= this->allFrames.size() - 1) {
this->currentFrame = 0;
this->currentPixmap = this->allFrames.at(0).image;
} else {
this->currentPixmap = this->allFrames.at(++this->currentFrame).image;
}
}
}
}

View file

@ -7,7 +7,7 @@
namespace chatterino {
namespace messages {
class LazyLoadedImage
class LazyLoadedImage : QObject
{
public:
explicit LazyLoadedImage(const QString &url, qreal scale = 1,
@ -15,7 +15,7 @@ public:
const QString &tooltip = "",
const QMargins &margin = QMargins(),
bool isHat = false);
explicit LazyLoadedImage(QPixmap *pixmap, qreal scale = 1,
explicit LazyLoadedImage(QPixmap *currentPixmap, qreal scale = 1,
const QString &name = "",
const QString &tooltip = "",
const QMargins &margin = QMargins(),
@ -29,7 +29,7 @@ public:
loadImage();
}
return pixmap;
return currentPixmap;
}
qreal
@ -77,23 +77,30 @@ public:
int
getWidth() const
{
if (pixmap == NULL) {
if (currentPixmap == NULL) {
return 16;
}
return pixmap->width();
return currentPixmap->width();
}
int
getHeight() const
{
if (pixmap == NULL) {
if (currentPixmap == NULL) {
return 16;
}
return pixmap->height();
return currentPixmap->height();
}
private:
QPixmap *pixmap;
struct FrameData {
QPixmap *image;
float duration;
};
QPixmap *currentPixmap;
std::vector<FrameData> allFrames;
int currentFrame;
QString url;
QString name;
@ -106,6 +113,8 @@ private:
bool isLoading;
void loadImage();
void gifUpdateTimout();
};
}
}