fixed image animations

This commit is contained in:
fourtf
2018-08-11 17:15:17 +02:00
parent c719bb6b74
commit c768bd9bd9
16 changed files with 229 additions and 210 deletions
+30 -9
View File
@@ -30,14 +30,27 @@ Frames::Frames()
Frames::Frames(const QVector<Frame<QPixmap>> &frames)
: items_(frames)
{
assertInGuiThread();
DebugCount::increase("images");
if (this->animated()) DebugCount::increase("animated images");
if (this->animated()) {
DebugCount::increase("animated images");
this->gifTimerConnection_ = getApp()->emotes->gifTimer.signal.connect(
[this] { this->advance(); });
}
}
Frames::~Frames()
{
assertInGuiThread();
DebugCount::decrease("images");
if (this->animated()) DebugCount::decrease("animated images");
if (this->animated()) {
DebugCount::decrease("animated images");
}
this->gifTimerConnection_.disconnect();
}
void Frames::advance()
@@ -46,6 +59,11 @@ void Frames::advance()
while (true) {
this->index_ %= this->items_.size();
if (this->index_ >= this->items_.size()) {
this->index_ = this->index_;
}
if (this->durationOffset_ > this->items_[this->index_].duration) {
this->durationOffset_ -= this->items_[this->index_].duration;
this->index_ = (this->index_ + 1) % this->items_.size();
@@ -195,12 +213,14 @@ Image::Image(const Url &url, qreal scale)
: url_(url)
, scale_(scale)
, shouldLoad_(true)
, frames_(std::make_unique<Frames>())
{
}
Image::Image(const QPixmap &pixmap, qreal scale)
: scale_(scale)
, frames_(QVector<Frame<QPixmap>>{Frame<QPixmap>{pixmap, 1}})
, frames_(std::make_unique<Frames>(
QVector<Frame<QPixmap>>{Frame<QPixmap>{pixmap, 1}}))
{
}
@@ -218,7 +238,7 @@ boost::optional<QPixmap> Image::pixmap() const
const_cast<Image *>(this)->load();
}
return this->frames_.current();
return this->frames_->current();
}
qreal Image::scale() const
@@ -235,14 +255,14 @@ bool Image::animated() const
{
assertInGuiThread();
return this->frames_.animated();
return this->frames_->animated();
}
int Image::width() const
{
assertInGuiThread();
if (auto pixmap = this->frames_.first())
if (auto pixmap = this->frames_->first())
return pixmap->width() * this->scale_;
else
return 16;
@@ -252,7 +272,7 @@ int Image::height() const
{
assertInGuiThread();
if (auto pixmap = this->frames_.first())
if (auto pixmap = this->frames_->first())
return pixmap->height() * this->scale_;
else
return 16;
@@ -277,7 +297,8 @@ void Image::load()
auto parsed = readFrames(reader, that->url());
postToThread(makeConvertCallback(parsed, [weak](auto frames) {
if (auto shared = weak.lock()) shared->frames_ = frames;
if (auto shared = weak.lock())
shared->frames_ = std::make_unique<Frames>(frames);
}));
return Success;
@@ -290,7 +311,7 @@ bool Image::operator==(const Image &other) const
{
if (this->isEmpty() && other.isEmpty()) return true;
if (!this->url_.string.isEmpty() && this->url_ == other.url_) return true;
if (this->frames_.first() == other.frames_.first()) return true;
if (this->frames_->first() == other.frames_->first()) return true;
return false;
}