diff options
-rw-r--r-- | include/Body.hpp | 2 | ||||
-rw-r--r-- | src/Body.cpp | 69 |
2 files changed, 42 insertions, 29 deletions
diff --git a/include/Body.hpp b/include/Body.hpp index 875ad61..09b477d 100644 --- a/include/Body.hpp +++ b/include/Body.hpp @@ -143,7 +143,7 @@ namespace QuickMedia { // This happens because of |draw| sets thumbnails as unreferenced at the beginning and cleans them up at the end if they are not drawn in the same function call. void draw_item(sf::RenderWindow &window, BodyItem *item, sf::Vector2f pos, sf::Vector2f size); - float get_item_height(BodyItem *item); + float get_item_height(BodyItem *item, bool load_texture = true); float get_spacing_y() const; static bool string_find_case_insensitive(const std::string &str, const std::string &substr); diff --git a/src/Body.cpp b/src/Body.cpp index ac63e0f..75706c1 100644 --- a/src/Body.cpp +++ b/src/Body.cpp @@ -361,7 +361,7 @@ namespace QuickMedia { int i = prev_selected_item; while(num_items_scrolled < selected_int_diff_abs && i < num_items) { if(items[i]->visible) { - page_scroll += (get_item_height(items[i].get()) + spacing_y); + page_scroll += (get_item_height(items[i].get(), selected_int_diff_abs < 50) + spacing_y); } ++num_items_scrolled; ++i; @@ -372,7 +372,7 @@ namespace QuickMedia { int i = prev_selected_item - 1; while(num_items_scrolled < selected_int_diff_abs && i >= 0) { if(items[i]->visible) { - page_scroll -= (get_item_height(items[i].get()) + spacing_y); + page_scroll -= (get_item_height(items[i].get(), selected_int_diff_abs < 50) + spacing_y); } ++num_items_scrolled; --i; @@ -561,28 +561,15 @@ namespace QuickMedia { void Body::draw_item(sf::RenderWindow &window, BodyItem *item, const sf::Vector2f &pos, const sf::Vector2f &size, const float item_height, const int item_index, const Json::Value &content_progress) { // TODO: Instead of generating a new hash everytime to access textures, cache the hash of the thumbnail url std::shared_ptr<ThumbnailData> item_thumbnail; - auto item_thumbnail_it = item_thumbnail_textures.find(item->thumbnail_url); - if(item_thumbnail_it == item_thumbnail_textures.end()) { - item_thumbnail = std::make_shared<ThumbnailData>(); - item_thumbnail_textures.insert(std::make_pair(item->thumbnail_url, item_thumbnail)); - } else { - item_thumbnail = item_thumbnail_it->second; - } - item_thumbnail->referenced = true; - if(draw_thumbnails) { - if(!item->thumbnail_url.empty() && item_thumbnail->loading_state == LoadingState::NOT_LOADED) { - async_image_loader.load_thumbnail(item->thumbnail_url, item->thumbnail_is_local, thumbnail_resize_target_size, program->is_tor_enabled(), item_thumbnail); - } - - if(item_thumbnail->loading_state == LoadingState::FINISHED_LOADING && item_thumbnail->image->getSize().x > 0 && item_thumbnail->image->getSize().y > 0) { - if(!item_thumbnail->texture.loadFromImage(*item_thumbnail->image)) - fprintf(stderr, "Warning: failed to load texture from image: %s\n", item->thumbnail_url.c_str()); - //item_thumbnail->texture.setSmooth(true); - //item_thumbnail->texture.generateMipmap(); - item_thumbnail->image.reset(); - item_thumbnail->loading_state = LoadingState::APPLIED_TO_TEXTURE; + auto item_thumbnail_it = item_thumbnail_textures.find(item->thumbnail_url); + if(item_thumbnail_it == item_thumbnail_textures.end()) { + item_thumbnail = std::make_shared<ThumbnailData>(); + item_thumbnail_textures.insert(std::make_pair(item->thumbnail_url, item_thumbnail)); + } else { + item_thumbnail = item_thumbnail_it->second; } + item_thumbnail->referenced = true; } sf::Vector2f item_pos; @@ -686,7 +673,7 @@ namespace QuickMedia { } } - float Body::get_item_height(BodyItem *item) { + float Body::get_item_height(BodyItem *item, bool load_texture) { float item_height = 0.0f; if(item->title_text) { item_height += item->title_text->getHeight() - 2.0f; @@ -699,14 +686,40 @@ namespace QuickMedia { } if(draw_thumbnails && !item->thumbnail_url.empty()) { float image_height = image_fallback.getSize().y; + + std::shared_ptr<ThumbnailData> item_thumbnail; auto item_thumbnail_it = item_thumbnail_textures.find(item->thumbnail_url); - if(item_thumbnail_it != item_thumbnail_textures.end()) { - std::shared_ptr<ThumbnailData> &item_thumbnail = item_thumbnail_it->second; - if(item_thumbnail->loading_state == LoadingState::APPLIED_TO_TEXTURE && item_thumbnail->texture.getNativeHandle() != 0) { - auto image_size = item_thumbnail->texture.getSize(); - image_height = std::min(image_max_height, (float)image_size.y); + if(item_thumbnail_it == item_thumbnail_textures.end()) { + if(load_texture) { + item_thumbnail = std::make_shared<ThumbnailData>(); + item_thumbnail_textures.insert(std::make_pair(item->thumbnail_url, item_thumbnail)); } + } else { + item_thumbnail = item_thumbnail_it->second; } + + if(load_texture) { + item_thumbnail->referenced = true; + + if(!item->thumbnail_url.empty() && item_thumbnail->loading_state == LoadingState::NOT_LOADED) { + async_image_loader.load_thumbnail(item->thumbnail_url, item->thumbnail_is_local, thumbnail_resize_target_size, program->is_tor_enabled(), item_thumbnail); + } + + if(item_thumbnail->loading_state == LoadingState::FINISHED_LOADING && item_thumbnail->image->getSize().x > 0 && item_thumbnail->image->getSize().y > 0) { + if(!item_thumbnail->texture.loadFromImage(*item_thumbnail->image)) + fprintf(stderr, "Warning: failed to load texture from image: %s\n", item->thumbnail_url.c_str()); + //item_thumbnail->texture.setSmooth(true); + //item_thumbnail->texture.generateMipmap(); + item_thumbnail->image.reset(); + item_thumbnail->loading_state = LoadingState::APPLIED_TO_TEXTURE; + } + } + + if(item_thumbnail && item_thumbnail->loading_state == LoadingState::APPLIED_TO_TEXTURE && item_thumbnail->texture.getNativeHandle() != 0) { + auto image_size = item_thumbnail->texture.getSize(); + image_height = std::min(image_max_height, (float)image_size.y); + } + item_height = std::max(item_height, image_height); } return item_height + padding_y * 2.0f; |