From 35508a1c737056c4ff821d1d5c40bea4715dffc8 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 24 Sep 2020 01:26:12 +0200 Subject: Fix broken author text --- include/Body.hpp | 2 +- include/Text.hpp | 5 ++++- src/Body.cpp | 52 +++++++++++++++++++++++++++++++--------------------- src/Text.cpp | 5 +++++ 4 files changed, 41 insertions(+), 23 deletions(-) diff --git a/include/Body.hpp b/include/Body.hpp index 235de72..f246b5a 100644 --- a/include/Body.hpp +++ b/include/Body.hpp @@ -56,6 +56,7 @@ namespace QuickMedia { bool thumbnail_is_local; std::unique_ptr title_text; std::unique_ptr description_text; + std::unique_ptr author_text; // Used by image boards for example. The elements are indices to other body items std::vector replies; std::string post_number; @@ -106,7 +107,6 @@ namespace QuickMedia { sf::Font *bold_font; sf::Font *cjk_font; sf::Text progress_text; - sf::Text author_text; sf::Text replies_text; BodyItems items; std::thread thumbnail_load_thread; diff --git a/include/Text.hpp b/include/Text.hpp index d70f356..bc6f7ab 100644 --- a/include/Text.hpp +++ b/include/Text.hpp @@ -69,8 +69,11 @@ namespace QuickMedia void setLineSpacing(float lineSpacing); void setCharacterSpacing(float characterSpacing); void setEditable(bool editable); + + // Note: won't update until @draw is called + float getWidth() const; - // Warning: won't update until @draw is called + // Note: won't update until @draw is called float getHeight() const; void processEvent(const sf::Event &event); diff --git a/src/Body.cpp b/src/Body.cpp index fd6ad07..39f6e01 100644 --- a/src/Body.cpp +++ b/src/Body.cpp @@ -46,6 +46,10 @@ namespace QuickMedia { description_text = std::make_unique(*other.description_text); else description_text = nullptr; + if(other.author_text) + author_text = std::make_unique(*other.author_text); + else + author_text = nullptr; replies = other.replies; post_number = other.post_number; title_color = other.title_color; @@ -56,7 +60,6 @@ namespace QuickMedia { bold_font(bold_font), cjk_font(cjk_font), progress_text("", *font, 14), - author_text("", *bold_font, 16), replies_text("", *font, 14), draw_thumbnails(false), wrap_around(false), @@ -66,7 +69,6 @@ namespace QuickMedia { selected_item(0) { progress_text.setFillColor(sf::Color::White); - author_text.setFillColor(sf::Color::White); replies_text.setFillColor(sf::Color(129, 162, 190)); thumbnail_resize_target_size.x = 200; thumbnail_resize_target_size.y = 119; @@ -390,7 +392,11 @@ namespace QuickMedia { if(body_item->dirty_author) { body_item->dirty_author = false; sf::String str = sf::String::fromUtf8(body_item->get_author().data(), body_item->get_author().data() + body_item->get_author().size()); - author_text.setString(std::move(str)); + if(body_item->author_text) + body_item->author_text->setString(std::move(str)); + else + body_item->author_text = std::make_unique(std::move(str), bold_font, cjk_font, 14, size.x - 50 - image_padding_x * 2.0f); + body_item->author_text->updateGeometry(); } } @@ -403,13 +409,13 @@ namespace QuickMedia { if(item->visible) { float item_height = 0.0f; if(!item->get_title().empty()) { - item_height += item->title_text->getHeight(); + item_height += item->title_text->getHeight() - 2.0f; } if(!item->get_author().empty()) { - item_height += author_text.getCharacterSize() + 2.0f; + item_height += item->author_text->getHeight() - 2.0f; } if(item->description_text) { - item_height += item->description_text->getHeight(); + item_height += item->description_text->getHeight() - 2.0f; } if(draw_thumbnails && !item->thumbnail_url.empty()) { auto &item_thumbnail = item_thumbnail_textures[item->thumbnail_url]; @@ -421,7 +427,7 @@ namespace QuickMedia { } item_height = std::max(item_height, image_height); } - item_height += spacing_y + padding_y * 2.0f; + item_height += (spacing_y + padding_y * 2.0f); visible_height += item_height; if(visible_height >= size.y) { --first_visible_item; @@ -452,13 +458,13 @@ namespace QuickMedia { float item_height = 0.0f; if(!item->get_title().empty()) { - item_height += item->title_text->getHeight(); + item_height += item->title_text->getHeight() - 2.0f; } if(!item->get_author().empty()) { - item_height += author_text.getCharacterSize() + 2.0f; + item_height += item->author_text->getHeight() - 2.0f; } if(item->description_text) { - item_height += item->description_text->getHeight(); + item_height += item->description_text->getHeight() - 2.0f; } if(draw_thumbnails && !item->thumbnail_url.empty()) { float image_height = image_fallback.getSize().y; @@ -514,27 +520,31 @@ namespace QuickMedia { } if(!item->get_author().empty()) { - author_text.setPosition(std::floor(item_pos.x + text_offset_x), std::floor(item_pos.y + padding_y)); - window.draw(author_text); + item->author_text->setPosition(std::floor(item_pos.x + text_offset_x), std::floor(item_pos.y + padding_y - 6.0f)); + item->author_text->setMaxWidth(size.x - text_offset_x - image_padding_x * 2.0f); + item->author_text->draw(window); + + sf::Vector2f replies_text_pos = item->author_text->getPosition() + sf::Vector2f(0.0f, 5.0f); + replies_text_pos.x += item->author_text->getWidth() + 5.0f; + replies_text.setPosition(replies_text_pos); - sf::Vector2f replies_text_pos = author_text.getPosition(); - replies_text_pos.x += author_text.getLocalBounds().width + 5.0f; + sf::String replies_text_str; for(size_t reply_index : item->replies) { BodyItem *reply_item = items[reply_index].get(); - replies_text.setString(">>" + reply_item->post_number); - replies_text.setPosition(replies_text_pos); - window.draw(replies_text); - replies_text_pos.x += replies_text.getLocalBounds().width + 5.0f; + replies_text_str += ">>"; + replies_text_str += reply_item->post_number; } + replies_text.setString(std::move(replies_text_str)); + window.draw(replies_text); - item_pos.y += author_text.getCharacterSize() + 2.0f; + item_pos.y += item->author_text->getHeight() - 2.0f; } //title_text.setString(item->title); //title_text.setPosition(std::floor(item_pos.x + text_offset_x), std::floor(item_pos.y + padding_y)); //window.draw(title_text); if(!item->get_title().empty()) { item->title_text->setFillColor(item->title_color); - item->title_text->setPosition(std::floor(item_pos.x + text_offset_x), std::floor(item_pos.y + padding_y - 4.0f)); + item->title_text->setPosition(std::floor(item_pos.x + text_offset_x), std::floor(item_pos.y + padding_y - 8.0f)); item->title_text->setMaxWidth(size.x - text_offset_x - image_padding_x * 2.0f); item->title_text->draw(window); } @@ -544,7 +554,7 @@ namespace QuickMedia { if(!item->get_title().empty()) { height_offset = item->title_text->getHeight(); } - item->description_text->setPosition(std::floor(item_pos.x + text_offset_x), std::floor(item_pos.y + padding_y - 4.0f + height_offset)); + item->description_text->setPosition(std::floor(item_pos.x + text_offset_x), std::floor(item_pos.y + padding_y - 8.0f + height_offset)); item->description_text->setMaxWidth(size.x - text_offset_x - image_padding_x * 2.0f); item->description_text->draw(window); } diff --git a/src/Text.cpp b/src/Text.cpp index 4aaf1c6..8f58e3d 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -155,6 +155,11 @@ namespace QuickMedia dirtyCaret = true; } } + + float Text::getWidth() const + { + return boundingBox.width; + } float Text::getHeight() const { -- cgit v1.2.3