From 275cb68f08f4fa5bb5a970ea016c7c45269964c9 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 17 Jun 2021 05:13:42 +0200 Subject: Better body item size handling, better styling when touch is enabled or shaders fail to load --- include/Body.hpp | 2 +- src/Body.cpp | 35 +++++++++++++---------------------- src/QuickMedia.cpp | 2 ++ src/RoundedRectangle.cpp | 9 +++++---- 4 files changed, 21 insertions(+), 27 deletions(-) diff --git a/include/Body.hpp b/include/Body.hpp index 7d19cf5..8fffcb0 100644 --- a/include/Body.hpp +++ b/include/Body.hpp @@ -151,7 +151,7 @@ namespace QuickMedia { std::string post_number; void *userdata; // Not managed, should be deallocated by whoever sets this float loaded_height = 0.0f; - float loaded_image_height = 0.0f; + sf::Vector2f loaded_image_size; float loaded_content_height = 0.0f; FetchStatus embedded_item_status = FetchStatus::NONE; // Important! Should refer to a new BodyItem, not one that already exists in the body. diff --git a/src/Body.cpp b/src/Body.cpp index 67c5c8e..fa56ded 100644 --- a/src/Body.cpp +++ b/src/Body.cpp @@ -125,7 +125,7 @@ namespace QuickMedia { post_number = other.post_number; userdata = other.userdata; loaded_height = other.loaded_height; - loaded_image_height = other.loaded_image_height; + loaded_image_size = other.loaded_image_size; loaded_content_height = other.loaded_content_height; embedded_item_status = other.embedded_item_status; if(other.embedded_item) { @@ -790,7 +790,7 @@ namespace QuickMedia { } void Body::update_dirty_state(BodyItem *body_item, float width) { - if(body_item->dirty) { + if(body_item->dirty || (body_size_changed && body_item->title_text)) { body_item->dirty = false; // TODO: Find a way to optimize fromUtf8 sf::String str = sf::String::fromUtf8(body_item->get_title().begin(), body_item->get_title().end()); @@ -804,7 +804,7 @@ namespace QuickMedia { body_item->title_text->updateGeometry(); } - if(body_item->dirty_description) { + if(body_item->dirty_description || (body_size_changed && body_item->description_text)) { body_item->dirty_description = false; sf::String str = sf::String::fromUtf8(body_item->get_description().begin(), body_item->get_description().end()); if(body_item->description_text) { @@ -817,7 +817,7 @@ namespace QuickMedia { body_item->description_text->updateGeometry(); } - if(body_item->dirty_author) { + if(body_item->dirty_author || (body_size_changed && body_item->author_text)) { body_item->dirty_author = false; sf::String str = sf::String::fromUtf8(body_item->get_author().begin(), body_item->get_author().end()); if(body_item->author_text) { @@ -830,7 +830,7 @@ namespace QuickMedia { body_item->author_text->updateGeometry(); } - if(body_item->dirty_timestamp) { + if(body_item->dirty_timestamp || (body_size_changed && body_item->timestamp_text)) { body_item->dirty_timestamp = false; if(body_item->get_timestamp() != 0) { @@ -1215,21 +1215,18 @@ namespace QuickMedia { float text_offset_y = 0.0f; if(item->author_text) { item->author_text->setPosition(text_pos); - item->author_text->setMaxWidth(card_max_image_size.x); item->author_text->draw(window); text_offset_y += item->author_text->getHeight(); } if(item->title_text) { item->title_text->setPosition(text_pos + sf::Vector2f(0.0f, text_offset_y)); - item->title_text->setMaxWidth(card_max_image_size.x); item->title_text->draw(window); text_offset_y += item->title_text->getHeight(); } if(item->description_text) { item->description_text->setPosition(text_pos + sf::Vector2f(0.0f, text_offset_y)); - item->description_text->setMaxWidth(card_max_image_size.x); item->description_text->draw(window); text_offset_y += item->description_text->getHeight(); } @@ -1367,7 +1364,7 @@ namespace QuickMedia { loading_icon.setScale(get_ratio(loading_icon_size, new_loading_icon_size)); loading_icon.setRotation(elapsed_time_sec * 400.0); window.draw(loading_icon); - text_offset_x += body_spacing[body_theme].image_padding_x + content_size.x; + text_offset_x += body_spacing[body_theme].image_padding_x + item->loaded_image_size.x; } } else if(item->thumbnail_size.x > 0) { text_offset_x += body_spacing[body_theme].image_padding_x + thumbnail_size.x; @@ -1379,7 +1376,6 @@ namespace QuickMedia { const float timestamp_text_y = std::floor(item_pos.y + padding_y - text_offset_y); if(item->author_text && !merge_with_previous) { item->author_text->setPosition(std::floor(item_pos.x + text_offset_x), std::floor(item_pos.y + padding_y - text_offset_y)); - item->author_text->setMaxWidth(text_max_width); item->author_text->draw(window); sf::Vector2f replies_text_pos = item->author_text->getPosition() + sf::Vector2f(0.0f, 5.0f); @@ -1424,7 +1420,6 @@ namespace QuickMedia { //window.draw(title_text); if(item->title_text) { item->title_text->setPosition(std::floor(item_pos.x + text_offset_x), std::floor(item_pos.y + padding_y - text_offset_y)); - item->title_text->setMaxWidth(text_max_width); item->title_text->draw(window); item_pos.y += item->title_text->getHeight() - 2.0f + std::floor(3.0f * get_ui_scale()); } @@ -1432,7 +1427,6 @@ namespace QuickMedia { if(item->description_text) { float height_offset = 0.0f; item->description_text->setPosition(std::floor(item_pos.x + text_offset_x), std::floor(item_pos.y + padding_y - text_offset_y) + height_offset); - item->description_text->setMaxWidth(text_max_width); item->description_text->draw(window); item_pos.y += item->description_text->getHeight() - 2.0f; } @@ -1444,7 +1438,6 @@ namespace QuickMedia { // TODO: Fix first row wrap-around for(int i = 0; i < item->reactions.size(); ++i) { auto &reaction = item->reactions[i]; - reaction.text->setMaxWidth(text_max_width); reaction.text->updateGeometry(); reaction_max_height = std::max(reaction_max_height, reaction.text->getHeight()); reaction.text->setPosition(std::floor(item_pos.x + text_offset_x + reaction_offset_x + body_spacing[body_theme].reaction_background_padding_x), std::floor(item_pos.y + padding_y - 4.0f + body_spacing[body_theme].reaction_background_padding_y)); @@ -1489,7 +1482,6 @@ namespace QuickMedia { const bool rendering_card_view = card_view && card_view_enabled; sf::Vector2i content_size = get_item_thumbnail_size(item); - float text_offset_x = body_spacing[body_theme].padding_x; if(draw_thumbnails && load_texture && !item->thumbnail_url.empty() && !merge_with_previous) { std::shared_ptr item_thumbnail = AsyncImageLoader::get_instance().get_thumbnail(item->thumbnail_url, item->thumbnail_is_local, content_size); content_size = clamp_to_size_x(content_size, sf::Vector2i(width - (rendering_card_view ? 0.0f : body_spacing[body_theme].image_padding_x * 2.0f), content_size.y)); @@ -1507,20 +1499,19 @@ namespace QuickMedia { auto image_size = item_thumbnail->texture.getSize(); sf::Vector2f image_size_f(image_size.x, image_size.y); auto new_image_size = clamp_to_size(image_size_f, to_vec2f(content_size)); - text_offset_x += body_spacing[body_theme].image_padding_x + new_image_size.x; - item->loaded_image_height = new_image_size.y; + item->loaded_image_size = new_image_size; } else { - text_offset_x += body_spacing[body_theme].image_padding_x + content_size.x; - if(item->loaded_image_height < 0.1f) - item->loaded_image_height = content_size.y; + if(item->loaded_image_size.y < 0.1f) + item->loaded_image_size = to_vec2f(content_size); } } else if(item->thumbnail_size.x > 0) { - text_offset_x += body_spacing[body_theme].image_padding_x + content_size.x; + item->loaded_image_size = sf::Vector2f(body_spacing[body_theme].image_padding_x + content_size.x, 0.0f); // TODO: Fix. This makes the body item have incorrect position when loading and if the item is merge_with_previous? and has an embedded item //if(!merge_with_previous) // image_height = content_size.y; } + const float text_offset_x = body_spacing[body_theme].padding_x + body_spacing[body_theme].image_padding_x + item->loaded_image_size.x; const float text_max_width = rendering_card_view ? width : (width - text_offset_x - body_spacing[body_theme].image_padding_x); if(load_texture) @@ -1574,11 +1565,11 @@ namespace QuickMedia { item_height = item->loaded_content_height; if(rendering_card_view) { - item_height += item->loaded_image_height; + item_height += item->loaded_image_size.y; } else { const bool has_thumbnail = draw_thumbnails && !item->thumbnail_url.empty() && !merge_with_previous; const float padding_y = has_thumbnail ? body_spacing[body_theme].padding_y : body_spacing[body_theme].padding_y_text_only; - item_height = std::max(item_height, item->loaded_image_height); + item_height = std::max(item_height, item->loaded_image_size.y); item_height += (padding_y * 2.0f); } diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 646d065..82741bb 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -1448,6 +1448,8 @@ namespace QuickMedia { } std::unique_ptr Program::create_body(bool plain_text_list, bool prefer_card_view) { + if(rounded_rectangle_mask_shader.getNativeHandle() == 0) + plain_text_list = true; auto body = std::make_unique(plain_text_list ? BODY_THEME_MINIMAL : BODY_THEME_MODERN_SPACIOUS, loading_icon, &rounded_rectangle_shader, &rounded_rectangle_mask_shader); body->card_view = prefer_card_view; body->thumbnail_mask_shader = &circle_mask_shader; diff --git a/src/RoundedRectangle.cpp b/src/RoundedRectangle.cpp index 20abe14..4776618 100644 --- a/src/RoundedRectangle.cpp +++ b/src/RoundedRectangle.cpp @@ -19,10 +19,11 @@ namespace QuickMedia { void RoundedRectangle::set_position(sf::Vector2f pos) { this->pos = pos; - vertices[0].position = pos + sf::Vector2f(-shadow_radius, -shadow_radius); - vertices[1].position = pos + sf::Vector2f(shadow_radius, -shadow_radius) + sf::Vector2f(size.x, 0.0f); - vertices[2].position = pos + sf::Vector2f(shadow_radius, shadow_radius) + sf::Vector2f(size.x, size.y); - vertices[3].position = pos + sf::Vector2f(-shadow_radius, shadow_radius) + sf::Vector2f(0.0f, size.y); + const float shadow_radius_ins = rounded_rectangle_shader->getNativeHandle() == 0 ? 0.0f : shadow_radius; + vertices[0].position = pos + sf::Vector2f(-shadow_radius_ins, -shadow_radius_ins); + vertices[1].position = pos + sf::Vector2f(shadow_radius_ins, -shadow_radius_ins) + sf::Vector2f(size.x, 0.0f); + vertices[2].position = pos + sf::Vector2f(shadow_radius_ins, shadow_radius_ins) + sf::Vector2f(size.x, size.y); + vertices[3].position = pos + sf::Vector2f(-shadow_radius_ins, shadow_radius_ins) + sf::Vector2f(0.0f, size.y); } void RoundedRectangle::set_size(sf::Vector2f size) { -- cgit v1.2.3