From 5e7215b4675955fee8197076914599fe62f39c26 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 17 Sep 2020 16:44:52 +0200 Subject: Disable wrap around body, detect wrap around --- include/Body.hpp | 14 +++++++----- src/Body.cpp | 66 +++++++++++++++++++++++++++++++++++++++++------------- src/QuickMedia.cpp | 22 +++++++++--------- 3 files changed, 71 insertions(+), 31 deletions(-) diff --git a/include/Body.hpp b/include/Body.hpp index c487ad9..81e3a69 100644 --- a/include/Body.hpp +++ b/include/Body.hpp @@ -73,11 +73,12 @@ namespace QuickMedia { public: Body(Program *program, sf::Font *font, sf::Font *bold_font); - // Select previous item, ignoring invisible items - void select_previous_item(); + // Select previous item, ignoring invisible items. Returns true if the item was changed. This can be used to check if the top was hit when wrap_around is set to false + bool select_previous_item(); - // Select next item, ignoring invisible items - void select_next_item(); + // Select next item, ignoring invisible items. Returns true if the item was changed. This can be used to check if the bottom was hit when wrap_around is set to false + bool select_next_item(); + void set_selected_item(int item); void select_first_item(); void reset_selected(); @@ -99,15 +100,17 @@ namespace QuickMedia { bool no_items_visible() const; + int get_selected_item() const { return selected_item; } + sf::Font *font; sf::Font *bold_font; sf::Text progress_text; sf::Text author_text; sf::Text replies_text; - int selected_item; BodyItems items; std::thread thumbnail_load_thread; bool draw_thumbnails; + bool wrap_around; private: struct ThumbnailData { bool referenced; @@ -117,5 +120,6 @@ namespace QuickMedia { std::shared_ptr load_thumbnail_from_url(const std::string &url); std::unordered_map item_thumbnail_textures; bool loading_thumbnail; + int selected_item; }; } \ No newline at end of file diff --git a/src/Body.cpp b/src/Body.cpp index 6500e16..2233f92 100644 --- a/src/Body.cpp +++ b/src/Body.cpp @@ -20,39 +20,73 @@ namespace QuickMedia { replies_text("", *font, 14), selected_item(0), draw_thumbnails(false), - loading_thumbnail(false) + loading_thumbnail(false), + wrap_around(false) { progress_text.setFillColor(sf::Color::White); author_text.setFillColor(sf::Color::White); replies_text.setFillColor(sf::Color(129, 162, 190)); } - void Body::select_previous_item() { + bool Body::select_previous_item() { if(items.empty()) - return; + return false; + int new_selected_item = selected_item; int num_items = (int)items.size(); + for(int i = 0; i < num_items; ++i) { - --selected_item; - if(selected_item < 0) - selected_item = num_items - 1; - if(items[selected_item]->visible) - return; + if(new_selected_item - 1 < 0) { + if(wrap_around) + new_selected_item = num_items - 1; + else { + new_selected_item = selected_item; + break; + } + } else { + --new_selected_item; + } + if(items[new_selected_item]->visible) + break; } + + if(selected_item == new_selected_item) + return false; + selected_item = new_selected_item; + return true; } - void Body::select_next_item() { + bool Body::select_next_item() { if(items.empty()) - return; + return false; + int new_selected_item = selected_item; int num_items = (int)items.size(); + for(int i = 0; i < num_items; ++i) { - ++selected_item; - if(selected_item == num_items) - selected_item = 0; - if(items[selected_item]->visible) - return; + if(new_selected_item + 1 == num_items) { + if(wrap_around) { + new_selected_item = 0; + } else { + new_selected_item = selected_item; + break; + } + } else { + ++new_selected_item; + } + if(items[new_selected_item]->visible) + break; } + + if(selected_item == new_selected_item) + return false; + selected_item = new_selected_item; + return true; + } + + void Body::set_selected_item(int item) { + assert(item >= 0 && item < (int)items.size()); + selected_item = item; } void Body::select_first_item() { @@ -178,7 +212,7 @@ namespace QuickMedia { body_item->title_text->setString(body_item->get_title()); else body_item->title_text = std::make_unique(body_item->get_title(), font, 14, size.x - 50 - image_padding_x * 2.0f); - //body_item->title_text->updateGeometry(); // TODO: Call this to make getHeight work on first frame (called below) + body_item->title_text->updateGeometry(); } if(!body_item->get_description().empty() && !body_item->description_text) { diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 377729f..75bc027 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -2025,10 +2025,11 @@ namespace QuickMedia { if(image_index > 0) { --image_index; return; - } else if(image_index == 0 && body->selected_item < (int)body->items.size() - 1) { + } else if(image_index == 0 && body->get_selected_item() < (int)body->items.size() - 1) { // TODO: Make this work if the list is sorted differently than from newest to oldest. - body->selected_item++; - select_episode(body->items[body->selected_item].get(), true); + body->filter_search_fuzzy(""); + body->select_next_item(); + select_episode(body->items[body->get_selected_item()].get(), true); image_index = 99999; // Start at the page that shows we are at the end of the chapter return; } @@ -2036,10 +2037,11 @@ namespace QuickMedia { if(image_index < num_images) { ++image_index; return; - } else if(image_index == num_images && body->selected_item > 0) { + } else if(image_index == num_images && body->get_selected_item() > 0) { // TODO: Make this work if the list is sorted differently than from newest to oldest. - body->selected_item--; - select_episode(body->items[body->selected_item].get(), true); + body->filter_search_fuzzy(""); + body->select_previous_item(); + select_episode(body->items[body->get_selected_item()].get(), true); return; } } else if(event.key.code == sf::Keyboard::Escape) { @@ -2564,7 +2566,7 @@ namespace QuickMedia { } BodyItem *selected_item = body->get_selected(); - if(event.key.code == sf::Keyboard::Enter && selected_item && (comment_navigation_stack.empty() || body->selected_item != comment_navigation_stack.top()) && !selected_item->replies.empty()) { + if(event.key.code == sf::Keyboard::Enter && selected_item && (comment_navigation_stack.empty() || body->get_selected_item() != comment_navigation_stack.top()) && !selected_item->replies.empty()) { for(auto &body_item : body->items) { body_item->visible = false; } @@ -2572,7 +2574,7 @@ namespace QuickMedia { for(size_t reply_index : selected_item->replies) { body->items[reply_index]->visible = true; } - comment_navigation_stack.push(body->selected_item); + comment_navigation_stack.push(body->get_selected_item()); } else if(event.key.code == sf::Keyboard::BackSpace && !comment_navigation_stack.empty()) { size_t previous_selected = 0; if(!comment_navigation_stack.empty()) { @@ -2583,12 +2585,12 @@ namespace QuickMedia { for(auto &body_item : body->items) { body_item->visible = true; } - body->selected_item = previous_selected; + body->set_selected_item(previous_selected); } else { for(auto &body_item : body->items) { body_item->visible = false; } - body->selected_item = previous_selected; + body->set_selected_item(previous_selected); selected_item = body->items[comment_navigation_stack.top()].get(); selected_item->visible = true; for(size_t reply_index : selected_item->replies) { -- cgit v1.2.3