aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-11-30 23:42:28 +0100
committerdec05eba <dec05eba@protonmail.com>2019-11-30 23:42:28 +0100
commitfa59cd8c7e31afce5b5233e844084f881a411446 (patch)
tree3432b673dcedd585631a3548f144d57527905669
parente67b9899feb72027b246e3b63ce5aa0ccae2dd16 (diff)
Change size of body item rows depending on how many lines the text has
-rw-r--r--include/Body.hpp13
-rw-r--r--src/Body.cpp59
-rw-r--r--src/QuickMedia.cpp14
-rw-r--r--src/plugins/Fourchan.cpp2
4 files changed, 55 insertions, 33 deletions
diff --git a/include/Body.hpp b/include/Body.hpp
index 32e19dc..d10b482 100644
--- a/include/Body.hpp
+++ b/include/Body.hpp
@@ -12,8 +12,18 @@ namespace QuickMedia {
class BodyItem {
public:
- BodyItem(const std::string &_title): title(_title), visible(true) {
+ BodyItem(std::string _title): visible(true), num_lines(0) {
+ set_title(std::move(_title));
+ }
+ void set_title(std::string new_title) {
+ title = std::move(new_title);
+ // TODO: Optimize this
+ num_lines = 0;
+ for(char c : title) {
+ if(c == '\n')
+ ++num_lines;
+ }
}
std::string title;
@@ -22,6 +32,7 @@ namespace QuickMedia {
bool visible;
// Used by image boards for example. The elements are indices to other body items
std::vector<size_t> replies;
+ int num_lines;
};
using BodyItems = std::vector<std::unique_ptr<BodyItem>>;
diff --git a/src/Body.cpp b/src/Body.cpp
index 078c4fc..ed4862b 100644
--- a/src/Body.cpp
+++ b/src/Body.cpp
@@ -126,8 +126,11 @@ namespace QuickMedia {
// TODO: Show chapters (rows) that have been read differently to make it easier to see what
// hasn't been read yet.
void Body::draw(sf::RenderWindow &window, sf::Vector2f pos, sf::Vector2f size, const Json::Value &content_progress) {
- const float font_height = title_text.getCharacterSize() + 8.0f;
+ const float font_height = title_text.getCharacterSize() + 4.0f;
const float image_height = 100.0f;
+ const float spacing_y = 15.0f;
+ const float padding_y = 2.0f;
+ const float start_y = pos.y;
sf::RectangleShape image_fallback(sf::Vector2f(50, image_height));
image_fallback.setFillColor(sf::Color::White);
@@ -152,37 +155,45 @@ namespace QuickMedia {
if((int)item_thumbnail_textures.size() != num_items)
item_thumbnail_textures.resize(num_items);
- float row_height = font_height;
- if(draw_thumbnails)
- row_height = image_height;
- const float total_row_height = row_height + 15.0f;
- const int max_visible_rows = size.y / total_row_height - 1;
-
// Find the starting row that can be drawn to make selected row visible as well
- int visible_rows = 0;
int first_visible_item = selected_item;
assert(first_visible_item >= 0 && first_visible_item < (int)items.size());
- for(; first_visible_item >= 0 && visible_rows < max_visible_rows; --first_visible_item) {
+ float visible_height = 0.0f;
+ for(; first_visible_item >= 0; --first_visible_item) {
auto &item = items[first_visible_item];
- if(item->visible)
- ++visible_rows;
+ if(item->visible) {
+ float item_height = font_height * std::max(1, item->num_lines);
+ if(draw_thumbnails && !item->thumbnail_url.empty()) {
+ item_height = std::max(item_height, image_height);
+ }
+ item_height += spacing_y + padding_y * 2.0f;
+ visible_height += item_height;
+ if(visible_height >= size.y) {
+ --first_visible_item;
+ //pos.y += (size.y - (visible_height - item_height));
+ pos.y -= (visible_height - size.y);
+ break;
+ }
+ }
}
- auto window_size = window.getSize();
-
for(int i = first_visible_item + 1; i < num_items; ++i) {
const auto &item = items[i];
auto &item_thumbnail = item_thumbnail_textures[i];
- if(pos.y >= window_size.y)
- return;
+ if(pos.y >= start_y + size.y)
+ break;
if(!item->visible)
continue;
- float row_height = font_height;
+ float item_height = font_height * std::max(1, item->num_lines);
+ if(draw_thumbnails && !item->thumbnail_url.empty()) {
+ item_height = std::max(item_height, image_height);
+ }
+ item_height += (padding_y * 2.0f);
+
if(draw_thumbnails) {
- row_height = image_height;
// TODO: Should this be optimized by instead of checking if url changes based on index,
// put thumbnails in hash map based on url?
if(item->thumbnail_url.empty() && item_thumbnail.texture) {
@@ -197,7 +208,7 @@ namespace QuickMedia {
sf::Vector2f item_pos = pos;
if(i == selected_item) {
selected_border.setPosition(pos);
- selected_border.setSize(sf::Vector2f(selected_border_width, row_height));
+ selected_border.setSize(sf::Vector2f(selected_border_width, item_height));
window.draw(selected_border);
item_pos.x += selected_border_width;
item_background.setFillColor(sf::Color(0, 85, 119));
@@ -208,10 +219,10 @@ namespace QuickMedia {
item_pos.x = std::floor(item_pos.x);
item_pos.y = std::floor(item_pos.y);
item_background_shadow.setPosition(item_pos + sf::Vector2f(5.0f, 5.0f));
- item_background_shadow.setSize(sf::Vector2f(size.x, row_height));
+ item_background_shadow.setSize(sf::Vector2f(size.x, item_height));
window.draw(item_background_shadow);
item_background.setPosition(item_pos);
- item_background.setSize(sf::Vector2f(size.x, row_height));
+ item_background.setSize(sf::Vector2f(size.x, item_height));
window.draw(item_background);
float text_offset_x = 0.0f;
@@ -226,18 +237,18 @@ namespace QuickMedia {
auto image_scale_ratio = scale.x / scale.y;
const float width_ratio = height_ratio * image_scale_ratio;
image.setScale(width_ratio, height_ratio);
- image.setPosition(item_pos);
+ image.setPosition(item_pos + sf::Vector2f(0.0f, padding_y));
window.draw(image);
text_offset_x = width_ratio * image_size.x;
} else if(!item->thumbnail_url.empty()) {
- image_fallback.setPosition(item_pos);
+ image_fallback.setPosition(item_pos + sf::Vector2f(0.0f, padding_y));
window.draw(image_fallback);
text_offset_x = image_fallback.getSize().x;
}
}
title_text.setString(item->title);
- title_text.setPosition(std::floor(item_pos.x + text_offset_x + 10.0f), std::floor(item_pos.y));
+ title_text.setPosition(std::floor(item_pos.x + text_offset_x + 10.0f), std::floor(item_pos.y + padding_y));
window.draw(title_text);
// TODO: Do the same for non-manga content
@@ -253,7 +264,7 @@ namespace QuickMedia {
}
}
- pos.y += total_row_height;
+ pos.y += item_height + spacing_y;
}
}
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp
index aea8d60..55c983e 100644
--- a/src/QuickMedia.cpp
+++ b/src/QuickMedia.cpp
@@ -491,7 +491,7 @@ namespace QuickMedia {
float search_bottom = search_bar->getBottomWithoutShadow();
body_pos = sf::Vector2f(body_padding_horizontal, search_bottom + body_padding_vertical);
- body_size = sf::Vector2f(body_width, window_size.y - search_bottom);
+ body_size = sf::Vector2f(body_width, window_size.y - search_bottom - body_padding_vertical);
}
search_bar->update();
@@ -592,7 +592,7 @@ namespace QuickMedia {
float search_bottom = search_bar->getBottom();
body_pos = sf::Vector2f(body_padding_horizontal, search_bottom + body_padding_vertical);
- body_size = sf::Vector2f(body_width, window_size.y - search_bottom);
+ body_size = sf::Vector2f(body_width, window_size.y - search_bottom - body_padding_vertical);
}
search_bar->update();
@@ -948,7 +948,7 @@ namespace QuickMedia {
float search_bottom = search_bar->getBottom();
body_pos = sf::Vector2f(body_padding_horizontal, search_bottom + body_padding_vertical);
- body_size = sf::Vector2f(body_width, window_size.y - search_bottom);
+ body_size = sf::Vector2f(body_width, window_size.y - search_bottom - body_padding_vertical);
}
search_bar->update();
@@ -1280,7 +1280,7 @@ namespace QuickMedia {
float search_bottom = search_bar->getBottom();
body_pos = sf::Vector2f(body_padding_horizontal, search_bottom + body_padding_vertical);
- body_size = sf::Vector2f(body_width, window_size.y - search_bottom);
+ body_size = sf::Vector2f(body_width, window_size.y - search_bottom - body_padding_vertical);
}
search_bar->update();
@@ -1340,7 +1340,7 @@ namespace QuickMedia {
float search_bottom = search_bar->getBottom();
body_pos = sf::Vector2f(body_padding_horizontal, search_bottom + body_padding_vertical);
- body_size = sf::Vector2f(body_width, window_size.y - search_bottom);
+ body_size = sf::Vector2f(body_width, window_size.y - search_bottom - body_padding_vertical);
}
search_bar->update();
@@ -1405,7 +1405,7 @@ namespace QuickMedia {
float search_bottom = search_bar->getBottom();
body_pos = sf::Vector2f(body_padding_horizontal, search_bottom + body_padding_vertical);
- body_size = sf::Vector2f(body_width, window_size.y - search_bottom);
+ body_size = sf::Vector2f(body_width, window_size.y - search_bottom - body_padding_vertical);
}
search_bar->update();
@@ -1505,7 +1505,7 @@ namespace QuickMedia {
float search_bottom = search_bar->getBottom();
body_pos = sf::Vector2f(body_padding_horizontal, search_bottom + body_padding_vertical);
- body_size = sf::Vector2f(body_width, window_size.y - search_bottom);
+ body_size = sf::Vector2f(body_width, window_size.y - search_bottom - body_padding_vertical);
}
//search_bar->update();
diff --git a/src/plugins/Fourchan.cpp b/src/plugins/Fourchan.cpp
index 5a8cada..58a0a57 100644
--- a/src/plugins/Fourchan.cpp
+++ b/src/plugins/Fourchan.cpp
@@ -617,7 +617,7 @@ namespace QuickMedia {
);
html_unescape_sequences(comment_text);
BodyItem *body_item = result_items[body_item_index].get();
- body_item->title = std::move(comment_text);
+ body_item->set_title(std::move(comment_text));
const Json::Value &ext = post["ext"];
const Json::Value &tim = post["tim"];