diff options
Diffstat (limited to 'src/Body.cpp')
-rw-r--r-- | src/Body.cpp | 45 |
1 files changed, 23 insertions, 22 deletions
diff --git a/src/Body.cpp b/src/Body.cpp index 0af6407..1ea5be2 100644 --- a/src/Body.cpp +++ b/src/Body.cpp @@ -187,7 +187,7 @@ namespace QuickMedia { } void Body::set_selected_item(int item) { - assert(item >= 0 && item < (int)items.size()); + //assert(item >= 0 && item < (int)items.size()); selected_item = item; prev_selected_item = selected_item; clamp_selection(); @@ -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(), selected_int_diff_abs < 50) + spacing_y); + page_scroll += (get_item_height(items[i].get(), size.x, 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(), selected_int_diff_abs < 50) + spacing_y); + page_scroll -= (get_item_height(items[i].get(), size.x, selected_int_diff_abs < 50) + spacing_y); } ++num_items_scrolled; --i; @@ -380,8 +380,7 @@ namespace QuickMedia { prev_selected_item = selected_item; } - update_dirty_state(items[selected_item].get(), size); - float selected_item_height = get_item_height(items[selected_item].get()) + spacing_y; + float selected_item_height = get_item_height(items[selected_item].get(), size.x) + spacing_y; if(page_scroll > size.y - selected_item_height) { page_scroll = size.y - selected_item_height; } else if(page_scroll < 0.0f) { @@ -400,10 +399,8 @@ namespace QuickMedia { if(!item->visible) continue; - update_dirty_state(item.get(), size); item->last_drawn_time = elapsed_time_sec; - - float item_height = get_item_height(item.get()); + float item_height = get_item_height(item.get(), size.x); prev_pos.y -= (item_height + spacing_y); if(prev_pos.y + item_height + spacing_y <= start_y) @@ -430,9 +427,8 @@ namespace QuickMedia { break; } - update_dirty_state(item.get(), size); item->last_drawn_time = elapsed_time_sec; - float item_height = get_item_height(item.get()); + float item_height = get_item_height(item.get(), size.x); // This is needed here rather than above the loop, since update_dirty_text cant be called inside scissor because it corrupts the text for some reason glEnable(GL_SCISSOR_TEST); @@ -469,7 +465,7 @@ namespace QuickMedia { } } - void Body::update_dirty_state(BodyItem *body_item, sf::Vector2f size) { + void Body::update_dirty_state(BodyItem *body_item, float width) { if(body_item->dirty) { body_item->dirty = false; // TODO: Find a way to optimize fromUtf8 @@ -477,7 +473,7 @@ namespace QuickMedia { if(body_item->title_text) body_item->title_text->setString(std::move(str)); else - body_item->title_text = std::make_unique<Text>(std::move(str), font, cjk_font, 16, size.x - 50 - image_padding_x * 2.0f); + body_item->title_text = std::make_unique<Text>(std::move(str), font, cjk_font, 16, width - 50 - image_padding_x * 2.0f); body_item->title_text->setFillColor(body_item->get_title_color()); body_item->title_text->updateGeometry(); } @@ -488,7 +484,7 @@ namespace QuickMedia { if(body_item->description_text) body_item->description_text->setString(std::move(str)); else - body_item->description_text = std::make_unique<Text>(std::move(str), font, cjk_font, 14, size.x - 50 - image_padding_x * 2.0f); + body_item->description_text = std::make_unique<Text>(std::move(str), font, cjk_font, 14, width - 50 - image_padding_x * 2.0f); body_item->description_text->setFillColor(body_item->get_description_color()); body_item->description_text->updateGeometry(); } @@ -499,7 +495,7 @@ namespace QuickMedia { if(body_item->author_text) body_item->author_text->setString(std::move(str)); else - body_item->author_text = std::make_unique<Text>(std::move(str), bold_font, cjk_font, 14, size.x - 50 - image_padding_x * 2.0f); + body_item->author_text = std::make_unique<Text>(std::move(str), bold_font, cjk_font, 14, width - 50 - image_padding_x * 2.0f); body_item->author_text->setFillColor(body_item->get_author_color()); body_item->author_text->updateGeometry(); } @@ -566,7 +562,7 @@ namespace QuickMedia { } void Body::draw_item(sf::RenderWindow &window, BodyItem *item, sf::Vector2f pos, sf::Vector2f size, bool include_embedded_item) { - update_dirty_state(item, size); + update_dirty_state(item, size.x); item->last_drawn_time = draw_timer.getElapsedTime().asMilliseconds(); sf::Vector2u window_size = window.getSize(); glEnable(GL_SCISSOR_TEST); @@ -622,7 +618,7 @@ namespace QuickMedia { } float text_offset_x = padding_x; - if(draw_thumbnails && !item->thumbnail_url.empty()) { + if(draw_thumbnails && item_thumbnail) { double elapsed_time_thumbnail = 0.0; if(item_thumbnail->loading_state == LoadingState::APPLIED_TO_TEXTURE) elapsed_time_thumbnail = item_thumbnail->texture_applied_time.getElapsedTime().asSeconds(); //thumbnail_fade_duration_sec @@ -678,7 +674,7 @@ namespace QuickMedia { auto new_loading_icon_size = clamp_to_size(loading_icon_size, content_size); loading_icon.setPosition(item_pos + sf::Vector2f(image_padding_x, padding_y) + (content_size * 0.5f)); loading_icon.setScale(get_ratio(loading_icon_size, new_loading_icon_size)); - loading_icon.setRotation(-elapsed_time_sec * 400.0); + loading_icon.setRotation(elapsed_time_sec * 400.0); loading_icon.setColor(sf::Color(255, 255, 255, fallback_fade_alpha)); window.draw(loading_icon); @@ -710,8 +706,9 @@ namespace QuickMedia { } if(include_embedded_item && item->embedded_item_status != FetchStatus::NONE) { - float embedded_item_height = item->embedded_item ? get_item_height(item->embedded_item.get(), true, false) : (embedded_item_load_text.getLocalBounds().height + embedded_item_padding_y * 2.0f); const float border_width = 4.0f; + const float embedded_item_width = std::floor(size.x - text_offset_x - border_width - padding_x); + float embedded_item_height = item->embedded_item ? get_item_height(item->embedded_item.get(), embedded_item_width, true, false) : (embedded_item_load_text.getLocalBounds().height + embedded_item_padding_y * 2.0f); sf::RectangleShape border_left(sf::Vector2f(border_width, std::floor(embedded_item_height))); border_left.setFillColor(sf::Color::White); border_left.setPosition(std::floor(item_pos.x + text_offset_x), std::floor(item_pos.y + 4.0f)); @@ -719,7 +716,7 @@ namespace QuickMedia { if(item->embedded_item) { sf::Vector2f embedded_item_pos(std::floor(item_pos.x + text_offset_x + border_width + padding_x), std::floor(item_pos.y + embedded_item_padding_y + 4.0f)); - sf::Vector2f embedded_item_size(std::floor(size.x - text_offset_x - border_width - padding_x), embedded_item_height); + sf::Vector2f embedded_item_size(embedded_item_width, embedded_item_height); draw_item(window, item->embedded_item.get(), embedded_item_pos, embedded_item_size, false); } else { embedded_item_load_text.setString(embedded_item_status_to_string(item->embedded_item_status)); @@ -770,7 +767,9 @@ namespace QuickMedia { } } - float Body::get_item_height(BodyItem *item, bool load_texture, bool include_embedded_item) { + float Body::get_item_height(BodyItem *item, float width, bool load_texture, bool include_embedded_item) { + if(load_texture) + update_dirty_state(item, width); float item_height = 0.0f; if(item->title_text) { item_height += item->title_text->getHeight() - 2.0f; @@ -780,7 +779,7 @@ namespace QuickMedia { } if(include_embedded_item && item->embedded_item_status != FetchStatus::NONE) { if(item->embedded_item) - item_height += (get_item_height(item->embedded_item.get(), load_texture, false) + 4.0f + embedded_item_padding_y * 2.0f); + item_height += (get_item_height(item->embedded_item.get(), width, load_texture, false) + 4.0f + embedded_item_padding_y * 2.0f); else item_height += (embedded_item_load_text.getLocalBounds().height + 4.0f + embedded_item_padding_y * 2.0f); } @@ -802,7 +801,7 @@ namespace QuickMedia { item_thumbnail = item_thumbnail_it->second; } - if(load_texture) { + if(load_texture && item_thumbnail) { item_thumbnail->referenced = true; if(!item->thumbnail_url.empty() && item_thumbnail->loading_state == LoadingState::NOT_LOADED) @@ -864,6 +863,8 @@ namespace QuickMedia { body_item->visible = string_find_case_insensitive(body_item->get_title(), text); if(!body_item->visible && !body_item->get_description().empty()) body_item->visible = string_find_case_insensitive(body_item->get_description(), text); + if(!body_item->visible && !body_item->get_author().empty()) + body_item->visible = string_find_case_insensitive(body_item->get_author(), text); } bool Body::no_items_visible() const { |