From 6da71b830ead4b7ecb9a71e05dcd50c4abd3aa4f Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 9 Apr 2022 00:48:44 +0200 Subject: Proper reaction wrapping --- include/BodyItem.hpp | 1 + include/Text.hpp | 2 +- src/Body.cpp | 36 ++++++++++++++++++++---------------- src/Text.cpp | 4 ++++ 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/include/BodyItem.hpp b/include/BodyItem.hpp index 510d0e9..819b730 100644 --- a/include/BodyItem.hpp +++ b/include/BodyItem.hpp @@ -57,6 +57,7 @@ namespace QuickMedia { void *userdata = nullptr; mgl::vec2f size; mgl::Color text_color; + int num_lines = 1; }; class BodyItem { diff --git a/include/Text.hpp b/include/Text.hpp index a8a17be..fd1277f 100644 --- a/include/Text.hpp +++ b/include/Text.hpp @@ -75,7 +75,7 @@ namespace QuickMedia void replace(size_t start_index, size_t length, const std::string &insert_str); int getCaretIndex() const; - + int getNumLines() const; void set_color(mgl::Color color); void setLineSpacing(float lineSpacing); diff --git a/src/Body.cpp b/src/Body.cpp index c79f836..2747a22 100644 --- a/src/Body.cpp +++ b/src/Body.cpp @@ -935,6 +935,7 @@ namespace QuickMedia { } reaction.text->set_color(reaction.text_color); reaction.size = { 0.0f, 0.0f }; + reaction.num_lines = 1; } } } @@ -1441,6 +1442,7 @@ namespace QuickMedia { text_offset_x += body_spacing[body_theme].image_padding_x + item->loaded_image_size.x; } + const float text_max_width = size.x - text_offset_x; const float text_offset_y = std::floor(6.0f * get_config().scale * get_config().font_scale); const float timestamp_text_y = std::floor(item_pos.y + padding_y - text_offset_y - std::floor(4.0f * get_config().scale * get_config().font_scale)); @@ -1487,28 +1489,31 @@ namespace QuickMedia { float reaction_offset_x = 0.0f; item_pos.y += body_spacing[body_theme].reaction_padding_y; float reaction_max_height = 0.0f; - int row = 0; for(int i = 0; i < (int)item->reactions.size(); ++i) { auto &reaction = item->reactions[i]; reaction.text->updateGeometry(); reaction.size = { reaction.text->getWidth(), reaction.text->getHeight() }; + reaction.num_lines = reaction.text->getNumLines(); + const float reaction_max_height_prev = reaction_max_height; reaction_max_height = std::max(reaction_max_height, reaction.size.y); reaction_background.set_size(mgl::vec2f(reaction.size.x + body_spacing[body_theme].reaction_background_padding_x * 2.0f, reaction.size.y + body_spacing[body_theme].reaction_background_padding_y * 2.0f)); - reaction_background.draw(window); - const float new_reaction_offset_x = reaction.size.x + body_spacing[body_theme].reaction_background_padding_x * 2.0f + body_spacing[body_theme].reaction_spacing_x; - reaction.text->draw(window); - if(text_offset_x + new_reaction_offset_x > size.x && row != 0) { + const float new_reaction_offset_x = reaction_offset_x + reaction.size.x + body_spacing[body_theme].reaction_background_padding_x * 2.0f + body_spacing[body_theme].reaction_spacing_x; + if(i != 0 && (text_offset_x + new_reaction_offset_x > text_max_width || reaction.num_lines > 1)) { reaction_offset_x = 0.0f; - item_pos.y += reaction_max_height + body_spacing[body_theme].reaction_padding_y + text_offset_y; + item_pos.y += reaction_max_height_prev + body_spacing[body_theme].reaction_padding_y + text_offset_y; reaction_max_height = reaction.size.y; - row = 0; + reaction_background.set_position(vec2f_floor(item_pos.x + text_offset_x + reaction_offset_x, item_pos.y + padding_y)); + reaction_offset_x = reaction.size.x + body_spacing[body_theme].reaction_background_padding_x * 2.0f + body_spacing[body_theme].reaction_spacing_x; } else { reaction_background.set_position(vec2f_floor(item_pos.x + text_offset_x + reaction_offset_x, item_pos.y + padding_y)); - reaction.text->set_position(vec2f_floor(item_pos.x + text_offset_x + reaction_offset_x + body_spacing[body_theme].reaction_background_padding_x, item_pos.y + padding_y - 4.0f + body_spacing[body_theme].reaction_background_padding_y)); reaction_offset_x = new_reaction_offset_x; - ++row; } + + reaction.text->set_position(reaction_background.get_position() + mgl::vec2f(body_spacing[body_theme].reaction_background_padding_x, - 4.0f + body_spacing[body_theme].reaction_background_padding_y)); + + reaction_background.draw(window); + reaction.text->draw(window); } item_pos.y += reaction_max_height + body_spacing[body_theme].reaction_padding_y; } @@ -1715,25 +1720,24 @@ namespace QuickMedia { float reaction_offset_x = 0.0f; item_height += body_spacing[body_theme].reaction_padding_y; float reaction_max_height = 0.0f; - int row = 0; for(int i = 0; i < (int)item->reactions.size(); ++i) { auto &reaction = item->reactions[i]; if(reaction.size.x < 0.001f && reaction.text) { reaction.text->setMaxWidth(text_max_width); reaction.text->updateGeometry(); reaction.size = { reaction.text->getWidth(), reaction.text->getHeight() }; + reaction.num_lines = reaction.text->getNumLines(); } + const float reaction_max_height_prev = reaction_max_height; reaction_max_height = std::max(reaction_max_height, reaction.size.y); - const float new_reaction_offset_x = reaction.size.x + body_spacing[body_theme].reaction_background_padding_x * 2.0f + body_spacing[body_theme].reaction_spacing_x; - if(text_offset_x + new_reaction_offset_x > width && row != 0) { - reaction_offset_x = 0.0f; - item_height += reaction_max_height + body_spacing[body_theme].reaction_padding_y + std::floor(6.0f * get_config().scale); + const float new_reaction_offset_x = reaction_offset_x + reaction.size.x + body_spacing[body_theme].reaction_background_padding_x * 2.0f + body_spacing[body_theme].reaction_spacing_x; + if(i != 0 && (text_offset_x + new_reaction_offset_x > text_max_width || reaction.num_lines > 1)) { + item_height += reaction_max_height_prev + body_spacing[body_theme].reaction_padding_y + std::floor(6.0f * get_config().scale); reaction_max_height = reaction.size.y; - row = 0; + reaction_offset_x = reaction.size.x + body_spacing[body_theme].reaction_background_padding_x * 2.0f + body_spacing[body_theme].reaction_spacing_x; } else { reaction_offset_x = new_reaction_offset_x; - ++row; } } item_height += reaction_max_height + body_spacing[body_theme].reaction_padding_y; diff --git a/src/Text.cpp b/src/Text.cpp index 383256a..04d0d11 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -898,6 +898,10 @@ namespace QuickMedia } return num_vertices; } + + int Text::getNumLines() const { + return num_lines; + } // TODO: This can be optimized by using binary search int Text::getPreviousLineClosestPosition(int startIndex) const -- cgit v1.2.3