support animated guild icon

This commit is contained in:
ouwou
2020-12-21 19:08:44 -05:00
parent 486d184c83
commit d8ff05ddf8
8 changed files with 77 additions and 11 deletions

View File

@@ -20,9 +20,19 @@ Glib::RefPtr<Gdk::Pixbuf> ImageManager::ReadFileToPixbuf(std::string path) {
});
loader->write(static_cast<const guint8 *>(data.data()), data.size());
loader->close();
auto buf = loader->get_pixbuf();
return loader->get_pixbuf();
}
return buf;
Glib::RefPtr<Gdk::PixbufAnimation> ImageManager::ReadFileToPixbufAnimation(std::string path, int w, int h) {
const auto &data = ReadWholeFile(path);
if (data.size() == 0) return Glib::RefPtr<Gdk::PixbufAnimation>(nullptr);
auto loader = Gdk::PixbufLoader::create();
loader->signal_size_prepared().connect([&loader, w, h](int, int) {
loader->set_size(w, h);
});
loader->write(static_cast<const guint8 *>(data.data()), data.size());
loader->close();
return loader->get_animation();
}
void ImageManager::LoadFromURL(std::string url, callback_type cb) {
@@ -35,12 +45,28 @@ void ImageManager::LoadFromURL(std::string url, callback_type cb) {
m_cb_queue.push([signal, buf]() { signal.emit(buf); });
m_cb_dispatcher.emit();
m_cb_mutex.unlock();
} catch (std::exception &e) {
} catch (const std::exception &e) {
fprintf(stderr, "err loading pixbuf from %s: %s\n", path.c_str(), e.what());
}
});
}
void ImageManager::LoadAnimationFromURL(std::string url, int w, int h, callback_anim_type cb) {
sigc::signal<void(Glib::RefPtr<Gdk::PixbufAnimation>)> signal;
signal.connect(cb);
m_cache.GetFileFromURL(url, [this, url, signal, w, h](std::string path) {
try {
auto buf = ReadFileToPixbufAnimation(path, w, h);
m_cb_mutex.lock();
m_cb_queue.push([signal, buf]() { signal.emit(buf); });
m_cb_dispatcher.emit();
m_cb_mutex.unlock();
} catch (const std::exception &e) {
fprintf(stderr, "err loading pixbuf animation from %s: %s\n", path.c_str(), e.what());
}
});
}
void ImageManager::Prefetch(std::string url) {
m_cache.GetFileFromURL(url, [](const auto &) {});
}
@@ -60,6 +86,14 @@ Glib::RefPtr<Gdk::Pixbuf> ImageManager::GetFromURLIfCached(std::string url) {
return Glib::RefPtr<Gdk::Pixbuf>(nullptr);
}
Glib::RefPtr<Gdk::PixbufAnimation> ImageManager::GetAnimationFromURLIfCached(std::string url, int w, int h) {
std::string path = m_cache.GetPathIfCached(url);
if (path != "")
return ReadFileToPixbufAnimation(path, w, h);
return Glib::RefPtr<Gdk::PixbufAnimation>(nullptr);
}
Glib::RefPtr<Gdk::Pixbuf> ImageManager::GetPlaceholder(int size) {
std::string name = "/placeholder" + std::to_string(size);
if (m_pixs.find(name) != m_pixs.end())