diff --git a/UI/Store.cpp b/UI/Store.cpp index e744b99acf..2297539369 100644 --- a/UI/Store.cpp +++ b/UI/Store.cpp @@ -20,6 +20,7 @@ #include "Common/UI/Screen.h" #include "Common/UI/Context.h" #include "Common/UI/ViewGroup.h" +#include "Common/UI/IconCache.h" #include "Common/Render/DrawBuffer.h" #include "Common/Log.h" @@ -27,6 +28,7 @@ #include "Common/Data/Format/JSONReader.h" #include "Common/StringUtils.h" #include "Common/Render/ManagedTexture.h" +#include "Common/Net/NetBuffer.h" #include "Core/Config.h" #include "Core/System.h" #include "Core/Util/GameManager.h" @@ -51,8 +53,18 @@ std::string ResolveUrl(std::string baseUrl, std::string url) { class HttpImageFileView : public UI::View { public: - HttpImageFileView(http::Downloader *downloader, const std::string &path, UI::ImageSizeMode sizeMode = UI::IS_DEFAULT, UI::LayoutParams *layoutParams = 0) - : UI::View(layoutParams), path_(path), sizeMode_(sizeMode), downloader_(downloader) {} + HttpImageFileView(http::Downloader *downloader, const std::string &path, UI::ImageSizeMode sizeMode = UI::IS_DEFAULT, bool useIconCache = true, UI::LayoutParams *layoutParams = nullptr) + : UI::View(layoutParams), path_(path), sizeMode_(sizeMode), downloader_(downloader), useIconCache_(useIconCache) { + + if (useIconCache && !g_iconCache.Contains(path_)) { + const char *acceptMime = "image/png, image/jpeg, image/*; q=0.9, */*; q=0.8"; + downloader_->StartDownloadWithCallback(path_, Path(), [&](http::Download &download) { + std::string data; + download.buffer().TakeAll(&data); + g_iconCache.InsertIcon(path_, IconFormat::PNG, std::move(data)); + }, acceptMime); + } + } ~HttpImageFileView() { if (download_) @@ -76,7 +88,8 @@ private: void DownloadCompletedCallback(http::Download &download); bool canFocus_ = false; - std::string path_; + bool useIconCache_ = false; + std::string path_; // or cache key uint32_t color_ = 0xFFFFFFFF; UI::ImageSizeMode sizeMode_; http::Downloader *downloader_; @@ -97,21 +110,32 @@ void HttpImageFileView::GetContentDimensions(const UIContext &dc, float &w, floa break; case UI::IS_DEFAULT: default: - if (texture_) { - float texw = (float)texture_->Width(); - float texh = (float)texture_->Height(); - w = texw; - h = texh; + if (useIconCache_) { + int width, height; + if (g_iconCache.GetDimensions(path_, &width, &height)) { + w = width; + h = height; + } else { + w = 16; + h = 16; + } } else { - w = 16; - h = 16; + if (texture_) { + float texw = (float)texture_->Width(); + float texh = (float)texture_->Height(); + w = texw; + h = texh; + } else { + w = 16; + h = 16; + } } break; } } void HttpImageFileView::SetFilename(std::string filename) { - if (path_ != filename) { + if (!useIconCache_ && path_ != filename) { textureFailed_ = false; path_ = filename; texture_.reset(nullptr); @@ -132,19 +156,22 @@ void HttpImageFileView::DownloadCompletedCallback(http::Download &download) { void HttpImageFileView::Draw(UIContext &dc) { using namespace Draw; - if (!texture_ && !textureFailed_ && !path_.empty() && !download_) { - auto cb = std::bind(&HttpImageFileView::DownloadCompletedCallback, this, std::placeholders::_1); - const char *acceptMime = "image/png, image/jpeg, image/*; q=0.9, */*; q=0.8"; - download_ = downloader_->StartDownloadWithCallback(path_, Path(), cb, acceptMime); - download_->SetHidden(true); - } - if (!textureData_.empty()) { - texture_ = CreateTextureFromFileData(dc.GetDrawContext(), (const uint8_t *)(textureData_.data()), (int)textureData_.size(), DETECT, false, "store_icon"); - if (!texture_) - textureFailed_ = true; - textureData_.clear(); - download_.reset(); + if (!useIconCache_) { + if (!texture_ && !textureFailed_ && !path_.empty() && !download_) { + auto cb = std::bind(&HttpImageFileView::DownloadCompletedCallback, this, std::placeholders::_1); + const char *acceptMime = "image/png, image/jpeg, image/*; q=0.9, */*; q=0.8"; + download_ = downloader_->StartDownloadWithCallback(path_, Path(), cb, acceptMime); + download_->SetHidden(true); + } + + if (!textureData_.empty()) { + texture_ = CreateTextureFromFileData(dc.GetDrawContext(), (const uint8_t *)(textureData_.data()), (int)textureData_.size(), DETECT, false, "store_icon"); + if (!texture_) + textureFailed_ = true; + textureData_.clear(); + download_.reset(); + } } if (HasFocus()) { @@ -152,9 +179,16 @@ void HttpImageFileView::Draw(UIContext &dc) { } // TODO: involve sizemode - if (texture_) { - float tw = texture_->Width(); - float th = texture_->Height(); + Draw::Texture *texture = nullptr; + if (useIconCache_) { + texture = g_iconCache.BindIconTexture(&dc, path_); + } else { + texture = texture_->GetTexture(); + } + + if (texture) { + float tw = texture->Width(); + float th = texture->Height(); float x = bounds_.x; float y = bounds_.y; @@ -172,7 +206,7 @@ void HttpImageFileView::Draw(UIContext &dc) { } dc.Flush(); - dc.GetDrawContext()->BindTexture(0, texture_->GetTexture()); + dc.GetDrawContext()->BindTexture(0, texture); dc.Draw()->Rect(x, y, w, h, color_); dc.Flush(); dc.RebindTexture();